@ledgerhq/coin-icon
Version:
Ledger Icon Coin integration
104 lines • 4.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTransactionStatus = exports.getSendTransactionStatus = void 0;
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const logic_1 = require("./logic");
const index_1 = require("@ledgerhq/coin-framework/currencies/index");
const errors_2 = require("./errors");
const getSendTransactionStatus = async (account, transaction) => {
const errors = {};
const warnings = {};
// Check if fees are loaded
if (!transaction.fees) {
errors.fees = new errors_1.FeeNotLoaded();
}
// Validate recipient
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,
});
}
const estimatedFees = transaction.fees || new bignumber_js_1.BigNumber(0);
const amount = (0, logic_1.calculateAmount)({ account, transaction });
const totalSpent = amount.plus(estimatedFees);
// Check if amount is valid
if (amount.lte(0) && !transaction.useAllAmount) {
errors.amount = new errors_1.AmountRequired();
}
else {
const minimumBalanceExistential = (0, logic_1.getMinimumBalance)(account);
const leftover = account.spendableBalance.minus(totalSpent);
if (minimumBalanceExistential.gt(0) &&
leftover.lt(minimumBalanceExistential) &&
leftover.gt(0)) {
errors.amount = new errors_2.IconDoMaxSendInstead("Balance cannot be below {{minimumBalance}}. Send max to empty account.", {
minimumBalance: (0, index_1.formatCurrencyUnit)(account.currency.units[0], logic_1.EXISTENTIAL_DEPOSIT, {
showCode: true,
}),
});
}
else if (!errors.amount &&
!transaction.useAllAmount &&
account.spendableBalance.lte(logic_1.EXISTENTIAL_DEPOSIT.plus(logic_1.EXISTENTIAL_DEPOSIT_RECOMMENDED_MARGIN))) {
errors.amount = new errors_1.NotEnoughBalance();
}
else if (totalSpent.gt(account.spendableBalance)) {
errors.amount = new errors_1.NotEnoughBalance();
}
if (!errors.amount &&
new bignumber_js_1.BigNumber(account.iconResources?.totalDelegated)
.plus(account.iconResources?.votingPower)
.gt(0) &&
(transaction.useAllAmount ||
account.spendableBalance.minus(totalSpent).lt(logic_1.FEES_SAFETY_BUFFER))) {
warnings.amount = new errors_2.IconAllFundsWarning();
}
if (totalSpent.gt(account.spendableBalance)) {
errors.amount = new errors_1.NotEnoughBalance();
}
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount: amount.lt(0) ? new bignumber_js_1.BigNumber(0) : amount,
totalSpent,
});
};
exports.getSendTransactionStatus = getSendTransactionStatus;
const getTransactionStatus = async (account, transaction) => {
switch (transaction.mode) {
case "send":
return await (0, exports.getSendTransactionStatus)(account, transaction);
default: {
const errors = {};
const warnings = {};
const amount = (0, logic_1.calculateAmount)({ account, transaction });
const estimatedFees = transaction.fees || new bignumber_js_1.BigNumber(0);
const totalSpent = amount.plus(estimatedFees);
if (totalSpent.gt(account.spendableBalance)) {
errors.amount = new errors_1.NotEnoughBalance();
}
// Validate amount
if (amount.lte(0) && !transaction.useAllAmount) {
errors.amount = new errors_1.AmountRequired();
}
return {
errors,
warnings,
estimatedFees,
amount: amount.lt(0) ? new bignumber_js_1.BigNumber(0) : amount,
totalSpent,
};
}
}
};
exports.getTransactionStatus = getTransactionStatus;
//# sourceMappingURL=getTransactionStatus.js.map