@ledgerhq/coin-mina
Version:
62 lines • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const common_logic_1 = require("../common-logic");
const errors_2 = require("./errors");
const currencies_1 = require("@ledgerhq/coin-framework/currencies");
const getTransactionStatus = async (a, t) => {
const errors = {};
const warnings = {};
const useAllAmount = !!t.useAllAmount;
if (t.fees.fee.lte(0)) {
errors.fees = new errors_1.FeeNotLoaded();
}
if (!t.recipient) {
errors.recipient = new errors_1.RecipientRequired();
}
if (t.recipient && !(0, common_logic_1.isValidAddress)(t.recipient)) {
errors.recipient = new errors_1.InvalidAddress("", {
currencyName: a.currency.name,
});
}
if (t.memo && !(0, common_logic_1.isValidMemo)(t.memo)) {
errors.transaction = new errors_2.InvalidMemoMina();
}
if (t.recipient === a.freshAddress) {
errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource();
}
if (t.fees?.accountCreationFee.gt(0)) {
const fee = (0, currencies_1.formatCurrencyUnit)(a.currency.units[0], t.fees.accountCreationFee, {
showCode: true,
disableRounding: true,
});
warnings.recipient = new errors_2.AccountCreationFeeWarning(undefined, {
fee,
});
if (t.amount.lt(t.fees.accountCreationFee) && t.amount.gt(0)) {
errors.amount = new errors_2.AmountTooSmall(undefined, {
amount: fee,
});
}
}
const estimatedFees = t.fees.fee || new bignumber_js_1.BigNumber(0);
const maxAmountWithFees = (0, common_logic_1.getMaxAmount)(a, t, estimatedFees);
const totalSpent = (0, common_logic_1.getTotalSpent)(a, t, estimatedFees);
const amount = useAllAmount ? maxAmountWithFees : new bignumber_js_1.BigNumber(t.amount);
if (amount.lte(0) && !t.useAllAmount) {
errors.amount = new errors_1.AmountRequired();
}
if (amount.gt(maxAmountWithFees)) {
errors.amount = new errors_1.NotEnoughBalance();
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount,
totalSpent,
});
};
exports.default = getTransactionStatus;
//# sourceMappingURL=getTransactionStatus.js.map