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