UNPKG

@ledgerhq/coin-aptos

Version:
73 lines 3.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEstimatedGas = exports.getFee = void 0; const ts_sdk_1 = require("@aptos-labs/ts-sdk"); const logs_1 = require("@ledgerhq/logs"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const cache_1 = require("@ledgerhq/live-network/cache"); const logic_1 = require("./logic"); const constants_1 = require("../constants"); const buildTransaction_1 = __importDefault(require("../logic/buildTransaction")); const getFee = async (account, transaction, aptosClient) => { const res = { fees: new bignumber_js_1.default(constants_1.DEFAULT_GAS).multipliedBy(constants_1.DEFAULT_GAS_PRICE), estimate: { maxGasAmount: constants_1.DEFAULT_GAS.toString(), gasUnitPrice: constants_1.DEFAULT_GAS_PRICE.toString(), }, errors: { ...transaction.errors }, }; let gasLimit = constants_1.DEFAULT_GAS; let gasPrice = constants_1.DEFAULT_GAS_PRICE; transaction.options = { maxGasAmount: gasLimit.toString(), gasUnitPrice: gasPrice.toString(), }; if (account.xpub) { try { const publicKeyEd = new ts_sdk_1.Ed25519PublicKey(account.xpub); const tx = await (0, buildTransaction_1.default)(account, transaction, aptosClient); const [completedTx] = await aptosClient.simulateTransaction(publicKeyEd, tx); const gasMultiplier = constants_1.STAKING_TX_MODES.includes(transaction.mode) ? constants_1.ESTIMATE_GAS_MUL_FOR_STAKING : constants_1.ESTIMATE_GAS_MUL; gasLimit = new bignumber_js_1.default(completedTx.gas_used).multipliedBy(gasMultiplier).integerValue(); gasPrice = new bignumber_js_1.default(completedTx.gas_unit_price); const expectedGas = gasPrice.multipliedBy(gasLimit); if (!completedTx.success) { if (completedTx.vm_status.includes("MAX_GAS_UNITS_BELOW_MIN_TRANSACTION_GAS_UNITS")) { res.errors.maxGasAmount = "GasInsufficientBalance"; } else if (!completedTx.vm_status.includes("INSUFFICIENT_BALANCE") && !completedTx.vm_status.includes("EDELEGATOR_ACTIVE_BALANCE_TOO_LOW") && !completedTx.vm_status.includes("EDELEGATOR_PENDING_INACTIVE_BALANCE_TOO_LOW") && !completedTx.vm_status.includes("0x203ed") // 0x203ed -> PROLOGUE_ECANT_PAY_GAS_DEPOSIT equivalent to INSUFFICIENT_BALANCE_FOR_TRANSACTION_FEE ) { // INSUFFICIENT_BALANCE and EDELEGATOR_ACTIVE_BALANCE_TOO_LOW will be processed by getTransactionStatus throw Error(`Simulation failed with following error: ${completedTx.vm_status}`); } } res.fees = expectedGas; res.estimate.maxGasAmount = gasLimit.toString(); res.estimate.gasUnitPrice = completedTx.gas_unit_price; } catch (error) { (0, logs_1.log)(error.message); throw error; } } return res; }; exports.getFee = getFee; const CACHE = (0, cache_1.makeLRUCache)(exports.getFee, (account, transaction) => { const tokenAccount = (0, logic_1.getTokenAccount)(account, transaction); return `${tokenAccount ? tokenAccount.id : account.id}-${transaction.amount.toString()}}`; }, (0, cache_1.seconds)(30)); const getEstimatedGas = async (account, transaction, aptosClient) => { return await CACHE(account, transaction, aptosClient); }; exports.getEstimatedGas = getEstimatedGas; //# sourceMappingURL=getFeesForTransaction.js.map