In order to interact with a Smart Contract in a Go application, the ABI (application binary interface) of the contract must first be generated, and then compiled so that it can be imported in said application.
Also known as solc
, follow the instructions here to install it.
abigen
This is the tool used to generate the .go
files with the contract ABI as input.
protoc
tool (dependency)Check latest stable version and replace PROTOC_ZIP
accordingly.
PROTOC_ZIP=protoc-x.x.x-linux-x86_64.zip
curl -OL https://github.com/google/protobuf/releases/download/vx.x.x/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local include/*
rm -f $PROTOC_ZIP
abigen
toolInstall abigen
by doing:
go get -u github.com/ethereum/go-ethereum
cd $GOPATH/src/github.com/ethereum/go-ethereum/
make
make devtools
Save the Smart Contract ABI to your local drive and keep the location at hand.
If you do not have an ABI, you can generate one from the contract replacing the .sol
file with your own smart contract filename:
solc --abi Contract.sol -o build
For this example, the resulting file will be ./build/Contract.abi
.
To convert the ABI into a Go file, execute the following command:
abigen --abi=./build/Contract.abi --pkg=mypackage --out=Contract.go
Replace the contract and package names with your own.
In this fashion, the RNS Resolver was compiled by doing:
abigen --abi=MultiChainResolverABI.json --pkg=multichainresolver --out=multi_chain_resolver.go
and then imported to and used in the rns-go-lib
project.
Guide based on Smart Contract Compilation & ABI by Miguel Mota.
Go to top