UNPKG

@ledgerhq/coin-multiversx

Version:
107 lines 5.26 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 bignumber_js_1 = __importDefault(require("bignumber.js")); const errors_1 = require("@ledgerhq/errors"); const currencies_1 = require("@ledgerhq/coin-framework/currencies"); const account_1 = require("@ledgerhq/coin-framework/account"); const constants_1 = require("./constants"); const logic_1 = require("./logic"); const errors_2 = require("./errors"); const getTransactionStatus = async (account, transaction) => { const errors = {}; const warnings = {}; if (!transaction.recipient) { errors.recipient = new errors_1.RecipientRequired(); } else if ((0, logic_1.isSelfTransaction)(account, transaction)) { errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource(); } else if (!(0, logic_1.isValidAddress)(transaction.recipient)) { errors.recipient = new errors_1.InvalidAddress("", { currencyName: account.currency.name, }); } if (!transaction.fees) { errors.fees = new errors_1.FeeNotLoaded(); } if (!errors.amount && transaction.amount.eq(0) && !transaction.useAllAmount && !["unDelegate", "withdraw", "reDelegateRewards", "claimRewards"].includes(transaction.mode)) { errors.amount = new errors_1.AmountRequired(); } let totalSpent = new bignumber_js_1.default(0); // Will be in token amount for token transactions let totalSpentEgld = new bignumber_js_1.default(0); // Amount spent in main currency (EGLD) const tokenAccount = (transaction.subAccountId && account.subAccounts && account.subAccounts.find(ta => ta.id === transaction.subAccountId)) || null; if (tokenAccount) { totalSpent = transaction.amount; totalSpentEgld = transaction.fees || new bignumber_js_1.default(0); if (!errors.amount && transaction.amount.gt(tokenAccount.balance)) { errors.amount = new errors_1.NotEnoughBalance(); } if (!errors.amount && !totalSpentEgld.decimalPlaces(constants_1.DECIMALS_LIMIT).isEqualTo(totalSpentEgld)) { errors.amount = new errors_2.MultiversXDecimalsLimitReached(); } } else { totalSpent = totalSpentEgld = (0, logic_1.isAmountSpentFromBalance)(transaction.mode) ? transaction.fees?.plus(transaction.amount) || transaction.amount : transaction.fees || new bignumber_js_1.default(0); if (transaction.mode === "send" && transaction.fees && transaction.amount.div(10).lt(transaction.fees)) { warnings.feeTooHigh = new errors_1.FeeTooHigh(); } // All delegate and undelegate transactions must have an amount >= 1 EGLD if (!errors.amount && transaction.amount.lt(constants_1.MIN_DELEGATION_AMOUNT)) { const formattedAmount = (0, currencies_1.formatCurrencyUnit)((0, account_1.getAccountCurrency)(account).units[0], constants_1.MIN_DELEGATION_AMOUNT, { showCode: true, }); if (transaction.mode === "delegate") { errors.amount = new errors_2.MultiversXMinDelegatedAmountError("", { formattedAmount, }); } else if (transaction.mode === "unDelegate") { errors.amount = new errors_2.MultiversXMinUndelegatedAmountError("", { formattedAmount, }); } } // When undelegating, unless undelegating all, the delegation must remain >= 1 EGLD const delegationBalance = account.multiversxResources.delegations.find(d => d.contract === transaction.recipient)?.userActiveStake; const delegationRemainingBalance = new bignumber_js_1.default(delegationBalance || 0).minus(transaction.amount); const delegationBalanceForbidden = delegationRemainingBalance.gt(0) && delegationRemainingBalance.lt(constants_1.MIN_DELEGATION_AMOUNT); if (!errors.amount && transaction.mode === "unDelegate" && delegationBalanceForbidden) { const formattedAmount = (0, currencies_1.formatCurrencyUnit)((0, account_1.getAccountCurrency)(account).units[0], constants_1.MIN_DELEGATION_AMOUNT, { showCode: true, }); errors.amount = new errors_2.MultiversXDelegationBelowMinimumError("", { formattedAmount, }); } } if (!errors.amount && totalSpentEgld.gt(account.spendableBalance)) { errors.amount = tokenAccount || !["delegate", "send"].includes(transaction.mode) ? new errors_2.NotEnoughEGLDForFees() : new errors_1.NotEnoughBalance(); } return { errors, warnings, estimatedFees: transaction.fees || new bignumber_js_1.default(0), amount: transaction.amount, totalSpent, }; }; exports.getTransactionStatus = getTransactionStatus; exports.default = exports.getTransactionStatus; //# sourceMappingURL=getTransactionStatus.js.map