Skip to main content

Deploy with Go-ethereum

Introduction

Go-ethereum (Geth) is indeed the official implementation of the Ethereum protocol in Go. Its popularity stems from its robustness, performance, and active development community. Geth provides a comprehensive set of functionalities for interacting with the Ethereum blockchain, including creating and managing accounts, sending transactions, deploying and interacting with smart contracts, querying blockchain data, and more.

Developers often choose Geth for Ethereum-based application development in Go due to its efficiency and the familiarity of the Go programming language. Its extensive API allows developers to build complex decentralized applications (dApps) and blockchain-based solutions easily.

Overall, Geth is a powerful tool for developers building applications on the EVM based blockchains using the Go programming language.

Prerequisites

  • An EVM-compatible wallet, such as MetaMask connected to Devnet
  • A balance in Devnet NEON
  • NodeJS 16.x or newer
  • The latest Go version
  • Solidity compiler less than or equal to v0.8.24 (Neon EVM supports solidity versions less than or equal to v0.8.24)

Go installation

important

If your machine already has Go installed, then please proceed to the next step.

  1. Download the latest Go version from https://go.dev/doc/install.
  2. Create a directory GoProjects for your Go project development on your machine. Please run pwd inside GoProjects directory to get the full path. This will be used in Step 4 to set the $GOPATH env variable.
  3. Create 3 directories /GoProjects/src, /GoProjects/pkg and /GoProjects/bin.
  4. Set up the $GOPATH env variable on your machine -
  • Run nano ~/.zshrc on Mac machines.
  • Run nano ~/.bash_profile on Linux machines.
  • Paste the following lines -
export GOPATH=<PATH_TO_YOUR_GO_PROJECTS_DIRECTORY>
export PATH=$GOPATH/bin:$PATH
  1. Save your ~/.bash_profile or ~/.zshrc file.
  • Run source ~/.zshrc on Mac machines.
  • Run source ~/.bash_profile on Linux machines.
  1. Run echo $GOPATH to check if the GOPATH is set correctly in the machine.

How to deploy contracts using go-ethereum

This tutorial is based on the Github example and walks you through the steps of deploying the following smart contracts on Neon EVM Devnet-

  • Simple storage contract.
  • ERC20 token contract.

Step 1: Installation

info

Neon EVM doesn't support the latest JSON-RPC specifications. Therefore Neon EVM only supports go-ethereum versions less than or equal to 1.12.2.

1.1 Navigate to the src directory of your $GOPATH:

cd $GOPATH/src

1.2 Clone the example go-ethereum project from the remote repository and navigate to it:

git clone https://github.com/neonlabsorg/go-ethereum-tutorial.git
cd go-ethereum-tutorial

Note: All the following commands should be executed inside go-ethereum-tutorial folder.

1.3 Install the required libraries:

npm install
go install github.com/ethereum/go-ethereum/cmd/abigen@latest
go mod tidy

Step 2: Set up an environment file

info

This step requires an EVM-compatible wallet such as MetaMask, connected to Neon Devnet, with a balance in Devnet NEON available from NeonFaucet.

The following tutorials assist you to meet these prerequisites:

Or connect an existing wallet to Devnet :::

2.1 Obtain the private key for your wallet account.

To obtain the private key from MetaMask, from the hamburger menu, click Account Details > Show Private Key, enter your password, and click Confirm for access to the private key for that account.

2.2 Rename .env.example to .env and place your private key inside it:

PRIVATE_KEY=YOUR_PRIVATE_KEY
NEON_RPC_DEVNET=https://devnet.neonevm.org
NEON_RPC_MAINNET=https://neon-proxy-mainnet.solana.p2p.org
important

Replace YOUR_PRIVATE_KEY with your data.

2.3 Run:

source .env

Step 3: Generate the Go bindings for the smart contracts

3.1 Run the following commands to generate the smart contract ABI and bytecode:

solc --abi ./contracts/Storage.sol -o build
solc --bin ./contracts/Storage.sol -o build

3.2 Run the following command to generate the smart contract go binding inside contractsgo folder:

abigen --abi ./build/Storage.abi --pkg contractsgo --type Storage --out ./contractsgo/Storage.go --bin ./build/Storage.bin

Step 4: Deploy and run smart contract functions

important

Go applications can only have one main.go file which is the entry point to execute the functions from the .go files in other packages. To run only the Storage contract, please comment out deploy.RunTestERC20Contract() in main.go.

4.1 Run the following command to deploy the Storage contract, store a value in the deployed smart contract and reading the stored value from the deployed smart contract:

go run main.go

4.2 After successfully running this step you should get console output similar to:

You are now connected to Neon EVM Devnet
The NEON balance of the account is: 310387553758242748088682
------------------------------------------------------------------------
Deploying Storage contract...
The contract is deployed at address: 0x6b6Ba862e2bBc0C1305DF681d45f16a1D6F57baf
Transaction hash: 0xf84667ce0bd5d2da4dfcf81fe9c72bdc81c207a41a3c9baa4c43e9ebb6ae1b6e

You are now connected to Neon EVM Devnet
The NEON balance of the account is: 310383249542814769793482
------------------------------------------------------------------------
Storing value in the Storage contract...
Estimated gas: 25000
Transaction hash: 0x24e5af83df1e9f1536d684c08e903d1285f1f5e484df43d4616c925bb25ec9a9

You are now connected to Neon EVM Devnet
The NEON balance of the account is: 310383247282862115123482
------------------------------------------------------------------------
Reading value from the Storage contract...
Returned value: 45
Was this page helpful?