Search code examples
typescriptnext.jsblockchainethereumsmartcontracts

How to find the type for ethereum, provider, and contract


I have this interface

interface IWeb3 {  
  ethereum?: MetaMaskInpageProvider;
  provider?: any;
  contract?: any;
};

I found the type for ethereum from import { MetaMaskInpageProvider } from "@metamask/providers" but could not find others.


Solution

  • I had to install ethers library

    import { MetaMaskInpageProvider } from "@metamask/providers";
    import { Contract, providers } from "ethers";
    
    interface IWeb3 {  
      ethereum?: MetaMaskInpageProvider;
      provider?: providers.Web3Provider;
      contract?: Contract;
    };
    

    Also to be able to use window.ethereum, I had to set this:

    declare global {
      interface Window {
        ethereum: MetaMaskInpageProvider;
      }
    }