Skip to content

Commit b607cde

Browse files
authored
Use GroupRunner in dlc-btc-por (#3789)
1 parent ef5fdd1 commit b607cde

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

.changeset/wild-hotels-protect.md

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

packages/sources/dlc-btc-por/src/transport/proof-of-reserves.ts

Lines changed: 11 additions & 16 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 { TransportDependencies } from '@chainlink/external-adapter-framework/transports'
23
import { AdapterResponse, makeLogger, sleep } from '@chainlink/external-adapter-framework/util'
34
import { SubscriptionTransport } from '@chainlink/external-adapter-framework/transports/abstract/subscription'
@@ -95,24 +96,18 @@ export class DLCBTCPorTransport extends SubscriptionTransport<TransportTypes> {
9596
getBitcoinNetwork(this.settings.BITCOIN_NETWORK),
9697
)
9798

98-
let totalPoR = 0
9999
const concurrencyGroupSize = this.settings.BITCOIN_RPC_GROUP_SIZE || vaultData.length
100+
100101
// Process vault batches sequentially to not overload the BITCOIN_RPC server
101-
for (let i = 0; i < vaultData.length; i += concurrencyGroupSize) {
102-
let group = []
103-
if (this.settings.BITCOIN_RPC_GROUP_SIZE > 0) {
104-
group = vaultData.slice(i, i + concurrencyGroupSize)
105-
} else {
106-
group = vaultData
107-
}
108-
const deposits = await Promise.all(
109-
group.map(async (vault) => {
110-
return await this.verifyVaultDeposit(vault, attestorPublicKey)
111-
}),
112-
)
113-
// totalPoR represents total proof of reserves value in bitcoins
114-
totalPoR += deposits.reduce((sum, deposit) => sum + deposit, 0)
115-
}
102+
const runner = new GroupRunner(concurrencyGroupSize)
103+
const getVaultDeposit = runner.wrapFunction((vault: RawVault) =>
104+
this.verifyVaultDeposit(vault, attestorPublicKey),
105+
)
106+
107+
const deposits = await Promise.all(vaultData.map(getVaultDeposit))
108+
109+
// totalPoR represents total proof of reserves value in bitcoins
110+
const totalPoR = deposits.reduce((sum, deposit) => sum + deposit, 0)
116111

117112
// multiply by 10^8 to convert to satoshis
118113
const result = totalPoR * 10 ** 8

0 commit comments

Comments
 (0)