About this post
The Application Binary Interface (ABI) is a standarised way to interact with a contract in an Ethereum network. The ABI is typically implemented in JSON format and used in conjunction with web3.js Javascript libraries to interact with a deployed solidity contract. I’ll briefly cover this technique here.
In this post, I’ll present an alternative approach using Go based ABI in lieu of JSON/web3.js combination. For the purpose of illustration, I’ll be using a deployed contract, also referred to as a token, named Tronix on Ethereum Mainnet. I’ll be demonstrating the process of obtaining a balance of the token.
It is NOT the purpose of this post to avocate the use of Go instead of JSON/wbe3.js. The intention here is to demonstrate, if you choose to use Go, the steps involved. I’ll leave the choice of using Go over JSON/web3.js or vice-versa to you to decide.
Tronix Solidity and ABI/Web3.js
The full Tronix source code for this post is here. The truncated version of the code with emphasis on two methods is shown below.
The ABI equivalent in JSON/Web3.js is below:
Abigen tool
Before you create apps in Go, you need to install a tool known as abigen
. The process involve cloning the source code from https://github.com/ethereum/go-ethereum
and the running the command make gen
. Please refer to this post for details about building and using abigen tool.
Generating the Go ABI
Having built the abigen
tool, the next step is to generate the Go ABI binding from the solidity source code.
From this command, the Go ABI is generated and the full code is shown torntoken.go. The two main elements of the Go ABI are implemented as follows.
This is the implementation of the Decimals
method.
This is the implementation of the BalanceOf
method.
Using the Go ABI
When you have generated the ABI package, building a Go based app importing the ABI package and implementing a main package. To obtain the balance of Tronix
contract, here is a truncated example of a application main package:
Summing up
If you plan to create an app using Go based ABI, these are the steps:
-
Build the
abigen
tool from the go-ethereum project and install/build the Go solidity compiler. -
Use
abigen
tool to create a Go ABI package from your solidity code. -
Import your generated Go ABI package your main package.
To make the execution of the steps mentioned above easier, Open Consentia, an open source initiative, has created a framework named gosol
. Feel free to clone it from https://github.com/openconsentia/gosol and modify it to suit your requirements.
The pros and cons of using Go to build an application to interact with your contract, is beyomd the scope of this post. I shall leave that to you to decide.