Travis Reeder
Feb 19 · 3 min read

If you’ve deployed a smart contract like an ERC-20 token contract or ERC-721 non-fungible token contract, then you may have used OpenZeppelin’s great open source smart contracts to do it. They’ve been audited and battle-tested in the real world so if you aren’t using them to do your contracts, you probably should be.

If you’ve deployed a smart contract before, you may have used Truffle Framework to build and deploy the contracts since that’s what it says to use in their docs. The web3 CLI tool is an alternative that does not require any config files or JavaScript, just straight CLI commands.

Deploy an ERC-20 Contract in 2 Minutes

First, install the web3 command line tool which you’ll use throughout this article:

curl -LSs https://raw.githubusercontent.com/gochain-io/web3/master/install.sh | sh

The following is a simple ERC20 contract using OpenZeppelin. Copy the code and paste into a file called token.sol:

Get the OpenZeppelin libraries. In the same directory as token.sol, run:

git clone --depth 1 --branch master https://github.com/OpenZeppelin/openzeppelin-solidity lib/oz
rm -rf lib/oz/.git

Now build your contract.

web3 contract build token.sol

This will compile your contract and generate two files: CappedToken.bin and CappedToken.abi.

Let’s deploy it to a local node to test it out first. Start a local GoChain node for development (remember this is 100% Ethereum compatible so you can still use this for testing even if you plan to deploy to Ethereum later).

web3 start

This will start a GoChain node, automatically generate a new private key for you, and fund it with GO. You can then use that private key to deploy and use your node immediately. Copy the private key from the output and use that in the export below:

export WEB3_NETWORK=localhost
export WEB3_PRIVATE_KEY=[YOUR PRIVATE KEY]

Deploy the contract:

web3 contract deploy CappedToken.bin

Boom! It’s deployed. Easy huh? This will return the address of your contract. Copy the address and run:

export WEB3_ADDRESS=[CONTRACT_ADDRESS]

Now you can interact with your contract. Since this is an ERC-20 contract, let’s mint some tokens for yourself and then transfer some of those to another address.

web3 contract call --abi CappedToken.abi --function mint $(web3 myaddress) 1000000000000000000000

Now we can check our balance to make sure it worked.

web3 contract call --abi CappedToken.abi --function balanceOf $(web3 myaddress)

That should return: “Call results: 1000000000000000000000”. which means you have 1000 tokens! (1000* 10¹⁸, ERC-20 contracts typically use 18 decimal places so the number look pretty massive).

To transfer to another account, let’s first create a second account:

web3 account create

Copy the public address that returns then call:

web3 contract call --abi CappedToken.abi --function transfer [NEW_ADDRESS] 1000000000000000000

Now you can check your balance again and see that it went down and check the balance of the new account to see that there’s went up. Boom.

Ready for Production

Buy some GO to deploy to GoChain or ETH to deploy to Ethereum. Then set your network and your private key that has the real GO or ETH.

Deploy to GoChain:

export WEB3_NETWORK=gochain
export WEB3_PRIVATE_KEY=[YOUR GOCHAIN PRIVATE KEY]
web3 contract deploy CappedToken.bin

Or deploy to Ethereum:

export WEB3_NETWORK=ethereum
export WEB3_PRIVATE_KEY=[YOUR ETHEREUM PRIVATE KEY]
web3 contract deploy CappedToken.bin

You are now officially a DApp developer

No matter what your contract does, you can follow the same process to deploy and interact with it. Let us know what you think or how we can make the web3 tool better!


More about GoChain at https://gochain.io

GoChain

100% Ethereum Compatible, 100x Faster - https://gochain.io

Travis Reeder

Written by

CTO at GoChain - Advisor at Salesforce - Building and breaking things

GoChain

GoChain

100% Ethereum Compatible, 100x Faster - https://gochain.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade