@lifi/sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
37 lines • 1.91 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkBalance = void 0;
const viem_1 = require("viem");
const errors_js_1 = require("../errors/errors.js");
const balance_js_1 = require("../services/balance.js");
const sleep_js_1 = require("../utils/sleep.js");
const checkBalance = async (walletAddress, step, depth = 0) => {
const token = await (0, balance_js_1.getTokenBalance)(walletAddress, step.action.fromToken);
if (token) {
const currentBalance = token.amount ?? 0n;
const neededBalance = BigInt(step.action.fromAmount);
if (currentBalance < neededBalance) {
if (depth <= 3) {
await (0, sleep_js_1.sleep)(200);
await (0, exports.checkBalance)(walletAddress, step, depth + 1);
}
else if ((neededBalance *
BigInt((1 - (step.action.slippage ?? 0)) * 1_000_000_000)) /
1000000000n <=
currentBalance) {
step.action.fromAmount = currentBalance.toString();
}
else {
const needed = (0, viem_1.formatUnits)(neededBalance, token.decimals);
const current = (0, viem_1.formatUnits)(currentBalance, token.decimals);
let errorMessage = `Your ${token.symbol} balance is too low, you try to transfer ${needed} ${token.symbol}, but your wallet only holds ${current} ${token.symbol}. No funds have been sent.`;
if (currentBalance !== 0n) {
errorMessage += `If the problem consists, please delete this transfer and start a new one with a maximum of ${current} ${token.symbol}.`;
}
throw new errors_js_1.BalanceError('The balance is too low.', new Error(errorMessage));
}
}
}
};
exports.checkBalance = checkBalance;
//# sourceMappingURL=checkBalance.js.map
;