How can I use Infura as the provider for ethers.js? Whether I can use sendtransaction api in ethers.js if I use infura as the provider?
2 Answers
Yes, ethers.js has an Infura provider.
Since Infura doesn't provide ethereum accounts you have to manage it your side. Ethers.js has wallets that can be used with any of their providers Wallet and signers.
Here's is an example mainnet configuration in truffle-config.js
using Infura:
mainnet: {
provider: function() {
return new HDWalletProvider(
process.env.MNEMONIC,
`https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`
)
},
gas: 2500000,
gasPrice: 25000000000,
confirmations: 2,
network_id: 1
},
If you have "dotenv": "^8.2.0"
installed, you can add your Infura API key in a file called env:
INFURA_API_KEY="..."
Otherwise you can just enter your full Infura key in to the url in truffle-config.js
.