@@ -6,7 +6,13 @@ import {
6
6
chicmozL1L2ValidatorSchema ,
7
7
getL1NetworkId ,
8
8
} from "@chicmoz-pkg/types" ;
9
- import { PublicClient , createPublicClient , defineChain , webSocket } from "viem" ;
9
+ import {
10
+ PublicClient ,
11
+ createPublicClient ,
12
+ defineChain ,
13
+ http ,
14
+ webSocket ,
15
+ } from "viem" ;
10
16
import { foundry , mainnet , sepolia } from "viem/chains" ;
11
17
import {
12
18
ETHEREUM_HTTP_RPC_URL ,
@@ -18,11 +24,21 @@ import { logger } from "../logger.js";
18
24
import { getL1Contracts } from "./contracts/index.js" ;
19
25
export { startContractWatchers as watchContractsEvents } from "./contracts/index.js" ;
20
26
21
- let publicClient : PublicClient | undefined = undefined ;
27
+ let publicWsClient : PublicClient | undefined = undefined ;
28
+ let publicHttpClient : PublicClient | undefined = undefined ;
22
29
23
- export const getPublicClient = ( ) => {
24
- if ( ! publicClient ) throw new Error ( "Client not initialized" ) ;
25
- return publicClient ;
30
+ export const getPublicWsClient = ( ) => {
31
+ if ( ! publicWsClient ) {
32
+ throw new Error ( "Client not initialized" ) ;
33
+ }
34
+ return publicWsClient ;
35
+ } ;
36
+
37
+ export const getPublicHttpClient = ( ) => {
38
+ if ( ! publicHttpClient ) {
39
+ throw new Error ( "Client not initialized" ) ;
40
+ }
41
+ return publicHttpClient ;
26
42
} ;
27
43
28
44
export const initClient = ( ) => {
@@ -46,18 +62,19 @@ export const initClient = () => {
46
62
} ,
47
63
} ,
48
64
} ) ;
49
- publicClient = createPublicClient ( {
65
+ publicWsClient = createPublicClient ( {
50
66
chain,
51
67
transport : webSocket ( ) ,
52
68
} ) ;
69
+ publicHttpClient = createPublicClient ( { chain, transport : http ( ) } ) ;
53
70
} ;
54
71
55
72
export const getLatestHeight = async ( ) => {
56
- return await getPublicClient ( ) . getBlockNumber ( ) ;
73
+ return await getPublicHttpClient ( ) . getBlockNumber ( ) ;
57
74
} ;
58
75
59
76
export const getBlock = async ( blockNumber : number ) => {
60
- return await getPublicClient ( ) . getBlock ( {
77
+ return await getPublicHttpClient ( ) . getBlock ( {
61
78
blockNumber : BigInt ( blockNumber ) ,
62
79
} ) ;
63
80
} ;
@@ -67,21 +84,23 @@ const json = (param: unknown): string => {
67
84
param ,
68
85
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
69
86
( _key , value ) => ( typeof value === "bigint" ? value . toString ( ) : value ) ,
70
- 2
87
+ 2 ,
71
88
) ;
72
89
} ;
73
90
74
91
export const emitRandomizedChangeWithinRandomizedTime = async (
75
92
depth : number ,
76
- oldValues : ChicmozL1L2Validator
93
+ oldValues : ChicmozL1L2Validator ,
77
94
) => {
78
- if ( depth === 0 ) return ;
95
+ if ( depth === 0 ) {
96
+ return ;
97
+ }
79
98
const rand = Math . random ( ) ;
80
99
const sleepTime = 30000 ;
81
100
logger . info (
82
101
`ATTESTER ${ oldValues . attester } - DEPTH ${ depth } - SLEEP ${
83
102
sleepTime / 1000
84
- } s`
103
+ } s`,
85
104
) ;
86
105
await new Promise ( ( resolve ) => setTimeout ( resolve , sleepTime ) ) ;
87
106
let newValues = oldValues ;
@@ -129,8 +148,10 @@ export const emitRandomizedChangeWithinRandomizedTime = async (
129
148
export const queryStakingStateAndEmitUpdates = async ( ) => {
130
149
// TODO: this entire function should be replaced with a watch on the contract (and some initial state query)
131
150
const l1Contracts = await getL1Contracts ( ) ;
132
- if ( ! l1Contracts ) throw new Error ( "Contracts not initialized" ) ;
133
- const attesterCount = await getPublicClient ( ) . readContract ( {
151
+ if ( ! l1Contracts ) {
152
+ throw new Error ( "Contracts not initialized" ) ;
153
+ }
154
+ const attesterCount = await getPublicHttpClient ( ) . readContract ( {
134
155
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
135
156
address : l1Contracts . rollup . address as `0x${string } `,
136
157
abi : RollupAbi ,
@@ -139,14 +160,14 @@ export const queryStakingStateAndEmitUpdates = async () => {
139
160
logger . info ( `Active attester count: ${ attesterCount . toString ( ) } ` ) ;
140
161
if ( attesterCount > 0 ) {
141
162
for ( let i = 0 ; i < attesterCount ; i ++ ) {
142
- const attester = await getPublicClient ( ) . readContract ( {
163
+ const attester = await getPublicHttpClient ( ) . readContract ( {
143
164
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
144
165
address : l1Contracts . rollup . address as `0x${string } `,
145
166
abi : RollupAbi ,
146
167
functionName : "getAttesterAtIndex" ,
147
168
args : [ BigInt ( i ) ] ,
148
169
} ) ;
149
- const attesterInfo = await getPublicClient ( ) . readContract ( {
170
+ const attesterInfo = await getPublicHttpClient ( ) . readContract ( {
150
171
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
151
172
address : l1Contracts . rollup . address as `0x${string } `,
152
173
abi : RollupAbi ,
@@ -158,7 +179,7 @@ export const queryStakingStateAndEmitUpdates = async () => {
158
179
chicmozL1L2ValidatorSchema . parse ( {
159
180
...attesterInfo ,
160
181
attester,
161
- } )
182
+ } ) ,
162
183
) ;
163
184
}
164
185
} else {
@@ -186,16 +207,16 @@ export const queryStakingStateAndEmitUpdates = async () => {
186
207
chicmozL1L2ValidatorSchema . parse ( {
187
208
...attesterInfo ,
188
209
attester : attesterInfo . attester ,
189
- } )
210
+ } ) ,
190
211
) ;
191
212
}
192
213
for ( const attesterInfo of mockedAttesters ) {
193
214
await emitRandomizedChangeWithinRandomizedTime (
194
215
100 ,
195
- chicmozL1L2ValidatorSchema . parse ( attesterInfo )
216
+ chicmozL1L2ValidatorSchema . parse ( attesterInfo ) ,
196
217
) . catch ( ( e ) => {
197
218
logger . error (
198
- `Randomized change emission failed: ${ ( e as Error ) . stack } `
219
+ `Randomized change emission failed: ${ ( e as Error ) . stack } ` ,
199
220
) ;
200
221
} ) ;
201
222
}
0 commit comments