Skip to content

Commit 95b26c9

Browse files
authored
Use GroupRunner in por-address-list (#3792)
1 parent bee315b commit 95b26c9

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

.changeset/fresh-turkeys-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/por-address-list-adapter': patch
3+
---
4+
5+
Refactor how RPCs are grouped

packages/sources/por-address-list/src/transport/addressManager.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GroupRunner } from '@chainlink/external-adapter-framework/util/group-runner'
12
import { ethers } from 'ethers'
23
import { PoRAddress } from '@chainlink/external-adapter-framework/adapter/por'
34
import { makeLogger } from '@chainlink/external-adapter-framework/util'
@@ -22,31 +23,26 @@ export abstract class AddressManager<T> {
2223
batchGroupSize = 10,
2324
): Promise<T[]> {
2425
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>[] = []
3028

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))
4031

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+
)
4842
}
4943

44+
const addresses = await Promise.all(batchRequests)
45+
5046
if (addresses.length == 0) {
5147
logger.error('Received empty PoRAddressList')
5248
}

0 commit comments

Comments
 (0)