Errors related to my API key

Hello.

My URL with my API key in it says “404 page not found”.

I hear that the URL has to be used in my code. It has been used in a code, and yet it still says “404 page not found”.

And it says in my log in aircode.io: “TypeError: Cannot read properties of undefined (reading ‘JsonRpcProvider’)” Clearly, this has to do with the provider, namely Infura I assume.

A fix would be highly appreciated.

2 Likes

Hi @plankton5165,

Wellcome!

You are probably using JavaScript and ethers. When ethers says “provider” it’s referring to it’s family of classes that implement provider functionality, JsonRpcProvider is one of these. So you have problems in your code and maybe you should ask this question in a forum related to the tutorial you are following.

Without seeing your code I’m guessing a bit, but it is common for beginners to leave the API Key off the URL when they try to access the Infura endpoint.

If you can provide a small example we can read or better yet run then we can help a lot more!

1 Like

Well, this is the code:

const ethers = require('ethers');

const provider = new ethers.providers.JsonRpcProvider({url: 'https://mainnet.infura.io/v3/20fa90a3772c4497a74513c18b5f6cf3'})

const token0 = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'; // WETH
const token1 = '0x6b175474e89094c44da98b954eedeac495271d0f'; // DAI
const uniRouterAddress = '0x7a250d5630b4cf539739df2c5dacb4c659f2488d';
const sushiRouterAddress = '0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F';

const PATH = [token0, token1]

const routerAbi = [
    'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
]

const uniRouter = new ethers.Contract(uniRouterAddress, routerAbi, provider);
const sushiRouter = new ethers.Contract(sushiRouterAddress, routerAbi, provider);

const amountIn = ethers.utils.parseEther('1');

const main = async () => {
  const uniAmount = await uniRouter.getAmountsOut(amountIn, PATH)
  const sushiAmount = await sushiRouter.getAmountsOut(amountIn, PATH)

  const uniPrice = Number(uniAmount[1]) / Number(uniAmount[0])
  const sushiPrice = Number(sushiAmount[1]) / Number(sushiAmount[0])
  console.log('uniPrice', uniPrice)
  console.log('sushiPrice', sushiPrice)

  const TX_FEE = 0.003

  let effUniPrice
  let effSushiPrice
  let spread

  if (uniPrice > sushiPrice) {
    effUniPrice = uniPrice - (uniPrice * TX_FEE)
    effSushiPrice = sushiPrice + (sushiPrice * TX_FEE)
    spread = effUniPrice - effSushiPrice
    console.log('uni to sushi spread:', spread)
    if (spread > 0) {
      console.log('sell on sushi, buy on uni')
    } else {
      console.log('no arb opportunity')
    }
  } else if (sushiPrice > uniPrice) {
    effSushiPrice = sushiPrice - (sushiPrice * TX_FEE)
    effUniPrice = uniPrice - (uniPrice * TX_FEE)
    spread = effSushiPrice - effUniPrice
    console.log('sushi to uni spread:', spread)
    if (spread > 0) {
      console.log('sell on uni, buy on sushi')
    } else {
      console.log('no arb opportunity')
    }
  } else if (sushiPrice > uniPrice) {
    effSushiPrice = sushiPrice - (sushiPrice * TX_FEE)
    effUniPrice = uniPrice + (uniPrice * TX_FEE)
    spread = effSushiPrice - effUniPrice
    console.log('uni to sushi spread:', spread)
    if (spread > 0) {
      console.log('sell on sushi, buy on uni')
    } else {
      console.log('no arb opportunity')
      }
    }

}

main()

It’s supposed to indicate whether or not the transferring of a token is profitable.

It was actually copied from someone else’s video, admittedly. Specifically, it’s Uniswap and Sushiswap Arbitrage Code by Blockman Codes, and I tried to get his help resolving errors by commenting in his video but he did not respond.

I was like, “I don’t understand how it works for him but not for me!”

1 Like

Hi,
I think the essence of the problem is the changes from ethers@5 to ethers@6, there will be an upgrade guide on their site. There will be a lot of examples out there that break in this spot because of the changes to ethers.

you can get your example to run as is by installing ethers@5:
npm install ethers@5
this will replace the latest version (6) with 5 and it just works. Otherwise check out the ethers upgrade docs to understand how to get past each error one by one.

Good luck with it!

1 Like

What I did was change the version of Ethers from v6.7.1 to v5.7.2.

And this is the code now:

const ethers = require('ethers');

const provider = new ethers.providers.JsonRpcProvider({url: 'https://mainnet.infura.io/v3/20fa90a3772c4497a74513c18b5f6cf3'});

const token0 = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'; // WETH
const token1 = '0x6b175474e89094c44da98b954eedeac495271d0f'; // DAI
const uniRouterAddress = '0x7a250d5630b4cf539739df2c5dacb4c659f2488d';
const sushiRouterAddress = '0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F';

const PATH = [token0, token1]

const routerAbi = [
    'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
]

const uniRouter = new ethers.Contract(uniRouterAddress, routerAbi, provider);
const sushiRouter = new ethers.Contract(sushiRouterAddress, routerAbi, provider);

const amountIn = ethers.utils.parseEther('1');

const main = async () => {
  const uniAmount = await uniRouter.getAmountsOut(amountIn, PATH)
  const sushiAmount = await sushiRouter.getAmountsOut(amountIn, PATH)

  const uniPrice = Number(uniAmount[1]) / Number(uniAmount[0])
  const sushiPrice = Number(sushiAmount[1]) / Number(sushiAmount[0])
  console.log('uniPrice', uniPrice)
  console.log('sushiPrice', sushiPrice)

  const TX_FEE = 0.003

  let effUniPrice
  let effSushiPrice
  let spread

  if (uniPrice > sushiPrice) {
    effUniPrice = uniPrice - (uniPrice * TX_FEE)
    effSushiPrice = sushiPrice + (sushiPrice * TX_FEE)
    spread = effUniPrice - effSushiPrice
    console.log('uni to sushi spread:', spread)
    if (spread > 0) {
      console.log('sell on sushi, buy on uni')
    } else {
      console.log('no arb opportunity')
    }
  } else if (sushiPrice > uniPrice) {
    effSushiPrice = sushiPrice - (sushiPrice * TX_FEE)
    effUniPrice = uniPrice - (uniPrice * TX_FEE)
    spread = effSushiPrice - effUniPrice
    console.log('sushi to uni spread:', spread)
    if (spread > 0) {
      console.log('sell on uni, buy on sushi')
    } else {
      console.log('no arb opportunity')
    }
  } else if (sushiPrice > uniPrice) {
    effSushiPrice = sushiPrice - (sushiPrice * TX_FEE)
    effUniPrice = uniPrice + (uniPrice * TX_FEE)
    spread = effSushiPrice - effUniPrice
    console.log('uni to sushi spread:', spread)
    if (spread > 0) {
      console.log('sell on sushi, buy on uni')
    } else {
      console.log('no arb opportunity')
      }
    }

}

main()

However, I get this error:
{“error”:“arb.js is not callable”}

Still not getting how it works for him, not for me.

Edit: I’ve been able to get it to work on replit, but not aircode. I’ve been trying to use these two websites to run code that can also be run in node.js.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.