0

I'm still pretty fresh to all this code life, please help me out.

I am writing this price scanner to get the prices of pairs in Uniswap. Even though console.log(tradePrices) shows me an object with token names as keys and their /ETH pair prices as values, when I try to get the price of a token individually by its key, I get an 'undefined' error.

const tradePrices = {};
        for (let [tokenName, tokenValue] of Object.entries({dai, sushi, usdc, usdt, yfi, cover, wbtc, snx, link, comp, susd, crv, band, ampl, uni})) {
            const pair = await Fetcher.fetchPairData(tokenValue, weth);
            const route = new Route([pair], weth);
            trade = new Trade(route, new TokenAmount(weth, '100000000000000000'), TradeType.EXACT_INPUT);
            
tradePrices[`${tokenName.toUpperCase()}/ETH`] = trade.executionPrice.invert().toSignificant(6);
        }
         console.log(tradePrices.dai); // undefined

I would appreciate so much your input!

4
  • You are using tradePrices as a dictionary, where the keys are the trading pairs "DAI/ETH". You can retrieve the values with tradePrices["DAI/ETH"]. To use tokenName.dai you should have used tradePrices[`${tokenName.toLowerCase()}`] = xxx. – Ismael Dec 14 '20 at 23:00
  • Oh thank you so much Ismael, you are a star!!!!!!!! – Teo Dec 14 '20 at 23:22
  • Now how do I accept your answer so that is pinned as the answer to the question above? Also another question @Ismael, are you saying that to use tokenName.dai I just needed to use lowercase letters instead of uppercase? – Teo Dec 14 '20 at 23:24
  • The question is unrelated to ethereum is about the javascript language. The main site stackoverflow.com is a better place to ask such questions. Javascript is case sensitive so if you use uppercase for the dictionarys keys then you use uppercase always, if you mix them it will not work. – Ismael Dec 15 '20 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.