Writing Smart Contracts

A short guide dipping your toes into smart contract development for the Index Coop

Process Overview

Creating a smart contract for the Coop is generally done according to the following process:
  1. 1.
    Write a smart contract.
  2. 2.
    Build the repo to create the necessary typechain files.
  3. 3.
    Write tests and deployment methods to support your contract.
  4. 4.
    Test the contract.
You can use this PR as a reference the whole way through, and it will be referenced multiple times throughout.

Setup

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 visit Alchemy and set up an account. Once you’re set up and logged in, you’ll need to create an app for the Kovan testnet.
Alchemy App Creation
Once the app is created, go to the details for that app and select ‘View Key’. You’ll be shown a URL, but all you need is the hash at the end. Copy this into your .env file in place of the default ALCHEMY_TOKENvalue.
Alchemy App API Key
You should now be set up and good to go. Run yarnto get things built and install all the dependencies.

Development and Testing

Once you’re set up and ready to go, create your smart contract in the appropriate directory based on what the contract is supposed to be (eg. If you're building a module adapter, use /adapters. If you're building a generic smart contract involving ERC-20s, use /tokens).
Once you’ve written your contract, you’ll want to build by running yarn. After the repo builds, your contract should have all the necessary typechain files to continue.
You’ll want to start by updating utils/contracts.ts to export your contract to the rest of the project from the built files. Follow the lead of the other exports and you should be set. You'll want to add a deploy function under the appropriate directory in utils/deploys/. This will be used to deploy your contract to a locally run chain for testing, and will be needed to write a spec file.
Now, you’ll want to write a testing spec, an example of which can be found here. Be sure to test any function and modifier thoroughly before moving on.
Once you have a completed spec file, it’s time to run the tests. In one terminal window, run
yarn chain
Once the local chain is running, open another terminal and run
yarn test
This could take some time to test, but will give you solid feedback if you have written bad tests.

Submitting a PR

If you’ve passed all tests, and all boxes are ticked, submit a PR. If you want an example of how to do this, reference this 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.