UNPKG

@wormhole-foundation/sdk-connect

Version:

The core package for the Connect SDK, used in conjunction with 1 or more of the chain packages

59 lines 2.25 kB
import { isGatewayTransferMsg, isGatewayTransferWithPayloadMsg, isIbcMessageId, isTransactionIdentifier, } from "@wormhole-foundation/sdk-definitions"; import { DEFAULT_TASK_TIMEOUT } from "./config.js"; export async function retry(task, interval, timeout = DEFAULT_TASK_TIMEOUT, title) { const maxRetries = Math.floor(timeout / interval); let retries = 0; return new Promise((resolve, reject) => { task().then((result) => { if (result !== null) { resolve(result); return; } let intervalId = setInterval(async () => { if (retries >= maxRetries) { clearInterval(intervalId); resolve(null); return; } const result = await task(); if (result !== null) { clearInterval(intervalId); resolve(result); } else if (title) { console.log(`Retrying ${title}, attempt ${retries}/${maxRetries} `); } retries++; }, interval); }); }); } export async function isTokenBridgeVaaRedeemed(tb, vaa) { try { const isRedeemed = await tb.isTransferCompleted(vaa); // Only return a real value if its true, otherwise return null // signaling that we should retry return isRedeemed ? isRedeemed : null; } catch (e) { console.error(`Caught an error checking if VAA is redeemed: ${e}\n`); return null; } } export async function fetchIbcXfer(wcIbc, msg) { try { if (isIbcMessageId(msg)) return await wcIbc.lookupTransferFromIbcMsgId(msg); else if (isTransactionIdentifier(msg)) return await wcIbc.lookupTransferFromTx(msg.txid); else if (isGatewayTransferMsg(msg) || isGatewayTransferWithPayloadMsg(msg)) return await wcIbc.lookupTransferFromMsg(msg); else throw new Error("Invalid message type:" + JSON.stringify(msg)); } catch (e) { console.error("Caught an error looking for ibc transfer: ", e); } return null; } //# sourceMappingURL=tasks.js.map