@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
82 lines • 4.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const network_1 = require("../network");
const getFeesForTransaction_1 = require("./getFeesForTransaction");
const logic_1 = require("./logic");
const constants_1 = require("./../constants");
const staking_1 = require("../logic/staking");
const index_1 = require("@ledgerhq/coin-framework/account/index");
const checkSendConditions = (transaction, account) => transaction.mode === "send" && transaction.amount.gt(account.spendableBalance);
const checkStakeConditions = (transaction, account) => {
const txAmount = transaction.useAllAmount ? account.spendableBalance : transaction.amount;
const stakingPosition = account.aptosResources?.stakingPositions
?.find(stakingPosition => stakingPosition.validatorId === transaction.recipient)
?.active.gt(constants_1.MIN_COINS_ON_SHARES_POOL_IN_OCTAS);
const minimumToStake = stakingPosition
? constants_1.APTOS_MINIMUM_RESTAKE_IN_OCTAS
: constants_1.MIN_COINS_ON_SHARES_POOL_IN_OCTAS;
return (transaction.mode === "stake" &&
(txAmount.gt(account.spendableBalance) || txAmount.lt(minimumToStake)));
};
const checkRestakeConditions = (transaction, account) => {
const stakingPosition = (0, staking_1.getStakingPosition)(account, transaction.recipient)?.pendingInactive || 0;
return transaction.mode === "restake" && transaction.amount.gt(stakingPosition);
};
const checkUnstakeConditions = (transaction, account) => {
const stakingPosition = (0, staking_1.getStakingPosition)(account, transaction.recipient)?.active || 0;
return transaction.mode === "unstake" && transaction.amount.gt(stakingPosition);
};
const checkWithdrawConditions = (transaction, account) => {
const stakingPosition = (0, staking_1.getStakingPosition)(account, transaction.recipient)?.inactive || 0;
return transaction.mode === "withdraw" && transaction.amount.gt(stakingPosition);
};
const prepareTransaction = async (account, transaction) => {
if (!transaction.recipient ||
checkSendConditions(transaction, account) ||
checkStakeConditions(transaction, account) ||
checkRestakeConditions(transaction, account) ||
checkUnstakeConditions(transaction, account) ||
checkWithdrawConditions(transaction, account))
return transaction;
// if transaction.useAllAmount is true, then we expect transaction.amount to be 0
// so to check that actual amount is zero or not, we also need to check if useAllAmount is false
if (!transaction.useAllAmount && transaction.amount.isZero()) {
return {
...transaction,
fees: (0, bignumber_js_1.default)(0),
};
}
const aptosClient = new network_1.AptosAPI(account.currency.id);
const tokenAccount = (0, index_1.findSubAccountById)(account, transaction?.subAccountId ?? "");
const { fees, estimate, errors } = await (0, getFeesForTransaction_1.getEstimatedGas)(account, transaction, aptosClient);
const gas = (0, bignumber_js_1.default)(estimate.maxGasAmount);
const gasPrice = (0, bignumber_js_1.default)(estimate.gasUnitPrice);
if (transaction.useAllAmount) {
const maxAmount = tokenAccount
? (0, logic_1.getMaxSendBalance)(tokenAccount, account, gas, gasPrice)
: (0, logic_1.getMaxSendBalance)(account, undefined, gas, gasPrice);
if (transaction.mode === "send") {
transaction.amount = maxAmount;
}
else if (transaction.mode === "restake" ||
transaction.mode === "unstake" ||
transaction.mode === "withdraw") {
// Reserve a certain amount to cover future network fees to deactivate and withdraw
transaction.amount = (0, staking_1.getDelegationOpMaxAmount)(account, transaction.recipient, transaction.mode);
}
else if (transaction.mode === "stake") {
// Reserve a certain amount to cover future network fees to deactivate and withdraw
transaction.amount = maxAmount.minus(constants_1.APTOS_DELEGATION_RESERVE_IN_OCTAS);
}
}
transaction.fees = fees;
transaction.options = estimate;
transaction.errors = errors;
return transaction;
};
exports.default = prepareTransaction;
//# sourceMappingURL=prepareTransaction.js.map