Skip to content

Commit

Permalink
fix: default fee values for zksync eip712 transactions (#2168)
Browse files Browse the repository at this point in the history
* fix: add default values for fees for zksync eip712 transactions

* tweaks

* Create modern-elephants-invent.md

---------

Co-authored-by: Marko Arambasic <makiarambasic@gmail.com>
Co-authored-by: moxey.eth <jakemoxey@gmail.com>
  • Loading branch information
3 people committed Apr 23, 2024
1 parent ba856ba commit 1c550fa
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-elephants-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed default fee values for zkSync eip712 transactions.
2 changes: 1 addition & 1 deletion src/actions/wallet/prepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export async function prepareTransactionRequest<
if (parameters.includes('fees')) {
// TODO(4844): derive blob base fees once https://github.com/ethereum/execution-apis/pull/486 is merged.

if (request.type === 'eip1559' || request.type === 'eip4844') {
if (request.type !== 'legacy') {
// EIP-1559 fees
if (
typeof request.maxFeePerGas === 'undefined' ||
Expand Down
2 changes: 1 addition & 1 deletion src/zksync/actions/sendEip712Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export async function sendEip712Transaction<
// Prepare the request for signing (assign appropriate fees, etc.)
const request = await prepareTransactionRequest(client, {
...args,
parameters: ['gas', 'nonce'],
parameters: ['gas', 'nonce', 'fees'],
} as any)

let chainId: number | undefined
Expand Down
2 changes: 1 addition & 1 deletion src/zksync/actions/signEip712Transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('default', async () => {
...base,
}),
).toMatchInlineSnapshot(
`"0x71f8c680808080808000820144808082014494000000000000000000000000000000000000000080c0b8412e940527ab264b49c70e9c1bca7636ed2b87f94b5083140c2cbdcd570d99e2c149b697d0c6a037802d0a4757fbd5d1ffd980afccb270e9d182bf7c197bc4a2661bf85b94fd9ae5ebb0f6656f4b77a0e99dcbc5138d54b0bab8448c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"`,
`"0x71f8c880808080808000820144808082014494000000000000000000000000000000000000000082c350c0b841bb509f381d29a038bd2f700bd6a1f1138edfd7a3cf7234c13a03b01a023a30aa53e6bd5e6a50fdcdcf74587c9395b8a314690abbc85aadab5ebcb7678994eacf1bf85b94fd9ae5ebb0f6656f4b77a0e99dcbc5138d54b0bab8448c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"`,
)
})

Expand Down
2 changes: 1 addition & 1 deletion src/zksync/actions/signTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test('eip712', async () => {
...eip712,
}),
).toMatchInlineSnapshot(
`"0x71f8c680808080808000820144808082014494000000000000000000000000000000000000000080c0b8412e940527ab264b49c70e9c1bca7636ed2b87f94b5083140c2cbdcd570d99e2c149b697d0c6a037802d0a4757fbd5d1ffd980afccb270e9d182bf7c197bc4a2661bf85b94fd9ae5ebb0f6656f4b77a0e99dcbc5138d54b0bab8448c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"`,
`"0x71f8c880808080808000820144808082014494000000000000000000000000000000000000000082c350c0b841bb509f381d29a038bd2f700bd6a1f1138edfd7a3cf7234c13a03b01a023a30aa53e6bd5e6a50fdcdcf74587c9395b8a314690abbc85aadab5ebcb7678994eacf1bf85b94fd9ae5ebb0f6656f4b77a0e99dcbc5138d54b0bab8448c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"`,
)
})

Expand Down
1 change: 1 addition & 0 deletions src/zksync/constants/number.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { maxUint16 } from '../../constants/number.js'

