I'm a little bit noob, so bear with me, but I love the blockchain very much and that's why I'm here, so please hear/help me out. I am building a price scanner for Uniswap, so I have this program to check the price of DAI/WETH on Uniswap that runs just fine:
const { ChainId, Fetcher, WETH, Route, Trade, TokenAmount, TradeType, Percent } = require('@uniswap/sdk');
const ethers=require('ethers');
const chainId = ChainId.MAINNET;
const daiAddress = '0x6b175474e89094c44da98b954eedeac495271d0f';
const init = async () => {
const dai = await Fetcher.fetchTokenData(chainId, daiAddress);
const weth = WETH[chainId];
const pair = await Fetcher.fetchPairData(dai, weth);
const route = new Route([pair], weth);
const trade = new Trade(route, new TokenAmount(weth, '100000000000000000'), TradeType.EXACT_INPUT);
console.log(route.midPrice.toSignificant(6));
However, if I want to create a for loop on checking the price of more than one token, I add another token in a for loop, but node.js throws me an error: (node:8368) UnhandledPromiseRejectionWarning: Error: Invariant failed: CHAIN_ID
Here is the code:
const { ChainId, Fetcher, WETH, Route, Trade, TokenAmount, TradeType, Percent } = require('@uniswap/sdk');
const ethers=require('ethers');
const chainId = ChainId.MAINNET;
const daiAddress = '0x6b175474e89094c44da98b954eedeac495271d0f';
const sushiAddress = '0x6b3595068778dd592e39a122f4f5a5cf09c90fe2';
const init = async () => {
const dai = await Fetcher.fetchTokenData(chainId, daiAddress);
const sushi = await Fetcher.fetchTokenData(chainId, sushiAddress)
tokens = [dai, sushi];
const weth = WETH[chainId];
for (token in tokens) {
const pair = await Fetcher.fetchPairData(token, weth);
}
const route = new Route([pair], weth);
const trade = new Trade(route, new TokenAmount(weth, '100000000000000000'), TradeType.EXACT_INPUT);
console.log(route.midPrice.toSignificant(6));
How do I create a correct for loop that would check the tokens I type in?
Much love if you can help a n00b out on his path to the glorious web3 matrix <3