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

1
import { ethers } from 'ethers'
2
import dotenv from 'dotenv'
3
4
dotenv.config()
5
6
const getProvider = () => {
7
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC)
8
return new ethers.Wallet(process.env.PRIVATE_KEY, provider)
9
}
10
11
export { getProvider }
Copied!

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

1
import { ethers } from 'ethers'
2
3
const getProvider = () => {
4
return new ethers.providers.Web3Provider(window.ethereum)
5
}
6
7
export { getProvider }
Copied!

Other Providers

Based on your setup, you may want to create a custom provider or signer. Read the ethers.js documentation to learn more.
Providers
Signers
Last modified 4mo ago