Skip to content

Commit bee315b

Browse files
authored
Use GroupRunner in polkadot-balance (#3791)
1 parent 9fa3351 commit bee315b

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

.changeset/stale-feet-whisper.md

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

packages/sources/polkadot-balance/src/transport/balance.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1+
import { GroupRunner } from '@chainlink/external-adapter-framework/util/group-runner'
12
import { EndpointContext } from '@chainlink/external-adapter-framework/adapter'
23
import { BaseEndpointTypes, inputParameters } from '../endpoint/balance'
34
import { TransportDependencies } from '@chainlink/external-adapter-framework/transports'
45
import { SubscriptionTransport } from '@chainlink/external-adapter-framework/transports/abstract/subscription'
5-
import {
6-
AdapterResponse,
7-
makeLogger,
8-
sleep,
9-
splitArrayIntoChunks,
10-
} from '@chainlink/external-adapter-framework/util'
6+
import { AdapterResponse, makeLogger, sleep } from '@chainlink/external-adapter-framework/util'
117
import { ApiPromise, WsProvider } from '@polkadot/api'
128

139
const logger = makeLogger('PolkadotBalanceLogger')
@@ -78,23 +74,23 @@ export class BalanceTransport extends SubscriptionTransport<BalanceTransportType
7874
try {
7975
// Break addresses down into batches to execute asynchronously
8076
// Firing requests for all addresses all at once could hit rate limiting for large address pools
81-
const batchedAddresses = splitArrayIntoChunks(addresses, this.config.BATCH_SIZE)
82-
for (const batch of batchedAddresses) {
83-
await Promise.all(
84-
batch.map((address) => {
85-
const balancePromise = this.api.query.system.account(address).then((codec) => {
86-
const balance = codec.toJSON() as unknown as ProviderResponse
87-
if (balance) {
88-
result.push({
89-
address,
90-
balance: parseInt(balance.data?.free || '0x0', 16).toString(),
91-
})
92-
}
93-
})
94-
return balancePromise
95-
}),
96-
)
97-
}
77+
const runner = new GroupRunner(this.config.BATCH_SIZE)
78+
const queryAccount = runner.wrapFunction((address) => this.api.query.system.account(address))
79+
80+
await Promise.all(
81+
addresses.map((address) => {
82+
const balancePromise = queryAccount(address).then((codec) => {
83+
const balance = codec.toJSON() as unknown as ProviderResponse
84+
if (balance) {
85+
result.push({
86+
address,
87+
balance: parseInt(balance.data?.free || '0x0', 16).toString(),
88+
})
89+
}
90+
})
91+
return balancePromise
92+
}),
93+
)
9894
} catch (e) {
9995
logger.error(e, 'Failed to retrieve balances')
10096
return {

0 commit comments

Comments
 (0)