Creating a simple Ethereum Smart Contract in Golang
By: Owen Yuwono
Tags: Solidity, Smart Contracts, Ethereum, Golang, Blockchain

With all the hype around NFTs (which I will explain later in the next story), I figure it would be awesome to help you guys kickstart your first hello world Ethereum smart contract in your local machine.

This article will help you create your first Smart Contract using Solidity, and a backend that communicates with it using Golang:

Creating a development workflow on your local machine

Creating a simple Smart Contract using Solidity

Deploying a Smart Contract using go-ethereum

Creating a backend that communicates with the blockchain using go-ethereum

Preface

Update: Some of my friends asked me why don’t we use truffle and the javascript stack. While I think truffle is a really amazing tool to use, this article uses Golang for these considerations:

Truffle abstracts a lot of commands, so for education reason using Golang and Geth will interact and understand more with the Solidity cli.

The latest Solidity version Truffle supports is version 5, while we are using open zeppelin and eventually will use the ERC1155 token, which is only available on Solidity version 8.

Lastly, due to personal reason: because I am more experienced with Golang, and I personally prefer the type-safety of Golang compared to javascript.

Installation

Before we start, we would need to install these tools first.

Solidity, the language we use to build a smart contract.

Golang, the language we use to build the backend that communicates with the blockchain.

Geth, the Ethereum client for Golang.

Ganache or ganache-cli, a blockchain network that runs on your local machine.

Creating a Smart Contract

This simple Smart Contract, called MySmartContract contains 2 read-only functions which denoted by the view keyword.

Compiling the Smart Contract

$ solc --optimize --abi ./contracts/MySmartContract.sol -o build

$ solc --optimize --bin ./contracts/MySmartContract.sol -o build

$ abigen --abi=./build/Core.abi --bin=./build/MySmartContract.bin --pkg=api --out=./api/MySmartContract.go

I put my MySmartContract.sol file inside /contracts directory, so that the path I put into the solc command is ./contracts/MySmartContract.sol . If you put your Smart Contract in the root folder, the path should be ./MySmartContract.sol . Also the --optimize keyword is optional, but it optimizes your Smart Contract so it will cost fewer Ethers when being deployed into the blockchain.

abigen is a command-line function included in Geth installation, it is used for generating Golang interface from a compiled Solidity Smart Contract.

Deploying to the Blockchain

Before we start deploying our smart contract, we need to configure a few things first. First of all, start up your Ganache client using either UI or CLI.

Ganache UI

Ganache CLI

There are two things you need from Ganache: Host and Port, and a Private key of any which is available on Ganache.

After getting those two things, use this script below to create a deployment script, in which ethclient.Dial() function at line 2 below, supply the host and port as the parameter, it’s usually either http://127.0.0.1:7545 or http://127.0.0.1:8545 . And then, insert your private key into crypto.HexToECDSA() function’s argument at line 7 below.

Lastly, we should run this script using go run deploy.go . Function fmt.Println(address.Hex()) at line 48 will return an Ethereum address. This address is where your smart contract belongs. Take note of this address and we will use this to communicate with the blockchain in the next section.

Creating a Backend that Communicates with the Blockchain

First of all, create a go mod file, for this project I’ve used this go mod init github.com/owenyuwono/eth-contract . Also, we will be using echo for the http router (you can use your favorite http router as you please), and go-ethereum for the Golang Ethereum client.

Supply ethclient.Dial() with the same host and port as in the Deploy section. and also replace "CONTRACT_ADDRESS" with the address we received after the deployment finished.

Function api.NewApi(common.HexToAddress("CONTRACT_ADDRESS"), client) which is imported from the generated Golang interface from abigen connects your backend into a deployed contract in the Ethereum blockchain.

Finally, run the program and:

curl -X GET "http://localhost:1323/hello" will return Hello World .

will return . curl -X GET "http://localhost:1323/greet/owenyuwono" will return owenyuwono .

will return . curl -X GET "http://localhost:1323/greet/greetings" will return greetings .

will return . curl -X GET "http://localhost:1323/greet/randomtextforthequeryparameter" will return randomtextforthequeryparameter .

What’s Next?

If you’ve reached this part, then congratulations, you have successfully created your first Ethereum smart contract.

Next, we will learn more about Fungible Tokens (ERC-20), Non-Fungible Tokens (ERC-721), and eventually ERC-1155 interfaces. Stay tuned!

Thanks for reading, I hope this article helps you venture into Ethereum Blockchain easier.