UNPKG

@ledgerhq/coin-aptos

Version:
167 lines 7.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("@ledgerhq/errors"); const ts_sdk_1 = require("@aptos-labs/ts-sdk"); const bignumber_js_1 = require("bignumber.js"); const logic_1 = require("./logic"); const constants_1 = require("../constants"); const staking_1 = require("../logic/staking"); const checkSendTransaction = (t, a, tokenAccount, estimatedFees, errors) => { const newErrors = { ...errors }; if (!t.recipient) { newErrors.recipient = new errors_1.RecipientRequired(); } if (!ts_sdk_1.AccountAddress.isValid({ input: t.recipient }).valid && !newErrors.recipient) { newErrors.recipient = new errors_1.InvalidAddress("", { currencyName: a.currency.name }); } if (t.recipient === a.freshAddress && !newErrors.recipient) { newErrors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource(); } if (t.amount.gt(a.balance) && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughBalance(); } if (tokenAccount && t.errors?.maxGasAmount === "GasInsufficientBalance" && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughBalanceFees(); } if ((tokenAccount ? tokenAccount.spendableBalance.isLessThan(t.amount) || a.spendableBalance.isLessThan(estimatedFees) : a.spendableBalance.isLessThan(t.amount.plus(estimatedFees))) && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughBalance(); } return newErrors; }; const checkStakeTransaction = (t, stakingPosition, a, errors) => { const newErrors = { ...errors }; if (t.amount.gt(a.spendableBalance)) { newErrors.amount = new errors_1.NotEnoughBalance(); } if ((((!stakingPosition || stakingPosition?.active.isZero()) && ((!t.useAllAmount && t.amount.lt(constants_1.MIN_COINS_ON_SHARES_POOL_IN_OCTAS)) || (t.useAllAmount && a.spendableBalance .minus(constants_1.APTOS_DELEGATION_RESERVE_IN_OCTAS) .lt(constants_1.MIN_COINS_ON_SHARES_POOL_IN_OCTAS)))) || (stakingPosition && !stakingPosition.active.isZero() && t.amount.lt(constants_1.APTOS_MINIMUM_RESTAKE_IN_OCTAS))) && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughToStake("", { minStake: stakingPosition && !stakingPosition.active.isZero() ? constants_1.APTOS_MINIMUM_RESTAKE : constants_1.MIN_COINS_ON_SHARES_POOL, currency: a.currency.ticker, }); } return newErrors; }; const checkRestakeTransaction = (t, stakingPosition, errors) => { const newErrors = { ...errors }; if (!stakingPosition) { newErrors.recipient = new errors_1.RecipientRequired(); } else { if ((t.amount.gt(stakingPosition.pendingInactive) || t.amount.isZero()) && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughBalance(); } if (!t.useAllAmount && t.amount.lt(constants_1.MIN_COINS_ON_SHARES_POOL_IN_OCTAS) && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughToRestake("", { minAmount: `${constants_1.MIN_COINS_ON_SHARES_POOL.toNumber().toString()} APT`, }); } if (!t.useAllAmount && stakingPosition.pendingInactive.minus(t.amount).lt(constants_1.MIN_COINS_ON_SHARES_POOL_IN_OCTAS) && !newErrors.amount) { newErrors.amount = new errors_1.RestakeNotEnoughStakedBalanceLeft("", { maxAmount: `${stakingPosition.pendingInactive.minus(constants_1.MIN_COINS_ON_SHARES_POOL_IN_OCTAS).shiftedBy(-constants_1.APTOS_PRECISION).toNumber().toString()} APT`, totalAmount: `${stakingPosition.pendingInactive.shiftedBy(-constants_1.APTOS_PRECISION).toNumber().toString()} APT`, }); } } return newErrors; }; const checkUnstakeTransaction = (t, stakingPosition, errors) => { const newErrors = { ...errors }; if (!stakingPosition) { newErrors.recipient = new errors_1.RecipientRequired(); } else { if (t.amount.gt(stakingPosition.active) && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughBalance(); } if (!t.useAllAmount && t.amount.lt(constants_1.MIN_AMOUNT_TO_UNSTAKE_IN_OCTAS) && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughToUnstake("", { minAmount: `${constants_1.MIN_AMOUNT_TO_UNSTAKE.toNumber().toString()} APT`, }); } if (!t.useAllAmount && stakingPosition.active.minus(t.amount).lt(constants_1.MIN_AMOUNT_TO_UNSTAKE_IN_OCTAS) && !newErrors.amount) { newErrors.amount = new errors_1.UnstakeNotEnoughStakedBalanceLeft("", { maxAmount: `${stakingPosition.active.minus(constants_1.MIN_AMOUNT_TO_UNSTAKE_IN_OCTAS).shiftedBy(-constants_1.APTOS_PRECISION).toNumber().toString()} APT`, totalAmount: `${stakingPosition.active.shiftedBy(-constants_1.APTOS_PRECISION).toNumber().toString()} APT`, }); } } return newErrors; }; const checkWithdrawTransaction = (t, stakingPosition, errors) => { const newErrors = { ...errors }; if (!stakingPosition) { newErrors.recipient = new errors_1.RecipientRequired(); } else if (t.amount.gt(stakingPosition.inactive) && !newErrors.amount) { newErrors.amount = new errors_1.NotEnoughBalance(); } return newErrors; }; const getTransactionStatus = async (a, t) => { let errors = {}; const warnings = {}; const estimatedFees = t.fees || (0, bignumber_js_1.BigNumber)(0); const tokenAccount = (0, logic_1.getTokenAccount)(a, t); const stakingPosition = (0, staking_1.getStakingPosition)(a, t.recipient); if (!t.useAllAmount && t.amount.lte(0)) { errors.amount = new errors_1.AmountRequired(); } switch (t.mode) { case "send": errors = checkSendTransaction(t, a, tokenAccount, estimatedFees, errors); break; case "stake": errors = checkStakeTransaction(t, stakingPosition, a, errors); break; case "restake": errors = checkRestakeTransaction(t, stakingPosition, errors); break; case "unstake": errors = checkUnstakeTransaction(t, stakingPosition, errors); break; case "withdraw": errors = checkWithdrawTransaction(t, stakingPosition, errors); break; } if (!t.fees) { errors.fees = new errors_1.FeeNotLoaded(); } const maxSpendable = t.mode === "stake" ? a.spendableBalance.minus(estimatedFees).minus(constants_1.APTOS_DELEGATION_RESERVE_IN_OCTAS) : a.spendableBalance.minus(estimatedFees); const amount = t.useAllAmount ? tokenAccount ? tokenAccount.spendableBalance : a.spendableBalance.minus(estimatedFees).isLessThan(0) ? (0, bignumber_js_1.BigNumber)(0) : maxSpendable : t.amount; return Promise.resolve({ errors, warnings, estimatedFees, amount, totalSpent: tokenAccount ? amount : amount.plus(estimatedFees), }); }; exports.default = getTransactionStatus; //# sourceMappingURL=getTransactionStatus.js.map