UNPKG

@ledgerhq/coin-filecoin

Version:
97 lines 4.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.estimateMaxSpendable = void 0; const index_1 = require("@ledgerhq/coin-framework/account/index"); const common_logic_1 = require("../common-logic"); const utils_1 = require("./utils"); const errors_1 = require("@ledgerhq/errors"); const network_1 = require("../network"); const api_1 = require("../api"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const types_1 = require("../types"); const tokenAccounts_1 = require("../erc20/tokenAccounts"); const estimateMaxSpendable = async ({ account, parentAccount, transaction, }) => { // log("debug", "[estimateMaxSpendable] start fn"); if (transaction && !transaction.subAccountId) { transaction.subAccountId = account.type === "Account" ? null : account.id; } let tokenAccountTxn = false; let subAccount; const a = (0, index_1.getMainAccount)(account, parentAccount); if (account.type === utils_1.AccountType.TokenAccount) { tokenAccountTxn = true; subAccount = account; } if (transaction && transaction.subAccountId && !subAccount) { tokenAccountTxn = true; subAccount = (0, common_logic_1.getSubAccount)(a, transaction) ?? null; } let { address: sender } = (0, common_logic_1.getAddress)(a); let methodNum = utils_1.Methods.Transfer; let recipient = transaction?.recipient; const invalidAddressErr = new errors_1.InvalidAddress(undefined, { currencyName: subAccount ? subAccount.token.name : a.currency.name, }); const senderValidation = (0, network_1.validateAddress)(sender); if (!senderValidation.isValid) throw invalidAddressErr; sender = senderValidation.parsedAddress.toString(); if (recipient) { const recipientValidation = (0, network_1.validateAddress)(recipient); if (!recipientValidation.isValid) { throw invalidAddressErr; } recipient = recipientValidation.parsedAddress.toString(); methodNum = (0, network_1.isFilEthAddress)(recipientValidation.parsedAddress) || tokenAccountTxn ? utils_1.Methods.InvokeEVM : utils_1.Methods.Transfer; } let balance = new bignumber_js_1.default((await (0, api_1.fetchBalances)(sender)).spendable_balance); if (balance.eq(0)) return balance; const validatedContractAddress = (0, network_1.validateAddress)(subAccount?.token.contractAddress ?? ""); if (tokenAccountTxn && !validatedContractAddress.isValid) { throw invalidAddressErr; } const contractAddress = tokenAccountTxn && validatedContractAddress.isValid ? validatedContractAddress.parsedAddress.toString() : ""; const finalRecipient = tokenAccountTxn ? contractAddress : recipient; // If token transfer, the evm payload is required to estimate fees const params = tokenAccountTxn && transaction && subAccount ? (0, tokenAccounts_1.generateTokenTxnParams)(contractAddress, transaction.amount.isZero() ? (0, bignumber_js_1.default)(1) : transaction.amount) : undefined; const result = await (0, api_1.fetchEstimatedFees)({ to: finalRecipient, from: sender, methodNum, blockIncl: types_1.BroadcastBlockIncl, ...(params && { params: (0, tokenAccounts_1.encodeTxnParams)(params) }), // If token transfer, the eth call params are required to estimate fees ...(tokenAccountTxn && { value: "0" }), // If token transfer, the value should be 0 (avoid any native token transfer on fee estimation) }); const gasFeeCap = new bignumber_js_1.default(result.gas_fee_cap); const gasLimit = new bignumber_js_1.default(result.gas_limit); const estimatedFees = (0, utils_1.calculateEstimatedFees)(gasFeeCap, gasLimit); if (balance.lte(estimatedFees)) { if (tokenAccountTxn) { throw new errors_1.NotEnoughBalanceInParentAccount(undefined, { currencyName: a.currency.name, }); } throw new errors_1.NotEnoughSpendableBalance(undefined, { currencyName: a.currency.name, }); } balance = balance.minus(estimatedFees); if (tokenAccountTxn && subAccount) { return subAccount.spendableBalance; } // log("debug", "[estimateMaxSpendable] finish fn"); return balance; }; exports.estimateMaxSpendable = estimateMaxSpendable; //# sourceMappingURL=estimateMaxSpendable.js.map