UNPKG

@ledgerhq/coin-ton

Version:
39 lines 1.67 kB
import { getMainAccount, isTokenAccount } from "@ledgerhq/coin-framework/account/index"; import { BigNumber } from "bignumber.js"; import { fetchAccountInfo } from "./bridge/bridgeHelpers/api"; import { buildTonTransaction, findSubAccountById, getTonEstimatedFees } from "./utils"; const estimateMaxSpendable = async ({ account, parentAccount, transaction, }) => { const mainAccount = getMainAccount(account, parentAccount); let balance = mainAccount.spendableBalance; if (balance.eq(0)) return balance; const accountInfo = await fetchAccountInfo(mainAccount.freshAddress); const isTokenType = isTokenAccount(account); if (transaction && !transaction.subAccountId) { transaction.subAccountId = isTokenType ? account.id : null; } let tokenAccountTxn = false; let subAccount; if (isTokenType) { tokenAccountTxn = true; subAccount = account; } if (transaction?.subAccountId && !subAccount) { tokenAccountTxn = true; subAccount = findSubAccountById(mainAccount, transaction.subAccountId || "") ?? null; } if (tokenAccountTxn && subAccount) { return subAccount.spendableBalance; } let estimatedFees = BigNumber(0); if (transaction) { const tonTx = buildTonTransaction(transaction, accountInfo.seqno, mainAccount); estimatedFees = await getTonEstimatedFees(mainAccount, accountInfo.status === "uninit", tonTx); } if (balance.lte(estimatedFees)) return new BigNumber(0); balance = balance.minus(estimatedFees); return balance; }; export default estimateMaxSpendable; //# sourceMappingURL=estimateMaxSpendable.js.map