Deploying Smart Contracts

A hitchhikers guide to deploying smart contracts for the Index Coop

Process Overview

Deploying a smart contract for the Coop is generally done according to the following process:
  1. 2.
    Write a deployment script
  2. 3.
    Write tests for the deployment script
  3. 4.
    Test deployment script
  4. 5.
    Deploy to Kovan.


Begin by forking the repo, and creating a feature branch for your smart contract changes. Once you have it cloned locally, copy the environment variables over as shown below:
cp .env.default .env
Now you'll want to replace some of the variables in your .env file.
Visit Etherscan and set up an account. Once you’re set up and logged in, you’ll need to create an API key.
Once you have that key, replace ETHERSCAN_API_KEY in .env with your new API key.
Visit Infura and set up an account. Once you're set up and logged in, you'll need to create an app.
Once created, you'll see a Project ID on the overview page. Copy that ID and set it as the value of INFURA_TOKEN in your .env file.
Finally, replace the KOVAN_DEPLOY_PRIVATE_KEY with your wallet's private key. If you're nervous about this, then create a new wallet in MetaMask with nothing in it, and use that private key to avoid mixing your actual wallet.

Creating the Deployment Files

In a terminal window, run
yarn create:deployment my_contract_name
Where my_contract_name is the name of your contract deployment, for example:
yarn create:deployment btcfli_rebalance_viewer
New deployment files at:
> .../deploy/013_btcfli_rebalance_viewer.ts
> .../deployments/constants/013_btcfli_rebalance_viewer.ts
> .../test/deploys/013_btcfli_rebalance_viewer.spec.ts
This will create 3 files with the appropriate migration number. Respectively, these are the deployment script, the deployment constants, and deployment tests. Populate these similarly to the other files that precede them.

Testing the Deployment

In a terminal, run
yarn chain
In another terminal, run
yarn test
This will run all the unit tests you've written, and verify that everything is built out like you expect it to be. From here, you can also run
yarn clean-dev-deployment
yarn deploy:local
This will deploy your contract to your locally running blockchain from yarn chain and make sure the deployment goes smoothly before you deploy to Kovan.

Deploying to Kovan

Remember that wallet private keys you used to replace the KOVAN_DEPLOY_PRIVATE_KEYdefault value? That wallet will need some ETH to deploy the contract to the testnet. You can find a faucet from myCrypto here that can send you some Kovan ETH.
Once you've secured your Kovan ETH run the following commands in a terminal:
yarn deploy:kovan
yarn etherscan:kovan
After everything is finished running, you should receive an Etherscan URL and a contract address that will lead you to your contract! If your deployment actually deployed multiple contracts, you should get URLs and addresses for each as they're deployed.

Submitting a PR

If you’ve passed all tests, and all boxes are ticked, submit a PR. Some CI tests will run once the PR is published, so keep an eye out for those. If any issues arise, they’ll need to be addressed before being able to be reviewed and merged.