UNPKG

@ledgerhq/coin-stacks

Version:
53 lines 2.17 kB
"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 contants_1 = require("../contants"); const errors_2 = require("../errors"); const addresses_1 = require("./utils/addresses"); const misc_1 = require("./utils/misc"); const getTransactionStatus = async (account, transaction) => { const errors = {}; const warnings = {}; const { spendableBalance } = account; const { address } = (0, misc_1.getAddress)(account); const { memo, recipient, useAllAmount, fee } = transaction; let { amount } = transaction; if (!recipient) { errors.recipient = new errors_1.RecipientRequired(); } else if (!(0, addresses_1.validateAddress)(recipient).isValid) { errors.recipient = new errors_1.InvalidAddress("", { currencyName: account.currency.name, }); } else if (address === recipient) { errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource(); } else if (!fee || fee.eq(0)) { errors.gas = new errors_1.FeeNotLoaded(); } const estimatedFees = fee || new bignumber_js_1.default(0); const totalSpent = useAllAmount ? spendableBalance : amount.plus(estimatedFees); amount = useAllAmount ? spendableBalance.minus(estimatedFees) : amount; if (amount.lte(0)) errors.amount = new errors_1.AmountRequired(); if (totalSpent.gt(spendableBalance)) errors.amount = new errors_1.NotEnoughBalance(); const memoBytesLength = Buffer.from(memo ?? "", "utf-8").byteLength; if (memoBytesLength > contants_1.STACKS_MAX_MEMO_SIZE) errors.transaction = new errors_2.StacksMemoTooLong(); return { errors, warnings, estimatedFees, amount, totalSpent, }; }; exports.getTransactionStatus = getTransactionStatus; //# sourceMappingURL=getTransactionStatus.js.map