From aae6f3172d3985279b805b4abd9aad445a110a3a Mon Sep 17 00:00:00 2001 From: OleksiiM Date: Tue, 24 Dec 2024 17:38:55 +0000 Subject: [PATCH 1/3] Add amount check on unclassified errors --- .../src/families/aptos/js-getFeesForTransaction.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/ledger-live-common/src/families/aptos/js-getFeesForTransaction.ts b/libs/ledger-live-common/src/families/aptos/js-getFeesForTransaction.ts index cfa741d2e959..fbaa7074124b 100644 --- a/libs/ledger-live-common/src/families/aptos/js-getFeesForTransaction.ts +++ b/libs/ledger-live-common/src/families/aptos/js-getFeesForTransaction.ts @@ -69,7 +69,14 @@ export const getFee = async ( break; } default: { - throw Error(`Simulation failed with following error: ${completedTx.vm_status}`); + // HOTFIX: we need to check if there is enough balance for gas + // idealy it should be covered with EINSUFFICIENT_BALANCE from network + const expectedGas = BigNumber(gasLimit * gasPrice); + if (transaction.amount.plus(expectedGas).isGreaterThan(account.spendableBalance)) { + break; + } else { + throw Error(`Simulation failed with following error: ${completedTx.vm_status}`); + } } } } From dd95c255ff5ffdaa56845ed9b7e17e3367939d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Martins?= Date: Fri, 27 Dec 2024 19:08:20 +0000 Subject: [PATCH 2/3] Small code visual change. --- .../families/aptos/js-getFeesForTransaction.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/libs/ledger-live-common/src/families/aptos/js-getFeesForTransaction.ts b/libs/ledger-live-common/src/families/aptos/js-getFeesForTransaction.ts index fbaa7074124b..30f92283dc31 100644 --- a/libs/ledger-live-common/src/families/aptos/js-getFeesForTransaction.ts +++ b/libs/ledger-live-common/src/families/aptos/js-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; @@ -69,14 +74,7 @@ export const getFee = async ( break; } default: { - // HOTFIX: we need to check if there is enough balance for gas - // idealy it should be covered with EINSUFFICIENT_BALANCE from network - const expectedGas = BigNumber(gasLimit * gasPrice); - if (transaction.amount.plus(expectedGas).isGreaterThan(account.spendableBalance)) { - break; - } else { - throw Error(`Simulation failed with following error: ${completedTx.vm_status}`); - } + throw Error(`Simulation failed with following error: ${completedTx.vm_status}`); } } } From 0cf302300e5cc68d5f3daa3d98eab377d311c2a3 Mon Sep 17 00:00:00 2001 From: OleksiiM Date: Tue, 31 Dec 2024 15:21:13 +0000 Subject: [PATCH 3/3] Fix cached gas that blocked valid transaction --- .../src/families/aptos/getFeesForTransaction.ts | 9 +-------- .../src/families/aptos/prepareTransaction.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts b/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts index 77c97b1f7613..dd2cfe335316 100644 --- a/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts +++ b/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts @@ -102,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 d32884f0a231..60fa92ff7786 100644 --- a/libs/ledger-live-common/src/families/aptos/prepareTransaction.ts +++ b/libs/ledger-live-common/src/families/aptos/prepareTransaction.ts @@ -36,9 +36,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;