viem 0.0.0-main.20231026T205555 → 0.0.0-main.20231026T213343

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/_cjs/chains/definitions/zkSync.js +2 -0
  2. package/_cjs/chains/definitions/zkSync.js.map +1 -1
  3. package/_cjs/chains/definitions/zkSyncTestnet.js +2 -0
  4. package/_cjs/chains/definitions/zkSyncTestnet.js.map +1 -1
  5. package/_cjs/chains/zksync/formatters.js +4 -5
  6. package/_cjs/chains/zksync/formatters.js.map +1 -1
  7. package/_cjs/chains/zksync/serializers.js +74 -0
  8. package/_cjs/chains/zksync/serializers.js.map +1 -0
  9. package/_cjs/errors/node.js +11 -11
  10. package/_cjs/errors/node.js.map +1 -1
  11. package/_cjs/errors/rpc.js +18 -18
  12. package/_cjs/errors/rpc.js.map +1 -1
  13. package/_cjs/errors/version.js +1 -1
  14. package/_esm/chains/definitions/zkSync.js +2 -0
  15. package/_esm/chains/definitions/zkSync.js.map +1 -1
  16. package/_esm/chains/definitions/zkSyncTestnet.js +2 -0
  17. package/_esm/chains/definitions/zkSyncTestnet.js.map +1 -1
  18. package/_esm/chains/zksync/formatters.js +4 -5
  19. package/_esm/chains/zksync/formatters.js.map +1 -1
  20. package/_esm/chains/zksync/serializers.js +71 -0
  21. package/_esm/chains/zksync/serializers.js.map +1 -0
  22. package/_esm/errors/node.js +11 -22
  23. package/_esm/errors/node.js.map +1 -1
  24. package/_esm/errors/rpc.js +18 -36
  25. package/_esm/errors/rpc.js.map +1 -1
  26. package/_esm/errors/version.js +1 -1
  27. package/_types/chains/definitions/zkSync.d.ts +56 -6
  28. package/_types/chains/definitions/zkSync.d.ts.map +1 -1
  29. package/_types/chains/definitions/zkSyncTestnet.d.ts +56 -6
  30. package/_types/chains/definitions/zkSyncTestnet.d.ts.map +1 -1
  31. package/_types/chains/utils/index.d.ts +1 -1
  32. package/_types/chains/utils/index.d.ts.map +1 -1
  33. package/_types/chains/zksync/formatters.d.ts +57 -7
  34. package/_types/chains/zksync/formatters.d.ts.map +1 -1
  35. package/_types/chains/zksync/serializers.d.ts +9 -0
  36. package/_types/chains/zksync/serializers.d.ts.map +1 -0
  37. package/_types/chains/zksync/types.d.ts +35 -23
  38. package/_types/chains/zksync/types.d.ts.map +1 -1
  39. package/_types/errors/version.d.ts +1 -1
  40. package/chains/definitions/zkSync.ts +2 -0
  41. package/chains/definitions/zkSyncTestnet.ts +2 -0
  42. package/chains/utils/index.ts +18 -1
  43. package/chains/zksync/formatters.ts +8 -9
  44. package/chains/zksync/serializers.ts +124 -0
  45. package/chains/zksync/types.ts +55 -25
  46. package/errors/version.ts +1 -1
  47. package/package.json +1 -1
