Blockchain on Golang Part 4 : Smart Contract Compilation

Ajiyba Nesij Korkmaz
5 min readJun 11, 2023

Smart contracts are contracts written in code that automate complex processes, such as managing digital assets, without intermediaries.

This article provides a step-by-step guide to compiling a Smart Contract on a Linux machine. It includes instructions on downloading and installing the necessary tools, preparing the Smart Contract, downloading the OpenZeppelin libraries, generating binary and ABI files, and generating Go bindings for the Smart Contract. This guide is intended for developers who want to create Smart Contracts for the Ethereum blockchain using the Solidity programming language and Go programming language.

If you’re interested in learning more about blockchain on Golang, check out my articles that provide a step-by-step guide on creating a blockchain wallet, balancing, and transferring using Golang:


  • Linux
  • Golang 1.19 or latest version

Step 1: Download Ethereum Libraries and Install Solc

The provided commands are used to add the Ethereum repository, update package information, and install the Solidity compiler (Solc) on a Linux system. The Solc compiler is used to compile Solidity code into bytecode that can be executed on the Ethereum Virtual Machine (EVM). By installing Solc, you have the necessary tool to compile your Solidity smart contracts.

sudo add-apt-repository ppa:ethereum/ethereum 
sudo apt-get update
sudo apt-get install solc

Step 2: Install Go-Ethereum Library and Install Abigen

You need to install the Go-Ethereum library. The Go-Ethereum library is a widely-used library for interacting with the Ethereum blockchain. It provides a command-line interface for managing Ethereum nodes, as well as a set of APIs for interacting with the blockchain programmatically.

To install the Go-Ethereum Library, clone the library from the Github repository:

git clone <>
cd go-ethereum

In the directory, install geth by running the command “make geth”. This compiles the geth binary and places it in the “build/bin/” directory:

make geth

After installing the Go-Ethereum library, you also need to install abigen. Abigen is a tool used to generate Go bindings for Ethereum smart contracts. It is included in the Go-Ethereum library and can be installed together with the library.

By following these steps, you will have installed the necessary tools required for compiling your smart contract.

Step 3: Prepare Smart Contract

This code snippet represents a smart contract written in Solidity programming language. The contract is named TestToken and is an ERC20 token contract. ERC20 tokens are a standard for fungible tokens on the Ethereum blockchain. The contract inherits from the ERC20 token standard and the Ownable contract. The Ownable contract ensures that only the contract owner can perform certain actions.

The function named “mint” is defined to mint new tokens, and it can only be called by the contract owner. Token minting is the process of creating new tokens and adding them to the total supply of the token. The contract is initialized with a name “TestToken” and a symbol “TT”. The naming conventions and symbol used for the token are important for identification and differentiation from other tokens on the blockchain.

In summary, this code represents a basic ERC20 token smart contract written in Solidity programming language and is licensed under the MIT license.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract TestToken is ERC20, Ownable {
constructor(address initialOwner) Ownable(initialOwner) ERC20("TestToken", "TT") {
// Additional initialization code for your TestToken contract
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);

Step 3: Download @openzeppelin Libraries

This is a step-by-step instruction to download the OpenZeppelin libraries for smart contract development. Clone the OpenZeppelin repository using git, and then move the downloaded repository to a directory named “@openzeppelin” for easy imports in their smart contract code.

git clone <>
mv openzeppelin-contracts @openzeppelin

Step 4: Generate Binary and Abi Files Using .sol File

In this step, generate binary and ABI files by compiling the Smart Contract (.sol) file. Use the solc command to compile the .sol file and create two output files: a binary file that contains the bytecode of the contract, and an ABI (Application Binary Interface) file that defines how the contract can be interacted with. The -o flag specifies the output directory where the generated files will be saved.

solc --bin TestToken.sol --abi TestToken.sol -o build

Step 5: Generate .go File Using Binary and Abi files

To deploy a smart contract on the Ethereum blockchain, it needs to be compiled into bytecode that can be executed by the Ethereum Virtual Machine (EVM). This bytecode is stored in a binary file with a .bin extension. Additionally, the smart contract interface needs to be described in an Application Binary Interface (ABI) file, which defines how the contract can be interacted with by other contracts or external users.

Once you have generated the binary and ABI files for your smart contract, use the abigen tool to generate Go bindings for the smart contract. Go bindings are a way to interact with the smart contract from Go programming language. The abigen tool generates Go code that defines the functions and data structures used to interact with the smart contract.

The command to generate the Go bindings for the smart contract is:

abigen --bin=./build/TestToken.bin --abi=./build/TestToken.abi --pkg=TestToken --out=TestToken.go

Here, --bin specifies the location of the binary file, --abi specifies the location of the ABI file, --pkg specifies the name of the Go package to use for the generated code, and --out specifies the name of the output file.


The resulting file, TestToken.go(click here), can be imported into your Go code to interact with the smart contract. The generated Go code provides a type-safe interface to the smart contract functions and data, making it easy to write robust and reliable smart contract interactions.


By following these steps, you are now equipped with the necessary tools and files to deploy and interact with your smart contract on the Ethereum blockchain. Remember to adapt the instructions and code to your specific development environment and requirements.

Blockchain development in Golang offers exciting opportunities, and with this guide, you have taken the first steps towards building powerful and decentralized applications on the Ethereum platform.

Happy coding and exploring the world of blockchain technology!