Skip to content

Commit

Permalink
add statusCallback param
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Nov 26, 2024
1 parent 599a0fb commit 870da01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/mrl/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export async function getTransferData({
amount,
isAutomatic,
{ evmSigner, polkadotSigner }: Partial<Signers>,
statusCallback,
): Promise<string[]> {
const source = route.source.chain;

Expand Down Expand Up @@ -155,6 +156,7 @@ export async function getTransferData({
sourceAddress,
transfer,
polkadotSigner,
statusCallback,
);

return [hash];
Expand Down
3 changes: 2 additions & 1 deletion packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -24,6 +24,7 @@ export interface TransferData {
amount: bigint | number | string,
isAutomatic: boolean,
signers: Signers,
statusCallback?: (params: ISubmittableResult) => void,
): Promise<string[]>;
}

Expand Down
37 changes: 28 additions & 9 deletions packages/sdk/src/services/polkadot/PolkadotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,37 @@ export class PolkadotService {
account: string,
config: ExtrinsicConfig,
signer: PolkadotSigner | IKeyringPair,
statusCallback?: (params: ISubmittableResult) => void,
): Promise<string> {
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<string>((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 {
Expand Down

0 comments on commit 870da01

Please sign in to comment.