export const gasPerPubdataDefault = 50000n
export const maxBytecodeSize = maxUint16 * 32n
3 changes: 3 additions & 0 deletions src/zksync/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,7 @@ describe('transactionRequest', () => {
205,
],
],
"gasPerPubdata": "0xc350",
},
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x32c8",
Expand All @@ -1560,6 +1561,7 @@ describe('transactionRequest', () => {
18,
52,
],
"gasPerPubdata": "0xc350",
},
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x32c8",
Expand All @@ -1583,6 +1585,7 @@ describe('transactionRequest', () => {
).toMatchInlineSnapshot(`
{
"eip712Meta": {
"gasPerPubdata": "0xc350",
"paymasterParams": {
"paymaster": "0x4B5DF730c2e6b28E17013A1485E5d9BC41Efe021",
"paymasterInput": [
Expand Down
3 changes: 2 additions & 1 deletion src/zksync/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { formatLog } from '../utils/formatters/log.js'
import { defineTransaction } from '../utils/formatters/transaction.js'
import { defineTransactionReceipt } from '../utils/formatters/transactionReceipt.js'
import { defineTransactionRequest } from '../utils/formatters/transactionRequest.js'
import { gasPerPubdataDefault } from './constants/number.js'
import type {
ZkSyncBlockOverrides,
ZkSyncRpcBlockOverrides,
Expand Down Expand Up @@ -125,7 +126,7 @@ export const formatters = {
eip712Meta: {
...(args.gasPerPubdata
? { gasPerPubdata: toHex(args.gasPerPubdata) }
: {}),
: { gasPerPubdata: toHex(gasPerPubdataDefault) }),
...(args.paymaster && args.paymasterInput
? {
paymasterParams: {
Expand Down
6 changes: 3 additions & 3 deletions src/zksync/serializers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test('factoryDeps', () => {
}

expect(serializeTransaction(transaction)).toMatchInlineSnapshot(
`"0x71f8b50702840ee6b2808094111c3e89ce80e62ee88318c2804920d4c96f92bb880de0b6b3a7640000b864a4136862000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000017900000000000000000000000000000000000000000000000000000000000000820144808082014494f760bdd822fccf93c44be68d94c45133002b303780c883abcdef8312345680c0"`,
`"0x71f8b70702840ee6b2808094111c3e89ce80e62ee88318c2804920d4c96f92bb880de0b6b3a7640000b864a4136862000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000017900000000000000000000000000000000000000000000000000000000000000820144808082014494f760bdd822fccf93c44be68d94c45133002b303782c350c883abcdef8312345680c0"`,
)
})

Expand All @@ -94,7 +94,7 @@ test('customSignature', () => {
}

expect(serializeTransaction(transaction)).toMatchInlineSnapshot(
`"0x71f8b00702840ee6b2808094111c3e89ce80e62ee88318c2804920d4c96f92bb880de0b6b3a7640000b864a4136862000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000017900000000000000000000000000000000000000000000000000000000000000820144808082014494f760bdd822fccf93c44be68d94c45133002b303780c083abcdefc0"`,
`"0x71f8b20702840ee6b2808094111c3e89ce80e62ee88318c2804920d4c96f92bb880de0b6b3a7640000b864a4136862000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000017900000000000000000000000000000000000000000000000000000000000000820144808082014494f760bdd822fccf93c44be68d94c45133002b303782c350c083abcdefc0"`,
)
})

Expand All @@ -109,7 +109,7 @@ test('paymaster', () => {
}

expect(serializeTransaction(transaction)).toMatchInlineSnapshot(
`"0x71f901090702840ee6b2808094111c3e89ce80e62ee88318c2804920d4c96f92bb880de0b6b3a7640000b864a4136862000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000017900000000000000000000000000000000000000000000000000000000000000820144808082014494f760bdd822fccf93c44be68d94c45133002b303780c080f85b944b5df730c2e6b28e17013a1485e5d9bc41efe021b8448c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"`,
`"0x71f9010b0702840ee6b2808094111c3e89ce80e62ee88318c2804920d4c96f92bb880de0b6b3a7640000b864a4136862000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000017900000000000000000000000000000000000000000000000000000000000000820144808082014494f760bdd822fccf93c44be68d94c45133002b303782c350c080f85b944b5df730c2e6b28e17013a1485e5d9bc41efe021b8448c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"`,
)
})

Expand Down
3 changes: 2 additions & 1 deletion src/zksync/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { concatHex } from '../utils/data/concat.js'
import { toHex } from '../utils/encoding/toHex.js'
import { toRlp } from '../utils/encoding/toRlp.js'
import { serializeTransaction as serializeTransaction_ } from '../utils/transaction/serializeTransaction.js'
import { gasPerPubdataDefault } from './constants/number.js'
import type {
ZkSyncTransactionSerializable,
ZkSyncTransactionSerializableEIP712,
Expand Down Expand Up @@ -69,7 +70,7 @@ function serializeTransactionEIP712(
toHex(''),
toHex(chainId),
from ?? '0x',
gasPerPubdata ? toHex(gasPerPubdata) : '0x',
gasPerPubdata ? toHex(gasPerPubdata) : toHex(gasPerPubdataDefault),
factoryDeps ?? [],
customSignature ?? '0x', // EIP712 signature
paymaster && paymasterInput ? [paymaster, paymasterInput] : [],
Expand Down
3 changes: 2 additions & 1 deletion src/zksync/utils/getEip712Domain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toHex } from '../../utils/encoding/toHex.js'
import { gasPerPubdataDefault } from '../constants/number.js'
import type { EIP712DomainFn } from '../types/eip712.js'
import type {
ZkSyncEIP712TransactionSignable,
Expand Down Expand Up @@ -72,7 +73,7 @@ function transactionToMessage(
from: BigInt(from),
to: to ? BigInt(to) : 0n,
gasLimit: gas ?? 0n,
gasPerPubdataByteLimit: gasPerPubdata ?? 0n,
gasPerPubdataByteLimit: gasPerPubdata ?? gasPerPubdataDefault,
maxFeePerGas: maxFeePerGas ?? 0n,
maxPriorityFeePerGas: maxPriorityFeePerGas ?? 0n,
paymaster: paymaster ? BigInt(paymaster) : 0n,
Expand Down

0 comments on commit 1c550fa

Please sign in to comment.