From 870da01dc45089faaf391693bed0e9bad122e718 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 26 Nov 2024 17:00:37 +0100 Subject: [PATCH] add statusCallback param --- .../src/getTransferData/getTransferData.ts | 2 + packages/mrl/src/mrl.interfaces.ts | 3 +- .../src/services/polkadot/PolkadotService.ts | 37 ++++++++++++++----- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/packages/mrl/src/getTransferData/getTransferData.ts b/packages/mrl/src/getTransferData/getTransferData.ts index af2975aa..d15d5d5f 100644 --- a/packages/mrl/src/getTransferData/getTransferData.ts +++ b/packages/mrl/src/getTransferData/getTransferData.ts @@ -108,6 +108,7 @@ export async function getTransferData({ amount, isAutomatic, { evmSigner, polkadotSigner }: Partial, + statusCallback, ): Promise { const source = route.source.chain; @@ -155,6 +156,7 @@ export async function getTransferData({ sourceAddress, transfer, polkadotSigner, + statusCallback, ); return [hash]; diff --git a/packages/mrl/src/mrl.interfaces.ts b/packages/mrl/src/mrl.interfaces.ts index a552d5ed..60119009 100644 --- a/packages/mrl/src/mrl.interfaces.ts +++ b/packages/mrl/src/mrl.interfaces.ts @@ -4,7 +4,7 @@ import type { } from '@moonbeam-network/xcm-sdk'; import type { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types'; import type { Signer } from '@polkadot/api/types'; -import type { IKeyringPair } from '@polkadot/types/types'; +import type { IKeyringPair, ISubmittableResult } from '@polkadot/types/types'; import type { TokenTransfer } from '@wormhole-foundation/sdk-connect'; import type { WalletClient } from 'viem'; @@ -24,6 +24,7 @@ export interface TransferData { amount: bigint | number | string, isAutomatic: boolean, signers: Signers, + statusCallback?: (params: ISubmittableResult) => void, ): Promise; } diff --git a/packages/sdk/src/services/polkadot/PolkadotService.ts b/packages/sdk/src/services/polkadot/PolkadotService.ts index 9d8de27b..01b3ac46 100644 --- a/packages/sdk/src/services/polkadot/PolkadotService.ts +++ b/packages/sdk/src/services/polkadot/PolkadotService.ts @@ -90,18 +90,37 @@ export class PolkadotService { account: string, config: ExtrinsicConfig, signer: PolkadotSigner | IKeyringPair, + statusCallback?: (params: ISubmittableResult) => void, ): Promise { const extrinsic = this.getExtrinsic(config); - const hash = await extrinsic.signAndSend( - this.#isSigner(signer) ? account : signer, - { - nonce: -1, - signer: this.#isSigner(signer) ? signer : undefined, - withSignedTransaction: true, - }, - ); - return hash.toString(); + const hash = await new Promise((resolve, reject) => { + extrinsic + .signAndSend( + this.#isSigner(signer) ? account : signer, + { + nonce: -1, + signer: this.#isSigner(signer) ? signer : undefined, + withSignedTransaction: true, + }, + (result) => { + if (result.isError || result.dispatchError) { + reject( + new Error( + result.dispatchError?.toString() || 'Transaction failed', + ), + ); + } + if (result.txHash) { + resolve(result.txHash.toString()); + } + statusCallback?.(result); + }, + ) + .catch(reject); + }); + + return hash; } #isSigner(signer: PolkadotSigner | IKeyringPair): signer is PolkadotSigner {