viem 2.8.18 → 2.9.1

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 (254) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/_cjs/chains/definitions/blast.js +5 -1
  3. package/_cjs/chains/definitions/blast.js.map +1 -1
  4. package/_cjs/chains/definitions/blastSepolia.js +2 -1
  5. package/_cjs/chains/definitions/blastSepolia.js.map +1 -1
  6. package/_cjs/chains/definitions/optimismSepolia.js +5 -0
  7. package/_cjs/chains/definitions/optimismSepolia.js.map +1 -1
  8. package/_cjs/chains/definitions/reyaNetwork.js +23 -0
  9. package/_cjs/chains/definitions/reyaNetwork.js.map +1 -0
  10. package/_cjs/chains/index.js +4 -2
  11. package/_cjs/chains/index.js.map +1 -1
  12. package/_cjs/chains/opStack/abis.js +1070 -1
  13. package/_cjs/chains/opStack/abis.js.map +1 -1
  14. package/_cjs/chains/opStack/actions/buildProveWithdrawal.js +3 -3
  15. package/_cjs/chains/opStack/actions/buildProveWithdrawal.js.map +1 -1
  16. package/_cjs/chains/opStack/actions/getGame.js +20 -0
  17. package/_cjs/chains/opStack/actions/getGame.js.map +1 -0
  18. package/_cjs/chains/opStack/actions/getGames.js +56 -0
  19. package/_cjs/chains/opStack/actions/getGames.js.map +1 -0
  20. package/_cjs/chains/opStack/actions/getL2Output.js +12 -0
  21. package/_cjs/chains/opStack/actions/getL2Output.js.map +1 -1
  22. package/_cjs/chains/opStack/actions/getPortalVersion.js +25 -0
  23. package/_cjs/chains/opStack/actions/getPortalVersion.js.map +1 -0
  24. package/_cjs/chains/opStack/actions/getTimeToFinalize.js +52 -26
  25. package/_cjs/chains/opStack/actions/getTimeToFinalize.js.map +1 -1
  26. package/_cjs/chains/opStack/actions/getTimeToNextGame.js +45 -0
  27. package/_cjs/chains/opStack/actions/getTimeToNextGame.js.map +1 -0
  28. package/_cjs/chains/opStack/actions/getTimeToProve.js +9 -1
  29. package/_cjs/chains/opStack/actions/getTimeToProve.js.map +1 -1
  30. package/_cjs/chains/opStack/actions/getWithdrawalStatus.js +79 -28
  31. package/_cjs/chains/opStack/actions/getWithdrawalStatus.js.map +1 -1
  32. package/_cjs/chains/opStack/actions/proveWithdrawal.js +1 -1
  33. package/_cjs/chains/opStack/actions/proveWithdrawal.js.map +1 -1
  34. package/_cjs/chains/opStack/actions/waitForNextGame.js +35 -0
  35. package/_cjs/chains/opStack/actions/waitForNextGame.js.map +1 -0
  36. package/_cjs/chains/opStack/actions/waitForNextL2Output.js.map +1 -1
  37. package/_cjs/chains/opStack/actions/waitToProve.js +34 -3
  38. package/_cjs/chains/opStack/actions/waitToProve.js.map +1 -1
  39. package/_cjs/chains/opStack/decorators/publicL1.js +10 -0
  40. package/_cjs/chains/opStack/decorators/publicL1.js.map +1 -1
  41. package/_cjs/chains/opStack/errors/withdrawal.js +13 -1
  42. package/_cjs/chains/opStack/errors/withdrawal.js.map +1 -1
  43. package/_cjs/chains/opStack/index.js +11 -1
  44. package/_cjs/chains/opStack/index.js.map +1 -1
  45. package/_cjs/chains/zksync/actions/deployContract.js +26 -0
  46. package/_cjs/chains/zksync/actions/deployContract.js.map +1 -0
  47. package/_cjs/chains/zksync/constants/abis.js +427 -0
  48. package/_cjs/chains/zksync/constants/abis.js.map +1 -0
  49. package/_cjs/chains/zksync/constants/address.js +5 -0
  50. package/_cjs/chains/zksync/constants/address.js.map +1 -0
  51. package/_cjs/chains/zksync/constants/contract.js +6 -0
  52. package/_cjs/chains/zksync/constants/contract.js.map +1 -0
  53. package/_cjs/chains/zksync/constants/number.js +6 -0
  54. package/_cjs/chains/zksync/constants/number.js.map +1 -0
  55. package/_cjs/chains/zksync/decorators/eip712.js +2 -0
  56. package/_cjs/chains/zksync/decorators/eip712.js.map +1 -1
  57. package/_cjs/chains/zksync/errors/bytecode.js +41 -0
  58. package/_cjs/chains/zksync/errors/bytecode.js.map +1 -0
  59. package/_cjs/chains/zksync/formatters.js +5 -1
  60. package/_cjs/chains/zksync/formatters.js.map +1 -1
  61. package/_cjs/chains/zksync/index.js +3 -1
  62. package/_cjs/chains/zksync/index.js.map +1 -1
  63. package/_cjs/chains/zksync/types/contract.js +3 -0
  64. package/_cjs/chains/zksync/types/contract.js.map +1 -0
  65. package/_cjs/chains/zksync/utils/abi/encodeDeployData.js +68 -0
  66. package/_cjs/chains/zksync/utils/abi/encodeDeployData.js.map +1 -0
  67. package/_cjs/chains/zksync/utils/getEip712Domain.js +4 -2
  68. package/_cjs/chains/zksync/utils/getEip712Domain.js.map +1 -1
  69. package/_cjs/chains/zksync/utils/hashBytecode.js +36 -0
  70. package/_cjs/chains/zksync/utils/hashBytecode.js.map +1 -0
  71. package/_cjs/errors/version.js +1 -1
  72. package/_cjs/errors/version.js.map +1 -1
  73. package/_esm/chains/definitions/blast.js +5 -1
  74. package/_esm/chains/definitions/blast.js.map +1 -1
  75. package/_esm/chains/definitions/blastSepolia.js +2 -1
  76. package/_esm/chains/definitions/blastSepolia.js.map +1 -1
  77. package/_esm/chains/definitions/optimismSepolia.js +5 -0
  78. package/_esm/chains/definitions/optimismSepolia.js.map +1 -1
  79. package/_esm/chains/definitions/reyaNetwork.js +20 -0
  80. package/_esm/chains/definitions/reyaNetwork.js.map +1 -0
  81. package/_esm/chains/index.js +1 -0
  82. package/_esm/chains/index.js.map +1 -1
  83. package/_esm/chains/opStack/abis.js +1069 -0
  84. package/_esm/chains/opStack/abis.js.map +1 -1
  85. package/_esm/chains/opStack/actions/buildProveWithdrawal.js +4 -3
  86. package/_esm/chains/opStack/actions/buildProveWithdrawal.js.map +1 -1
  87. package/_esm/chains/opStack/actions/getGame.js +40 -0
  88. package/_esm/chains/opStack/actions/getGame.js.map +1 -0
  89. package/_esm/chains/opStack/actions/getGames.js +75 -0
  90. package/_esm/chains/opStack/actions/getGames.js.map +1 -0
  91. package/_esm/chains/opStack/actions/getL2Output.js +12 -0
  92. package/_esm/chains/opStack/actions/getL2Output.js.map +1 -1
  93. package/_esm/chains/opStack/actions/getPortalVersion.js +48 -0
  94. package/_esm/chains/opStack/actions/getPortalVersion.js.map +1 -0
  95. package/_esm/chains/opStack/actions/getTimeToFinalize.js +54 -27
  96. package/_esm/chains/opStack/actions/getTimeToFinalize.js.map +1 -1
  97. package/_esm/chains/opStack/actions/getTimeToNextGame.js +71 -0
  98. package/_esm/chains/opStack/actions/getTimeToNextGame.js.map +1 -0
  99. package/_esm/chains/opStack/actions/getTimeToProve.js +10 -1
  100. package/_esm/chains/opStack/actions/getTimeToProve.js.map +1 -1
  101. package/_esm/chains/opStack/actions/getWithdrawalStatus.js +84 -32
  102. package/_esm/chains/opStack/actions/getWithdrawalStatus.js.map +1 -1
  103. package/_esm/chains/opStack/actions/proveWithdrawal.js +1 -1
  104. package/_esm/chains/opStack/actions/proveWithdrawal.js.map +1 -1
  105. package/_esm/chains/opStack/actions/waitForNextGame.js +61 -0
  106. package/_esm/chains/opStack/actions/waitForNextGame.js.map +1 -0
  107. package/_esm/chains/opStack/actions/waitForNextL2Output.js.map +1 -1
  108. package/_esm/chains/opStack/actions/waitToProve.js +35 -3
  109. package/_esm/chains/opStack/actions/waitToProve.js.map +1 -1
  110. package/_esm/chains/opStack/decorators/publicL1.js +10 -0
  111. package/_esm/chains/opStack/decorators/publicL1.js.map +1 -1
  112. package/_esm/chains/opStack/errors/withdrawal.js +11 -0
  113. package/_esm/chains/opStack/errors/withdrawal.js.map +1 -1
  114. package/_esm/chains/opStack/index.js +5 -0
  115. package/_esm/chains/opStack/index.js.map +1 -1
  116. package/_esm/chains/zksync/actions/deployContract.js +52 -0
  117. package/_esm/chains/zksync/actions/deployContract.js.map +1 -0
  118. package/_esm/chains/zksync/constants/abis.js +425 -0
  119. package/_esm/chains/zksync/constants/abis.js.map +1 -0
  120. package/_esm/chains/zksync/constants/address.js +2 -0
  121. package/_esm/chains/zksync/constants/address.js.map +1 -0
  122. package/_esm/chains/zksync/constants/contract.js +3 -0
  123. package/_esm/chains/zksync/constants/contract.js.map +1 -0
  124. package/_esm/chains/zksync/constants/number.js +3 -0
  125. package/_esm/chains/zksync/constants/number.js.map +1 -0
  126. package/_esm/chains/zksync/decorators/eip712.js +2 -0
  127. package/_esm/chains/zksync/decorators/eip712.js.map +1 -1
  128. package/_esm/chains/zksync/errors/bytecode.js +35 -0
  129. package/_esm/chains/zksync/errors/bytecode.js.map +1 -0
  130. package/_esm/chains/zksync/formatters.js +5 -1
  131. package/_esm/chains/zksync/formatters.js.map +1 -1
  132. package/_esm/chains/zksync/index.js +1 -0
  133. package/_esm/chains/zksync/index.js.map +1 -1
  134. package/_esm/chains/zksync/types/contract.js +2 -0
  135. package/_esm/chains/zksync/types/contract.js.map +1 -0
  136. package/_esm/chains/zksync/utils/abi/encodeDeployData.js +65 -0
  137. package/_esm/chains/zksync/utils/abi/encodeDeployData.js.map +1 -0
  138. package/_esm/chains/zksync/utils/getEip712Domain.js +4 -2
  139. package/_esm/chains/zksync/utils/getEip712Domain.js.map +1 -1
  140. package/_esm/chains/zksync/utils/hashBytecode.js +36 -0
  141. package/_esm/chains/zksync/utils/hashBytecode.js.map +1 -0
  142. package/_esm/errors/version.js +1 -1
  143. package/_esm/errors/version.js.map +1 -1
  144. package/_types/chains/definitions/blast.d.ts +1 -0
  145. package/_types/chains/definitions/blast.d.ts.map +1 -1
  146. package/_types/chains/definitions/blastSepolia.d.ts +2 -1
  147. package/_types/chains/definitions/blastSepolia.d.ts.map +1 -1
  148. package/_types/chains/definitions/optimismSepolia.d.ts +5 -0
  149. package/_types/chains/definitions/optimismSepolia.d.ts.map +1 -1
  150. package/_types/chains/definitions/reyaNetwork.d.ts +36 -0
  151. package/_types/chains/definitions/reyaNetwork.d.ts.map +1 -0
  152. package/_types/chains/index.d.ts +1 -0
  153. package/_types/chains/index.d.ts.map +1 -1
  154. package/_types/chains/opStack/abis.d.ts +824 -0
  155. package/_types/chains/opStack/abis.d.ts.map +1 -1
  156. package/_types/chains/opStack/actions/buildProveWithdrawal.d.ts +6 -2
  157. package/_types/chains/opStack/actions/buildProveWithdrawal.d.ts.map +1 -1
  158. package/_types/chains/opStack/actions/getGame.d.ts +57 -0
  159. package/_types/chains/opStack/actions/getGame.d.ts.map +1 -0
  160. package/_types/chains/opStack/actions/getGames.d.ts +48 -0
  161. package/_types/chains/opStack/actions/getGames.d.ts.map +1 -0
  162. package/_types/chains/opStack/actions/getL2Output.d.ts +8 -1
  163. package/_types/chains/opStack/actions/getL2Output.d.ts.map +1 -1
  164. package/_types/chains/opStack/actions/getPortalVersion.d.ts +46 -0
  165. package/_types/chains/opStack/actions/getPortalVersion.d.ts.map +1 -0
  166. package/_types/chains/opStack/actions/getTimeToFinalize.d.ts.map +1 -1
  167. package/_types/chains/opStack/actions/getTimeToNextGame.d.ts +61 -0
  168. package/_types/chains/opStack/actions/getTimeToNextGame.d.ts.map +1 -0
  169. package/_types/chains/opStack/actions/getTimeToProve.d.ts +6 -3
  170. package/_types/chains/opStack/actions/getTimeToProve.d.ts.map +1 -1
  171. package/_types/chains/opStack/actions/getWithdrawalStatus.d.ts +7 -1
  172. package/_types/chains/opStack/actions/getWithdrawalStatus.d.ts.map +1 -1
  173. package/_types/chains/opStack/actions/proveWithdrawal.d.ts +1 -1
  174. package/_types/chains/opStack/actions/proveWithdrawal.d.ts.map +1 -1
  175. package/_types/chains/opStack/actions/waitForNextGame.d.ts +60 -0
  176. package/_types/chains/opStack/actions/waitForNextGame.d.ts.map +1 -0
  177. package/_types/chains/opStack/actions/waitForNextL2Output.d.ts.map +1 -1
  178. package/_types/chains/opStack/actions/waitToProve.d.ts +10 -2
  179. package/_types/chains/opStack/actions/waitToProve.d.ts.map +1 -1
  180. package/_types/chains/opStack/decorators/publicL1.d.ts +129 -0
  181. package/_types/chains/opStack/decorators/publicL1.d.ts.map +1 -1
  182. package/_types/chains/opStack/errors/withdrawal.d.ts +7 -0
  183. package/_types/chains/opStack/errors/withdrawal.d.ts.map +1 -1
  184. package/_types/chains/opStack/index.d.ts +5 -0
  185. package/_types/chains/opStack/index.d.ts.map +1 -1
  186. package/_types/chains/opStack/types/chain.d.ts +1 -1
  187. package/_types/chains/opStack/types/chain.d.ts.map +1 -1
  188. package/_types/chains/opStack/types/withdrawal.d.ts +7 -0
  189. package/_types/chains/opStack/types/withdrawal.d.ts.map +1 -1
  190. package/_types/chains/zksync/actions/deployContract.d.ts +49 -0
  191. package/_types/chains/zksync/actions/deployContract.d.ts.map +1 -0
  192. package/_types/chains/zksync/constants/abis.d.ts +70 -0
  193. package/_types/chains/zksync/constants/abis.d.ts.map +1 -0
  194. package/_types/chains/zksync/constants/address.d.ts +2 -0
  195. package/_types/chains/zksync/constants/address.d.ts.map +1 -0
  196. package/_types/chains/zksync/constants/contract.d.ts +3 -0
  197. package/_types/chains/zksync/constants/contract.d.ts.map +1 -0
  198. package/_types/chains/zksync/constants/number.d.ts +2 -0
  199. package/_types/chains/zksync/constants/number.d.ts.map +1 -0
  200. package/_types/chains/zksync/decorators/eip712.d.ts +32 -0
  201. package/_types/chains/zksync/decorators/eip712.d.ts.map +1 -1
  202. package/_types/chains/zksync/errors/bytecode.d.ts +30 -0
  203. package/_types/chains/zksync/errors/bytecode.d.ts.map +1 -0
  204. package/_types/chains/zksync/formatters.d.ts.map +1 -1
  205. package/_types/chains/zksync/index.d.ts +1 -0
  206. package/_types/chains/zksync/index.d.ts.map +1 -1
  207. package/_types/chains/zksync/types/contract.d.ts +2 -0
  208. package/_types/chains/zksync/types/contract.d.ts.map +1 -0
  209. package/_types/chains/zksync/utils/abi/encodeDeployData.d.ts +13 -0
  210. package/_types/chains/zksync/utils/abi/encodeDeployData.d.ts.map +1 -0
  211. package/_types/chains/zksync/utils/getEip712Domain.d.ts.map +1 -1
  212. package/_types/chains/zksync/utils/hashBytecode.d.ts +3 -0
  213. package/_types/chains/zksync/utils/hashBytecode.d.ts.map +1 -0
  214. package/_types/errors/version.d.ts +1 -1
  215. package/_types/errors/version.d.ts.map +1 -1
  216. package/chains/definitions/blast.ts +5 -1
  217. package/chains/definitions/blastSepolia.ts +2 -1
  218. package/chains/definitions/optimismSepolia.ts +5 -0
  219. package/chains/definitions/reyaNetwork.ts +20 -0
  220. package/chains/index.ts +1 -0
  221. package/chains/opStack/abis.ts +1071 -0
  222. package/chains/opStack/actions/buildProveWithdrawal.ts +7 -6
  223. package/chains/opStack/actions/getGame.ts +99 -0
  224. package/chains/opStack/actions/getGames.ts +133 -0
  225. package/chains/opStack/actions/getL2Output.ts +44 -3
  226. package/chains/opStack/actions/getPortalVersion.ts +87 -0
  227. package/chains/opStack/actions/getTimeToFinalize.ts +64 -27
  228. package/chains/opStack/actions/getTimeToNextGame.ts +143 -0
  229. package/chains/opStack/actions/getTimeToProve.ts +43 -5
  230. package/chains/opStack/actions/getWithdrawalStatus.ts +130 -38
  231. package/chains/opStack/actions/proveWithdrawal.ts +3 -1
  232. package/chains/opStack/actions/waitForNextGame.ts +122 -0
  233. package/chains/opStack/actions/waitForNextL2Output.ts +5 -1
  234. package/chains/opStack/actions/waitToProve.ts +65 -7
  235. package/chains/opStack/decorators/publicL1.ts +164 -0
  236. package/chains/opStack/errors/withdrawal.ts +10 -0
  237. package/chains/opStack/index.ts +30 -0
  238. package/chains/opStack/types/chain.ts +4 -1
  239. package/chains/opStack/types/withdrawal.ts +8 -0
  240. package/chains/zksync/actions/deployContract.ts +100 -0
  241. package/chains/zksync/constants/abis.ts +424 -0
  242. package/chains/zksync/constants/address.ts +2 -0
  243. package/chains/zksync/constants/contract.ts +2 -0
  244. package/chains/zksync/constants/number.ts +3 -0
  245. package/chains/zksync/decorators/eip712.ts +47 -0
  246. package/chains/zksync/errors/bytecode.ts +46 -0
  247. package/chains/zksync/formatters.ts +7 -1
  248. package/chains/zksync/index.ts +6 -0
  249. package/chains/zksync/types/contract.ts +6 -0
  250. package/chains/zksync/utils/abi/encodeDeployData.ts +120 -0
  251. package/chains/zksync/utils/getEip712Domain.ts +4 -2
  252. package/chains/zksync/utils/hashBytecode.ts +48 -0
  253. package/errors/version.ts +1 -1
  254. package/package.json +1 -1