@@ -0,0 +1,124 @@
1
+ import { InvalidAddressError } from '../../errors/address.js'
2
+ import { BaseError } from '../../errors/base.js'
3
+ import { InvalidChainIdError } from '../../errors/chain.js'
4
+ import type { ChainSerializers } from '../../types/chain.js'
5
+ import type { TransactionSerializable } from '../../types/transaction.js'
6
+ import { isAddress } from '../../utils/address/isAddress.js'
7
+ import { concatHex } from '../../utils/data/concat.js'
8
+ import { toHex } from '../../utils/encoding/toHex.js'
9
+ import { toRlp } from '../../utils/encoding/toRlp.js'
10
+ import {
11
+ type SerializeTransactionFn,
12
+ serializeTransaction,
13
+ } from '../../utils/transaction/serializeTransaction.js'
14
+ import type {
15
+ ZkSyncTransactionSerializable,
16
+ ZkSyncTransactionSerializableEIP712,
17
+ ZkSyncTransactionSerializedEIP712,
18
+ } from './types.js'
19
+
20
+ export const serializeTransactionZkSync: SerializeTransactionFn<
21
+ ZkSyncTransactionSerializable
22
+ > = (tx, signature) => {
23
+ if (isEIP712(tx))
24
+ return serializeTransactionZkSyncEIP712(
25
+ tx as ZkSyncTransactionSerializableEIP712,
26
+ )
27
+ return serializeTransaction(tx as TransactionSerializable, signature)
28
+ }
29
+
30
+ export const serializersZkSync = {
31
+ transaction: serializeTransactionZkSync,
32
+ } as const satisfies ChainSerializers
33
+
34
+ //////////////////////////////////////////////////////////////////////////////
35
+ // Serializers
36
+
37
+ export type SerializeTransactionEIP712ReturnType =
38
+ ZkSyncTransactionSerializedEIP712
39
+
40
+ function serializeTransactionZkSyncEIP712(
41
+ transaction: ZkSyncTransactionSerializableEIP712,
42
+ ): SerializeTransactionEIP712ReturnType {
43
+ const {
44
+ chainId,
45
+ gas,
46
+ nonce,
47
+ to,
48
+ from,
49
+ value,
50
+ maxFeePerGas,
51
+ maxPriorityFeePerGas,
52
+ customSignature,
53
+ factoryDeps,
54
+ paymaster,
55
+ paymasterInput,
56
+ gasPerPubdata,
57
+ data,
58
+ } = transaction
59
+
60
+ assertTransactionEIP712(transaction)
61
+
62
+ const serializedTransaction = [
63
+ nonce ? toHex(nonce) : '0x',
64
+ maxPriorityFeePerGas ? toHex(maxPriorityFeePerGas) : '0x',
65
+ maxFeePerGas ? toHex(maxFeePerGas) : '0x',
66
+ gas ? toHex(gas) : '0x',
67
+ to ?? '0x',
68
+ value ? toHex(value) : '0x',
69
+ data ?? '0x',
70
+ toHex(chainId),
71
+ toHex(''),
72
+ toHex(''),
73
+ toHex(chainId),
74
+ from ?? '0x',
75
+ gasPerPubdata ? toHex(gasPerPubdata) : '0x',
76
+ factoryDeps ?? [],
77
+ customSignature ?? '0x', // EIP712 signature
78
+ paymaster && paymasterInput ? [paymaster, paymasterInput] : [],
79
+ ]
80
+
81
+ return concatHex([
82
+ '0x71',
83
+ toRlp(serializedTransaction),
84
+ ]) as SerializeTransactionEIP712ReturnType
85
+ }
86
+
87
+ //////////////////////////////////////////////////////////////////////////////
88
+ // Utilities
89
+
90
+ function isEIP712(transaction: ZkSyncTransactionSerializable) {
91
+ if (
92
+ 'customSignature' in transaction ||
93
+ 'paymaster' in transaction ||
94
+ 'paymasterInput' in transaction ||
95
+ 'gasPerPubdata' in transaction ||
96
+ 'factoryDeps' in transaction
97
+ )
98
+ return true
99
+ return false
100
+ }
101
+
102
+ export function assertTransactionEIP712(
103
+ transaction: ZkSyncTransactionSerializableEIP712,
104
+ ) {
105
+ const { chainId, to, from, paymaster, paymasterInput } = transaction
106
+ if (chainId <= 0) throw new InvalidChainIdError({ chainId })
107
+
108
+ if (to && !isAddress(to)) throw new InvalidAddressError({ address: to })
109
+ if (from && !isAddress(from)) throw new InvalidAddressError({ address: from })
110
+ if (paymaster && !isAddress(paymaster))
111
+ throw new InvalidAddressError({ address: paymaster })
112
+
113
+ if (paymaster && !paymasterInput) {
114
+ throw new BaseError(
115
+ '`paymasterInput` must be provided when `paymaster` is defined',
116
+ )
117
+ }
118
+
119
+ if (!paymaster && paymasterInput) {
120
+ throw new BaseError(
121
+ '`paymaster` must be provided when `paymasterInput` is defined',
122
+ )
123
+ }
124
+ }
@@ -1,4 +1,4 @@
1
- import type { Address } from 'abitype'
1
+ import type { Abi, AbiEvent, Address } from 'abitype'
2
2
  import type { Block, BlockTag } from '../../types/block.js'
3
3
  import type { FeeValuesEIP1559 } from '../../types/fee.js'
4
4
  import type { Log as Log_ } from '../../types/log.js'
@@ -11,6 +11,11 @@ import type {
11
11
  RpcTransactionRequest as RpcTransactionRequest_,
12
12
  TransactionType,
13
13
  } from '../../types/rpc.js'
14
+ import type {
15
+ TransactionSerializable,
16
+ TransactionSerializableEIP1559,
17
+ TransactionSerialized,
18
+ } from '../../types/transaction.js'
14
19
  import type {
15
20
  Transaction as Transaction_,
16
21
  TransactionBase,
@@ -22,12 +27,10 @@ import type {
22
27
  } from '../../types/transaction.js'
23
28
  import type { UnionOmit } from '../../types/utils.js'
24
29
 
25
- import type { Abi, AbiEvent } from 'abitype'
26
-
27
30
  // Types
28
31
  // https://era.zksync.io/docs/api/js/types.html
29
32
 
30
- export type Log<
33
+ export type ZkSyncLog<
31
34
  TQuantity = bigint,
32
35
  TIndex = number,
33
36
  TPending extends boolean = boolean,
@@ -45,7 +48,7 @@ export type Log<
45
48
  logType: Hex | null
46
49
  }
