@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
41 lines • 2.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genericEstimateMaxSpendable = genericEstimateMaxSpendable;
const account_1 = require("../../account");
const alpaca_1 = require("./alpaca");
const createTransaction_1 = require("./createTransaction");
const utils_1 = require("./utils");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const bridge_1 = require("./bridge");
function genericEstimateMaxSpendable(network, kind) {
return async ({ account, parentAccount, transaction }) => {
if (account.type === "TokenAccount") {
return account.spendableBalance;
}
const mainAccount = (0, account_1.getMainAccount)(account, parentAccount);
const alpacaApi = (0, alpaca_1.getAlpacaApi)(mainAccount.currency.id, kind);
const bridgeApi = (0, bridge_1.getBridgeApi)(mainAccount.currency, network);
const draftTransaction = {
...(0, createTransaction_1.createTransaction)(account),
...transaction,
amount: mainAccount.spendableBalance,
useAllAmount: true,
};
let fees = transaction?.fees;
if (!bignumber_js_1.default.isBigNumber(fees)) {
const { value } = await alpacaApi.estimateFees((0, utils_1.transactionToIntent)(mainAccount, draftTransaction, alpacaApi.computeIntentType));
fees = new bignumber_js_1.default(value.toString());
}
// TODO Remove the call to `validateIntent` https://ledgerhq.atlassian.net/browse/LIVE-22229
const { amount } = await alpacaApi.validateIntent((0, utils_1.transactionToIntent)(account, { ...draftTransaction }, alpacaApi.computeIntentType), (0, utils_1.extractBalances)(account, bridgeApi.getAssetFromToken), (0, utils_1.bigNumberToBigIntDeep)({ value: transaction?.fees ?? new bignumber_js_1.default(0) }));
if (["stellar", "tezos", "evm"].includes(network)) {
return amount > 0 ? new bignumber_js_1.default(amount.toString()) : new bignumber_js_1.default(0);
}
const bnFee = (0, bignumber_js_1.default)(fees.toString());
return bignumber_js_1.default.max(0, account.spendableBalance.minus(bnFee));
};
}
//# sourceMappingURL=estimateMaxSpendable.js.map