Skip to content

Commit

Permalink
improvement: chunk block service requests (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
namgold authored Apr 25, 2023
1 parent a438424 commit 892bf60
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const chunk = <T>(arr: readonly T[], chunkSize: number): T[][] => {
const result = []
for (let i = 0; i < arr.length; i += chunkSize) {
result.push(arr.slice(i, i + chunkSize))
}

return result
}
18 changes: 14 additions & 4 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { timeframeOptions } from '../constants'
import { BLOCK_SERVICE_API } from '../constants/env'
import Numeral from 'numeral'
import { OverflowTooltip } from '../components/Tooltip'
import { chunk } from './array'

// format libraries
const Decimal = toFormat(_Decimal)
Expand Down Expand Up @@ -195,10 +196,19 @@ async function getBlockFromTimestampSubgraph(timestamp, networkInfo) {

async function getBlocksFromTimestampsBlockService(timestamps, networkInfo) {
const result = (
await (
await fetch(`${BLOCK_SERVICE_API}/${networkInfo.blockServiceRoute}/api/v1/block?timestamps=${timestamps.join(',')}`)
).json()
).data.map(block => ({ timestamp: String(block.timestamp), number: String(block.number) }))
await Promise.all(
chunk(timestamps, 50).map(async timestampsChunk =>
(
await fetch(
`${BLOCK_SERVICE_API}/${networkInfo.blockServiceRoute}/api/v1/block?timestamps=${timestampsChunk.join(',')}`
)
).json()
)
)
)
.map(chunk => chunk.data)
.flat()
.map(block => ({ timestamp: String(block.timestamp), number: String(block.number) }))
return result
}

Expand Down

0 comments on commit 892bf60

Please sign in to comment.