From 8989566ce8c6a2455a7fc0f79589020a9d014fcd Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Fri, 20 Dec 2024 10:45:42 +0200 Subject: [PATCH] feat: mark ca methods experimental, adds polling to status update --- packages/walletkit/src/client.ts | 2 + .../src/controllers/chainAbstraction.ts | 39 ++++++++++++++----- packages/walletkit/src/types/client.ts | 20 ++++++++++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/packages/walletkit/src/client.ts b/packages/walletkit/src/client.ts index 45885ba..7bf5241 100644 --- a/packages/walletkit/src/client.ts +++ b/packages/walletkit/src/client.ts @@ -186,6 +186,8 @@ export class WalletKit extends IWalletKit { } }; + // ---------- Chain Abstraction ----------------------------------------------- // + public prepareFulfilment: IWalletKit["prepareFulfilment"] = async (params) => { try { return await this.engine.prepareFulfilment(params); diff --git a/packages/walletkit/src/controllers/chainAbstraction.ts b/packages/walletkit/src/controllers/chainAbstraction.ts index 1167375..53679e8 100644 --- a/packages/walletkit/src/controllers/chainAbstraction.ts +++ b/packages/walletkit/src/controllers/chainAbstraction.ts @@ -1,5 +1,6 @@ /* eslint-disable no-console */ import { ENV_MAP, getEnvironment } from "@walletconnect/utils"; +import { THIRTY_SECONDS, toMiliseconds } from "@walletconnect/time"; import { ChainAbstractionTypes, IChainAbstraction, IWalletKitEngine } from "../types"; import { FULFILMENT_STATUS, CAN_FULFIL_STATUS } from "../constants"; @@ -9,6 +10,7 @@ export class ChainAbstraction extends IChainAbstraction { private estimateFeesHandler: any; private getERC20BalanceHandler: any; private getFulfilmentDetailsHandler: any; + private statusPollingTimeout = THIRTY_SECONDS; private projectId: string; @@ -62,16 +64,35 @@ export class ChainAbstraction extends IChainAbstraction { const { fulfilmentId } = params; - console.log("fulfilmentStatus", params); - const result = (await this.fulfilmentStatusHandler({ - orchestrationId: fulfilmentId, - projectId: this.projectId, - })) as ChainAbstractionTypes.FulfilmentStatusHandlerResponse; + const timeout = setTimeout(() => { + throw new Error(`Fulfilment status polling timeout: ${fulfilmentId}`); + }, toMiliseconds(this.statusPollingTimeout)); + + let result; + + do { + const statusResult = (await this.fulfilmentStatusHandler({ + orchestrationId: fulfilmentId, + projectId: this.projectId, + })) as ChainAbstractionTypes.FulfilmentStatusHandlerResponse; + + console.log("fulfilmentStatus result", statusResult); + + if (statusResult.status === FULFILMENT_STATUS.pending) { + console.log("fulfilmentStatus pending retrying...", statusResult); + await new Promise((resolve) => setTimeout(resolve, statusResult.checkIn)); + continue; + } + + if (statusResult.status === FULFILMENT_STATUS.error) { + clearTimeout(timeout); + throw new Error(statusResult.reason); + } + + clearTimeout(timeout); + result = statusResult; + } while (!result); - if (result.status === FULFILMENT_STATUS.error) { - throw new Error(result.reason); - } - console.log("fulfilmentStatus result", result); return result; }; diff --git a/packages/walletkit/src/types/client.ts b/packages/walletkit/src/types/client.ts index a68188a..bac6f72 100644 --- a/packages/walletkit/src/types/client.ts +++ b/packages/walletkit/src/types/client.ts @@ -130,10 +130,30 @@ export abstract class IWalletKit { public abstract rejectSessionAuthenticate: IWalletKitEngine["rejectSessionAuthenticate"]; // chain abstraction // + /** + * @experimental + * This method is experimental and may change in the future. + */ public abstract prepareFulfilment: IWalletKitEngine["prepareFulfilment"]; + /** + * @experimental + * This method is experimental and may change in the future. + */ public abstract fulfilmentStatus: IWalletKitEngine["fulfilmentStatus"]; + /** + * @experimental + * This method is experimental and may change in the future. + */ public abstract estimateFees: IWalletKitEngine["estimateFees"]; + /** + * @experimental + * This method is experimental and may change in the future. + */ public abstract getERC20Balance: IWalletKitEngine["getERC20Balance"]; + /** + * @experimental + * This method is experimental and may change in the future. + */ public abstract getFulfilmentDetails: IWalletKitEngine["getFulfilmentDetails"]; // ---------- Event Handlers ----------------------------------------------- //