@ledgerhq/coin-algorand
Version:
Ledger Algorand Coin integration
44 lines • 1.98 kB
JavaScript
;
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 bignumber_js_1 = require("bignumber.js");
const cryptoassets_1 = require("@ledgerhq/cryptoassets");
const index_1 = require("@ledgerhq/coin-framework/account/index");
const getFeesForTransaction_1 = require("./getFeesForTransaction");
const createTransaction_1 = __importDefault(require("./createTransaction"));
const logic_1 = require("./logic");
const estimateMaxSpendable = async ({ account, parentAccount, transaction }) => {
const mainAccount = (0, index_1.getMainAccount)(account, parentAccount);
const { algorandResources } = mainAccount;
if (!algorandResources) {
throw new Error("Algorand account expected");
}
const tx = {
...(0, createTransaction_1.default)(account),
subAccountId: account.type === "Account" ? null : account.id,
...transaction,
recipient: transaction?.recipient || (0, cryptoassets_1.getAbandonSeedAddress)(mainAccount.currency.id),
useAllAmount: true,
};
const tokenAccount = tx.subAccountId
? (0, index_1.findSubAccountById)(mainAccount, tx.subAccountId)
: undefined;
if (tokenAccount) {
return tokenAccount.balance;
}
else {
const fees = await (0, getFeesForTransaction_1.getEstimatedFees)(mainAccount, tx);
let maxSpendable = (0, logic_1.computeAlgoMaxSpendable)({
accountBalance: mainAccount.balance,
nbAccountAssets: algorandResources.nbAssets,
mode: tx.mode,
});
maxSpendable = maxSpendable.minus(fees);
return maxSpendable.gte(0) ? maxSpendable : new bignumber_js_1.BigNumber(0);
}
};
exports.estimateMaxSpendable = estimateMaxSpendable;
//# sourceMappingURL=estimateMaxSpendable.js.map