2

I want to access the Ethereum blockchain through Cloudflare Ethereum Gateway. I am trying to use web3-react library, but useWeb3React doesn't return object with provider property.

codesandbox.io

index.js

import { StrictMode } from "react";
import ReactDOM from "react-dom";
import Web3 from "web3";
import { Web3ReactProvider } from "@web3-react/core";

import App from "./App";

function getLibrary(provider) {
  const web3Provider = new Web3.providers.HttpProvider(
    "https://cloudflare-eth.com"
  );
  const web3 = new Web3(web3Provider);
  return web3;
}

const rootElement = document.getElementById("root");
ReactDOM.render(
  <StrictMode>
    <Web3ReactProvider getLibrary={getLibrary}>
      <App />
    </Web3ReactProvider>
  </StrictMode>,
  rootElement
);

App.js

import { useWeb3React } from "@web3-react/core";

import "./styles.css";

export default function App() {
  const web3React = useWeb3React();
  console.log({ web3React });

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Matt
  • 8,195
  • 31
  • 115
  • 225

1 Answers1

3

There is an issue in the library describing this exact problem with the solution here: https://github.com/NoahZinsmeister/web3-react/issues/126#issuecomment-783057749

Seems like you have to call web3React.activate() in your App.js with whatever connector you want to use. So for example with the @web3-react/injected-connector:

import { useWeb3React } from "@web3-react/core";
import { InjectedConnector } from "@web3-react/injected-connector";

import "./styles.css";

export default function App() {
  const web3React = useWeb3React();

  web3React.activate(new InjectedConnector({
    supportedChainIds: [1, 3, 4, 5, 42]
  }));
  console.log({ web3React });

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}
David Losert
  • 4,652
  • 1
  • 25
  • 31
  • Thanks David, this is helpful, but when I import `InjectedConnector`, the project doesn't compile. I get error `node_modules/@web3-react/abstract-connector/dist/abstract-connector.esm.js:1:9: error: No matching export in "browser-external:events" for import "EventEmitter"` complete messsage [here](https://imgur.com/a/1l9iykN) – Matt Dec 06 '21 at 09:20
  • Sure thing matt, glad if I am able to help. Did you `npm install @web3-react/injected-connector` before? – David Losert Dec 06 '21 at 09:59
  • Yes, I installed the npm package. If better answer won't pop up, I will award your answer. – Matt Dec 06 '21 at 10:07
  • okay. nut sure if it helps, but in the linked GitHub issue, they actually call activate like this: `web3.activate(injected, undefined, true);` where injected is defined outside like `const injected = new InjectedConnector({ supportedChainIds: [1, 3, 4, 5, 42], });`. Maybe that'll work? (so the difference are the two additional arguments `undefined` and `true`) – David Losert Dec 06 '21 at 10:18
  • Thanks, I tried it out but it didn't help. I am using Vite instead of CRA. I will try it out with CRA later and let you know. – Matt Dec 06 '21 at 10:21
  • I just tried it out in CRA and now I am getting error: `NoEthereumProviderError: No Ethereum provider was found on window.ethereum.` – Matt Dec 06 '21 at 17:38
  • The code snippet is for using MetaMask, but I would like to connect through Cloudflare Ethereum Gateway – Matt Dec 06 '21 at 18:23
  • 1
    I figured this out, I need to use `NetworkConnector` instead of `InjectedConnector`. I will award your answer tomorrow. – Matt Dec 06 '21 at 18:46