6

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?

1

2 Answers 2

4
  1. Yes, ethers.js has an Infura provider.

  2. 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.

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.