viem 1.18.9 → 1.19.0

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 (133) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +1 -1
  3. package/_cjs/actions/test/dumpState.js +10 -0
  4. package/_cjs/actions/test/dumpState.js.map +1 -0
  5. package/_cjs/actions/test/loadState.js +11 -0
  6. package/_cjs/actions/test/loadState.js.map +1 -0
  7. package/_cjs/chains/definitions/lukso.js +31 -0
  8. package/_cjs/chains/definitions/lukso.js.map +1 -0
  9. package/_cjs/chains/index.js +5 -3
  10. package/_cjs/chains/index.js.map +1 -1
  11. package/_cjs/clients/decorators/test.js +4 -0
  12. package/_cjs/clients/decorators/test.js.map +1 -1
  13. package/_cjs/constants/bytes.js +5 -0
  14. package/_cjs/constants/bytes.js.map +1 -0
  15. package/_cjs/constants/strings.js +5 -0
  16. package/_cjs/constants/strings.js.map +1 -0
  17. package/_cjs/errors/node.js +11 -11
  18. package/_cjs/errors/node.js.map +1 -1
  19. package/_cjs/errors/rpc.js +18 -18
  20. package/_cjs/errors/rpc.js.map +1 -1
  21. package/_cjs/index.js +10 -6
  22. package/_cjs/index.js.map +1 -1
  23. package/_cjs/utils/formatters/transaction.js +14 -0
  24. package/_cjs/utils/formatters/transaction.js.map +1 -1
  25. package/_cjs/utils/signature/hashMessage.js +2 -1
  26. package/_cjs/utils/signature/hashMessage.js.map +1 -1
  27. package/_cjs/utils/transaction/serializeTransaction.js +20 -4
  28. package/_cjs/utils/transaction/serializeTransaction.js.map +1 -1
  29. package/_esm/actions/test/dumpState.js +26 -0
  30. package/_esm/actions/test/dumpState.js.map +1 -0
  31. package/_esm/actions/test/loadState.js +27 -0
  32. package/_esm/actions/test/loadState.js.map +1 -0
  33. package/_esm/chains/definitions/lukso.js +28 -0
  34. package/_esm/chains/definitions/lukso.js.map +1 -0
  35. package/_esm/chains/index.js +1 -0
  36. package/_esm/chains/index.js.map +1 -1
  37. package/_esm/clients/decorators/test.js +4 -0
  38. package/_esm/clients/decorators/test.js.map +1 -1
  39. package/_esm/constants/bytes.js +2 -0
  40. package/_esm/constants/bytes.js.map +1 -0
  41. package/_esm/constants/strings.js +2 -0
  42. package/_esm/constants/strings.js.map +1 -0
  43. package/_esm/errors/node.js +22 -11
  44. package/_esm/errors/node.js.map +1 -1
  45. package/_esm/errors/rpc.js +36 -18
  46. package/_esm/errors/rpc.js.map +1 -1
  47. package/_esm/index.js +2 -0
  48. package/_esm/index.js.map +1 -1
  49. package/_esm/utils/formatters/transaction.js +16 -0
  50. package/_esm/utils/formatters/transaction.js.map +1 -1
  51. package/_esm/utils/signature/hashMessage.js +2 -1
  52. package/_esm/utils/signature/hashMessage.js.map +1 -1
  53. package/_esm/utils/transaction/serializeTransaction.js +20 -6
  54. package/_esm/utils/transaction/serializeTransaction.js.map +1 -1
  55. package/_types/actions/test/dumpState.d.ts +31 -0
  56. package/_types/actions/test/dumpState.d.ts.map +1 -0
  57. package/_types/actions/test/loadState.d.ts +34 -0
  58. package/_types/actions/test/loadState.d.ts.map +1 -0
  59. package/_types/chains/celo/formatters.d.ts +24 -13
  60. package/_types/chains/celo/formatters.d.ts.map +1 -1
  61. package/_types/chains/definitions/base.d.ts +18 -10
  62. package/_types/chains/definitions/base.d.ts.map +1 -1
  63. package/_types/chains/definitions/baseGoerli.d.ts +18 -10
  64. package/_types/chains/definitions/baseGoerli.d.ts.map +1 -1
  65. package/_types/chains/definitions/baseSepolia.d.ts +18 -10
  66. package/_types/chains/definitions/baseSepolia.d.ts.map +1 -1
  67. package/_types/chains/definitions/celo.d.ts +24 -13
  68. package/_types/chains/definitions/celo.d.ts.map +1 -1
  69. package/_types/chains/definitions/celoAlfajores.d.ts +24 -13
  70. package/_types/chains/definitions/celoAlfajores.d.ts.map +1 -1
  71. package/_types/chains/definitions/celoCannoli.d.ts +24 -13
  72. package/_types/chains/definitions/celoCannoli.d.ts.map +1 -1
  73. package/_types/chains/definitions/lukso.d.ts +27 -0
  74. package/_types/chains/definitions/lukso.d.ts.map +1 -0
  75. package/_types/chains/definitions/optimism.d.ts +18 -10
  76. package/_types/chains/definitions/optimism.d.ts.map +1 -1
  77. package/_types/chains/definitions/optimismGoerli.d.ts +18 -10
  78. package/_types/chains/definitions/optimismGoerli.d.ts.map +1 -1
  79. package/_types/chains/definitions/optimismSepolia.d.ts +18 -10
  80. package/_types/chains/definitions/optimismSepolia.d.ts.map +1 -1
  81. package/_types/chains/definitions/zkSync.d.ts +11 -0
  82. package/_types/chains/definitions/zkSync.d.ts.map +1 -1
  83. package/_types/chains/definitions/zkSyncTestnet.d.ts +11 -0
  84. package/_types/chains/definitions/zkSyncTestnet.d.ts.map +1 -1
  85. package/_types/chains/definitions/zora.d.ts +18 -10
  86. package/_types/chains/definitions/zora.d.ts.map +1 -1
  87. package/_types/chains/definitions/zoraSepolia.d.ts +18 -10
  88. package/_types/chains/definitions/zoraSepolia.d.ts.map +1 -1
  89. package/_types/chains/definitions/zoraTestnet.d.ts +18 -10
  90. package/_types/chains/definitions/zoraTestnet.d.ts.map +1 -1
  91. package/_types/chains/index.d.ts +1 -0
  92. package/_types/chains/index.d.ts.map +1 -1
  93. package/_types/chains/optimism/formatters.d.ts +18 -10
  94. package/_types/chains/optimism/formatters.d.ts.map +1 -1
  95. package/_types/chains/zksync/formatters.d.ts +11 -0
  96. package/_types/chains/zksync/formatters.d.ts.map +1 -1
  97. package/_types/clients/decorators/test.d.ts +41 -0
  98. package/_types/clients/decorators/test.d.ts.map +1 -1
  99. package/_types/constants/bytes.d.ts +2 -0
  100. package/_types/constants/bytes.d.ts.map +1 -0
  101. package/_types/constants/strings.d.ts +2 -0
  102. package/_types/constants/strings.d.ts.map +1 -0
  103. package/_types/index.d.ts +2 -0
  104. package/_types/index.d.ts.map +1 -1
  105. package/_types/types/eip1193.d.ts +16 -0
  106. package/_types/types/eip1193.d.ts.map +1 -1
  107. package/_types/types/rpc.d.ts +2 -2
  108. package/_types/types/rpc.d.ts.map +1 -1
  109. package/_types/types/transaction.d.ts +10 -1
  110. package/_types/types/transaction.d.ts.map +1 -1
  111. package/_types/types/utils.d.ts +8 -0
  112. package/_types/types/utils.d.ts.map +1 -1
  113. package/_types/utils/formatters/transaction.d.ts +1 -1
  114. package/_types/utils/formatters/transaction.d.ts.map +1 -1
  115. package/_types/utils/formatters/transactionRequest.d.ts +1 -1
  116. package/_types/utils/signature/hashMessage.d.ts.map +1 -1
  117. package/_types/utils/transaction/serializeTransaction.d.ts.map +1 -1
  118. package/actions/test/dumpState.ts +44 -0
  119. package/actions/test/loadState.ts +47 -0
  120. package/chains/definitions/lukso.ts +28 -0
  121. package/chains/index.ts +1 -0
  122. package/clients/decorators/test.ts +50 -0
  123. package/constants/bytes.ts +2 -0
  124. package/constants/strings.ts +1 -0
  125. package/index.ts +2 -0
  126. package/package.json +1 -1
  127. package/types/eip1193.ts +16 -0
  128. package/types/rpc.ts +15 -6
  129. package/types/transaction.ts +10 -1
  130. package/types/utils.ts +11 -0
  131. package/utils/formatters/transaction.ts +17 -2
  132. package/utils/signature/hashMessage.ts +2 -1
  133. package/utils/transaction/serializeTransaction.ts +18 -12
