FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
interface/src/utils/chunkArray.ts at main · Jchicode/interface · GitHub
Jchicode
/
interface
Public
forked from
Uniswap/interface
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
interface
/
src
/
utils
/
chunkArray.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (31 loc) · 1.38 KB
Breadcrumbs
interface
/
src
/
utils
/
chunkArray.ts
Copy path
File metadata and controls
37 lines (31 loc) · 1.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
33
34
35
36
37
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
const
MAXIMUM_CHUNK_SIZE
=
50
// 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
.
length
<
MAXIMUM_CHUNK_SIZE
)
{
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
}
Back
|
FazBrowse Home
|
New Git URL