0

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

1 Answer 1

2

Change this:

for (token in tokens)

To this:

for (token of tokens)

It's a common mistake in Javascript (which has nothing to do with Ethereum, by the way).

2
  • Thanks so much!!!! I come from a Python background (well, not really a background, just thought learning Python was better for the blockchain, but soon realized node.js is the way to go), and in Python the loop goes for/in, not for/of! Haha, thanks , it works beautifully now (just needed to move the constants pair and trade to the local scope of the function). :************
    – Teo
    Commented Dec 4, 2020 at 13:47
  • Boom, goodvibrations, you da best
    – Teo
    Commented Dec 4, 2020 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.