@ledgerhq/coin-multiversx
Version:
Ledger MultiversX Coin integration
35 lines • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.estimateMaxSpendable = void 0;
const bignumber_js_1 = require("bignumber.js");
const account_1 = require("@ledgerhq/coin-framework/account");
const api_1 = require("./api");
const createTransaction_1 = require("./createTransaction");
/**
* Returns the maximum possible amount for transaction
*
* @param {Object} param - the account, parentAccount and transaction
*/
const estimateMaxSpendable = async ({ account, parentAccount, transaction, }) => {
const mainAccount = (0, account_1.getMainAccount)(account, parentAccount);
const tx = {
...(0, createTransaction_1.createTransaction)(account),
subAccountId: account.type === "Account" ? null : account.id,
...transaction,
useAllAmount: true,
};
const tokenAccount = tx.subAccountId
? (0, account_1.findSubAccountById)(mainAccount, tx.subAccountId)
: undefined;
if (tokenAccount) {
return tokenAccount.balance;
}
const fees = await (0, api_1.getFees)(tx);
if (fees.gt(mainAccount.spendableBalance)) {
return new bignumber_js_1.BigNumber(0);
}
return mainAccount.spendableBalance.minus(fees);
};
exports.estimateMaxSpendable = estimateMaxSpendable;
exports.default = exports.estimateMaxSpendable;
//# sourceMappingURL=estimateMaxSpendable.js.map