@@ -21,7 +21,7 @@ import type {
21
21
  GetChainParameter,
22
22
  } from '../../../types/chain.js'
23
23
  import { type Hex } from '../../../types/misc.js'
24
- import type { Prettify } from '../../../types/utils.js'
24
+ import type { OneOf, Prettify } from '../../../types/utils.js'
25
25
  import { fromRlp } from '../../../utils/encoding/fromRlp.js'
26
26
  import { toRlp } from '../../../utils/encoding/toRlp.js'
27
27
  import { keccak256 } from '../../../utils/hash/keccak256.js'
@@ -31,6 +31,7 @@ import {
31
31
  type GetWithdrawalHashStorageSlotErrorType,
32
32
  getWithdrawalHashStorageSlot,
33
33
  } from '../utils/getWithdrawalHashStorageSlot.js'
34
+ import { type GetGameReturnType } from './getGame.js'
34
35
  import type { GetL2OutputReturnType } from './getL2Output.js'
35
36
  import type { ProveWithdrawalParameters } from './proveWithdrawal.js'
36
37
 
@@ -49,8 +50,7 @@ export type BuildProveWithdrawalParameters<
49
50
  > = GetAccountParameter<account, accountOverride, false> &
50
51
  GetChainParameter<chain, chainOverride> & {
51
52
  withdrawal: Withdrawal
52
- output: GetL2OutputReturnType
53
- }
53
+ } & OneOf<{ output: GetL2OutputReturnType } | { game: GetGameReturnType }>
54
54
 
