Interacting with Ethereum Blockchain Using Go (Part 3)

·

Introduction

In the previous article (Interacting with Ethereum Blockchain Using Go (Part 2)), we covered generating private keys with go-ethereum and signing transactions. This article explores smart contract operations on Ethereum nodes, including reading public variables, handling transactions, and processing events.

Note: This article assumes prior knowledge of establishing a connection to an Ethereum node. For setup guidance, refer to this tutorial.
Go development environment setup is not covered here—search online for guides.

Smart Contract Basics

To interact with Ethereum nodes externally, use JSON-RPC APIs. Key methods for smart contracts include:

  1. Reading state values: eth_call
  2. Modifying state/deploying contracts: eth_sendRawTransaction or eth_sendTransaction (requires signing only for state changes)
  3. Event listening: Filter APIs like eth_newFilter

Smart contract function calls involve encoding the function name and parameters. Encoding rules distinguish between dynamic and static parameters—details in the Solidity ABI documentation.

Pro Tip: Contract transactions with value must target payable functions. For deployments, ensure the fallback function is payable.

Setting Up Solidity Development

Step 1: Install Solidity Compiler

npm install -g solc
Rename solcjs to solc for compatibility with abigen.

Step 2: Compile Contract Files

solcjs --bin --abi HW.sol EIP20.sol SafeMath.sol EIP20interface.sol

Step 3: Install abigen

go get go-ethereum
cd $GOPATH/src/github.com/ethereum/go-ethereum/
make devtools

Step 4: Generate Go Bindings

abigen --bin HW_sol_HW.bin --abi HW_sol_HW.abi --pkg contract --out HW.go

ERC20 Token Example

1. Deploying an ERC20 Token

Method A: Manual Transaction Creation

data := common.FromHex(Contract.ContractBin)
tx := types.NewContractCreation(nonce, amount, gasLimit, gasPrice, data)
signedTx, _ := types.SignTx(tx, signer, privKey)

Method B: Using DeployContract

txOpts := makeTxOpts(from, non, amount, gasPrice, gasLimit, privKey, 0)
contractAddress, deployTx, contract, err := Contract.DeployContract(txOpts, client.EthClient)
👉 Learn more about contract deployment

2. Fetching Token Balance

contract, _ := Contract.NewContract(contractAddress, client.EthClient)
balance, _ := contract.BalanceOf(nil, from)
fmt.Printf("Balance: %d\n", balance)

3. Listening to Transfer Events

WebSocket Approach

ch := make(chan *Contract.ContractTransfer)
sub, _ := contract.WatchTransfer(nil, ch, nil, nil)

Filter Logs (Fallback)

logs, err := contract.FilterTransfer(filterOpts, nil, nil)

4. Transferring Tokens

txOpts := makeTxOpts(from, non, amount, gasPrice, gasLimit, privKey, 0)
transferTx, err := contract.Transfer(txOpts, toAddress, bal)

Conclusion

This guide demonstrated smart contract interactions using go-ethereum, covering deployment, balance checks, event listening, and token transfers. For full code, visit the GitHub repository.


FAQs

Q1: Why use eth_call for reading state?

It’s a non-state-changing operation that doesn’t require gas or signing.

Q2: How to handle transaction errors?

Check for revert reasons using revertReason in the transaction receipt.

Q3: Can I deploy without a private key?

No—deployments require signed transactions.

👉 Explore advanced Ethereum tools