From 224e33c93d2acd22c82892148b240df004284037 Mon Sep 17 00:00:00 2001 From: Thomas Brillard Date: Fri, 6 Sep 2024 15:48:02 +0200 Subject: [PATCH] fix: bnb custom fee staying as type2 after prepare (#7741) * fix: bnb custom fee staying as type2 after prepare * fix: better check and more readable * chore: using explorer gasOption to determine type * fix: tests --- .changeset/rotten-cooks-juggle.md | 6 ++++ .../unit/prepareTransaction.unit.test.ts | 31 +++++++++++++++++-- .../coin-evm/src/prepareTransaction.ts | 12 +++++-- .../src/exchange/swap/webApp/utils.ts | 3 +- 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 .changeset/rotten-cooks-juggle.md diff --git a/.changeset/rotten-cooks-juggle.md b/.changeset/rotten-cooks-juggle.md new file mode 100644 index 000000000000..68587d9363b6 --- /dev/null +++ b/.changeset/rotten-cooks-juggle.md @@ -0,0 +1,6 @@ +--- +"@ledgerhq/coin-evm": minor +"@ledgerhq/live-common": minor +--- + +fixed bnb custom fee crashes and erased gasLimit diff --git a/libs/coin-modules/coin-evm/src/__tests__/unit/prepareTransaction.unit.test.ts b/libs/coin-modules/coin-evm/src/__tests__/unit/prepareTransaction.unit.test.ts index 1440e8952e93..1cb1d55abd49 100644 --- a/libs/coin-modules/coin-evm/src/__tests__/unit/prepareTransaction.unit.test.ts +++ b/libs/coin-modules/coin-evm/src/__tests__/unit/prepareTransaction.unit.test.ts @@ -179,6 +179,33 @@ describe("EVM Family", () => { }); }); + it("should return a legacy coin transaction when passing a gasPrice and custom feesStrategy", async () => { + jest.spyOn(nodeApi, "getFeeData").mockImplementationOnce(async () => ({ + gasPrice: new BigNumber(1), + maxFeePerGas: null, + maxPriorityFeePerGas: null, + nextBaseFee: null, + })); + + // @ts-expect-error - mixed type0/2 + const tx = await prepareTransaction(account, { + ...transaction, + feesStrategy: "custom", + gasPrice: new BigNumber(1), + maxFeePerGas: new BigNumber(0), + maxPriorityFeePerGas: new BigNumber(0), + }); + + expect(tx).toEqual({ + ...transaction, + gasPrice: new BigNumber(1), + feesStrategy: "custom", + maxFeePerGas: undefined, + maxPriorityFeePerGas: undefined, + type: 0, + }); + }); + it("should create a coin transaction using all amount in the account", async () => { const accountWithBalance = { ...account, @@ -555,13 +582,13 @@ describe("EVM Family", () => { }); describe("When custom feesStrategy provided", () => { - it("should use transaction provided data for fees", async () => { + it("should also call getFeeData to determine the gas price and type", async () => { const tx = await prepareTransaction(account, { ...transaction, feesStrategy: "custom", }); - expect(nodeApi.getFeeData).toBeCalledTimes(0); + expect(nodeApi.getFeeData).toBeCalledTimes(1); expect(tx).toEqual({ ...transaction, diff --git a/libs/coin-modules/coin-evm/src/prepareTransaction.ts b/libs/coin-modules/coin-evm/src/prepareTransaction.ts index 0a7ae7076d70..085757b3770f 100644 --- a/libs/coin-modules/coin-evm/src/prepareTransaction.ts +++ b/libs/coin-modules/coin-evm/src/prepareTransaction.ts @@ -210,10 +210,16 @@ export const prepareTransaction = async ( // Get the current network status fees const feeData: FeeData = await (async (): Promise => { if (transaction.feesStrategy === "custom") { + const gasOption = await nodeApi.getFeeData(currency, transaction); + return { - gasPrice: transaction.gasPrice ?? null, - maxFeePerGas: transaction.maxFeePerGas ?? null, - maxPriorityFeePerGas: transaction.maxPriorityFeePerGas ?? null, + gasPrice: gasOption.gasPrice && transaction.gasPrice ? transaction.gasPrice : null, + maxFeePerGas: + gasOption.maxFeePerGas && transaction.maxFeePerGas ? transaction.maxFeePerGas : null, + maxPriorityFeePerGas: + gasOption.maxPriorityFeePerGas && transaction.maxPriorityFeePerGas + ? transaction.maxPriorityFeePerGas + : null, nextBaseFee: transaction.gasOptions?.medium?.nextBaseFee ?? null, }; } diff --git a/libs/ledger-live-common/src/exchange/swap/webApp/utils.ts b/libs/ledger-live-common/src/exchange/swap/webApp/utils.ts index 938071a799b9..247e68c73deb 100644 --- a/libs/ledger-live-common/src/exchange/swap/webApp/utils.ts +++ b/libs/ledger-live-common/src/exchange/swap/webApp/utils.ts @@ -2,7 +2,7 @@ import { BigNumber } from "bignumber.js"; import { AccountLike } from "@ledgerhq/types-live"; export const getCustomFeesPerFamily = transaction => { - const { family, maxFeePerGas, maxPriorityFeePerGas, feePerByte, fees, utxoStrategy } = + const { family, maxFeePerGas, maxPriorityFeePerGas, feePerByte, fees, utxoStrategy, gasPrice } = transaction; switch (family) { @@ -10,6 +10,7 @@ export const getCustomFeesPerFamily = transaction => { return { maxFeePerGas, maxPriorityFeePerGas, + gasPrice, }; } case "bitcoin": {