@@ -0,0 +1,2 @@
1
+ export const zeroHash =
2
+ '0x0000000000000000000000000000000000000000000000000000000000000000' as const
@@ -0,0 +1 @@
1
+ export const presignMessagePrefix = '\x19Ethereum Signed Message:\n'
package/index.ts CHANGED
@@ -582,6 +582,8 @@ export {
582
582
  minInt248,
583
583
  minInt256,
584
584
  } from './constants/number.js'
585
+ export { zeroHash } from './constants/bytes.js'
586
+ export { presignMessagePrefix } from './constants/strings.js'
585
587
  export {
586
588
  AbiConstructorNotFoundError,
587
589
  type AbiConstructorNotFoundErrorType,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "viem",
3
3
  "description": "TypeScript Interface for Ethereum",
4
- "version": "1.18.9",
4
+ "version": "1.19.0",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
7
7
  "types": "./_types/index.d.ts",
package/types/eip1193.ts CHANGED
@@ -707,6 +707,14 @@ export type TestRpcSchema<TMode extends string> = [
707
707
  Parameters: [hash: Hash]
708
708
  ReturnType: void
709
709
  },
710
+ /**
711
+ * @description Serializes the current state (including contracts code, contract's storage, accounts properties, etc.) into a savable data blob.
712
+ */
713
+ {
714
+ Method: `${TMode}_dumpState`
715
+ Parameters?: undefined
716
+ ReturnType: Hex
717
+ },
710
718
  /**
711
719
  * @description Turn on call traces for transactions that are returned to the user when they execute a transaction (instead of just txhash/receipt).
712
720
  */
@@ -733,6 +741,14 @@ export type TestRpcSchema<TMode extends string> = [
733
741
  Parameters?: undefined
734
742
  ReturnType: boolean
735
743
  },
744
+ /**
745
+ * @description Adds state previously dumped with `dumpState` to the current chain.
746
+ */
747
+ {
748
+ Method: `${TMode}_loadState`
749
+ Parameters?: [Hex]
750
+ ReturnType: void
751
+ },
736
752
  /**
737
753
  * @description Advance the block number of the network by a certain number of blocks
738
754
  * @link https://hardhat.org/hardhat-network/docs/reference#hardhat_mine
package/types/rpc.ts CHANGED
@@ -17,7 +17,7 @@ import type {
17
17
  TransactionRequestEIP2930,
18
18
  TransactionRequestLegacy,
19
19
  } from './transaction.js'
20
- import type { UnionOmit } from './utils.js'
20
+ import type { UnionOmit, UnionPartialBy } from './utils.js'
21
21
 
22
22
  export type Index = `0x${string}`
23
23
  export type Quantity = `0x${string}`
@@ -46,9 +46,18 @@ export type RpcTransactionRequest =
46
46
  | TransactionRequestLegacy<Quantity, Index, '0x0'>
47
47
  | TransactionRequestEIP2930<Quantity, Index, '0x1'>
48
48
  | TransactionRequestEIP1559<Quantity, Index, '0x2'>
49
- export type RpcTransaction<TPending extends boolean = boolean> = UnionOmit<
50
- | TransactionLegacy<Quantity, Index, TPending, '0x0'>
51
- | TransactionEIP2930<Quantity, Index, TPending, '0x1'>
52
- | TransactionEIP1559<Quantity, Index, TPending, '0x2'>,
53
- 'typeHex'
49
+ export type RpcTransaction<
50
+ TPending extends boolean = boolean,
51
+ // `yParity` is optional on the RPC type as some nodes do not return it
52
+ // for 1559 & 2930 transactions (they should!).
53
+ _optionalProperties extends 'yParity' = 'yParity',
54
+ _excludedProperties extends string = 'typeHex',
55
+ > = UnionOmit<
56
+ UnionPartialBy<
57
+ | TransactionLegacy<Quantity, Index, TPending, '0x0'>
58
+ | TransactionEIP2930<Quantity, Index, TPending, '0x1'>
59
+ | TransactionEIP1559<Quantity, Index, TPending, '0x2'>,
60
+ _optionalProperties
61
+ >,
62
+ _excludedProperties
54
63
  >
@@ -80,16 +80,21 @@ export type TransactionBase<
80
80
  v: TQuantity
81
81
  /** Value in wei sent with this transaction */
82
82
  value: TQuantity
83
+ /** The parity of the y-value of the secp256k1 signature. */
84
+ yParity: TIndex
83
85
  }
