Deploy Your Smart Contract in 5 Steps

Ying Tong Lai
Akomba
Published in
6 min readAug 31, 2018

Deploying a smart contract shouldn’t involve black box magic or manual copy/pasting. Let’s keep it neat with a Go flattener and 40 lines of JavaScript.

It’s 2018 and deploying a smart contract is still way harder than it should be. Many existing guides are either too limited (only contracts with no dependencies), too magical (truffle migrate), too manual (“copy and paste from Remix”), or just plain outdated.

The interns of Akomba present a 5-step guide to deploying your smart contract, using a Go flattener and 40 lines of JavaScript. We hope our guide is general enough to remain sound even with future iterations of these tools, and will require at most minor syntactic tweaks.

  1. Flatten your smart contract
  2. Compile your flattened contract to get its ABI and bytecode
  3. Set up your Ethereum account and Infura API
  4. Deploy your smart contract using the ethers.js library
  5. (Optional) Verify and publish your source code on Etherscan

1. Flatten your smart contract

So you’ve written up a tight Solidity smart contract, Contract.sol, and now you want to deploy it. Our first step is to flatten your contract’s dependencies into a single file. This is because Etherscan requires that we upload our source code in a single file, and we’re all about making our source code publicly available (Step 5).

If your contract uses any libraries (e.g. OpenZeppelin), you’ve most likely downloaded these dependencies’ source files and imported them by specifying their file paths.

Quite a few Solidity flatteners exist; for this tutorial, we’re using the best one, Akomba’s SolidityFlattery. (This is written in Go, so install Go if you haven’t yet.) Let’s get flattening:

  • From the command line, clone or download the SolidityFlattery GitHub repo: git clone https://github.com/akombalabs/SolidityFlattery
  • Import one dependency: go get gopkg.in/natefinch/lumberjack.v2
  • Navigate into the SolidityFlattery directory: cd SolidityFlattery
  • Build the flattener: go build flat.go utils.go
  • If the executable isn’t in your path yet, add it: export PATH=$PATH:.
  • Assuming your contract is in path/to/Contract.sol and you want to create Contract_flat.sol:
    flat -input path/to/Contract.sol -output Contract_flat
After running flat -input path/to/Contract.sol -output Contract_flat,
your console output should look something like this. (Continued from example above)
  • Yay! You should now see Contract_flat.sol in the SolidityFlattery directory.
  • Copy Contract_flat.sol into your main contracts folder.

2. Compile your flattened contract to get its ABI and bytecode

The Ethereum Virtual Machine interacts with your smart contract through its Application Binary Interface (ABI) and bytecode. Most tutorials tell us to copy-and-paste the ABI and bytecode from Remix, or even from the command line, but we’ve got better things to do. Let’s automate it in a few lines of JavaScript (install Node.js and npm if you’ve not yet):

  • Create a new JavaScript file, say deployContract.js.
  • Install the npm fs package: npm install fs. This allows the script to read your .sol file.
  • Install the npm solc package: npm install solc. Javascript bindings for the Solidity compiler allows the script to compile your .sol file.
  • Require fs and solc in your script
  • Read your flattened contract from Step 1. In this example Contract_flat.sol represents the flattened contract, and ../path/to/Contract_flat.sol its file path. You should replace this with your real flattened contract and file path:
  • Compile your flattened contract to get its bytecode and ABI. We use Contract to represent the main, unflattened contract. Replace this with its real name.
  • And there we have it: our bytecode and ABI! If you want to see what they look like, do console.log(bytecode) or console.log(abi) respectively. You could also write them to separate files if you want to.

3. Set up your Ethereum account and Infura API

If you already have an Ethereum account and run a full Ethereum node, skip this step. There are many ways to set up Ethereum accounts and access Ethereum nodes, and we’ve chosen the most painless ones:

  • Set up an Ethereum account using Metamask (https://metamask.io/). Choose an account and blockchain (e.g. Ethereum mainnet, Rinkeby testnet, Kovan testnet) you want to use and note down its private key and public address.
  • Register with Infura (https://infura.io/) and get your Infura API.

We’ve left most of the details to the excellent documentation in these tools. We’ll be using your private key, public address, and Infura API to deploy your smart contract.

4. Deploy your smart contract using the ethers.js library

We use the ethers.js library to deploy our contract. (web3.js is another popular library.) We’ll continue working in the same deployContract.js script from Step 2:

Recap from Step 2: we used fs and solc to read and compile our flattened Solidity contract.
  • Install the npm ethers package: npm install --save ethers
  • Require ethers in your script.
  • Specify the private key and public address of your chosen Ethereum (mainnet or testnet) account to instantiate an ethers.js Wallet. You can get this from Metamask in Step 3, or use your existing account.
The private key shown here is fake. You should never share your private key: this script lives only on your machine.
  • Deploy your smart contract!
Note that the default ethers gas limit is 1500000, but you can change this (that’s what deployTransaction.gasLimit is doing in this example). An upcoming version of ethers should estimate gas limit for you.

5. (Optional) Verify and publish your source code on Etherscan

Etherscan has become the de facto Ethereum block explorer, and it is optional but good practice to verify and publish your source code there.

To view your deployed contract, go to etherscan.io/address/<your_public_address>, where you’ll find a list of transactions linked to your account. (NB: if you used Rinkeby, go to rinkeby.etherscan.io/address/<your_public_address>, similarly for Ropsten or Kovan.)

View the list of transactions associated with your account on Etherscan.
  • Find the transaction where your contract was created and navigate to your contract address. Select the “Code” tab and click on the “Verify and Publish” link:
  • Copy and paste your flattened Solidity file from Step 1 into the box. Under the “Contract Name” field, enter the name of your main contract: from our example, simply Contract. Note that your Compiler version should match that of your main Contract, not your flattened Contract_flat.sol.
  • And we’re done! After your source code has been verified by Etherscan (i.e. it compiles to the same bytecode), you should see a green tick on the Code tab, and be able to view your Contract Source Code and ABI.

To interact with your contract, you can set up a contract instance and call its functions in your script. Read the ethers.js docs for more details, or stay tuned for our next tutorial!

--

--