@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
38 lines • 2.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genericEstimateMaxSpendable = void 0;
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"));
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 draftTransaction = {
...(0, createTransaction_1.createTransaction)(account),
...transaction,
amount: mainAccount.spendableBalance,
useAllAmount: true,
};
let fees = transaction?.fees;
if (transaction?.fees === null || transaction?.fees === undefined) {
fees = (await alpacaApi.estimateFees((0, utils_1.transactionToIntent)(mainAccount, draftTransaction, alpacaApi.computeIntentType))).value;
}
const { amount } = await alpacaApi.validateIntent((0, utils_1.transactionToIntent)(account, { ...draftTransaction }, alpacaApi.computeIntentType), { value: transaction?.fees ? BigInt(transaction.fees.toString()) : 0n });
if (["stellar", "tezos"].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));
};
}
exports.genericEstimateMaxSpendable = genericEstimateMaxSpendable;
//# sourceMappingURL=estimateMaxSpendable.js.map