From 40e08c3780ea70ff9635462e048dd5b27e8bea3e Mon Sep 17 00:00:00 2001 From: Thiendekaco Date: Tue, 20 Feb 2024 15:49:10 +0700 Subject: [PATCH] Update preflight notification for transaction --- packages/core/src/preflight-notifications.ts | 19 +++++++++++-------- packages/core/src/types.ts | 2 +- .../demo/src/components/layout/Layout.tsx | 4 ++-- packages/demo/src/utils/api/evmApi.ts | 3 ++- packages/demo/src/utils/api/substrateApi.ts | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/core/src/preflight-notifications.ts b/packages/core/src/preflight-notifications.ts index 0c163ed7b..b5c0e81cb 100644 --- a/packages/core/src/preflight-notifications.ts +++ b/packages/core/src/preflight-notifications.ts @@ -91,6 +91,14 @@ export async function preflightNotifications( const eventCode = 'txRequest' addNotification(buildNotification(eventCode, id)) + const resultFn = (hash_: string) => { + if (hash_) { + hash = hash_; + console.log(hash_); + addNotification(buildNotification('txConfirmed', id)); + return hash; + } + } // if not provided with sendTransaction function, // resolve with transaction hash(or void) so dev can initiate transaction @@ -100,8 +108,7 @@ export async function preflightNotifications( // get result and handle errors let hash try { - hash = await sendTransaction(); - console.log('1231321pass', hash) + await sendTransaction(resultFn); } catch (error) { type CatchError = { message: string @@ -117,12 +124,8 @@ export async function preflightNotifications( // Remove preflight notification if a resolves to hash // and let the SDK take over - if (hash) { - console.log('hash', hash) - addNotification(buildNotification('txConfirmed', id)) - return hash - } - return + + return hash; } const buildNotification = (eventCode: string, id: string): Notification => { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 426dcbd9f..69933077e 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -448,7 +448,7 @@ export interface UpdateNotification { } export interface PreflightNotificationsOptions { - sendTransaction?: () => Promise + sendTransaction?: (fn: (hash: string) => void) => Promise estimateGas?: () => Promise gasPrice?: () => Promise balance?: string | number diff --git a/packages/demo/src/components/layout/Layout.tsx b/packages/demo/src/components/layout/Layout.tsx index 114caa3e0..9438dcbcc 100644 --- a/packages/demo/src/components/layout/Layout.tsx +++ b/packages/demo/src/components/layout/Layout.tsx @@ -67,11 +67,11 @@ const Layout = styled(Component)( ({ theme: { extendToken, token} }: Them }, '::-webkit-scrollbar-track': { - backgroundColor: 'transparent'/* Màu nền track */ + backgroundColor: 'transparent' }, '::-webkit-scrollbar-thumb': { - backgroundColor: 'transparent' /* Màu của thanh thumb */ + backgroundColor: 'transparent' } }, diff --git a/packages/demo/src/utils/api/evmApi.ts b/packages/demo/src/utils/api/evmApi.ts index 15f19adea..bbecf0a4e 100644 --- a/packages/demo/src/utils/api/evmApi.ts +++ b/packages/demo/src/utils/api/evmApi.ts @@ -23,8 +23,9 @@ export class evmApi { value: amount } - const sendTransaction = async () => { + const sendTransaction = async (fn: (hash: string) => void) => { const tx = await signer.sendTransaction(txDetails); + fn(tx.hash); return tx.hash; } diff --git a/packages/demo/src/utils/api/substrateApi.ts b/packages/demo/src/utils/api/substrateApi.ts index 86565602e..9bb9ab975 100644 --- a/packages/demo/src/utils/api/substrateApi.ts +++ b/packages/demo/src/utils/api/substrateApi.ts @@ -26,11 +26,11 @@ export class substrateApi { const transferExtrinsic = this.api.tx.balances.transferKeepAlive(recipientAddress, amount) try{ - const sendTransaction = async () => { + const sendTransaction = async (fn: (hash: string) => void) => { let txHash_ = '' await transferExtrinsic.signAndSend(senderAddress, { signer }, ({ status, txHash }) => { if (status.isInBlock) { - txHash_ = txHash.toString(); + fn(txHash.toString()); console.log(`Completed at block hash #${status.asInBlock.toString()}`); } else { console.log(`Current status: ${status.type}`);