Skip to content

Commit

Permalink
fix: bnb custom fee staying as type2 after prepare (#7741)
Browse files Browse the repository at this point in the history
* fix: bnb custom fee staying as type2 after prepare

* fix: better check and more readable

* chore: using explorer gasOption to determine type

* fix: tests
  • Loading branch information
CremaFR committed Sep 6, 2024
1 parent 4999d67 commit 224e33c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/rotten-cooks-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ledgerhq/coin-evm": minor
"@ledgerhq/live-common": minor
---

fixed bnb custom fee crashes and erased gasLimit
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions libs/coin-modules/coin-evm/src/prepareTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ export const prepareTransaction = async (
// Get the current network status fees
const feeData: FeeData = await (async (): Promise<FeeData> => {
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,
};
}
Expand Down
3 changes: 2 additions & 1 deletion libs/ledger-live-common/src/exchange/swap/webApp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ 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) {
case "evm": {
return {
maxFeePerGas,
maxPriorityFeePerGas,
gasPrice,
};
}
case "bitcoin": {
Expand Down

0 comments on commit 224e33c

Please sign in to comment.