|
| 1 | +import type { Hex } from "thirdweb"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { _updateGasFees } from "../worker/tasks/sendTransactionWorker"; |
| 4 | + |
| 5 | +describe("_updateGasFees", () => { |
| 6 | + const base = { |
| 7 | + // Irrelevant values for testing. |
| 8 | + chainId: 1, |
| 9 | + data: "0x0" as Hex, |
| 10 | + gas: 21000n, |
| 11 | + to: undefined, |
| 12 | + nonce: undefined, |
| 13 | + accessList: undefined, |
| 14 | + value: undefined, |
| 15 | + }; |
| 16 | + |
| 17 | + it("returns the original transaction on first send (resendCount = 0)", () => { |
| 18 | + let result = _updateGasFees({ ...base, gasPrice: 100n }, 0, undefined); |
| 19 | + expect(result.gasPrice).toEqual(100n); |
| 20 | + |
| 21 | + result = _updateGasFees( |
| 22 | + { ...base, maxFeePerGas: 100n, maxPriorityFeePerGas: 10n }, |
| 23 | + 0, |
| 24 | + undefined, |
| 25 | + ); |
| 26 | + expect(result.maxFeePerGas).toEqual(100n); |
| 27 | + expect(result.maxPriorityFeePerGas).toEqual(10n); |
| 28 | + }); |
| 29 | + |
| 30 | + it("doubles gasPrice for legacy transactions", () => { |
| 31 | + const result = _updateGasFees({ ...base, gasPrice: 100n }, 1, {}); |
| 32 | + expect(result.gasPrice).toBe(200n); |
| 33 | + }); |
| 34 | + |
| 35 | + it("caps gasPrice multiplier at 10x", () => { |
| 36 | + const result = _updateGasFees({ ...base, gasPrice: 100n }, 10, {}); |
| 37 | + expect(result.gasPrice).toBe(1000n); |
| 38 | + }); |
| 39 | + |
| 40 | + it("updates maxPriorityFeePerGas and maxFeePerGas for EIP-1559 transactions", () => { |
| 41 | + const result = _updateGasFees( |
| 42 | + { ...base, maxFeePerGas: 100n, maxPriorityFeePerGas: 10n }, |
| 43 | + 3, |
| 44 | + {}, |
| 45 | + ); |
| 46 | + expect(result.maxPriorityFeePerGas).toBe(60n); |
| 47 | + expect(result.maxFeePerGas).toBe(260n); |
| 48 | + }); |
| 49 | + |
| 50 | + it("respects overrides for maxPriorityFeePerGas", () => { |
| 51 | + const result = _updateGasFees( |
| 52 | + { ...base, maxFeePerGas: 100n, maxPriorityFeePerGas: 10n }, |
| 53 | + 3, |
| 54 | + { maxPriorityFeePerGas: 10n }, |
| 55 | + ); |
| 56 | + expect(result.maxPriorityFeePerGas).toBe(10n); // matches override |
| 57 | + expect(result.maxFeePerGas).toBe(210n); |
| 58 | + }); |
| 59 | + |
| 60 | + it("respects overrides for maxFeePerGas", () => { |
| 61 | + const result = _updateGasFees( |
| 62 | + { ...base, maxFeePerGas: 100n, maxPriorityFeePerGas: 10n }, |
| 63 | + 3, |
| 64 | + { maxFeePerGas: 100n }, |
| 65 | + ); |
| 66 | + expect(result.maxPriorityFeePerGas).toBe(60n); |
| 67 | + expect(result.maxFeePerGas).toBe(100n); // matches override |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns correct values when only maxPriorityFeePerGas is set", () => { |
| 71 | + const result = _updateGasFees( |
| 72 | + { ...base, maxPriorityFeePerGas: 10n }, |
| 73 | + 3, |
| 74 | + {}, |
| 75 | + ); |
| 76 | + expect(result.maxPriorityFeePerGas).toBe(60n); |
| 77 | + expect(result.maxFeePerGas).toBeUndefined(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns correct values when only maxFeePerGas is set", () => { |
| 81 | + const result = _updateGasFees({ ...base, maxFeePerGas: 80n }, 3, {}); |
| 82 | + expect(result.maxFeePerGas).toBe(160n); |
| 83 | + }); |
| 84 | +}); |
0 commit comments