84
86
  export type TransactionLegacy<
85
87
  TQuantity = bigint,
86
88
  TIndex = number,
87
89
  TPending extends boolean = boolean,
88
90
  TType = 'legacy',
89
- > = TransactionBase<TQuantity, TIndex, TPending> &
91
+ > = Omit<TransactionBase<TQuantity, TIndex, TPending>, 'yParity'> &
90
92
  FeeValuesLegacy<TQuantity> & {
93
+ /** EIP-2930 Access List. */
91
94
  accessList?: never
95
+ /** Chain ID that this transaction is valid on. */
92
96
  chainId?: TIndex
97
+ yParity?: never
93
98
  type: TType
94
99
  }
95
100
  export type TransactionEIP2930<
@@ -99,7 +104,9 @@ export type TransactionEIP2930<
99
104
  TType = 'eip2930',
100
105
  > = TransactionBase<TQuantity, TIndex, TPending> &
101
106
  FeeValuesLegacy<TQuantity> & {
107
+ /** EIP-2930 Access List. */
102
108
  accessList: AccessList
109
+ /** Chain ID that this transaction is valid on. */
103
110
  chainId: TIndex
104
111
  type: TType
105
112
  }
@@ -110,7 +117,9 @@ export type TransactionEIP1559<
110
117
  TType = 'eip1559',
