Skip to content

Commit 40b1a13

Browse files
committed
querytokens accepts start and end chunk parameters
1 parent 7f01020 commit 40b1a13

File tree

2 files changed

+45
-52
lines changed

2 files changed

+45
-52
lines changed

scripts/querytokens.js

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,28 @@ async function main() {
2727

2828
let allTokens = [];
2929

30+
let startIndex, endIndex;
31+
let startChunk_, endChunk_;
3032
const chunks = Math.ceil(tokens.length / configs.tokensChunkSize);
3133
if (chunks <= 1) {
34+
console.log('Getting all tokens in one request');
3235
allTokens.push(await uv2query.getERC20Tokens(tokens));
3336
}
3437
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) {
3646
let toIndex = i + configs.tokensChunkSize - 1;
3747
if (toIndex > tokens.length - 1) {
3848
toIndex = tokens.length - 1;
3949
}
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);
4252
let tokensChunk = [];
4353
try {
4454
tokensChunk = await uv2query.getERC20Tokens(slice);
@@ -57,6 +67,29 @@ async function main() {
5767
}
5868
await utils.sleep(configs.cooldownMs);
5969
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 };
6093
}
6194
}
6295

@@ -68,52 +101,13 @@ async function main() {
68101
decimals: t[3]
69102
}
70103
});
71-
tokensResult = tokensResult.filter(t => t.symbol && t.decimals);
72-
73-
console.log('Adding token data to pairs file');
74104

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`)
111108
console.log(`Saving ${tokensOut}`);
112109
fs.writeFileSync(tokensOut, JSON.stringify(tokensResult, null, 2), {encoding: 'utf8'});
113110

114-
console.log(`Updating ${pairsOut}`);
115-
fs.writeFileSync(pairsOut, JSON.stringify(pairsResult, null, 2), {encoding: 'utf8'});
116-
117111
console.log('Completed');
118112
}
119113

scripts/utils.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
const hre = require('hardhat');
22

3-
exports.toTitleCase = function toTitleCase(str) {
4-
return str.charAt(0).toUpperCase() + str.slice(1);
5-
}
3+
exports.toTitleCase = str => str.charAt(0).toUpperCase() + str.slice(1);
64

7-
exports.getRanges = function getRanges(length, step) {
5+
exports.getRanges = (length, step) =>{
86
const ranges = [];
97
if (step > length) {
108
throw 'step must be less than length';
@@ -21,10 +19,11 @@ exports.getRanges = function getRanges(length, step) {
2119
return ranges;
2220
}
2321

24-
const sleep = ms => new Promise(r => setTimeout(r, ms));
25-
exports.sleep = sleep;
22+
exports.sleep = ms => new Promise(r => setTimeout(r, ms));
23+
24+
exports.zeroPad = (num, places) => String(num).padStart(places, '0')
2625

27-
exports.connectAccount = function connectAccount(network, key) {
26+
exports.connectAccount = (network, key) => {
2827
let networkUrl = process.env[`PROVIDER_URL_${network.toUpperCase()}`];
2928
console.log(`Connecting to ${network} provider`);
3029
const provider = new hre.ethers.providers.JsonRpcProvider(networkUrl);

0 commit comments

Comments
 (0)