UNPKG

@ledgerhq/coin-canton

Version:
78 lines 3.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTransactionStatus = void 0; const errors_1 = require("@ledgerhq/errors"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const index_1 = require("@ledgerhq/coin-framework/currencies/index"); const utils_1 = require("../common-logic/utils"); const config_1 = __importDefault(require("../config")); const getTransactionStatus = async (account, transaction) => { const errors = {}; const warnings = {}; // reserveAmount is the minimum amount of currency that an account must hold in order to stay activated const reserveAmount = new bignumber_js_1.default(config_1.default.getCoinConfig().minReserve); const estimatedFees = new bignumber_js_1.default(transaction.fee || 0); const totalSpent = new bignumber_js_1.default(transaction.amount).plus(estimatedFees); const amount = new bignumber_js_1.default(transaction.amount); if (amount.gt(0) && estimatedFees.times(10).gt(amount)) { // if the fee is more than 10 times the amount, we warn the user that fee is high compared to what he is sending warnings.feeTooHigh = new errors_1.FeeTooHigh(); } if (!transaction.fee) { // if the fee is not loaded, we can't do much errors.fee = new errors_1.FeeNotLoaded(); } else if (transaction.fee.eq(0)) { // On some chains, 0 fee could still work so this is optional errors.fee = new errors_1.FeeRequired(); } else if (totalSpent.gt(account.balance.minus(reserveAmount))) { // if the total spent is greater than the balance minus the reserve amount, tx is invalid errors.amount = new errors_1.NotEnoughSpendableBalance("", { minimumAmount: (0, index_1.formatCurrencyUnit)(account.currency.units[0], reserveAmount, { disableRounding: true, useGrouping: false, showCode: true, }), }); } else if (transaction.recipient && transaction.amount.lt(reserveAmount)) { // if we send an amount lower than reserve amount AND target account is new, we need to warn the user that the target account will not be activated errors.amount = new errors_1.NotEnoughBalanceBecauseDestinationNotCreated("", { minimalAmount: (0, index_1.formatCurrencyUnit)(account.currency.units[0], reserveAmount, { disableRounding: true, useGrouping: false, showCode: true, }), }); } if (!transaction.recipient) { errors.recipient = new errors_1.RecipientRequired(""); } else if (account.freshAddress === transaction.recipient) { // we want to prevent user from sending to themselves (even if it's technically feasible) errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource(); } else if (!(0, utils_1.isRecipientValid)(transaction.recipient)) { // We want to prevent user from sending to an invalid address errors.recipient = new errors_1.InvalidAddress("", { currencyName: account.currency.name, }); } if (!errors.amount && amount.eq(0)) { // if the amount is 0, we prevent the user from sending the tx (even if it's technically feasible) errors.amount = new errors_1.AmountRequired(); } return { errors, warnings, estimatedFees, amount, totalSpent, }; }; exports.getTransactionStatus = getTransactionStatus; //# sourceMappingURL=getTransactionStatus.js.map