111
118
  > = TransactionBase<TQuantity, TIndex, TPending> &
112
119
  FeeValuesEIP1559<TQuantity> & {
120
+ /** EIP-2930 Access List. */
113
121
  accessList: AccessList
122
+ /** Chain ID that this transaction is valid on. */
114
123
  chainId: TIndex
115
124
  type: TType
116
125
  }
package/types/utils.ts CHANGED
@@ -194,6 +194,17 @@ export type UnionOmit<T, K extends keyof any> = T extends any
194
194
  */
195
195
  export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
196
196
 
197
+ /**
198
+ * @description Creates a type that is a partial of T, but with the required keys K.
199
+ *
200
+ * @example
201
+ * PartialBy<{ a: string, b: number } | { a: string, b: undefined, c: number }, 'a'>
202
+ * => { a?: string, b: number } | { a?: string, b: undefined, c: number }
203
+ */
204
+ export type UnionPartialBy<T, K extends keyof T> = T extends any
205
+ ? PartialBy<T, K>
206
+ : never
207
+
197
208
  /**
198
209
  * @description Combines members of an intersection into a readable type.
199
210
  *
@@ -68,18 +68,33 @@ export function formatTransaction(transaction: Partial<RpcTransaction>) {
68
68
  typeHex: transaction.type ? transaction.type : undefined,
69
69
  value: transaction.value ? BigInt(transaction.value) : undefined,
70
70
  v: transaction.v ? BigInt(transaction.v) : undefined,
71
- }
71
+ } as Transaction
72
+
73
+ transaction_.yParity = (() => {
74
+ // If `yParity` is provided, we will use it.
75
+ if (transaction.yParity) return Number(transaction.yParity)
76
+
77
+ // If no `yParity` provided, try derive from `v`.
78
+ if (typeof transaction_.v === 'bigint') {
79
+ if (transaction_.v === 0n || transaction_.v === 27n) return 0
80
+ if (transaction_.v === 1n || transaction_.v === 28n) return 1
81
+ if (transaction_.v >= 35n) return transaction_.v % 2n === 0n ? 1 : 0
82
+ }
83
+
84
+ return undefined
85
+ })()
72
86
 
73
87
  if (transaction_.type === 'legacy') {
74
88
  delete transaction_.accessList
75
89
  delete transaction_.maxFeePerGas
76
90
  delete transaction_.maxPriorityFeePerGas
91
+ delete transaction_.yParity
77
92
  }
78
93
  if (transaction_.type === 'eip2930') {
79
94
  delete transaction_.maxFeePerGas
80
95
  delete transaction_.maxPriorityFeePerGas
81
96
  }
82
- return transaction_ as Transaction
97
+ return transaction_
83
98
  }
84
99
 
85
100
  export type DefineTransactionErrorType = DefineFormatterErrorType | ErrorType
@@ -1,3 +1,4 @@
1
+ import { presignMessagePrefix } from '../../constants/strings.js'
1
2
  import type { ErrorType } from '../../errors/utils.js'
2
3
  import type { ByteArray, Hex, SignableMessage } from '../../types/misc.js'
3
4
  import { type ConcatErrorType, concat } from '../data/concat.js'
@@ -32,7 +33,7 @@ export function hashMessage<TTo extends To = 'hex'>(
32
33
  return toBytes(message.raw)
33
34
  })()
34
35
  const prefixBytes = stringToBytes(
35
- `\x19Ethereum Signed Message:\n${messageBytes.length}`,
36
+ `${presignMessagePrefix}${messageBytes.length}`,
36
37
  )
37
38
  return keccak256(concat([prefixBytes, messageBytes]), to_)
38
39
  }
@@ -121,12 +121,15 @@ function serializeTransactionEIP1559(
121
121
  serializedAccessList,
122
122
  ]
123
123
 
124
- if (signature)
125
- serializedTransaction.push(
126
- signature.v === 27n ? '0x' : toHex(1), // yParity
127
- trim(signature.r),
128
- trim(signature.s),
129
- )
124
+ if (signature) {
125
+ const yParity = (() => {
126
+ if (signature.v === 0n) return '0x'
127
+ if (signature.v === 1n) return toHex(1)
128
+
129
+ return signature.v === 27n ? '0x' : toHex(1)
130
+ })()
131
+ serializedTransaction.push(yParity, trim(signature.r), trim(signature.s))
132
+ }
130
133
 
131
134
  return concatHex([
132
135
  '0x02',
@@ -165,12 +168,15 @@ function serializeTransactionEIP2930(
165
168
  serializedAccessList,
166
169
  ]
167
170
 
168
- if (signature)
169
- serializedTransaction.push(
170
- signature.v === 27n ? '0x' : toHex(1), // yParity
171
- signature.r,
172
- signature.s,
173
- )
171
+ if (signature) {
172
+ const yParity = (() => {
173
+ if (signature.v === 0n) return '0x'
174
+ if (signature.v === 1n) return toHex(1)
175
+
176
+ return signature.v === 27n ? '0x' : toHex(1)
177
+ })()
178
+ serializedTransaction.push(yParity, trim(signature.r), trim(signature.s))
179
+ }
174
180
 
175
181
  return concatHex([
176
182
  '0x01',