@@ -62,22 +62,36 @@ function getValidatorsFromFunctionData<
6262 return args ;
6363}
6464
65- function updateAccumulator ( acc : Set < Address > , input : Hex ) {
66- const [ validators , states ] = getValidatorsFromFunctionData ( {
67- abi : [ setValidatorV3Dot1ABI ] ,
68- data : input ,
69- } ) ;
65+ function iterateThroughValidatorsList (
66+ acc : Set < Address > ,
67+ validators : Readonly < Address [ ] > | undefined ,
68+ enabled : Readonly < boolean [ ] > | undefined ,
69+ ) {
70+ if ( typeof validators === 'undefined' || typeof enabled === 'undefined' ) {
71+ return acc ;
72+ }
73+
74+ const copy = new Set < Address > ( acc ) ;
7075
7176 validators . forEach ( ( validator , i ) => {
72- const isAdd = states [ i ] ;
77+ const isAdd = enabled [ i ] ;
7378 if ( isAdd ) {
74- acc . add ( validator ) ;
79+ copy . add ( validator ) ;
7580 } else {
76- acc . delete ( validator ) ;
81+ copy . delete ( validator ) ;
7782 }
7883 } ) ;
7984
80- return acc ;
85+ return copy ;
86+ }
87+
88+ function updateAccumulator ( acc : Set < Address > , input : Hex ) {
89+ const [ validators , enabled ] = getValidatorsFromFunctionData ( {
90+ abi : [ setValidatorV3Dot1ABI ] ,
91+ data : input ,
92+ } ) ;
93+
94+ return iterateThroughValidatorsList ( acc , validators , enabled ) ;
8195}
8296
8397export type GetValidatorsParams = {
@@ -150,36 +164,16 @@ export async function getValidators<TChain extends Chain>(
150164 . filter ( ( event ) => event . eventName === 'ValidatorsSet' )
151165 . reduce ( ( acc , event ) => {
152166 const { validators : _validators , enabled : _enabled } = event . args ;
153-
154- if ( typeof _validators === 'undefined' || typeof _enabled === 'undefined' ) {
155- return acc ;
156- }
157-
158- const copy = new Set < Address > ( acc ) ;
159-
160- for ( let i = 0 ; i < _validators . length ; i ++ ) {
161- if ( _enabled [ i ] ) {
162- copy . add ( _validators [ i ] ) ;
163- } else {
164- copy . delete ( _validators [ i ] ) ;
165- }
166- }
167-
168- return copy ;
167+ return iterateThroughValidatorsList ( acc , _validators , _enabled ) ;
169168 } , new Set < Address > ( ) ) ;
170169
171- const txs = await Promise . all ( [
172- ...preV3Dot1Events . map ( ( event ) =>
173- publicClient . getTransaction ( {
174- hash : event . transactionHash ,
175- } ) ,
176- ) ,
177- ...v3Dot1ValidatorsSetEvents . map ( ( event ) =>
170+ const txs = await Promise . all (
171+ preV3Dot1Events . map ( ( event ) =>
178172 publicClient . getTransaction ( {
179173 hash : event . transactionHash ,
180174 } ) ,
181175 ) ,
182- ] ) ;
176+ ) ;
183177
184178 let isAccurate = true ;
185179 const validators = txs . reduce ( ( acc , tx ) => {
@@ -253,7 +247,7 @@ export async function getValidators<TChain extends Chain>(
253247 return acc ;
254248 }
255249 }
256- } , new Set < Address > ( ) ) ;
250+ } , validatorsFromV3Dot1Events ) ;
257251
258252 return {
259253 isAccurate,
0 commit comments