Skip to content

Using Infura (or a custom provider)

Infura Truffle logos

Infura’s API suite provides instant HTTPS and WebSocket access to the Ethereum and IPFS networks. By using Infura, you can connect easily to Web 3.0 without having to spin-up and maintain your own infrastructure. Their core service is free and provides everything you need to start building awesome decentralized applications today!

You may not be familiar with Infura by name, but if you've used MetaMask then you've used Infura, as it is the Ethereum provider that powers MetaMask.

For security reasons, Infura does not manage your private keys, which means Infura cannot sign transactions on your behalf.

However, Truffle can sign transactions through the use of its HDWalletProvider. This provider can handle the transaction signing as well as the connection to the Ethereum network. (Read more about HDWalletProvider.)

This tutorial will show you how to use Infura to migrate an existing dapp to an Ethereum network supported by Infura. In this specific instance, we'll migrate to Ropsten. We'll assume that you already have a dapp to migrate. If you want a test dapp, feel free to use our Pet Shop tutorial dapp.

Install HDWalletProvider

Truffle's HDWalletProvider is a separate npm package:

npm install @truffle/hdwallet-provider

Note: If you are on Windows and get an `MSBUILD` error, you may need to install the Windows build tools. In a terminal with Administrator rights, run `npm install -g windows-build-tools` and then try installation again.

Register with Infura and create a new project

Before you can use Infura, you need to register. Upon registration, this guide will walk you through creating a new project, authenticating with your new Project ID and Secret, securely copying your keys and selecting the appropriate network endpoint.

Configure your Truffle project

The next step is to edit your truffle-config.js file to use HDWalletProvider and provide all the necessary configuration for deploying to Ropsten.

  1. First, define the HDWalletProvider object in your configuration file. Add this line at the top of your truffle-config.js file:
const HDWalletProvider = require("@truffle/hdwallet-provider");
  1. Next, provide a reference to your mnemonic that generates your accounts. If you don't have a mnemonic, you can generate one using an online mnemonic generator or a hardware wallet such as a product from Ledger.
const mnemonic = "orange apple banana ... ";

Warning: In production, we highly recommend storing the mnemonic in another (secret) file, to reduce the risk of the mnemonic becoming known. If someone knows your mnemonic, they have all of your addresses and private keys!

  1. Add a Ropsten network definition:
module.exports = {
  networks: {
    ropsten: {
      provider: function() {
        return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/<INFURA_PROJECT_ID>")
      },
      network_id: 3
    }
  }
};

Things to notice:

  • While the example has only a single network defined, you can define multiple networks as normal.

  • The provider for the ropsten network definition instantiates the HDWalletProvider.

  • The HDWalletProvider takes as arguments a mnemonic and the desired network. A list of Infura-supported networks is available in the Endpoints dropdown on your Infura Project Settings page.

Infura Project Details
  • Make sure to replace <INFURA_PROJECT_ID> with your Infura Project ID.

  • The provider value is wrapped in a function, which ensures that it won't get initialized until it's needed. This is especially important if connecting to multiple networks. (See the Networks configuration section of the documentation for more on this topic.)

    Note: If you encounter issues with this construction, you can skip the function wrapper and use this instead:
    provider: new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/<INFURA_PROJECT_ID>"),

  • Without any other arguments, the account in charge of migration will be the first one generated by the mnemonic. But if desired, you can pass in an argument to specify which account to use. As an example, to use the third account:

    new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/<INFURA_PROJECT_ID>", 2);
    

    (Recall that the index is zero-based, so 2 is the third address.)

Use an ether faucet

Make sure you have enough ether in your account to do the deployment. You can acquire ether on the Ropsten network through a service known as a faucet. While there are multiple faucet sites out there, one service we recommend is hosted by MetaMask.

  1. Navigate to MetaMask's Test Ether Faucet.

  2. Connect to the Ropsten Test Network using MetaMask.

  3. The faucet will link to your first account. Click "Request 1 Ether From Faucet" to submit your request.

  4. Within a short period of time, your account will be populated with the requested ether.

We are now ready to deploy to Ropsten!

Deploy the contract

  1. Compile your project, if not already done:
truffle compile
  1. Deploy to the Ropsten network:
truffle migrate --network ropsten

If all goes well, you should see a response that looks similar to the following:

Starting migrations...
======================
> Network name:    'ropsten'
> Network id:      3
> Block gas limit: 0x6691b7


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x166c1791caa73cca6a75fe4258866bd1f2d1bcf2cd4c3a2a1e03fab29c42829d
   > Blocks: 0            Seconds: 0
   > contract address:    0x5ccb4dc04600cffA8a67197d5b644ae71856aEE4
   ......
   ......

Note that your transaction hash and contract address will be different from the ones above.

Note: If you receive an error `Error: Exceeds block gas limit `, you may need to manually set the gas limit for your contract. See the [Truffle Configuration](/docs/advanced/configuration) documentation for details.

If you want to verify that your contract was deployed successfully, you can check this on the Ropsten section of Etherscan. In the search field, type in the transaction ID for your contract. In the above example, the transaction ID is:

0x166c1791caa73cca6a75fe4258866bd1f2d1bcf2cd4c3a2a1e03fab29c42829d

You should see details about the transaction, including the block number where the transaction was secured.

Congratulations! You've deployed your contract to Ropsten using the combined power of Infura and Truffle.