47
50
 
48
- export type RpcLog = RpcLog_ & {
51
+ export type ZkSyncRpcLog = RpcLog_ & {
49
52
  l1BatchNumber: Hex | null
50
53
  // These are returned but doesn't apear in Log structure neither is mentioned in https://era.zksync.io/docs/api/js/types.html
51
54
  transactionLogIndex: Hex
@@ -57,14 +60,14 @@ type PaymasterParams = {
57
60
  paymasterInput: number[]
58
61
  }
59
62
 
60
- export type Eip712Meta = {
63
+ export type ZkSyncEip712Meta = {
61
64
  gasPerPubdata?: Hex
62
65
  factoryDeps?: Hex[]
63
66
  customSignature?: Hex
64
67
  paymasterParams?: PaymasterParams
65
68
  }
66
69
 
67
- export type L2ToL1Log = {
70
+ export type ZkSyncL2ToL1Log = {
68
71
  blockNumber: bigint
69
72
  blockHash: string
70
73
  l1BatchNumber: bigint
@@ -78,7 +81,7 @@ export type L2ToL1Log = {
78
81
  logIndex: bigint
79
82
  }
80
83
 
81
- export type RpcL2ToL1Log = {
84
+ export type ZkSyncRpcL2ToL1Log = {
82
85
  blockNumber: Hex
83
86
  blockHash: Hex
84
87
  l1BatchNumber: Hex
@@ -154,7 +157,7 @@ type TransactionPriority<TPending extends boolean = boolean> = TransactionBase<
154
157
  type: 'priority'
155
158
  }
156
159
 
157
- export type TransactionEIP712<TPending extends boolean = boolean> =
160
+ export type ZkSyncTransactionEIP712<TPending extends boolean = boolean> =
158
161
  TransactionBase<bigint, number, TPending> &
159
162
  TransactionOverrides &
160
163
  FeeValuesEIP1559 & {
@@ -171,7 +174,7 @@ type Transaction<TPending extends boolean = boolean> = Transaction_<
171
174
  export type ZkSyncTransaction<TPending extends boolean = boolean> =
172
175
  | Transaction<TPending>
173
176
  | TransactionPriority<TPending>
174
- | TransactionEIP712<TPending>
177
+ | ZkSyncTransactionEIP712<TPending>
175
178
 
176
179
  // Transaction (RPC)
177
180
 
@@ -180,28 +183,28 @@ type RpcTransactionOverrides = {
180
183
  l1BatchTxIndex: Hex
181
184
  }
182
185
 
183
- export type RpcTransactionLegacy<TPending extends boolean = boolean> =
186
+ type RpcTransactionLegacy<TPending extends boolean = boolean> =
184
187
  TransactionLegacy<Quantity, Index, TPending, '0x0'> & RpcTransactionOverrides
185
188
 
186
- export type RpcTransactionEIP2930<TPending extends boolean = boolean> =
189
+ type RpcTransactionEIP2930<TPending extends boolean = boolean> =
187
190
  TransactionEIP2930<Quantity, Index, TPending, '0x1'> & RpcTransactionOverrides
188
191
 
189
192
  // Cannot use default EIP1559 transaction because the fee `gasPrice` parameter is set to `never`
190
- export type RpcTransactionEIP1559<TPending extends boolean = boolean> =
193
+ type RpcTransactionEIP1559<TPending extends boolean = boolean> =
191
194
  TransactionBase<Quantity, Index, TPending> &
192
195
  ZkSyncFeeValues<Quantity> &
193
196
  RpcTransactionOverrides & {
194
197
  type: '0x2'
195
198
  }
196
199
 
197
- export type RpcTransactionPriority<TPending extends boolean = boolean> =
200
+ export type ZkSyncRpcTransactionPriority<TPending extends boolean = boolean> =
198
201
  TransactionBase<Quantity, Index, TPending> &
199
202
  ZkSyncFeeValues<Quantity> &
200
203
  RpcTransactionOverrides & {
201
204
  type: PriorityType
202
205
  }
203
206
 
204
- export type RpcTransactionEIP712<TPending extends boolean = boolean> =
207
+ export type ZkSyncRpcTransactionEIP712<TPending extends boolean = boolean> =
205
208
  TransactionBase<Quantity, Index, TPending> &
206
209
  ZkSyncFeeValues<Quantity> &
207
210
  RpcTransactionOverrides & {
@@ -213,8 +216,8 @@ export type ZkSyncRpcTransaction<TPending extends boolean = boolean> =
213
216
  | RpcTransactionLegacy<TPending>
214
217
  | RpcTransactionEIP2930<TPending>
215
218
  | RpcTransactionEIP1559<TPending>
216
- | RpcTransactionPriority<TPending>
217
- | RpcTransactionEIP712<TPending>,
219
+ | ZkSyncRpcTransactionPriority<TPending>
220
+ | ZkSyncRpcTransactionEIP712<TPending>,
218
221
  'typeHex'
219
222
  >
220
223
 
@@ -237,7 +240,7 @@ export type ZkSyncTransactionRequestEIP712 = Omit<
237
240
  gasPerPubdata?: bigint
238
241
  customSignature?: Hex
239
242
  factoryDeps?: Hex[]
240
- type: 'eip712' | 'priority'
243
+ type?: 'eip712' | 'priority'
241
244
  } & (
242
245
  | { paymaster: Address; paymasterInput: Hex }
243
246
  | { paymaster?: undefined; paymasterInput?: undefined }
@@ -249,18 +252,18 @@ export type ZkSyncTransactionRequest =
249
252
 
250
253
  type RpcTransactionRequest = RpcTransactionRequest_ & { eip712Meta?: undefined }
251
254
 
252
- export type RpcTransactionRequestEIP712 = TransactionRequestBase<
255
+ export type ZkSyncRpcTransactionRequestEIP712 = TransactionRequestBase<
253
256
  Quantity,
254
257
  Index
255
258
  > &
256
259
  Partial<FeeValuesEIP1559<Quantity>> & {
257
- eip712Meta: Eip712Meta
260
+ eip712Meta: ZkSyncEip712Meta
258
261
  type: EIP712Type | PriorityType
259
262
  }
260
263
 
261
264
  export type ZkSyncRpcTransactionRequest =
262
265
  | RpcTransactionRequest
263
- | RpcTransactionRequestEIP712
266
+ | ZkSyncRpcTransactionRequestEIP712
264
267
 
265
268
  export type ZkSyncTransactionType = TransactionType | 'eip712' | 'priority'
266
269
 
@@ -270,16 +273,43 @@ export type ZkSyncTransactionType = TransactionType | 'eip712' | 'priority'
270
273
  export type ZkSyncRpcTransactionReceiptOverrides = {
271
274
  l1BatchNumber: Hex
272
275
  l1BatchTxIndex: Hex
273
- logs: RpcLog[]
274
- l2ToL1Logs: RpcL2ToL1Log[]
276
+ logs: ZkSyncRpcLog[]
277
+ l2ToL1Logs: ZkSyncRpcL2ToL1Log[]
275
278
  root: Hex
276
279
  }
277
280
 
278
281
  export type ZkSyncTransactionReceiptOverrides = {
279
282
  l1BatchNumber: bigint | null
280
283
  l1BatchTxIndex: bigint | null
281
- logs: Log[]
282
- l2ToL1Logs: L2ToL1Log[]
284
+ logs: ZkSyncLog[]
285
+ l2ToL1Logs: ZkSyncL2ToL1Log[]
283
286
  }
284
287
  export type ZkSyncTransactionReceipt = Omit<TransactionReceipt, 'logs'> &
285
288
  ZkSyncTransactionReceiptOverrides
289
+
290
+ // Serializers
291
+
292
+ export type ZkSyncTransactionSerializable =
293
+ | TransactionSerializable
294
+ | ZkSyncTransactionSerializableEIP712
295
+
296
+ export type ZkSyncTransactionSerialized<
297
+ TType extends TransactionType = 'eip712',
298
+ > = TType extends 'eip712'
299
+ ? ZkSyncTransactionSerializedEIP712
300
+ : TransactionSerialized<TType>
301
+
302
+ export type ZkSyncTransactionSerializedEIP712 = `0x71${string}`
303
+
304
+ export type ZkSyncTransactionSerializableEIP712<
305
+ TQuantity = bigint,
306
+ TIndex = number,
307
+ > = Omit<TransactionSerializableEIP1559<TQuantity, TIndex>, 'type'> & {
308
+ from: Hex
309
+ gasPerPubdata?: bigint
310
+ paymaster?: Address
311
+ factoryDeps?: Hex[]
312
+ paymasterInput?: Hex
313
+ customSignature?: Hex
314
+ type?: 'eip712'
315
+ }
package/errors/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '0.0.0-main.20231026T205555'
1
+ export const version = '0.0.0-main.20231026T213343'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "viem",
3
3
  "description": "TypeScript Interface for Ethereum",
4
- "version": "0.0.0-main.20231026T205555",
4
+ "version": "0.0.0-main.20231026T213343",
5
5
  "type": "module",
6
6
  "main": "./_cjs/index.js",
7
7
  "module": "./_esm/index.js",