Access ContractFactory in web3-react
Asked Answered
E

0

6

I'm using web3-react@^5.0.5 to interact with web3 from my react app. I can connect to metamask just fine.

I set the library to ethers.js and connected to metmask. Connecting worked fine, but almost none of ethers.js's methods are there. For example, useWeb3Context().library.ContractFactory doesn't exist.

Is this normal?

Code

Root component:

  ...
  return (
    <Web3Provider connectors={connectors} libraryName="ethers.js">
      ...
    </Web3Provider>
  );
  ...

connectors object:

const { InjectedConnector } = Connectors;

// Initially support Ropsten and Mainnet
const MetaMask = new InjectedConnector({ supportedNetworks: [1, 3] });

export const connectors = { MetaMask };

Subcomonent:

function SubComponent() {
  const web3 = useWeb3Context();

  function connect() { /* Connect to MetaMask, works fine */ }

  async function deployContract() {
    // Fails:
    // TypeError: web3.library.ContractFactory is not a constructor
    const factory = new web3.library.ContractFactory(ABI, BINARY);
  }

  return (/* ... */);
}
Elate answered 7/6, 2021 at 0:29 Comment(1)
An example of integrating web3-react and ethers.js was done here: github.com/Uniswap/interfaceFinalist

© 2022 - 2024 — McMap. All rights reserved.