Skip to content

Commit

Permalink
Fix/DF-20791 minconfirmations (#3581)
Browse files Browse the repository at this point in the history
* DF-20791 do minConfirmations per provider

* only request blocknumber once per provider
  • Loading branch information
mmcallister-cll authored Nov 22, 2024
1 parent 07e73c0 commit e6e5825
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-plums-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/eth-balance-adapter': minor
---

Fix minConfirmations only using default provider
4 changes: 2 additions & 2 deletions packages/sources/eth-balance/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const constructChainIdProviderMap = (): Map<string, ethers.providers.Provider> =

const provider = new ethers.providers.JsonRpcProvider(rpcUrl, Number(chainId))
chainIdToProviderMap.set(chainId, provider)
Logger.info(`created provider for network: ${networkName}, chain ID: ${chainId}`)
Logger.debug(`created provider for network: ${networkName}, chain ID: ${chainId}`)
}
return chainIdToProviderMap
}
Expand All @@ -65,7 +65,7 @@ export const makeConfig = (prefix?: string): Config => {

const chainIdToProviderMap = constructChainIdProviderMap()

Logger.info('creating new Json RPC Provider')
Logger.debug('creating new Json RPC Provider')
return {
...Requester.getDefaultConfig(prefix),
defaultEndpoint: DEFAULT_ENDPOINT,
Expand Down
38 changes: 23 additions & 15 deletions packages/sources/eth-balance/src/endpoint/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export const execute: ExecuteWithConfig<Config> = async (request, _, config) =>
})
}

// The limitation of 64 is to make it work with both full and light/fast sync nodes
if (!Number.isInteger(minConfirmations) || minConfirmations < 0 || minConfirmations > 64) {
throw new AdapterInputError({
jobRunID,
message: `Min confirmations must be an integer between 0 and 64`,
statusCode: 400,
})
}

const providerSet = new Set<ethers.providers.Provider>()
const addressProviders = []
for (const address of addresses) {
let provider
Expand All @@ -87,30 +97,28 @@ export const execute: ExecuteWithConfig<Config> = async (request, _, config) =>
statusCode: 400,
})
}
providerSet.add(provider)
addressProviders.push({ address: address.address, provider })
}

// The limitation of 64 is to make it work with both full and light/fast sync nodes
if (!Number.isInteger(minConfirmations) || minConfirmations < 0 || minConfirmations > 64) {
throw new AdapterInputError({
jobRunID,
message: `Min confirmations must be an integer between 0 and 64`,
statusCode: 400,
})
}

let targetBlockTag: string | number = 'latest'
const providerBlockTags = new Map<ethers.providers.Provider, number | string>()
if (minConfirmations !== 0) {
const lastBlockNumber = await config.provider.getBlockNumber()
targetBlockTag = lastBlockNumber - minConfirmations
const providerBlockTagRequests = Array.from(providerSet).map((provider) =>
provider.getBlockNumber().then((result) => {
const targetBlockTag = result - minConfirmations
providerBlockTags.set(provider, targetBlockTag)
}),
)
await Promise.all(providerBlockTagRequests)
}

let balances
try {
balances = await Promise.all(
addressProviders.map((address) =>
getBalance(address.address, targetBlockTag, address.provider),
),
addressProviders.map((address) => {
const targetBlockTag = providerBlockTags.get(address.provider) || 'latest'
return getBalance(address.address, targetBlockTag, address.provider)
}),
)
} catch (e: any) {
throw new AdapterDataProviderError({
Expand Down

0 comments on commit e6e5825

Please sign in to comment.