UNPKG

@getclave/lifi-sdk

Version:

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

43 lines (42 loc) 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.waitForTransactionReceipt = waitForTransactionReceipt; const actions_1 = require("viem/actions"); const constants_1 = require("../../errors/constants"); const errors_1 = require("../../errors/errors"); const publicClient_1 = require("./publicClient"); async function waitForTransactionReceipt({ client, chainId, txHash, onReplaced, }) { let { transactionReceipt, replacementReason } = await waitForReceipt(client, txHash, onReplaced); if (!transactionReceipt?.status) { const publicClient = await (0, publicClient_1.getPublicClient)(chainId); const result = await waitForReceipt(publicClient, txHash, onReplaced); transactionReceipt = result.transactionReceipt; replacementReason = result.replacementReason; } if (transactionReceipt?.status === 'reverted') { throw new errors_1.TransactionError(constants_1.LiFiErrorCode.TransactionFailed, 'Transaction was reverted.'); } // We should only allow repriced transaction to continue the execution. // Cancelled and replaced transactions should be treated as failed. if (replacementReason === 'cancelled' || replacementReason === 'replaced') { throw new errors_1.TransactionError(constants_1.LiFiErrorCode.TransactionCanceled, 'Transaction was canceled or replaced.'); } return transactionReceipt; } async function waitForReceipt(client, txHash, onReplaced) { let replacementReason; let transactionReceipt; try { transactionReceipt = await (0, actions_1.waitForTransactionReceipt)(client, { hash: txHash, onReplaced: (response) => { replacementReason = response.reason; onReplaced?.(response); }, }); } catch { // We can ignore errors from waitForTransactionReceipt as we have a status check fallback } return { transactionReceipt, replacementReason }; }