UNPKG

@ledgerhq/coin-near

Version:
135 lines 6.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTransactionStatus = void 0; const bignumber_js_1 = require("bignumber.js"); const errors_1 = require("@ledgerhq/errors"); const index_1 = require("@ledgerhq/cryptoassets/index"); const index_2 = require("@ledgerhq/coin-framework/currencies/index"); const logic_1 = require("./logic"); const api_1 = require("./api"); const preload_1 = require("./preload"); const errors_2 = require("./errors"); const constants_1 = require("./constants"); const getTransactionStatus = async (account, transaction) => { if (transaction.mode === "send") { return await getSendTransactionStatus(account, transaction); } const errors = {}; const warnings = {}; const useAllAmount = !!transaction.useAllAmount; if (!transaction.fees) { errors.fees = new errors_1.FeeNotLoaded(); } const estimatedFees = transaction.fees || new bignumber_js_1.BigNumber(0); const maxAmount = (0, logic_1.getMaxAmount)(account, transaction, estimatedFees); const maxAmountWithFees = (0, logic_1.getMaxAmount)(account, transaction); const spendableBalanceWithFees = (0, logic_1.getMaxAmount)(account, { ...transaction, mode: "stake" }); const stakingThreshold = (0, logic_1.getYoctoThreshold)(); const totalSpent = (0, logic_1.getTotalSpent)(account, transaction, estimatedFees); const amount = useAllAmount ? maxAmount : new bignumber_js_1.BigNumber(transaction.amount); const isStakeAndNotEnoughBalance = transaction.mode === "stake" && (totalSpent.gt(maxAmountWithFees) || maxAmountWithFees.lt(estimatedFees)); const isUnstakeOrWithdrawAndNotEnoughBalance = ["unstake", "withdraw"].includes(transaction.mode) && (totalSpent.gt(spendableBalanceWithFees) || spendableBalanceWithFees.lt(estimatedFees)); if (isStakeAndNotEnoughBalance || isUnstakeOrWithdrawAndNotEnoughBalance) { errors.amount = new errors_1.NotEnoughBalance(); } else if (["stake", "unstake", "withdraw"].includes(transaction.mode) && amount.lt(stakingThreshold)) { const currency = (0, index_1.getCryptoCurrencyById)("near"); const formattedStakingThreshold = (0, index_2.formatCurrencyUnit)(currency.units[0], stakingThreshold.plus(constants_1.YOCTO_THRESHOLD_VARIATION), { showCode: true, }); errors.amount = new errors_2.NearStakingThresholdNotMet(undefined, { threshold: formattedStakingThreshold, }); } else if (transaction.mode === "unstake" && amount.gt(maxAmount)) { errors.amount = new errors_2.NearNotEnoughStaked(); } else if (transaction.mode === "withdraw" && amount.gt(maxAmount)) { errors.amount = new errors_2.NearNotEnoughAvailable(); } else if (amount.lte(0) && !transaction.useAllAmount) { errors.amount = new errors_1.AmountRequired(); } if (transaction.mode === "stake" && !errors.amount && transaction.useAllAmount) { warnings.amount = new errors_2.NearUseAllAmountStakeWarning(); } return { errors, warnings, estimatedFees, amount, totalSpent, }; }; exports.getTransactionStatus = getTransactionStatus; const getSendTransactionStatus = async (account, transaction) => { const errors = {}; const warnings = {}; const useAllAmount = !!transaction.useAllAmount; const { storageCost } = (0, preload_1.getCurrentNearPreloadData)(); const newAccountStorageCost = storageCost.multipliedBy(constants_1.NEW_ACCOUNT_SIZE); const currency = (0, index_1.getCryptoCurrencyById)("near"); const formattedNewAccountStorageCost = (0, index_2.formatCurrencyUnit)(currency.units[0], newAccountStorageCost, { showCode: true, }); let recipientIsNewAccount; if (!transaction.recipient) { errors.recipient = new errors_1.RecipientRequired(); } else if (!(0, logic_1.isValidAddress)(transaction.recipient)) { errors.recipient = new errors_1.InvalidAddress("", { currencyName: account.currency.name, }); } else { const accountDetails = await (0, api_1.fetchAccountDetails)(transaction.recipient); if (!accountDetails) { recipientIsNewAccount = true; if ((0, logic_1.isImplicitAccount)(transaction.recipient)) { warnings.recipient = new errors_2.NearNewAccountWarning(undefined, { formattedNewAccountStorageCost, }); } else { errors.recipient = new errors_2.NearNewNamedAccountError(); } } } if (account.freshAddress === transaction.recipient) { warnings.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource(); } if (!transaction.fees) { errors.fees = new errors_1.FeeNotLoaded(); } const estimatedFees = transaction.fees || new bignumber_js_1.BigNumber(0); const totalSpent = (0, logic_1.getTotalSpent)(account, transaction, estimatedFees); const maxAmount = (0, logic_1.getMaxAmount)(account, transaction, estimatedFees); const maxAmountWithFees = (0, logic_1.getMaxAmount)(account, transaction); const amount = useAllAmount ? maxAmount : new bignumber_js_1.BigNumber(transaction.amount); if (totalSpent.gt(maxAmountWithFees) || maxAmountWithFees.lt(estimatedFees)) { errors.amount = new errors_1.NotEnoughBalance(); } else if (amount.lte(0) && !transaction.useAllAmount) { errors.amount = new errors_1.AmountRequired(); } else if (recipientIsNewAccount && amount.lt(newAccountStorageCost)) { errors.amount = new errors_2.NearActivationFeeNotCovered(undefined, { formattedNewAccountStorageCost, }); } if (account.nearResources?.stakingPositions.length > 0 && useAllAmount) { warnings.amount = new errors_2.NearRecommendUnstake(); } return { errors, warnings, estimatedFees, amount, totalSpent, }; }; exports.default = exports.getTransactionStatus; //# sourceMappingURL=getTransactionStatus.js.map