From 089a7dad26a46be6f10a43ce78b336145aa4d90e Mon Sep 17 00:00:00 2001 From: Oleksii Date: Tue, 31 Dec 2024 16:25:28 +0000 Subject: [PATCH] Feat/live 15542 aptos send receive fix send funds simulation failed when amount to send is slightly above max spendable amount (#8766) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add amount check on unclassified errors * Fix cached gas that blocked valid transaction --------- Co-authored-by: João Martins --- .../src/families/aptos/getFeesForTransaction.ts | 16 +++++++--------- .../src/families/aptos/prepareTransaction.ts | 10 +++++++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts b/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts index 9f686025cb66..dd2cfe335316 100644 --- a/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts +++ b/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts @@ -54,7 +54,12 @@ export const getFee = async ( const simulation = await aptosClient.simulateTransaction(publicKeyEd, tx); const completedTx = simulation[0]; - if (!completedTx.success) { + const expectedGas = BigNumber(gasLimit * gasPrice); + const isUnderMaxSpendable = !transaction.amount + .plus(expectedGas) + .isGreaterThan(account.spendableBalance); + + if (isUnderMaxSpendable && !completedTx.success) { switch (true) { case completedTx.vm_status.includes("SEQUENCE_NUMBER"): { res.errors.sequenceNumber = completedTx.vm_status; @@ -97,14 +102,7 @@ export const getFee = async ( res.estimate.sequenceNumber = sequenceNumber.toString(); res.estimate.maxGasAmount = gasLimit.toString(); - if (transaction.firstEmulation) { - res.fees = res.fees.plus(BigNumber(gasPrice)).multipliedBy(BigNumber(gasLimit)); - } else { - res.fees = res.fees - .plus(transaction.options.gasUnitPrice) - .multipliedBy(BigNumber(transaction.options.maxGasAmount)); - } - + res.fees = res.fees.plus(BigNumber(gasPrice)).multipliedBy(BigNumber(gasLimit)); CACHE.delete(getCacheKey(transaction)); return res; }; diff --git a/libs/ledger-live-common/src/families/aptos/prepareTransaction.ts b/libs/ledger-live-common/src/families/aptos/prepareTransaction.ts index 03e16465421e..54a180306cea 100644 --- a/libs/ledger-live-common/src/families/aptos/prepareTransaction.ts +++ b/libs/ledger-live-common/src/families/aptos/prepareTransaction.ts @@ -45,9 +45,13 @@ const prepareTransaction = async ( : transaction.amount; transaction.amount = amount; - if (transaction.firstEmulation) { - transaction.options.maxGasAmount = estimate.maxGasAmount; - } + transaction.options = { + ...transaction.options, + maxGasAmount: estimate.maxGasAmount, + gasUnitPrice: estimate.gasUnitPrice, + sequenceNumber: estimate.sequenceNumber, + expirationTimestampSecs: estimate.expirationTimestampSecs, + }; transaction.fees = fees; transaction.estimate = estimate;