1
+ import { GroupRunner } from '@chainlink/external-adapter-framework/util/group-runner'
1
2
import { ethers } from 'ethers'
2
3
import { PoRAddress } from '@chainlink/external-adapter-framework/adapter/por'
3
4
import { makeLogger } from '@chainlink/external-adapter-framework/util'
@@ -22,31 +23,26 @@ export abstract class AddressManager<T> {
22
23
batchGroupSize = 10 ,
23
24
) : Promise < T [ ] > {
24
25
const blockTag = latestBlockNum - confirmations
25
- const numAddresses = await this . contract . getPoRAddressListLength ( { blockTag } )
26
- let totalRequestedAddressesCount = 0
27
- let startIdx = ethers . BigNumber . from ( 0 )
28
- const addresses : T [ ] = [ ]
29
- let batchRequests : Promise < T > [ ] = [ ]
26
+ const numAddresses = ( await this . contract . getPoRAddressListLength ( { blockTag } ) ) . toNumber ( )
27
+ const batchRequests : Promise < T > [ ] = [ ]
30
28
31
- while ( totalRequestedAddressesCount < numAddresses . toNumber ( ) ) {
32
- const nextEndIdx = startIdx . add ( batchSize - 1 )
33
- const endIdx = nextEndIdx . gte ( numAddresses ) ? numAddresses . sub ( 1 ) : nextEndIdx
34
- const batchCall = this . getPoRAddressListCall ( startIdx , endIdx , blockTag )
35
- batchRequests . push ( batchCall )
36
- // element at endIdx is included in result
37
- const addressesRequested : number = endIdx . sub ( startIdx ) . add ( 1 ) . toNumber ( )
38
- totalRequestedAddressesCount += addressesRequested
39
- startIdx = endIdx . add ( 1 )
29
+ const runner = new GroupRunner ( batchGroupSize )
30
+ const getPoRAddressListCall = runner . wrapFunction ( this . getPoRAddressListCall . bind ( this ) )
40
31
41
- if (
42
- batchRequests . length >= batchGroupSize ||
43
- totalRequestedAddressesCount >= numAddresses . toNumber ( )
44
- ) {
45
- addresses . push ( ...( await Promise . all ( batchRequests ) ) )
46
- batchRequests = [ ]
47
- }
32
+ for ( let startIndex = 0 ; startIndex < numAddresses ; startIndex += batchSize ) {
33
+ const endIndex = Math . min ( startIndex + batchSize , numAddresses )
34
+ batchRequests . push (
35
+ getPoRAddressListCall (
36
+ ethers . BigNumber . from ( startIndex ) ,
37
+ // Subtract 1 because endIndex is inclusive
38
+ ethers . BigNumber . from ( endIndex - 1 ) ,
39
+ blockTag ,
40
+ ) ,
41
+ )
48
42
}
49
43
44
+ const addresses = await Promise . all ( batchRequests )
45
+
50
46
if ( addresses . length == 0 ) {
51
47
logger . error ( 'Received empty PoRAddressList' )
52
48
}
0 commit comments