Update transaction details and enhance logging in App component
- Changed the recipient address in the balance transfer transaction. - Added logging for the result of the transaction in a human-readable format.
This commit is contained in:
1 parent
54c2c2141f
commit
a1aadf8fbc
3 files changed
+39
-58
No files matched your search
+2
-1
@@ -34,11 +34,12 @@ function App() {
|
|||||||
}
|
}
|
||||||
const apiPromise = new GearApi({ providerAddress: 'wss://rpc.vara.network' });
|
const apiPromise = new GearApi({ providerAddress: 'wss://rpc.vara.network' });
|
||||||
await apiPromise.isReady;
|
await apiPromise.isReady;
|
||||||
const tx = apiPromise.balance.transfer('kGi1Ui7VXBFmPmaoMD5xgWd2VHNixZ5BbLNhHFYD39T85rUi3', 100)
|
const tx = apiPromise.balance.transfer('kGgMeagrS8yiLMech46HKYGBti5bxyQ5VrRYyD2EgTDvY9jj2', 100)
|
||||||
console.log('[VARANINJECT] tx', tx);
|
console.log('[VARANINJECT] tx', tx);
|
||||||
// @ts-expect-error - signer is not typed
|
// @ts-expect-error - signer is not typed
|
||||||
const result = await tx.signAndSend(address, { signer: extension?.signer })
|
const result = await tx.signAndSend(address, { signer: extension?.signer })
|
||||||
console.log('[VARANINJECT] result', result);
|
console.log('[VARANINJECT] result', result);
|
||||||
|
console.log('[VARANINJECT] result toHuman', result.toHuman());
|
||||||
}
|
}
|
||||||
|
|
||||||
const signTransaction = async () => {
|
const signTransaction = async () => {
|
||||||
|
|||||||
+5
-13
@@ -20,7 +20,7 @@ export interface AcknowledgmentMessage extends WebSocketMessage {
|
|||||||
// Server message wrapper - contains actual message data
|
// Server message wrapper - contains actual message data
|
||||||
export interface ServerMessageWrapper extends WebSocketMessage {
|
export interface ServerMessageWrapper extends WebSocketMessage {
|
||||||
type: 'server_message';
|
type: 'server_message';
|
||||||
message: ServerMessage;
|
data: ServerMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data message wrapper - contains data payload
|
// Data message wrapper - contains data payload
|
||||||
@@ -183,21 +183,16 @@ export class WalletSocket {
|
|||||||
if (message.type === 'connection' && message.id) {
|
if (message.type === 'connection' && message.id) {
|
||||||
const connMessage = message as ConnectionMessage;
|
const connMessage = message as ConnectionMessage;
|
||||||
this.uniqueId = connMessage.id;
|
this.uniqueId = connMessage.id;
|
||||||
console.log(`[VARANINJECT] Unique ID received: ${this.uniqueId}`);
|
|
||||||
// Resolve connection promise if pending
|
// Resolve connection promise if pending
|
||||||
if (this.connectionResolve) {
|
if (this.connectionResolve) {
|
||||||
this.connectionResolve();
|
this.connectionResolve();
|
||||||
}
|
}
|
||||||
} else if (message.type === 'acknowledgment') {
|
|
||||||
console.log('[VARANINJECT] Data acknowledged by server');
|
|
||||||
} else if (message.type === 'server_message') {
|
} else if (message.type === 'server_message') {
|
||||||
console.log('[VARANINJECT] Received server message');
|
|
||||||
const serverWrapper = message as ServerMessageWrapper;
|
const serverWrapper = message as ServerMessageWrapper;
|
||||||
if (serverWrapper.message) {
|
if (serverWrapper.data) {
|
||||||
this.handleServerMessage(serverWrapper.message);
|
this.handleServerMessage(serverWrapper.data);
|
||||||
}
|
}
|
||||||
} else if (message.type === 'data' && message.data) {
|
} else if (message.type === 'data' && message.data) {
|
||||||
console.log('[VARANINJECT] Received data message');
|
|
||||||
const dataWrapper = message as DataMessageWrapper;
|
const dataWrapper = message as DataMessageWrapper;
|
||||||
if (dataWrapper.data) {
|
if (dataWrapper.data) {
|
||||||
// Type guard to ensure data is a ServerMessage
|
// Type guard to ensure data is a ServerMessage
|
||||||
@@ -226,16 +221,13 @@ export class WalletSocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async send(data: ClientRequest): Promise<void> {
|
async send(data: ClientRequest): Promise<void> {
|
||||||
const id = await this.ensureConnected();
|
// const id = await this.ensureConnected();
|
||||||
|
|
||||||
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
||||||
throw new Error('WebSocket is not connected');
|
throw new Error('WebSocket is not connected');
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = {
|
const message = data;
|
||||||
...data,
|
|
||||||
id,
|
|
||||||
};
|
|
||||||
|
|
||||||
this.ws.send(JSON.stringify(message));
|
this.ws.send(JSON.stringify(message));
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-44
@@ -39,35 +39,6 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
private async _initializeSocket(): Promise<void> {
|
private async _initializeSocket(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await this.socket.connect();
|
await this.socket.connect();
|
||||||
|
|
||||||
// Listen for wallet connection messages
|
|
||||||
this.socket.onMessage<ConnectResponseMessage>('connect', (data) => {
|
|
||||||
if (data.success && data.wallets && Array.isArray(data.wallets)) {
|
|
||||||
const allWallets = Array.from(
|
|
||||||
new Set(
|
|
||||||
this.store.get('accounts').concat(data.wallets)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
this.store.set('accounts', allWallets);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Listen for transaction signing responses
|
|
||||||
this.socket.onMessage<SignTransactionResponseMessage>('signTransaction', (data) => {
|
|
||||||
const currentTransactions = this.store.get('transactions');
|
|
||||||
const pendingTransactionIndex = currentTransactions.findIndex(
|
|
||||||
t => t.id === data.id && t.status === 'signing'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (pendingTransactionIndex !== -1) {
|
|
||||||
currentTransactions[pendingTransactionIndex] = {
|
|
||||||
...currentTransactions[pendingTransactionIndex],
|
|
||||||
status: data.success ? 'success' : 'failed',
|
|
||||||
signedTx: data.signedTx || '',
|
|
||||||
};
|
|
||||||
this.store.set('transactions', currentTransactions);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[VARANINJECT] Error initializing socket:', error);
|
console.error('[VARANINJECT] Error initializing socket:', error);
|
||||||
}
|
}
|
||||||
@@ -120,10 +91,9 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
method: 'sT',
|
method: 'sT',
|
||||||
tx: `p:${JSON.stringify(payload)}`,
|
tx: `p:${JSON.stringify(payload)}`,
|
||||||
id: requestId,
|
id: requestId,
|
||||||
|
wallet: payload.address,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('[VARANINJECT] signPayloadTransaction request', request);
|
|
||||||
|
|
||||||
// Store transaction as pending
|
// Store transaction as pending
|
||||||
const currentTransactions = this.store.get('transactions');
|
const currentTransactions = this.store.get('transactions');
|
||||||
currentTransactions.push({
|
currentTransactions.push({
|
||||||
@@ -134,7 +104,7 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
this.store.set('transactions', currentTransactions);
|
this.store.set('transactions', currentTransactions);
|
||||||
|
|
||||||
// Send via WebSocket
|
// Send via WebSocket
|
||||||
await this.socket.sendViaAPI(request);
|
await this.socket.send(request);
|
||||||
|
|
||||||
const botUrl = `${this.varanWalletUrl}${this.socket.getUniqueId()}`;
|
const botUrl = `${this.varanWalletUrl}${this.socket.getUniqueId()}`;
|
||||||
|
|
||||||
@@ -148,12 +118,20 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
|
|
||||||
const currentTransactions = this.store.get('transactions');
|
const currentTransactions = this.store.get('transactions');
|
||||||
const transaction = currentTransactions.find(t => t.id === requestId);
|
const transaction = currentTransactions.find(t => t.id === requestId);
|
||||||
|
const pendingTransactionIndex = currentTransactions.findIndex(t => t.id === requestId);
|
||||||
|
|
||||||
if (transaction && transaction.status !== 'signing') {
|
if (transaction && transaction.status === 'signing') {
|
||||||
if (transaction.status === 'success') {
|
currentTransactions[pendingTransactionIndex] = {
|
||||||
|
...currentTransactions[pendingTransactionIndex],
|
||||||
|
status: data.success ? 'success' : 'failed',
|
||||||
|
signedTx: data.signedTx || '',
|
||||||
|
};
|
||||||
|
this.store.set('transactions', currentTransactions);
|
||||||
|
|
||||||
|
if (data.success) {
|
||||||
resolve({
|
resolve({
|
||||||
id: transaction.id,
|
id: data.id,
|
||||||
signature: transaction.signedTx,
|
signature: data.signedTx,
|
||||||
} as SignerResult);
|
} as SignerResult);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error('Transaction signing failed'));
|
reject(new Error('Transaction signing failed'));
|
||||||
@@ -166,7 +144,6 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
|
|
||||||
private async signRawTransaction(raw: SignerPayloadRaw): Promise<SignerResult> {
|
private async signRawTransaction(raw: SignerPayloadRaw): Promise<SignerResult> {
|
||||||
if (raw.type === 'payload') {
|
if (raw.type === 'payload') {
|
||||||
console.log('[VARANINJECT] signRawTransaction payload', raw.data);
|
|
||||||
return this.signPayloadTransaction(JSON.parse(raw.data));
|
return this.signPayloadTransaction(JSON.parse(raw.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +171,9 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
this.store.set('transactions', currentTransactions);
|
this.store.set('transactions', currentTransactions);
|
||||||
|
|
||||||
// Send via WebSocket
|
// Send via WebSocket
|
||||||
await this.socket.sendViaAPI(request);
|
await this.socket.send(request);
|
||||||
|
|
||||||
|
await this.webApp.openTelegramLink(`${this.varanWalletUrl}${this.socket.getUniqueId()}`);
|
||||||
|
|
||||||
// Wait for response
|
// Wait for response
|
||||||
return new Promise<SignerResult>((resolve, reject) => {
|
return new Promise<SignerResult>((resolve, reject) => {
|
||||||
@@ -203,13 +182,21 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
unsubscribe();
|
unsubscribe();
|
||||||
|
|
||||||
const currentTransactions = this.store.get('transactions');
|
const currentTransactions = this.store.get('transactions');
|
||||||
const transaction = currentTransactions.find(t => t.id === requestId);
|
const pendingTransactionIndex = currentTransactions.findIndex(t => t.id === requestId);
|
||||||
|
const transaction = currentTransactions[pendingTransactionIndex];
|
||||||
|
|
||||||
if (transaction && transaction.status !== 'signing') {
|
if (transaction && transaction.status === 'signing') {
|
||||||
if (transaction.status === 'success') {
|
currentTransactions[pendingTransactionIndex] = {
|
||||||
|
...currentTransactions[pendingTransactionIndex],
|
||||||
|
status: data.success ? 'success' : 'failed',
|
||||||
|
signedTx: data.signedTx || '',
|
||||||
|
};
|
||||||
|
this.store.set('transactions', currentTransactions);
|
||||||
|
|
||||||
|
if (data.success) {
|
||||||
resolve({
|
resolve({
|
||||||
id: transaction.id,
|
id: data.id,
|
||||||
signature: transaction.signedTx,
|
signature: data.signedTx,
|
||||||
} as SignerResult);
|
} as SignerResult);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error('Transaction signing failed'));
|
reject(new Error('Transaction signing failed'));
|
||||||
@@ -226,7 +213,7 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
await this.socket.connect();
|
await this.socket.connect();
|
||||||
|
|
||||||
// Send connect request via WebSocket
|
// Send connect request via WebSocket
|
||||||
await this.socket.sendViaAPI({
|
await this.socket.send({
|
||||||
method: 'connect',
|
method: 'connect',
|
||||||
} as ConnectRequest);
|
} as ConnectRequest);
|
||||||
|
|
||||||
@@ -237,6 +224,7 @@ export class Wallet implements InjectedWindowProvider {
|
|||||||
return new Promise<void>((resolve) => {
|
return new Promise<void>((resolve) => {
|
||||||
const unsubscribe = this.socket.onMessage<ConnectResponseMessage>('connect', (data) => {
|
const unsubscribe = this.socket.onMessage<ConnectResponseMessage>('connect', (data) => {
|
||||||
if (data.success && data.wallets && data.wallets.length > 0) {
|
if (data.success && data.wallets && data.wallets.length > 0) {
|
||||||
|
this.store.set('accounts', data.wallets);
|
||||||
unsubscribe();
|
unsubscribe();
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user