UNPKG

@wormhole-foundation/sdk-connect

Version:

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

51 lines 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkAndCompleteTransfer = void 0; const route_js_1 = require("./route.js"); const types_js_1 = require("../types.js"); /** * track the transfer until the destination is initiated * * @param route The route that can be used to track the receipt * @param receipt The receipt to track * @param destinationSigner The signer for the destination chain if */ async function checkAndCompleteTransfer(route, receipt, destinationSigner, timeout = 120 * 1000, // byo logger but im dumping to console rn 🙃 log = console.log) { const start = Date.now(); log("Checking transfer state..."); // overwrite receipt var as we receive updates, will return when it's complete // but can be called again if the destination is not finalized // this construct is to drain an async generator and return the last value for await (receipt of route.track(receipt, 120 * 1000)) { log("Current Transfer State: ", types_js_1.TransferState[receipt.state]); } // gucci if ((0, types_js_1.isCompleted)(receipt)) return receipt; // if the route is one we need to complete, do it if ((0, route_js_1.isManual)(route) && (0, types_js_1.isAttested)(receipt) && destinationSigner) { log("Completing transfer..."); const completedTxids = await route.complete(destinationSigner, receipt); log("Completed transfer with txids: ", completedTxids); } // if the route is one we need to finalize, do it if ((0, route_js_1.isFinalizable)(route) && (0, types_js_1.isRedeemed)(receipt) && destinationSigner) { log("Finalizing transfer..."); const completedTxids = await route.finalize(destinationSigner, receipt); log("Finalized transfer with txids: ", completedTxids); } const leftover = timeout - (Date.now() - start); // do we still have time? if (leftover > 0) { // give it a second, computers need to rest sometimes const wait = 2 * 1000; log(`Transfer not complete, trying again in a ${wait}ms...`); await new Promise((resolve) => setTimeout(resolve, wait)); return checkAndCompleteTransfer(route, receipt, destinationSigner, leftover); } return receipt; } exports.checkAndCompleteTransfer = checkAndCompleteTransfer; //# sourceMappingURL=common.js.map