gsscoder
gsscoder

Reputation: 3392

Nework error when calling QuickSwap contract method on Polygon mainnet

I wrote a simple node script to call the getAmountsOut method of QuickSwap contract. It perfectly works on Polygon testnet but fails on mainnet.

I obviously changed provider URL and contract addresses. This should be a read-only method and should not require gas.

The code is the following:

const { ethers } = require("ethers");
const uniswapV2abi =  [
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amountIn",
        "type": "uint256"
      },
      {
        "internalType": "address[]",
        "name": "path",
        "type": "address[]"
      }
    ],
    "name": "getAmountsOut",
    "outputs": [
      {
        "internalType": "uint256[]",
        "name": "amounts",
        "type": "uint256[]"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amountIn",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "amountOutMin",
        "type": "uint256"
      },
      {
        "internalType": "address[]",
        "name": "path",
        "type": "address[]"
      },
      {
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "deadline",
        "type": "uint256"
      }
    ],
    "name": "swapExactTokensForTokens",
    "outputs": [
      {
        "internalType": "uint256[]",
        "name": "amounts",
        "type": "uint256[]"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
  }
]

const key = '...'
const quickSwapAddr = '0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff';
const DAI = '0x8f3cf7ad23cd3cadbd9735aff958023239c6a063';
const MATIC = '0x00000000000000000000000000000000000010100;'

async function main() {

  const provider = new ethers.providers.JsonRpcProvider('https://polygon-mainnet.g.alchemy.com/v2/...');
  const signer = new ethers.Wallet(key, provider);
  
  const router = new ethers.Contract(quickSwapAddr, uniswapV2abi, signer);

  const amount = 100000;

  const amounts = await router.getAmountsOut(amount, [DAI, MATIC]);

  console.log(amounts[1]);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

and it produces the following error:

Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.6.8)
    at Logger.makeError (C:\Hacking\Bc\uniswaptest1\node_modules\@ethersproject\logger\lib\index.js:233:21)
    at Logger.throwError (C:\Hacking\Bc\uniswaptest1\node_modules\@ethersproject\logger\lib\index.js:242:20)
    at JsonRpcProvider.<anonymous> (C:\Hacking\Bc\uniswaptest1\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:561:54)
    at step (C:\Hacking\Bc\uniswaptest1\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
    at Object.throw (C:\Hacking\Bc\uniswaptest1\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
    at rejected (C:\Hacking\Bc\uniswaptest1\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  reason: 'could not detect network',
  code: 'NETWORK_ERROR',
  event: 'noNetwork'
}

What am I doing wrong?

Upvotes: 0

Views: 1115

Answers (1)

Crazy Punch Man
Crazy Punch Man

Reputation: 126

I found some small errors in your code and then did a little optimization, I recommend using the WSS type of node, and then in this case I used the GetBlock free node, by the way if you run the script without any feedback and errors just stuck, that is usually a node problem :)

const { ethers } = require("ethers");

const key = '...'
const quickSwapAddr = '0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff';
const DAI = '0x8f3cf7ad23cd3cadbd9735aff958023239c6a063';
const MATIC = '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270';

async function main() {

    const provider = new ethers.providers.WebSocketProvider('wss://matic.getblock.io/mainnet/?api_key=...');
    const signer = new ethers.Wallet(key, provider);
    const account = signer.connect(provider);

    const router = new ethers.Contract(quickSwapAddr, [
        'function getAmountsOut(uint amountIn, address[] memory path) public view  returns (uint[] memory amounts)'
    ], account);

    const amount = 100000;

    const amounts = await router.getAmountsOut(amount, [DAI, MATIC]);

    console.log(amounts[1]);
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

Upvotes: 2

Related Questions