Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set fees to zero for zero base fee chains (e.g. anvil) #963

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/utils/transaction/prepareRequest.test.ts
Expand Up @@ -70,6 +70,42 @@ describe('prepareRequest', () => {
`)
})

test('zero base fee', async () => {
await setup()
await setNextBlockBaseFeePerGas(testClient, {
baseFeePerGas: 0n,
})

const {
maxFeePerGas,
nonce: _nonce,
...rest
} = await prepareRequest(walletClient, {
account: privateKeyToAccount(sourceAccount.privateKey),
to: targetAccount.address,
value: parseEther('1'),
})
expect(maxFeePerGas).toEqual(0n)
expect(rest).toMatchInlineSnapshot(`
{
"account": {
"address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"publicKey": "0x048318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5",
"signMessage": [Function],
"signTransaction": [Function],
"signTypedData": [Function],
"source": "privateKey",
"type": "local",
},
"from": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"gas": 21000n,
"maxPriorityFeePerGas": 1500000000n,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
"value": 1000000000000000000n,
}
`)
})

test('legacy fees', async () => {
await setup()

Expand Down
14 changes: 10 additions & 4 deletions src/utils/transaction/prepareRequest.ts
Expand Up @@ -78,10 +78,16 @@ export async function prepareRequest<

// EIP-1559 fees
if (typeof maxFeePerGas === 'undefined') {
// Set a buffer of 1.2x on top of the base fee to account for fluctuations.
request.maxPriorityFeePerGas = maxPriorityFeePerGas ?? defaultTip
request.maxFeePerGas =
(block.baseFeePerGas * 120n) / 100n + request.maxPriorityFeePerGas
// Set fees to zero when running anvil with zero base fee
if (block.baseFeePerGas === 0n) {
request.maxFeePerGas = 0n
request.maxPriorityFeePerGas = 0n
} else {
// Set a buffer of 1.2x on top of the base fee to account for fluctuations.
request.maxPriorityFeePerGas = maxPriorityFeePerGas ?? defaultTip
request.maxFeePerGas =
(block.baseFeePerGas * 120n) / 100n + request.maxPriorityFeePerGas
}
} else {
if (
typeof maxPriorityFeePerGas === 'undefined' &&
Expand Down