Search code examples
node.jsblockchainethereumgethethers.js

Cannot Connect to local Ethereum node (Mainnet) using Ethers in node.js with Uniswap SDK


I installed Geth in my local environment, and successfully synchronized with ETH Mainnet. The command I used to start Geth is as follows:

geth --ethash.dagdir F:\Ethereum\Ethash --datadir F:\Ethereum --http --graphql --http --http.corsdomain "*" --http.api personal,eth,net,web3

However, when I try to connect to this local Ethereum node using ethers provider:

let provider = new ethers.providers.JsonRpcProvider();

And then try to use this provider to fetch pair information with Uniswap SDK:

var pair = await uniswap.Fetcher.fetchPairData(tokens.WETH, tokens.DAI, provider);

Below error message was shown:

(node:32252) UnhandledPromiseRejectionWarning: Error: call revert exception (method="getReserves()", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.4.0)  

Could someone help to advise whether it is related to the incorrect setup of the Geth Ethereum node, or related to the incorrect use of ethers local ETH node provider with the uniswap SDK (above works perfectly fine when I used an Infura endpoint) ?


Solution

  • I found the solution !

    Turns out that my Geth Ethereum node is not fully synchronized and thus whatever API call I conduct via the ethers library, it will act as like the Ethereum chain is empty.

    I connected into my Geth Ethereum node via:

    geth attach http://localhost:8545
    

    and then run the command:

    eth.syncing
    

    to check if the synchronization is completed. If it is done, then it will return "false".