Skip to content

Commit

Permalink
Update preflight notification for transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiendekaco committed Feb 20, 2024
1 parent 9fb8bc2 commit 40e08c3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
19 changes: 11 additions & 8 deletions packages/core/src/preflight-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export interface UpdateNotification {
}

export interface PreflightNotificationsOptions {
sendTransaction?: () => Promise<string | void>
sendTransaction?: (fn: (hash: string) => void) => Promise<string | void>
estimateGas?: () => Promise<string>
gasPrice?: () => Promise<string>
balance?: string | number
Expand Down
4 changes: 2 additions & 2 deletions packages/demo/src/components/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ const Layout = styled(Component)<Props>( ({ 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'
}
},

Expand Down
3 changes: 2 additions & 1 deletion packages/demo/src/utils/api/evmApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/demo/src/utils/api/substrateApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down

0 comments on commit 40e08c3

Please sign in to comment.