UNPKG

@ledgerhq/coin-celo

Version:
112 lines 5.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTransactionStatus = void 0; const errors_1 = require("@ledgerhq/errors"); const bignumber_js_1 = require("bignumber.js"); const address_1 = require("@celo/utils/lib/address"); const logic_1 = require("../logic"); const errors_2 = require("../errors"); const sdk_1 = require("../network/sdk"); const index_1 = require("@ledgerhq/coin-framework/account/index"); const kit = (0, sdk_1.celoKit)(); // Arbitrary buffer for paying fees of next transactions. 0.05 Celo for ~100 transactions const FEES_SAFETY_BUFFER = new bignumber_js_1.BigNumber(5000000000000000); const getTransactionStatus = async (account, transaction) => { const errors = {}; const warnings = {}; const useAllAmount = !!transaction.useAllAmount; if (account.freshAddress === transaction.recipient) { errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource(); } if (!transaction.fees || !transaction.fees.gt(0)) { errors.fees = new errors_1.FeeNotLoaded(); } const pendingOperationAmounts = (0, logic_1.getPendingStakingOperationAmounts)(account); const lockedGold = await kit.contracts.getLockedGold(); const nonvotingLockedGoldBalance = await lockedGold.getAccountNonvotingLockedGold(account.freshAddress); // Deduct pending vote operations from the non-voting locked balance const totalNonVotingLockedBalance = nonvotingLockedGoldBalance.minus(pendingOperationAmounts.vote); // Deduct pending lock operations from the spendable balance const totalSpendableBalance = account.spendableBalance.minus(pendingOperationAmounts.lock); const estimatedFees = transaction.fees || new bignumber_js_1.BigNumber(0); const tokenAccount = (0, index_1.findSubAccountById)(account, transaction.subAccountId || ""); const isTokenTransaction = tokenAccount?.type === "TokenAccount"; let amount = new bignumber_js_1.BigNumber(0); if (useAllAmount && (transaction.mode === "unlock" || transaction.mode === "vote")) { amount = totalNonVotingLockedBalance ?? new bignumber_js_1.BigNumber(0); } else if (useAllAmount && transaction.mode === "revoke") { const revoke = (0, logic_1.getVote)(account, transaction.recipient, transaction.index); if (revoke?.amount) amount = revoke.amount; } else if (useAllAmount) { amount = isTokenTransaction ? tokenAccount.spendableBalance : totalSpendableBalance.minus(estimatedFees); } else { amount = new bignumber_js_1.BigNumber(transaction.amount); } if (amount.lt(0)) amount = new bignumber_js_1.BigNumber(0); if ((account.celoResources?.lockedBalance.gt(0) || !!account.celoResources?.pendingWithdrawals?.length) && (transaction.useAllAmount || totalSpendableBalance.minus(amount).lt(FEES_SAFETY_BUFFER)) && ["send", "lock"].includes(transaction.mode)) { warnings.amount = new errors_2.CeloAllFundsWarning(); } if (!["register", "withdraw", "activate"].includes(transaction.mode)) { if (amount.lte(0) && !useAllAmount) { errors.amount = new errors_1.AmountRequired(); } } const totalSpent = amount.plus(estimatedFees); if (transaction.mode === "unlock" || transaction.mode === "vote") { if (!errors.amount && totalNonVotingLockedBalance && amount.gt(totalNonVotingLockedBalance)) { errors.amount = new errors_1.NotEnoughBalance(); } } else if (transaction.mode === "revoke") { const revoke = (0, logic_1.getVote)(account, transaction.recipient, transaction.index); if (!errors.amount && revoke?.amount && amount.gt(revoke.amount)) errors.amount = new errors_1.NotEnoughBalance(); } else { if (!errors.amount && totalSpent.gt(totalSpendableBalance)) { errors.amount = new errors_1.NotEnoughBalance(); } } if (!errors.amount && totalSpendableBalance.lt(estimatedFees)) { errors.amount = new errors_1.NotEnoughBalance(); } if (transaction.mode === "send") { if (!transaction.recipient && !errors.recipient) { errors.recipient = new errors_1.RecipientRequired(); } else if (!(0, address_1.isValidAddress)(transaction.recipient) && !errors.recipient) { errors.recipient = new errors_1.InvalidAddress("", { currencyName: account.currency.name, }); } if (isTokenTransaction) { return { errors, warnings, estimatedFees, amount, totalSpent: amount, }; } } return { errors, warnings, estimatedFees, amount, totalSpent, }; }; exports.getTransactionStatus = getTransactionStatus; exports.default = exports.getTransactionStatus; //# sourceMappingURL=getTransactionStatus.js.map