@ledgerhq/coin-stacks
Version:
Ledger Stacks Coin integration
125 lines • 4.41 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 errors_1 = require("@ledgerhq/errors");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("../constants");
const errors_2 = require("../errors");
const addresses_1 = require("./utils/addresses");
const misc_1 = require("./utils/misc");
const token_1 = require("./utils/token");
/**
* Validates the recipient address
*/
function validateRecipient(recipient, accountAddress, currencyName, errors) {
if (!recipient) {
errors.recipient = new errors_1.RecipientRequired();
return;
}
if (!(0, addresses_1.validateAddress)(recipient).isValid) {
errors.recipient = new errors_1.InvalidAddress("", { currencyName });
return;
}
if (accountAddress === recipient) {
errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource();
}
}
/**
* Validates the transaction fee
*/
function validateFee(fee, errors) {
if (!fee || fee.eq(0)) {
errors.gas = new errors_1.FeeNotLoaded();
}
}
/**
* Handles amount validation and calculation for token transfers
*/
function handleTokenTransaction(amount, useAllAmount, tokenSpendable, estimatedFees, spendableBalance, errors) {
// Check if main account has enough balance to pay fees
if (estimatedFees.gt(spendableBalance)) {
errors.amount = new errors_1.NotEnoughBalance();
}
// Validate token amount
if (amount.gt(tokenSpendable)) {
errors.amount = new errors_1.NotEnoughBalance();
}
else if (amount.lte(0)) {
errors.amount = new errors_1.AmountRequired();
}
// Handle use all amount for tokens
const finalAmount = useAllAmount ? tokenSpendable : amount;
return {
amount: finalAmount,
totalSpent: finalAmount,
};
}
/**
* Handles amount validation and calculation for STX transfers
*/
function handleStxTransaction(amount, useAllAmount, estimatedFees, spendableBalance, errors) {
if (useAllAmount) {
const totalSpent = spendableBalance;
const finalAmount = totalSpent.minus(estimatedFees);
if (finalAmount.lte(0)) {
errors.amount = new errors_1.NotEnoughBalance();
}
return { amount: finalAmount, totalSpent };
}
const totalSpent = amount.plus(estimatedFees);
if (amount.lte(0)) {
errors.amount = new errors_1.AmountRequired();
}
else if (totalSpent.gt(spendableBalance)) {
errors.amount = new errors_1.NotEnoughBalance();
}
return { amount, totalSpent };
}
/**
* Validates memo length
*/
function validateMemo(memo, errors) {
const memoBytesLength = Buffer.from(memo ?? "", "utf-8").byteLength;
if (memoBytesLength > constants_1.STACKS_MAX_MEMO_SIZE) {
errors.transaction = new errors_2.StacksMemoTooLong();
}
}
const getTransactionStatus = async (account, transaction) => {
const errors = {};
const warnings = {};
const { spendableBalance } = account;
const { address } = (0, misc_1.getAddress)(account);
const subAccount = (0, token_1.getSubAccount)(account, transaction);
const { memo, recipient, useAllAmount, fee } = transaction;
let { amount } = transaction;
// Validate recipient and fee
validateRecipient(recipient, address, account.currency.name, errors);
validateFee(fee, errors);
const estimatedFees = fee || new bignumber_js_1.default(0);
let totalSpent;
// Handle token vs STX transactions
if (subAccount) {
const result = handleTokenTransaction(amount, useAllAmount, subAccount.spendableBalance, estimatedFees, spendableBalance, errors);
amount = result.amount;
totalSpent = result.totalSpent;
}
else {
const result = handleStxTransaction(amount, useAllAmount, estimatedFees, spendableBalance, errors);
amount = result.amount;
totalSpent = result.totalSpent;
}
// Validate memo
validateMemo(memo, errors);
return {
errors,
warnings,
estimatedFees,
amount,
totalSpent,
};
};
exports.getTransactionStatus = getTransactionStatus;
//# sourceMappingURL=getTransactionStatus.js.map