How to Deploy Your Smart Contract to Production

In this post, I will teach you how to deploy your Solidity smart contract to the mainnet via Infura with BuddyCI and Github.

Prior requirements:

  • knowledge of javascript, git, solidity, CLI
  • have a github account, infura account, and buddy CI account.
  • smart contracts already written, audited, and ready for deployment

Select Github Repo

buddy-ci(1)

buddy-ci(2)

Add a Pipeline to Buddy CI

buddy-ci(3)

Execute it “On Push”.

buddy-ci(4)

Name it “SimpleStorage Deploy to Production”

Pick “Single Branch” and “Master”

Install Truffle-HDWallet-Provider

Run npm install truffle-hdwallet-provider on your local computer.

Edit your truffle-config.js file on your computer

Copy and paste the following into your truffle-config.js file, replacing the <project_id> with your Infura project id.

const path = require("path");
const HDWalletProvider = require("truffle-hdwallet-provider");

const infuraURL = "https://mainnet.infura.io/v3/<project_id>";
const infuraRinkebyURL = "https://rinkeby.infura.io/v3/<project_id>";
const mnemonic = process.env.PRIV_SEED;

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // to customize your Truffle configuration!
  contracts_build_directory: path.join(__dirname, "client/src/contracts"),
  networks: {
    develop: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    },
    "rinkeby":{
      provider: ()=> new HDWalletProvider(mnemonic, infuraRinkebyURL),
      network_id: 4
    },
    "mainnet":{
      provider: () => new HDWalletProvider(mnemonic, infuraURL),
      network_id: 1
    }
  }
};

Grab your mnemonic seed phrase!

Get your mnemonic seed phrase from your Metamask account by selecting Settings and Security then enter your password to gain access to your seed phrase and add it as an environment variable in Buddy CI pipeline called “PRIV_SEED”. Save it there.

Grab Test Ether from Rinkeby Faucet

If you do not have Rinkeby test ether in your account, then now is a good time to get some here at the Rinkeby Faucet by following the instructions there.

Add the install truffle-hdwallet-provder script

Add npm install truffle-hdwallet-provider to your environment on the Buddy CI pipeline you created below the npm i -g truffle script.

Rinkeby or Mainnet

Decide if you want to deploy your smart contract to the testnet (Rinkeby in this case) or mainnet (warning: costs real ETH).

Depending on what you decide, you want to configure your pipeline action to run the following:

Mainnet Command

truffle migrate --network mainnet

Rinkeby Command

truffle migrate --network rinkeby

Make a Github Push to Deploy to Production

run git init within your project directory to start a git
run git add . to add the changes in your truffle-config.js file to your staging area.
run git commit -m 'changing truffle-config.js to include mainnet and rinkeby'
run git remote add origin <your github repo address here>
run git push origin master

This will push all the files and history up to your github repo, which will automaticaly trigger the Buddy CI to run.

Check the logs

Check the logs on your Buddy CI pipeline by logging into your account and selecting your project, if you don’t already have it open in another window.

Success!

buddy-ci(success)

Hopefully, you have succeeded in deploying your contract to the mainnet or rinkeby. It’s your choice. Depending on what you choose, you will see some variation of the above and it will take a variable amount of time to complete the mining of the transaction/contract on the network (ethereum mainnet takes approximately ~12 seconds per block).

Etherscan

Check your contract deployed by visiting Etherscan.io and copy pasting your Rinkeby or Mainnet account address from Metamask.

You should see the contract creation transaction hash and a timestamp for when it was migrated to the Rinkeby or Mainnet.

buddy-ci(ether)

Enter in your Rinkeby wallet address into the search bar.

buddy-ci(etherscan)

Check out the transactions on the account.

buddy-ci(etherscan)(3)

You can check the Rinkeby deployment of this contract by going to rinkeby.etherscan.io

Extra Credit

Explore Buddy CI and see how to send yourself a notification email on production deploy or to setup an approval requirement for admins to execute deploy.

Published 10 Nov 2019

full stack && ethereum engineer.
Max Goodman on Twitter