Skip to content

Commit

Permalink
feat: mark ca methods experimental, adds polling to status update
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov committed Dec 20, 2024
1 parent 0399cd4 commit 8989566
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/walletkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ export class WalletKit extends IWalletKit {
}
};

// ---------- Chain Abstraction ----------------------------------------------- //

public prepareFulfilment: IWalletKit["prepareFulfilment"] = async (params) => {
try {
return await this.engine.prepareFulfilment(params);
Expand Down
39 changes: 30 additions & 9 deletions packages/walletkit/src/controllers/chainAbstraction.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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;

Expand Down Expand Up @@ -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;
};

Expand Down
20 changes: 20 additions & 0 deletions packages/walletkit/src/types/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------------------- //
Expand Down

0 comments on commit 8989566

Please sign in to comment.