5

How do you create a new Provider using a custom node url using the ethers package?

Looking to do something like this:

 const provider = new ethers.providers.Web3Provider('http://my-node.com')
cormacncheese
  • 1,251
  • 1
  • 13
  • 27

1 Answers1

9

In the documentation here it says to use JsonRpcProvider instead of Web3Provider.

// When using the JSON-RPC API, the network will be automatically detected


// Default: http://localhost:8545
let httpProvider = new ethers.providers.JsonRpcProvider();


// To connect to a custom URL:
let url = "http://something-else.com:8546";
let customHttpProvider = new ethers.providers.JsonRpcProvider(url);


// Connect over named pipes using IPC:
let path = "/var/run/parity.ipc";
let ipcProvider = new ethers.providers.IpcProvider(path);
Naren Murali
  • 19,250
  • 3
  • 27
  • 54
  • 1
    And this method also solves the problem of calling public view functions without signing requests. In other words, it doesn't require you to have a wallet connected or installed in your browser. – Konstantin Komelin Nov 13 '22 at 20:03