forked from Uniswap/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunkArray.ts
More file actions
32 lines (27 loc) · 1.28 KB
/
Copy pathchunkArray.ts
File metadata and controls
32 lines (27 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const CONSERVATIVE_BLOCK_GAS_LIMIT = 10_000_000 // conservative, hard-coded estimate of the current block gas limit
export const DEFAULT_GAS_REQUIRED = 200_000 // the default value for calls that don't specify gasRequired
// chunks array into chunks
// evenly distributes items among the chunks
export default function chunkArray<T>(items: T[], gasLimit = CONSERVATIVE_BLOCK_GAS_LIMIT * 10): T[][] {
const chunks: T[][] = []
let currentChunk: T[] = []
let currentChunkCumulativeGas = 0
for (let i = 0; i < items.length; i++) {
const item = items[i]
// calculate the gas required by the current item
const gasRequired = (item as { gasRequired?: number })?.gasRequired ?? DEFAULT_GAS_REQUIRED
// if the current chunk is empty, or the current item wouldn't push it over the gas limit,
// append the current item and increment the cumulative gas
if (currentChunk.length === 0 || currentChunkCumulativeGas + gasRequired < gasLimit) {
currentChunk.push(item)
currentChunkCumulativeGas += gasRequired
} else {
// otherwise, push the current chunk and create a new chunk
chunks.push(currentChunk)
currentChunk = [item]
currentChunkCumulativeGas = gasRequired
}
}
if (currentChunk.length > 0) chunks.push(currentChunk)
return chunks
}