@ledgerhq/coin-tezos
Version:
108 lines • 4.66 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTransactionStatus = void 0;
const errors_1 = require("@ledgerhq/errors");
const logs_1 = require("@ledgerhq/logs");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const errors_2 = require("../types/errors");
const logic_1 = require("../logic");
const bakers_1 = require("../network/bakers");
const tzkt_1 = __importDefault(require("../network/tzkt"));
const EXISTENTIAL_DEPOSIT = new bignumber_js_1.default(275000);
const getTransactionStatus = async (account, transaction) => {
const errors = {};
const warnings = {};
let resetTotalSpent = false;
// Recipient validation logic
if (transaction.mode !== "undelegate") {
if (account.freshAddress === transaction.recipient) {
errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource();
}
else {
const { recipientError, recipientWarning } = await (0, logic_1.validateRecipient)(transaction.recipient);
if (recipientError) {
errors.recipient = recipientError;
}
if (recipientWarning) {
warnings.recipient = recipientWarning;
}
}
}
// Pre validation of amount field
const estimatedFees = transaction.estimatedFees || new bignumber_js_1.default(0);
if (transaction.mode === "send") {
if (!errors.amount && transaction.amount.eq(0) && !transaction.useAllAmount) {
resetTotalSpent = true;
errors.amount = new errors_1.AmountRequired();
}
else if (transaction.amount.gt(0) && estimatedFees.times(10).gt(transaction.amount)) {
warnings.feeTooHigh = new errors_1.FeeTooHigh();
}
const thresholdWarning = 0.5 * 10 ** account.currency.units[0].magnitude;
if (!errors.amount &&
account.balance.minus(transaction.amount).minus(estimatedFees).lt(thresholdWarning)) {
if ((0, bakers_1.isAccountDelegating)(account)) {
if (transaction.useAllAmount) {
errors.amount = new errors_1.RecommendUndelegation();
}
else {
warnings.amount = new errors_1.RecommendUndelegation();
}
}
}
}
// effective amount
// if we also have taquitoError, we interprete them and they override the previously inferred errors
if (transaction.taquitoError) {
(0, logs_1.log)("taquitoerror", String(transaction.taquitoError));
// remap taquito errors
if (transaction.taquitoError.endsWith("balance_too_low") ||
transaction.taquitoError.endsWith("subtraction_underflow")) {
if (transaction.mode === "send") {
resetTotalSpent = true;
errors.amount = new errors_1.NotEnoughBalance();
}
else {
errors.amount = new errors_1.NotEnoughBalanceToDelegate();
}
}
else if (transaction.taquitoError.endsWith("delegate.unchanged")) {
errors.recipient = new errors_2.InvalidAddressBecauseAlreadyDelegated();
}
else if (!errors.amount) {
// unidentified error case
errors.amount = new Error(transaction.taquitoError);
resetTotalSpent = true;
}
}
if (!errors.amount && account.balance.lte(0)) {
resetTotalSpent = true;
errors.amount = new errors_1.NotEnoughBalance();
}
if (!errors.amount && transaction.mode === "delegate" && account.balance.lte(estimatedFees)) {
resetTotalSpent = true;
errors.amount = new errors_1.NotEnoughBalance();
}
// Catch a specific case that requires a minimum amount
if (!errors.amount &&
transaction.mode === "send" &&
transaction.amount.lt(EXISTENTIAL_DEPOSIT) &&
(await tzkt_1.default.getAccountByAddress(transaction.recipient)).type === "empty") {
resetTotalSpent = true;
errors.amount = new errors_1.NotEnoughBalanceBecauseDestinationNotCreated("", {
minimalAmount: "0.275 XTZ",
});
}
return {
errors,
warnings,
estimatedFees,
amount: transaction.amount,
totalSpent: resetTotalSpent ? new bignumber_js_1.default(0) : transaction.amount.plus(estimatedFees),
};
};
exports.getTransactionStatus = getTransactionStatus;
//# sourceMappingURL=getTransactionStatus.js.map