@@ -27,18 +27,28 @@ async function main() {
27
27
28
28
let allTokens = [ ] ;
29
29
30
+ let startIndex , endIndex ;
31
+ let startChunk_ , endChunk_ ;
30
32
const chunks = Math . ceil ( tokens . length / configs . tokensChunkSize ) ;
31
33
if ( chunks <= 1 ) {
34
+ console . log ( 'Getting all tokens in one request' ) ;
32
35
allTokens . push ( await uv2query . getERC20Tokens ( tokens ) ) ;
33
36
}
34
37
else {
35
- for ( let i = 0 ; i < tokens . length ; i += configs . tokensChunkSize ) {
38
+ console . log ( `Total chunks: ${ chunks } ` ) ;
39
+ const { startChunk, endChunk } = parseArgs ( ) ;
40
+ startChunk_ = startChunk ;
41
+ endChunk_ = endChunk ;
42
+ startIndex = ( startChunk * configs . tokensChunkSize ) - configs . tokensChunkSize ;
43
+ endIndex = ( endChunk * configs . tokensChunkSize ) - 1 ;
44
+ let chunkIndex = startChunk ;
45
+ for ( let i = startIndex ; i < tokens . length ; i += configs . tokensChunkSize ) {
36
46
let toIndex = i + configs . tokensChunkSize - 1 ;
37
47
if ( toIndex > tokens . length - 1 ) {
38
48
toIndex = tokens . length - 1 ;
39
49
}
40
- console . log ( `Chunk from ${ i } to ${ toIndex } ` ) ;
41
- const slice = tokens . slice ( i , toIndex ) ;
50
+ console . log ( `[ ${ chunkIndex } ] Chunk from ${ i } to ${ toIndex } ` ) ;
51
+ const slice = tokens . slice ( i , toIndex + 1 ) ;
42
52
let tokensChunk = [ ] ;
43
53
try {
44
54
tokensChunk = await uv2query . getERC20Tokens ( slice ) ;
@@ -57,6 +67,29 @@ async function main() {
57
67
}
58
68
await utils . sleep ( configs . cooldownMs ) ;
59
69
allTokens = allTokens . concat ( tokensChunk ) ;
70
+ if ( toIndex == endIndex ) {
71
+ break ;
72
+ }
73
+ chunkIndex ++ ;
74
+ }
75
+
76
+ function parseArgs ( ) {
77
+ const args = process . argv . slice ( 2 ) ;
78
+ if ( args . length != 2 ) {
79
+ console . log ( 'Usage: node querytokens START_CHUNK END_CHUNK' ) ;
80
+ process . exit ( ) ;
81
+ }
82
+ const start = parseInt ( args [ 0 ] ) ;
83
+ if ( start < 1 || start > chunks ) {
84
+ console . log ( `START_CHUNK must be between 1 and ${ chunks } ` ) ;
85
+ process . exit ( ) ;
86
+ }
87
+ const end = parseInt ( args [ 1 ] ) ;
88
+ if ( end < 1 || end > chunks || end < start ) {
89
+ console . log ( `START_CHUNK must be between 1 and ${ chunks } and lesser than ${ start } ` ) ;
90
+ process . exit ( ) ;
91
+ }
92
+ return { startChunk : start , endChunk : end } ;
60
93
}
61
94
}
62
95
@@ -68,52 +101,13 @@ async function main() {
68
101
decimals : t [ 3 ]
69
102
}
70
103
} ) ;
71
- tokensResult = tokensResult . filter ( t => t . symbol && t . decimals ) ;
72
-
73
- console . log ( 'Adding token data to pairs file' ) ;
74
104
75
- const pairsResult = [ ] ;
76
- for ( let i = 0 ; i < pairs . length ; i ++ ) {
77
- const token0 = tokensResult . find ( t => t . address === pairs [ i ] . token0 . address ) ;
78
- const token1 = tokensResult . find ( t => t . address === pairs [ i ] . token1 . address ) ;
79
- if ( ! token0 || ! token1 ) {
80
- console . log ( `WARNING: pair ${ pairs [ i ] . address } is invalid` ) ;
81
- continue ;
82
- }
83
- let reserve0 = 0 ;
84
- if ( pairs [ i ] . token0 . reserve !== undefined && pairs [ i ] . token0 . reserve !== "0" ) {
85
- reserve0 = hre . ethers . BigNumber . from ( pairs [ i ] . token0 . reserve ) / Math . pow ( 10 , token0 . decimals ) ;
86
- }
87
- let reserve1 = 0 ;
88
- if ( pairs [ i ] . token1 . reserve !== undefined && pairs [ i ] . token1 . reserve !== "0" ) {
89
- reserve1 = hre . ethers . BigNumber . from ( pairs [ i ] . token1 . reserve ) / Math . pow ( 10 , token1 . decimals ) ;
90
- }
91
- pairsResult . push ( {
92
- address : pairs [ i ] . address ,
93
- token0 : {
94
- address : token0 ?. address ,
95
- symbol : token0 ?. symbol ,
96
- name : token0 ?. name ,
97
- decimals : token0 ?. decimals ,
98
- reserve : reserve0
99
- } ,
100
- token1 : {
101
- address : token1 ?. address ,
102
- symbol : token1 ?. symbol ,
103
- name : token1 ?. name ,
104
- decimals : token1 ?. decimals ,
105
- reserve : reserve1
106
- }
107
- } ) ;
108
- }
109
-
110
- const tokensOut = path . join ( configs . outputPath , `${ network . toLowerCase ( ) } Tokens.json` )
105
+ const start = utils . zeroPad ( startChunk_ , 4 ) ;
106
+ const end = utils . zeroPad ( endChunk_ , 4 ) ;
107
+ const tokensOut = path . join ( configs . outputPath , `${ network . toLowerCase ( ) } Tokens_${ start } -${ end } .json` )
111
108
console . log ( `Saving ${ tokensOut } ` ) ;
112
109
fs . writeFileSync ( tokensOut , JSON . stringify ( tokensResult , null , 2 ) , { encoding : 'utf8' } ) ;
113
110
114
- console . log ( `Updating ${ pairsOut } ` ) ;
115
- fs . writeFileSync ( pairsOut , JSON . stringify ( pairsResult , null , 2 ) , { encoding : 'utf8' } ) ;
116
-
117
111
console . log ( 'Completed' ) ;
118
112
}
119
113
0 commit comments