@ledgerhq/coin-filecoin
Version:
Ledger Filecoin Coin integration
75 lines • 2.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTransactionStatus = void 0;
const errors_1 = require("@ledgerhq/errors");
const network_1 = require("../network");
const common_logic_1 = require("../common-logic");
const utils_1 = require("./utils");
const errors_2 = require("../errors");
const getTransactionStatus = async (account, transaction) => {
// log("debug", "[getTransactionStatus] start fn");
const errors = {};
const warnings = {};
const { balance } = account;
const { address } = (0, common_logic_1.getAddress)(account);
const subAccount = (0, common_logic_1.getSubAccount)(account, transaction);
const { recipient, useAllAmount, gasPremium, gasFeeCap, gasLimit } = transaction;
let { amount } = transaction;
const invalidAddressErr = new errors_1.InvalidAddress(undefined, {
currencyName: account.currency.name,
});
if (!recipient)
errors.recipient = new errors_1.RecipientRequired();
else if (!(0, network_1.validateAddress)(recipient).isValid)
errors.recipient = invalidAddressErr;
else if (!(0, network_1.validateAddress)(address).isValid)
errors.sender = invalidAddressErr;
if (gasFeeCap.eq(0) || gasPremium.eq(0) || gasLimit.eq(0))
errors.gas = new errors_1.FeeNotLoaded();
// This is the worst case scenario (the tx won't cost more than this value)
const estimatedFees = (0, utils_1.calculateEstimatedFees)(gasFeeCap, gasLimit);
let totalSpent;
if (useAllAmount && !subAccount) {
totalSpent = account.spendableBalance;
amount = totalSpent.minus(estimatedFees);
if (amount.lte(0) || totalSpent.gt(balance)) {
errors.amount = new errors_1.NotEnoughBalance();
}
}
if (subAccount) {
totalSpent = estimatedFees;
if (totalSpent.gt(account.spendableBalance))
errors.amount = new errors_1.NotEnoughBalance();
}
else {
totalSpent = amount.plus(estimatedFees);
if (amount.eq(0)) {
errors.amount = new errors_1.AmountRequired();
}
else if (totalSpent.gt(account.spendableBalance))
errors.amount = new errors_1.NotEnoughBalance();
}
if (subAccount) {
const spendable = subAccount.spendableBalance;
if (transaction.amount.gt(spendable)) {
errors.amount = new errors_1.NotEnoughBalance();
}
if (useAllAmount) {
amount = spendable;
}
totalSpent = amount;
if (recipient && !(0, network_1.isRecipientValidForTokenTransfer)(recipient)) {
errors.recipient = new errors_2.InvalidRecipientForTokenTransfer();
}
}
// log("debug", "[getTransactionStatus] finish fn");
return {
errors,
warnings,
estimatedFees,
amount,
totalSpent,
};
};
exports.getTransactionStatus = getTransactionStatus;
//# sourceMappingURL=getTransactionStatus.js.map