UNPKG

@getclave/lifi-sdk

Version:

LI.FI Any-to-Any Cross-Chain-Swap SDK

69 lines (68 loc) 3.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.waitForDestinationChainTransaction = waitForDestinationChainTransaction; const constants_1 = require("../errors/constants"); const getTransactionMessage_1 = require("../utils/getTransactionMessage"); const waitForTransactionStatus_1 = require("./waitForTransactionStatus"); async function waitForDestinationChainTransaction(step, process, fromChain, toChain, statusManager, pollingInterval) { const transactionHash = process.txHash; let processType = process.type; try { // Wait for the transaction status on the destination chain if (!transactionHash) { throw new Error('Transaction hash is undefined.'); } const isBridgeExecution = fromChain.id !== toChain.id; if (isBridgeExecution) { const receivingChainProcess = statusManager.findOrCreateProcess({ step, type: 'RECEIVING_CHAIN', status: 'PENDING', chainId: toChain.id, startedAt: process.doneAt, }); processType = receivingChainProcess.type; } const statusResponse = (await (0, waitForTransactionStatus_1.waitForTransactionStatus)(transactionHash, statusManager, processType, step, pollingInterval)); const statusReceiving = statusResponse.receiving; // Update process status statusManager.updateProcess(step, processType, 'DONE', { substatus: statusResponse.substatus, substatusMessage: statusResponse.substatusMessage, txHash: statusReceiving?.txHash, txLink: `${toChain.metamask.blockExplorerUrls[0]}tx/${statusReceiving?.txHash}`, }); // Update execution status statusManager.updateExecution(step, 'DONE', { ...(statusResponse.sending.amount && { fromAmount: statusResponse.sending.amount, }), ...(statusReceiving?.amount && { toAmount: statusReceiving.amount }), ...(statusReceiving?.token && { toToken: statusReceiving.token }), gasCosts: [ { amount: statusResponse.sending.gasAmount, amountUSD: statusResponse.sending.gasAmountUSD, token: statusResponse.sending.gasToken, estimate: statusResponse.sending.gasUsed, limit: statusResponse.sending.gasUsed, price: statusResponse.sending.gasPrice, type: 'SEND', }, ], }); return step; } catch (e) { const htmlMessage = await (0, getTransactionMessage_1.getTransactionFailedMessage)(step, `${toChain.metamask.blockExplorerUrls[0]}tx/${transactionHash}`); statusManager.updateProcess(step, processType, 'FAILED', { error: { code: constants_1.LiFiErrorCode.TransactionFailed, message: 'Failed while waiting for status of destination chain transaction.', htmlMessage, }, }); statusManager.updateExecution(step, 'FAILED'); throw e; } }