Provider Configuration

In the upcoming documents, you will see that we are passing the provider argument whenever we perform a write operation on the blockchain. You may want to create a standalone provider.js file and reuse it whenever required.

JSON RPC Provider#

import { ethers } from 'ethers'
import dotenv from 'dotenv'

dotenv.config()

const getProvider = () => {
  const provider = new ethers.providers.JsonRpcProvider(process.env.RPC)
  return new ethers.Wallet(process.env.PRIVATE_KEY, provider)
}

export { getProvider }

Web3 Provider (Metamask, Trust Wallet, Coinbase Wallet, etc)#

import { ethers } from 'ethers'

const getProvider = () => {
  return new ethers.providers.Web3Provider(window.ethereum)
}

export { getProvider }

Other Providers#

Based on your setup, you may want to create a custom provider or signer. Read the ethers.js documentation to learn more. 

https://docs.ethers.org/v5/api/providers/

https://docs.ethers.org/v5/api/signer/