UNPKG

@ledgerhq/coin-stacks

Version:
46 lines 1.86 kB
import { AmountRequired, FeeNotLoaded, InvalidAddress, InvalidAddressBecauseDestinationIsAlsoSource, NotEnoughBalance, RecipientRequired, } from "@ledgerhq/errors"; import BigNumber from "bignumber.js"; import { STACKS_MAX_MEMO_SIZE } from "../contants"; import { StacksMemoTooLong } from "../errors"; import { validateAddress } from "./utils/addresses"; import { getAddress } from "./utils/misc"; export const getTransactionStatus = async (account, transaction) => { const errors = {}; const warnings = {}; const { spendableBalance } = account; const { address } = getAddress(account); const { memo, recipient, useAllAmount, fee } = transaction; let { amount } = transaction; if (!recipient) { errors.recipient = new RecipientRequired(); } else if (!validateAddress(recipient).isValid) { errors.recipient = new InvalidAddress("", { currencyName: account.currency.name, }); } else if (address === recipient) { errors.recipient = new InvalidAddressBecauseDestinationIsAlsoSource(); } else if (!fee || fee.eq(0)) { errors.gas = new FeeNotLoaded(); } const estimatedFees = fee || new BigNumber(0); const totalSpent = useAllAmount ? spendableBalance : amount.plus(estimatedFees); amount = useAllAmount ? spendableBalance.minus(estimatedFees) : amount; if (amount.lte(0)) errors.amount = new AmountRequired(); if (totalSpent.gt(spendableBalance)) errors.amount = new NotEnoughBalance(); const memoBytesLength = Buffer.from(memo ?? "", "utf-8").byteLength; if (memoBytesLength > STACKS_MAX_MEMO_SIZE) errors.transaction = new StacksMemoTooLong(); return { errors, warnings, estimatedFees, amount, totalSpent, }; }; //# sourceMappingURL=getTransactionStatus.js.map