Smart Contract with Golang

Aman Agarwal
Nerd For Tech
Published in
5 min readFeb 11, 2022

Interacting with smart contracts through golang

Aim

Our focus is to deploy as well as interact with smart contract’s functionality to make it accessible through golang API. So we can use it easily and talk to them firmly.

Remember the smart contract we created in our previous article, so we will be using the same contract to work on as we were supposed to. So let's see our contract again…

Let's have a quick recap on this contract:-

  • BalanceCheck is the name of the contract.
  • balance is a global variable that will be holding the total balance.
  • admin is another worldwide variable that will be holding the address of the admin who will be deploying this contract.
  • constructor executes at the very prior level and initializes all the values.
  • Balance function returns the current value to balance.
  • Deposit function updates the balance by the value sent along with the function call.
  • Withdrawl function does the basic work of withdrawing and reducing the balance by amount, but this power is only with the admin in our contract scenario.

Pre-requirements

This time we will be compiling contracts through Golang instead of remix, and we will be using Ganache for Ethereum virtual environments and accounts.

Ganache provides us with 10 accounts with a 100 ether balance in each. And we will also be seeing a complete blockchain of transactions through Ganache.

  1. Installing Ganache: https://trufflesuite.com/ganache
  2. Installing Solidity: https://docs.soliditylang.org/en/v0.8.2/installing-solidity.html#linux-packages
  3. Installing Geth: https://geth.ethereum.org/docs/install-and-build/installing-geth

Architecture

We will be using hexagonal architecture as we used in our other golang project.

The smart contract is in the contracts directory and our main.go will be found in the cmd directory.

To make it accessible through our go program, we need to go through some steps and create a go file embedded in outsmart contract.

Compiling

  • Compile the contracts and create a ABI file of our contract though solidity compiler which we install above, ABI is created through the following command in the build directory.
  • Now Create BIN file for the same contract through solidity compiler through this command in the build directory.

As of now we have both .abi and .bin file of the contract in the build directory.

  • Need to create a go file with whom we can do some conversation through our golang program, therefore we will be using both .abi and .bin file to create a go file through abigen and create it in our api directory through the following command.

The file created in api the folder is generated by binding we cant edit it, But in that file, we will find all the functions and member variables of the contract in golang structure and interactable mode.

How to connect with Smart-contract

  1. Connect with EVM(Ganache).
  2. Connect any account to make the transaction.
  3. Deploying a smart contract with an admin account.
  4. Creating endpoints.
  5. Connecting with smart contract functions.
  6. Using multiple accounts for transactions.

1. Connect with EVM (Ganache)

Ganache provides us test Ethereum environment, It comes with a user-friendly interface as well as a command-line interface we will be using GUI based which somewhere looks like this.

Ganache

It provides us with 10 accounts each with 100 ether to test, a couple of things that are use full are `RPC SERVER` which provide us the address through which we can connect with Ganache http://127.0.0.1:7545 .

Here we connected with ganache and created an object. Now with this object, we will be starting our conversation with EVM.

2. Connect any account to make the transaction

We will be connecting the above ganache’s accounts and using them for making any transaction. To do so we will be creating a function that will take client and account addresses and in return give us a Transaction object to use for all transactions.

In the above function, we fetch the private key from the account address and then takes the address to bind it with chain id of Ganache to make it continue the transaction object.

We also fetch last nonce of the account and then update the nonce value with other necessary params.

3. Deploying a smart contract with an admin account

After getting the auth we will deploy our contract with the contract’s go file Deploy api function with chelp of client and auth object as a parameter. It gives us deployed address of contract where we will be sending our all requests.

4. Creating endpoints

We will be using the echo framework to create restful API endpoints.

This endpoint will be our talking partner with our contract like we use them to interact with database.

5. Connecting with smart contract functions

To interact with our contract’s function we will be using deployed contract address and as always our ganache EVM's client to create API pointer which will be used as a connection object to connect with functions.

With the connobject we called the Balance function of our deployed contract.

6. Using multiple accounts for transactions

For making our transaction independent of accounts we will be taking account address in our param and then create auth with an updated nonce to make the transaction successful.

You can find complete source code here.

Let's test our creation

You can find a complete postman collection in the repo for a test or you can even use curl commands for testing provided below.

  • To check balance:-

curl --location --request GET 'http://localhost:1323/balance'

  • To check admin address:-

curl --location --request GET 'http://localhost:1323/admin'

  • To deposit 50 amount of value from account’s public address:-

curl --location --request POST 'http://localhost:1323/deposite/50' \

--header 'Content-Type: application/json' \

--data-raw '{ "accountPrivateKey":"fd4eef6dec5575cc78f3f14d4b749094f8b88ad7883caaa8d1d24e9a01e3732d" }'

  • To withdrawal 10:-

curl --location --request POST 'http://localhost:1323/withdrawl/50' \

--header 'Content-Type: application/json' \

--data-raw '{ "accountPrivateKey":"fd4eef6dec5575cc78f3f14d4b749094f8b88ad7883caaa8d1d24e9a01e3732d" }'

Implementation

We can use this concept to create a DAPP with a proper frontend and some database to keep records on some of the limitations and for extending scope.

Conclusion

We witness a working smart contract with golang. We interacted with our smart contract through golang API and covered everything from creating to compiling to deploying then interacting with our smart contract step by step. And at the end, we also tested different functionality. You can find the complete source code in my repo.

You can support me and my content by buying a coffee ☕ here.

Follow me on Twitter and LinkedIn.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Aman Agarwal
Aman Agarwal

Written by Aman Agarwal

Engineer | Explorer | Blockchain | Golang | JavaScript developer