Create and Deploy Common Smart Contracts in 30 Seconds —ERC20, ERC721

Travis Reeder
GoChain

--

If you want to deploy one of the common, standard smart contracts such as ERC-20 or ERC-721, now you can do it in literally 30 seconds. This works on Ethereum, GoChain and any other web3 based network. Here’s how.

First, be sure to install the web3 CLI tool and be sure to set your key and which network you want to deploy to (see the web3 README), then just follow the instructions below.

Generate Contract

Let’s generate an ERC-20 contract:

web3 generate contract erc20 --name "Test Tokens" --symbol TEST --mintable

There are other options you can pass in such as --pausable to enable pausing in your contract, and several others. This will generate you a brand spanking new contract at TEST.sol ready to be deployed. The generated contract uses OpenZeppelin contracts so you can be sure these are secure and industry standard.

You can look at the code and modify it if you’d like, but for now, let’s just get it deployed.

Deploy Contract

web3 contract build TEST.sol
web3 contract deploy TESTToken.bin

This will return:

Contract address is: 0xC2b5774944a95a1008129e9983220DCFa29D60d3

Which is the address of your new contract that you’ll give to people to interact with it.

I with there were more to write here, but… there isn’t. That’s all there is to it! You now have an ERC-20 token live and ready to go!

Interacting with the Contract

Now of course, you can interact with it and integrate it with your application. We’ll interact with it using web3 CLI. To make the rest of the commands easier, set the contract address in an environment variable:

export WEB3_ADDRESS=0xC2b5774944a95a1008129e9983220DCFa29D60d3

You can also pass in the address to each command using --address .

Here’s a few read functions to get information about the contract:

web3 contract call --abi TESTToken.abi --function name
# Call results: Test Token
web3 contract call --abi TESTToken.abi --function decimals
# Call results: 18
web3 contract call --abi TESTToken.abi --function totalSupply
# Call results: 0

Now let’s mint some tokens (this is what you’d do if you were doing an ICO and wanted to sell some):

web3 contract call --abi TESTToken.abi --function mint 0xe893d5415C87962eb6c05e7e7e9bc924446aA119 1000000000000000000000

The last two parameters are for the mint function that looks like this:

function mint(address to, uint256 value)

For the to parameter, use an address you own so you can transfer them later. The value parameter looks really big, but since this contract uses 18 decimals (most common), it’s really only 1000 tokens.

Now if we check totalSupply again, it will be what we just minted.

web3 contract call --abi TESTToken.abi --function totalSupply
# Call results: 1000000000000000000000

Check out the web3 tool to make developing DApps way, way easier

--

--

Travis Reeder
GoChain

Founder, CTO at GoChain - Building and breaking things