@ledgerhq/coin-ton
Version:
121 lines • 5.08 kB
JavaScript
"use strict";
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 index_1 = require("@ledgerhq/coin-framework/account/index");
const errors_1 = require("@ledgerhq/errors");
const core_1 = require("@ton/core");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("./constants");
const errors_2 = require("./errors");
const utils_1 = require("./utils");
/**
* Validate an address for account transaction
*/
const validateRecipient = (account, tx) => {
const errors = {};
if (tx.recipient) {
// Check if recipient is matching the format of account valid eth address or not
const isRecipientValidate = (0, utils_1.isAddressValid)(tx.recipient);
if (!isRecipientValidate) {
errors.recipient = new errors_1.InvalidAddress("", {
currencyName: account.currency.name,
});
}
if ((0, utils_1.addressesAreEqual)(account.freshAddress, tx.recipient)) {
errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource("", {
currencyName: account.currency.name,
});
}
}
else {
errors.recipient = new errors_1.RecipientRequired(); // ""
}
return [errors];
};
/**
* Validate the sender address for account transaction
*/
const validateSender = (account) => {
const errors = {};
// Check if sender is matching the format of account valid ton address or not
const isSenderValidate = (0, utils_1.isAddressValid)(account.freshAddress);
if (!isSenderValidate) {
errors.sender = new errors_1.InvalidAddress("", {
currencyName: account.currency.name,
});
}
return [errors];
};
const validateAmount = (account, transaction, totalSpent) => {
const errors = {};
const warnings = {};
const subAccount = (0, utils_1.findSubAccountById)(account, transaction.subAccountId ?? "");
const tokenTransfer = Boolean(subAccount && (0, index_1.isTokenAccount)(subAccount));
// if no amount or 0
if (!transaction.amount || transaction.amount.isZero()) {
errors.amount = new errors_1.AmountRequired(); // "Amount required"
}
else if (totalSpent.isGreaterThan(tokenTransfer && subAccount ? subAccount?.spendableBalance : account.balance)) {
// if not enough to make the transaction
errors.amount = new errors_1.NotEnoughBalance(); // "Sorry, insufficient funds"
}
if (tokenTransfer) {
if (account.balance.isLessThan(new bignumber_js_1.default((0, core_1.toNano)(constants_1.TOKEN_TRANSFER_MAX_FEE).toString()))) {
// if not enough for the fee to make the transaction
errors.amount = new errors_2.TonNotEnoughBalanceInParentAccount(); // "Sorry, insufficient funds in the parent account"
}
warnings.amount = new errors_2.TonExcessFee();
}
else {
if (account.balance.isLessThan(new bignumber_js_1.default((0, core_1.toNano)(constants_1.MINIMUM_REQUIRED_BALANCE).toString()))) {
errors.amount = new errors_2.TonMinimumRequired();
}
}
return [errors, warnings];
};
const validateComment = (transaction) => {
const errors = {};
// if the comment isn'transaction encrypted, it should be valid
if (transaction.comment.isEncrypted || !(0, utils_1.commentIsValid)(transaction.comment)) {
// We use transaction as an error here.
// It will be usefull to block a memo wrong format
// on the ledger-live mobile
errors.transaction = new errors_2.TonCommentInvalid();
}
return [errors];
};
const getTransactionStatus = async (account, transaction) => {
const subAccount = (0, utils_1.findSubAccountById)(account, transaction.subAccountId ?? "");
const tokenTransfer = Boolean(subAccount && (0, index_1.isTokenAccount)(subAccount));
const totalSpent = tokenTransfer ? transaction.amount : transaction.amount.plus(transaction.fees);
// Recipient related errors and warnings
const [recipientErr] = validateRecipient(account, transaction);
// Sender related errors and warnings
const [senderErr] = validateSender(account);
// Amount related errors and warnings
const [amountErr, amountWarn] = validateAmount(account, transaction, totalSpent);
// Transaction related errors and warnings
const [transactionErr] = validateComment(transaction);
const errors = {
...recipientErr,
...senderErr,
...amountErr,
...transactionErr,
};
const warnings = {
...amountWarn,
};
return {
amount: transaction.amount,
errors,
warnings,
estimatedFees: transaction.fees,
totalSpent,
};
};
exports.getTransactionStatus = getTransactionStatus;
exports.default = exports.getTransactionStatus;
//# sourceMappingURL=getTransactionStatus.js.map