cor*_*ese 7 javascript ethereum ethers.js
如何使用ethers包使用自定义节点 url 创建新的 Provider?
想做这样的事情:
const provider = new ethers.providers.Web3Provider('http://my-node.com')
Run Code Online (Sandbox Code Playgroud)
Nar*_*ali 11
在此处的文档中,它说使用JsonRpcProvider而不是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);
Run Code Online (Sandbox Code Playgroud)
以太坊 v6更新
ethers.providers.*更新为ethers.*for 提供者,如文档中所示。此外,为了支持EIP-1193Web3Provider,for已window.ethereum更改为BrowserProvider如下:
// v5
provider = new ethers.providers.Web3Provider(window.ethereum)
// v6:
provider = new ethers.BrowserProvider(window.ethereum)
Run Code Online (Sandbox Code Playgroud)