Deploy to a test network - cogeorg/teaching GitHub Wiki

You can deploy to a test network or the Mainnet by syncing the chain as described in Syncing a public node and deploying to it as you would deploy to Ganache or a private network. However, syncing the chain first is neither quick nor efficient, unless you are deploying to Mainnet and have serious security concerns.

A quick alternative is Infura. Infura provides an open API that lets you deploy to a test network within minutes (you can also deploy to Mainnet, however, this is not recommended). To deploy using Infura, you need an API key. You can create one by signing up to their platform (it is free) and creating a project (call it Test for now). When you view the project, you will find the endpoints for all the networks. They look like this:

rinkeby.infura.io/v3/<your-api-key>

For demonstration purposes, let us assume we want to deploy a contract to the Rinkeby network. We already have the Infura endpoint. Next, we need an account on the network. To create one, open MetaMask and switch to the Rinkeby network by selecting it from the dropdown menu on the top. Click on the colorful circle next to the dropdown menu to create the account. Don't forget to rename it!

The account has no Ether in it at the moment but we need Ether to pay the gas for deployment. Luckily, we only need Rinkeby-Ether and those are for free at the faucet.

Once you have a funded account, it is time for deployment. Open your truffle project and within it the truffle-config.json. Paste the following code:

var HDWalletProvider = require("truffle-hdwallet-provider");
const MNEMONIC = "<your-wallet-words>";

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    },
    rinkeby: {
      provider: function() {
        return new HDWalletProvider(MNEMONIC, "https://rinkeby.infura.io/v3/<your-api-key>")
      },
      network_id: 4
    }
  }
};

Please replace <your-wallet-words> with your seed phrase. You should have written them down somewhere but you can also get them out of MetaMask by clicking on the colorful circle on the top right --> Settings --> Security & Privacy --> Reveal Seed Words. Replace <your-api-key> with the API key provided by Infura.

Before deployment, make sure to install the dependency truffle-hdwallet-provider:

$ npm install truffle-hdwallet-provider

All what is left to do is to deploy by running

$ truffle migrate --network rinkeby