|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const utils = require('./utils'); |
| 4 | +const hre = require('hardhat'); |
| 5 | +const configs = require('../appconfigs.json'); |
| 6 | + |
| 7 | +const hasSN = n => { |
| 8 | + const str = n.toString(); |
| 9 | + return str.includes('e-') || str.includes('e+'); |
| 10 | +} |
| 11 | + |
| 12 | +const network = configs.network; |
| 13 | +const dex = configs.dex; |
| 14 | + |
| 15 | +console.log('Adding token data to pairs file'); |
| 16 | + |
| 17 | +const pairsOut = path.join(configs.outputPath, `${network.toLowerCase()}${utils.toTitleCase(dex)}Pairs.json`) |
| 18 | +const pairs = JSON.parse(fs.readFileSync(pairsOut, {encoding: 'utf8'})); |
| 19 | + |
| 20 | +const tokensOut = path.join(configs.outputPath, `${network.toLowerCase()}Tokens.json`) |
| 21 | +const tokens = JSON.parse(fs.readFileSync(tokensOut, {encoding: 'utf8'})); |
| 22 | + |
| 23 | +const pairsResult = []; |
| 24 | +for (let i = 0; i < pairs.length; i++) { |
| 25 | + const token0 = tokens.find(t => t.address === pairs[i].token0.address); |
| 26 | + const token1 = tokens.find(t => t.address === pairs[i].token1.address); |
| 27 | + if (!token0 || !token1) { |
| 28 | + console.log(`WARNING: pair ${pairs[i].address} is invalid`); |
| 29 | + continue; |
| 30 | + } |
| 31 | + let reserve0 = 0; |
| 32 | + if (pairs[i].token0.reserve !== undefined && pairs[i].token0.reserve !== "0") { |
| 33 | + reserve0 = Number((hre.ethers.BigNumber.from(pairs[i].token0.reserve) / Math.pow(10, token0.decimals)).toFixed(token0.decimals)); |
| 34 | + if (hasSN(reserve0)) { |
| 35 | + console.log(`WARNING: token ${pairs[i].token0.address} has not meaningful reserve`); |
| 36 | + continue; |
| 37 | + } |
| 38 | + } |
| 39 | + let reserve1 = 0; |
| 40 | + if (pairs[i].token1.reserve !== undefined && pairs[i].token1.reserve !== "0") { |
| 41 | + reserve1 = Number(hre.ethers.BigNumber.from(pairs[i].token1.reserve) / Math.pow(10, token1.decimals).toFixed(token1.decimals)); |
| 42 | + if (hasSN(reserve1)) { |
| 43 | + console.log(`WARNING: token ${pairs[i].token0.address} has not meaningful reserve`); |
| 44 | + continue; |
| 45 | + } |
| 46 | + } |
| 47 | + pairsResult.push({ |
| 48 | + address: pairs[i].address, |
| 49 | + token0: { |
| 50 | + address: token0?.address, |
| 51 | + symbol: token0?.symbol, |
| 52 | + name: token0?.name, |
| 53 | + decimals: token0?.decimals, |
| 54 | + reserve: reserve0 |
| 55 | + }, |
| 56 | + token1: { |
| 57 | + address: token1?.address, |
| 58 | + symbol: token1?.symbol, |
| 59 | + name: token1?.name, |
| 60 | + decimals: token1?.decimals, |
| 61 | + reserve: reserve1 |
| 62 | + } |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +console.log(`Updating ${pairsOut}`); |
| 67 | +fs.writeFileSync(pairsOut, JSON.stringify(pairsResult, null, 2), {encoding: 'utf8'}); |
| 68 | + |
| 69 | +console.log('Completed'); |
0 commit comments