55
55
  export type BuildProveWithdrawalReturnType<
56
56
  chain extends Chain | undefined = Chain | undefined,
@@ -116,9 +116,10 @@ export async function buildProveWithdrawal<
116
116
  ): Promise<
117
117
  BuildProveWithdrawalReturnType<chain, account, chainOverride, accountOverride>
118
118
  > {
119
- const { account, chain = client.chain, output, withdrawal } = args
119
+ const { account, chain = client.chain, game, output, withdrawal } = args
120
+
120
121
  const { withdrawalHash } = withdrawal
121
- const { l2BlockNumber } = output
122
+ const { l2BlockNumber } = game ?? output
122
123
 
123
124
  const slot = getWithdrawalHashStorageSlot({ withdrawalHash })
124
125
  const [proof, block] = await Promise.all([
@@ -134,7 +135,7 @@ export async function buildProveWithdrawal<
134
135
 
135
136
  return {
136
137
  account,
137
- l2OutputIndex: output.outputIndex,
138
+ l2OutputIndex: game?.index ?? output?.outputIndex,
138
139
  outputRootProof: {
139
140
  latestBlockhash: block.hash,
140
141
  messagePasserStorageRoot: proof.storageHash,
@@ -0,0 +1,99 @@
1
+ import type { Client } from '../../../clients/createClient.js'
2
+ import type { Transport } from '../../../clients/transports/createTransport.js'
3
+ import type { ErrorType } from '../../../errors/utils.js'
4
+ import type { Account } from '../../../types/account.js'
5
+ import type {
6
+ Chain,
7
+ DeriveChain,
8
+ GetChainParameter,
9
+ } from '../../../types/chain.js'
10
+ import {
11
+ GameNotFoundError,
12
+ type GameNotFoundErrorType,
13
+ } from '../errors/withdrawal.js'
14
+ import type { GetContractAddressParameter } from '../types/contract.js'
15
+ import type { Game } from '../types/withdrawal.js'
16
+ import { type GetGamesErrorType, getGames } from './getGames.js'
17
+
18
+ export type GetGameParameters<
19
+ chain extends Chain | undefined = Chain | undefined,
20
+ chainOverride extends Chain | undefined = Chain | undefined,
21
+ _derivedChain extends Chain | undefined = DeriveChain<chain, chainOverride>,
22
+ > = GetChainParameter<chain, chainOverride> &
23
+ GetContractAddressParameter<
24
+ _derivedChain,
25
+ 'portal' | 'disputeGameFactory'
26
+ > & {
27
+ /**
28
+ * The minimum block number of the dispute game.
29
+ */
30
+ l2BlockNumber: bigint
31
+ /**
32
+ * Limit of games to extract.
33
+ * @default 100
34
+ */
35
+ limit?: number | undefined
36
+ /**
37
+ * Strategy for extracting a dispute game.
38
+ *
39
+ * - `latest` - Returns the latest dispute game.
40
+ * - `random` - Returns a random dispute game.
41
+ */
42
+ strategy?: 'latest' | 'random'
43
+ }
44
+ export type GetGameReturnType = Game & {
45
+ l2BlockNumber: bigint
46
+ }
47
+
48
+ export type GetGameErrorType =
49
+ | GetGamesErrorType
50
+ | GameNotFoundErrorType
51
+ | ErrorType
52
+
53
+ /**
54
+ * Retrieves a valid dispute game on an L2 that occurred after a provided L2 block number.
55
+ *
56
+ * - Docs: https://viem.sh/op-stack/actions/getGame
57
+ *
58
+ * @param client - Client to use
59
+ * @param parameters - {@link GetGameParameters}
60
+ * @returns A valid dispute game. {@link GetGameReturnType}
61
+ *
62
+ * @example
63
+ * import { createPublicClient, http } from 'viem'
64
+ * import { mainnet, optimism } from 'viem/chains'
65
+ * import { getGame } from 'viem/op-stack'
66
+ *
67
+ * const publicClientL1 = createPublicClient({
68
+ * chain: mainnet,
69
+ * transport: http(),
70
+ * })
71
+ *
72
+ * const game = await getGame(publicClientL1, {
73
+ * l2BlockNumber: 69420n,
74
+ * targetChain: optimism
75
+ * })
76
+ */
77
+ export async function getGame<
78
+ chain extends Chain | undefined,
79
+ account extends Account | undefined,
80
+ chainOverride extends Chain | undefined = undefined,
81
+ >(
82
+ client: Client<Transport, chain, account>,
83
+ parameters: GetGameParameters<chain, chainOverride>,
84
+ ): Promise<GetGameReturnType> {
85
+ const { l2BlockNumber, strategy = 'latest' } = parameters
86
+
87
+ const latestGames = await getGames(client, parameters)
88
+
89
+ const games = latestGames.filter((game) => game.l2BlockNumber > l2BlockNumber)
90
+
91
+ const game = (() => {
92
+ if (strategy === 'random')
93
+ return games[Math.floor(Math.random() * games.length)]
94
+ return games[0]
95
+ })()
96
+
97
+ if (!game) throw new GameNotFoundError()
98
+ return game
99
+ }
@@ -0,0 +1,133 @@
1
+ import {
2
+ type ReadContractErrorType,
3
+ readContract,
4
+ } from '../../../actions/public/readContract.js'
5
+ import type { Client } from '../../../clients/createClient.js'
6
+ import type { Transport } from '../../../clients/transports/createTransport.js'
7
+ import type { ErrorType } from '../../../errors/utils.js'
8
+ import type { Account } from '../../../types/account.js'
9
+ import type {
10
+ Chain,
11
+ DeriveChain,
12
+ GetChainParameter,
13
+ } from '../../../types/chain.js'
14
+ import { decodeAbiParameters } from '../../../utils/abi/decodeAbiParameters.js'
15
+ import { disputeGameFactoryAbi, portal2Abi } from '../abis.js'
16
+ import type { GetContractAddressParameter } from '../types/contract.js'
17
+ import type { Game } from '../types/withdrawal.js'
18
+
19
+ export type GetGamesParameters<
20
+ chain extends Chain | undefined = Chain | undefined,
21
+ chainOverride extends Chain | undefined = Chain | undefined,
22
+ _derivedChain extends Chain | undefined = DeriveChain<chain, chainOverride>,
23
+ > = GetChainParameter<chain, chainOverride> &
24
+ GetContractAddressParameter<
25
+ _derivedChain,
26
+ 'portal' | 'disputeGameFactory'
27
+ > & {
28
+ /**
29
+ * Filter by minimum block number of the dispute games.
30
+ */
31
+ l2BlockNumber?: bigint | undefined
32
+ /**
33
+ * Limit of games to extract.
34
+ * @default 100
35
+ */
36
+ limit?: number | undefined
37
+ }
38
+ export type GetGamesReturnType = (Game & {
39
+ l2BlockNumber: bigint
40
+ })[]
41
+ export type GetGamesErrorType = ReadContractErrorType | ErrorType
42
+
43
+ /**
44
+ * Retrieves dispute games for an L2.
45
+ *
46
+ * - Docs: https://viem.sh/op-stack/actions/getGame
47
+ *
48
+ * @param client - Client to use
49
+ * @param parameters - {@link GetGameParameters}
50
+ * @returns Dispute games. {@link GetGameReturnType}
51
+ *
52
+ * @example
53
+ * import { createPublicClient, http } from 'viem'
54
+ * import { mainnet, optimism } from 'viem/chains'
55
+ * import { getGames } from 'viem/op-stack'
56
+ *
57
+ * const publicClientL1 = createPublicClient({
58
+ * chain: mainnet,
59
+ * transport: http(),
60
+ * })
61
+ *
62
+ * const games = await getGames(publicClientL1, {
63
+ * targetChain: optimism
64
+ * })
65
+ */
66
+ export async function getGames<
67
+ chain extends Chain | undefined,
68
+ account extends Account | undefined,
69
+ chainOverride extends Chain | undefined = undefined,
70
+ >(
71
+ client: Client<Transport, chain, account>,
72
+ parameters: GetGamesParameters<chain, chainOverride>,
73
+ ): Promise<GetGamesReturnType> {
74
+ const {
75
+ chain = client.chain,
76
+ l2BlockNumber,
77
+ limit = 100,
78
+ targetChain,
79
+ } = parameters
80
+
81
+ const portalAddress = (() => {
82
+ if (parameters.portalAddress) return parameters.portalAddress
83
+ if (chain) return targetChain!.contracts.portal[chain.id].address
84
+ return Object.values(targetChain!.contracts.portal)[0].address
85
+ })()
86
+
87
+ const disputeGameFactoryAddress = (() => {
88
+ if (parameters.disputeGameFactoryAddress)
89
+ return parameters.disputeGameFactoryAddress
90
+ if (chain)
91
+ return targetChain!.contracts.disputeGameFactory[chain.id].address
92
+ return Object.values(targetChain!.contracts.disputeGameFactory)[0].address
93
+ })()
94
+
95
+ const [gameCount, gameType] = await Promise.all([
96
+ readContract(client, {
97
+ abi: disputeGameFactoryAbi,
98
+ functionName: 'gameCount',
99
+ args: [],
100
+ address: disputeGameFactoryAddress,
101
+ }),
102
+ readContract(client, {
103
+ abi: portal2Abi,
104
+ functionName: 'respectedGameType',
105
+ address: portalAddress,
106
+ }),
107
+ ])
108
+
109
+ const games = (
110
+ (await readContract(client, {
111
+ abi: disputeGameFactoryAbi,
112
+ functionName: 'findLatestGames',
113
+ address: disputeGameFactoryAddress,
114
+ args: [
115
+ gameType,
116
+ BigInt(Math.max(0, Number(gameCount - 1n))),
117
+ BigInt(Math.min(limit, Number(gameCount))),
118
+ ],
119
+ })) as Game[]
120
+ )
121
+ .map((game) => {
122
+ const [blockNumber] = decodeAbiParameters(
123
+ [{ type: 'uint256' }],
124
+ game.extraData,
125
+ )
126
+ return !l2BlockNumber || blockNumber > l2BlockNumber
127
+ ? { ...game, l2BlockNumber: blockNumber }
128
+ : null
129
+ })
130
+ .filter(Boolean) as GetGamesReturnType
131
+
132
+ return games
133
+ }
@@ -12,15 +12,34 @@ import type {
12
12
  GetChainParameter,
13
13
  } from '../../../types/chain.js'
14
14
  import type { Hex } from '../../../types/misc.js'
15
+ import type { OneOf } from '../../../types/utils.js'
15
16
  import { l2OutputOracleAbi } from '../abis.js'
17
+ import type { TargetChain } from '../types/chain.js'
16
18
  import type { GetContractAddressParameter } from '../types/contract.js'
19
+ import { type GetGameParameters, getGame } from './getGame.js'
20
+ import {
21
+ type GetPortalVersionParameters,
22
+ getPortalVersion,
23
+ } from './getPortalVersion.js'
17
24
 
18
25
  export type GetL2OutputParameters<
19
26
  chain extends Chain | undefined = Chain | undefined,
20
27
  chainOverride extends Chain | undefined = Chain | undefined,
21
28
  _derivedChain extends Chain | undefined = DeriveChain<chain, chainOverride>,
22
29
  > = GetChainParameter<chain, chainOverride> &
23
- GetContractAddressParameter<_derivedChain, 'l2OutputOracle'> & {
30
+ OneOf<
31
+ | GetContractAddressParameter<_derivedChain, 'l2OutputOracle'>
32
+ | (GetContractAddressParameter<
33
+ _derivedChain,
34
+ 'portal' | 'disputeGameFactory'
35
+ > & {
36
+ /**
37
+ * Limit of games to extract.
38
+ * @default 100
39
+ */
40
+ limit?: number | undefined
41
+ })
42
+ > & {
24
43
  l2BlockNumber: bigint
25
44
  }
26
45
  export type GetL2OutputReturnType = {
@@ -65,11 +84,33 @@ export async function getL2Output<
65
84
  ): Promise<GetL2OutputReturnType> {
66
85
  const { chain = client.chain, l2BlockNumber, targetChain } = parameters
67
86
 
87
+ const version = await getPortalVersion(
88
+ client,
89
+ parameters as GetPortalVersionParameters,
90
+ )
91
+
92
+ if (version.major >= 3) {
93
+ const game = await getGame(client, parameters as GetGameParameters)
94
+ return {
95
+ l2BlockNumber: game.l2BlockNumber,
96
+ outputIndex: game.index,
97
+ outputRoot: game.rootClaim,
98
+ timestamp: game.timestamp,
99
+ }
100
+ }
101
+
68
102
  const l2OutputOracleAddress = (() => {
69
103
  if (parameters.l2OutputOracleAddress)
70
104
  return parameters.l2OutputOracleAddress
71
- if (chain) return targetChain!.contracts.l2OutputOracle[chain.id].address
72
- return Object.values(targetChain!.contracts.l2OutputOracle)[0].address
105
+ if (chain)
106
+ return (targetChain as unknown as TargetChain)!.contracts.l2OutputOracle[
107
+ chain.id
108
+ ].address
109
+ return (
110
+ Object.values(
111
+ (targetChain as unknown as TargetChain)!.contracts.l2OutputOracle,
112
+ ) as any
113
+ )[0].address
73
114
  })()
74
115
 
75
116
  const outputIndex = await readContract(client, {
@@ -0,0 +1,87 @@
1
+ import {
2
+ type ReadContractErrorType,
3
+ readContract,
4
+ } from '../../../actions/public/readContract.js'
5
+ import type { Client } from '../../../clients/createClient.js'
6
+ import type { Transport } from '../../../clients/transports/createTransport.js'
7
+ import type { ErrorType } from '../../../errors/utils.js'
8
+ import type {
9
+ Chain,
10
+ DeriveChain,
11
+ GetChainParameter,
12
+ } from '../../../types/chain.js'
13
+ import { withCache } from '../../../utils/promise/withCache.js'
14
+ import { portal2Abi } from '../abis.js'
15
+ import type { GetContractAddressParameter } from '../types/contract.js'
16
+
17
+ export type GetPortalVersionParameters<
18
+ chain extends Chain | undefined = Chain | undefined,
19
+ chainOverride extends Chain | undefined = Chain | undefined,
20
+ _derivedChain extends Chain | undefined = DeriveChain<chain, chainOverride>,
21
+ > = GetChainParameter<chain, chainOverride> &
22
+ GetContractAddressParameter<_derivedChain, 'portal'>
23
+
24
+ export type GetPortalVersionReturnType = {
25
+ major: number
26
+ minor: number
27
+ patch: number
28
+ }
29
+
30
+ export type GetPortalVersionErrorType = ReadContractErrorType | ErrorType
31
+
32
+ /**
33
+ * Retrieves the current version of the Portal contract.
34
+ *
35
+ * - Docs: https://viem.sh/op-stack/actions/getPortalVersion
36
+ *
37
+ * @param client - Client to use
38
+ * @param parameters - {@link GetPortalVersionParameters}
39
+ * @returns The version object.
40
+ *
41
+ * @example
42
+ * import { createPublicClient, http } from 'viem'
43
+ * import { mainnet } from 'viem/chains'
44
+ * import { getPortalVersion } from 'viem/op-stack'
45
+ *
46
+ * const publicClientL1 = createPublicClient({
47
+ * chain: mainnet,
48
+ * transport: http(),
49
+ * })
50
+ *
51
+ * const version = await getPortalVersion(publicClientL1, {
52
+ * targetChain: optimism,
53
+ * })
54
+ *
55
+ * if (version.major > 3)
56
+ * console.log('Fault proofs are enabled on this version of optimism')
57
+ * console.log('Fault proofs are not enabled on this version of optimism')
58
+ */
59
+ export async function getPortalVersion<
60
+ chain extends Chain | undefined,
61
+ chainOverride extends Chain | undefined = undefined,
62
+ >(
63
+ client: Client<Transport, chain>,
64
+ parameters: GetPortalVersionParameters<chain, chainOverride>,
65
+ ) {
66
+ const { chain = client.chain, targetChain } = parameters
67
+
68
+ const portalAddress = (() => {
69
+ if (parameters.portalAddress) return parameters.portalAddress
70
+ if (chain) return targetChain!.contracts.portal[chain.id].address
71
+ return Object.values(targetChain!.contracts.portal)[0].address
72
+ })()
73
+
74
+ const version = await withCache(
75
+ () =>
76
+ readContract(client, {
77
+ abi: portal2Abi,
78
+ address: portalAddress,
79
+ functionName: 'version',
80
+ }),
81
+ { cacheKey: ['portalVersion', portalAddress].join('.'), cacheTime: 300 },
82
+ )
83
+
84
+ const [major, minor, patch] = version.split('.').map(Number)
85
+
86
+ return { major, minor, patch }
87
+ }
@@ -2,8 +2,10 @@ import {
2
2
  type MulticallErrorType,
3
3
  multicall,
4
4
  } from '../../../actions/public/multicall.js'
5
+ import { readContract } from '../../../actions/public/readContract.js'
5
6
  import type { Client } from '../../../clients/createClient.js'
6
7
  import type { Transport } from '../../../clients/transports/createTransport.js'
8
+ import { BaseError } from '../../../errors/base.js'
7
9
  import type { ErrorType } from '../../../errors/utils.js'
8
10
  import type { Account } from '../../../types/account.js'
9
11
  import type {
@@ -12,8 +14,9 @@ import type {
12
14
  GetChainParameter,
13
15
  } from '../../../types/chain.js'
14
16
  import type { Hash } from '../../../types/misc.js'
15
- import { l2OutputOracleAbi, portalAbi } from '../abis.js'
17
+ import { l2OutputOracleAbi, portal2Abi, portalAbi } from '../abis.js'
16
18
  import type { GetContractAddressParameter } from '../types/contract.js'
19
+ import { getPortalVersion } from './getPortalVersion.js'
17
20
 
18
21
  export type GetTimeToFinalizeParameters<
19
22
  chain extends Chain | undefined = Chain | undefined,
@@ -80,43 +83,77 @@ export async function getTimeToFinalize<
80
83
  ): Promise<GetTimeToFinalizeReturnType> {
81
84
  const { chain = client.chain, withdrawalHash, targetChain } = parameters
82
85
 
83
- const l2OutputOracleAddress = (() => {
84
- if (parameters.l2OutputOracleAddress)
85
- return parameters.l2OutputOracleAddress
86
- if (chain) return targetChain!.contracts.l2OutputOracle[chain.id].address
87
- return Object.values(targetChain!.contracts.l2OutputOracle)[0].address
88
- })()
89
86
  const portalAddress = (() => {
90
87
  if (parameters.portalAddress) return parameters.portalAddress
91
88
  if (chain) return targetChain!.contracts.portal[chain.id].address
92
89
  return Object.values(targetChain!.contracts.portal)[0].address
93
90
  })()
94
91
 
95
- const [[_outputRoot, proveTimestamp, _l2OutputIndex], period] =
96
- await multicall(client, {
97
- allowFailure: false,
98
- contracts: [
99
- {
100
- abi: portalAbi,
101
- address: portalAddress,
102
- functionName: 'provenWithdrawals',
103
- args: [withdrawalHash],
104
- },
105
- {
106
- abi: l2OutputOracleAbi,
107
- address: l2OutputOracleAddress,
108
- functionName: 'FINALIZATION_PERIOD_SECONDS',
109
- },
110
- ],
111
- })
92
+ const portalVersion = await getPortalVersion(client, { portalAddress })
93
+
94
+ // Legacy
95
+ if (portalVersion.major < 3) {
96
+ const l2OutputOracleAddress = (() => {
97
+ if (parameters.l2OutputOracleAddress)
98
+ return parameters.l2OutputOracleAddress
99
+ if (chain) return targetChain!.contracts.l2OutputOracle[chain.id].address
100
+ return Object.values(targetChain!.contracts.l2OutputOracle)[0].address
101
+ })()
102
+ const [[_outputRoot, proveTimestamp, _l2OutputIndex], period] =
103
+ await multicall(client, {
104
+ allowFailure: false,
105
+ contracts: [
106
+ {
107
+ abi: portalAbi,
108
+ address: portalAddress,
109
+ functionName: 'provenWithdrawals',
110
+ args: [withdrawalHash],
111
+ },
112
+ {
113
+ abi: l2OutputOracleAbi,
114
+ address: l2OutputOracleAddress,
115
+ functionName: 'FINALIZATION_PERIOD_SECONDS',
116
+ },
117
+ ],
118
+ })
119
+
120
+ const secondsSinceProven = Date.now() / 1000 - Number(proveTimestamp)
121
+ const secondsToFinalize = Number(period) - secondsSinceProven
122
+
123
+ const seconds = Math.floor(
124
+ secondsToFinalize < 0 ? 0 : secondsToFinalize + buffer,
125
+ )
126
+ const timestamp = Date.now() + seconds * 1000
127
+
128
+ return { period: Number(period), seconds, timestamp }
129
+ }
130
+
131
+ const [[_disputeGameProxy, proveTimestamp], proofMaturityDelaySeconds] =
132
+ await Promise.all([
133
+ readContract(client, {
134
+ abi: portal2Abi,
135
+ address: portalAddress,
136
+ functionName: 'provenWithdrawals',
137
+ args: [withdrawalHash],
138
+ }),
139
+ readContract(client, {
140
+ abi: portal2Abi,
141
+ address: portalAddress,
142
+ functionName: 'proofMaturityDelaySeconds',
143
+ }),
144
+ ])
145
+
146
+ if (proveTimestamp === 0n)
147
+ throw new BaseError('Withdrawal has not been proven on L1.')
112
148
 
113
149
  const secondsSinceProven = Date.now() / 1000 - Number(proveTimestamp)
114
- const secondsToFinalize = Number(period) - secondsSinceProven
150
+ const secondsToFinalize =
151
+ Number(proofMaturityDelaySeconds) - secondsSinceProven
115
152
 
116
153
  const seconds = Math.floor(
117
- secondsToFinalize < 0 ? 0 : secondsToFinalize + buffer,
154
+ secondsToFinalize < 0n ? 0 : secondsToFinalize + buffer,
118
155
  )
119
156
  const timestamp = Date.now() + seconds * 1000
120
157
 
121
- return { period: Number(period), seconds, timestamp }
158
+ return { period: Number(proofMaturityDelaySeconds), seconds, timestamp }
122
159
  }