Skip to content

Commit dd5cd99

Browse files
feat: Golang example (#2584)
* feat: Golang example Signed-off-by: Mariusz Jasuwienas <[email protected]> * feat: implementing review suggestions from acuarica Signed-off-by: Mariusz Jasuwienas <[email protected]> --------- Signed-off-by: Mariusz Jasuwienas <[email protected]>
1 parent 238ce54 commit dd5cd99

File tree

10 files changed

+926
-0
lines changed

10 files changed

+926
-0
lines changed

tools/golang-example/.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# The operator private keys start with "0x" and are in hexadecimal format
2+
# Your account ECDSA hex-encoded private key (Ex: 0xb46751179bc8aa9e129d34463e46cd...)
3+
OPERATOR_PRIVATE_KEY='OPERATOR_PRIVATE_KEY'

tools/golang-example/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.env
2+
3+
# Go binary, generated by build command:
4+
hedera-golang-example-project

tools/golang-example/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Hedera Golang Example Project
2+
3+
This Hedera Golang Example Project offers boilerplate code for testing and deploying smart contracts via golang ethereum. It can be configured to communicate with both community-hosted and local ([Hedera Local Node](https://github.com/hashgraph/hedera-local-node)) instances of the [Hedera JSON RPC Relay](https://github.com/hashgraph/hedera-json-rpc-relay).
4+
5+
This project utilizes the ethereum/go-ethereum library to prepare the connection client. Please note that there are alternative libraries available for the Go language, such as ethrpc.
6+
7+
## Project Files and Folders
8+
9+
- `/contracts` - This folder holds:
10+
- All the Solidity smart contract files that make up the core logic of your dApp. Contracts are written in `.sol` files.
11+
- Go files generated with `abigen`, which are used to interact with the smart contracts from the Go code. These files include Go bindings for the Solidity contracts, allowing you to call smart contract methods directly from your Go application.
12+
13+
### How to Generate Go Files with `abigen`
14+
15+
The `/contracts` folder currently contains Go files generated with the `abigen` tool. These files are used to interact with the Solidity contracts. Here’s how the Go files were generated:
16+
17+
1. **Install Solidity Compiler (`solc`)**:
18+
- Install `solc` by following the instructions in the [Solidity documentation](https://docs.soliditylang.org/en/latest/installing-solidity.html).
19+
- On Ubuntu, you can run:
20+
```sh
21+
sudo apt install solc
22+
```
23+
24+
2. **Install `abigen`**:
25+
- Install `abigen` using the Go toolchain:
26+
```sh
27+
go install github.com/ethereum/go-ethereum/cmd/abigen@latest
28+
```
29+
30+
3. **Generate ABI and Binary Files**:
31+
- Compile your Solidity contract to generate the ABI and binary files. For example, for a contract named `Greeter.sol`:
32+
```sh
33+
solc --abi Greeter.sol -o .
34+
solc --bin Greeter.sol -o .
35+
```
36+
37+
4. **Generate Go Bindings**:
38+
- Use `abigen` to generate the Go bindings:
39+
```sh
40+
abigen --bin=Greeter.bin --abi=Greeter.abi --pkg=greeter --out=Greeter.go
41+
```
42+
43+
The `Greeter.go` file will was created in the `/contracts` folder and can be used in your Go application to deploy and interact with the `Greeter` Smart Contract.
44+
45+
## Requirements
46+
Install go: https://go.dev/doc/install
47+
48+
## Setup
49+
50+
1. Clone this repo to your local machine:
51+
52+
```shell
53+
git clone https://github.com/hashgraph/hedera-json-rpc-relay.git
54+
```
55+
56+
2. Once you've cloned the repository, open your IDE terminal and navigate to the root directory of the project:
57+
58+
```shell
59+
cd hedera-json-rpc-relay/tools/golang-example
60+
```
61+
62+
3. Run the following command to install all the necessary dependencies:
63+
64+
```shell
65+
go get github.com/ethereum/go-ethereum/ethclient \
66+
github.com/ethereum/go-ethereum \
67+
github.com/ethereum/go-ethereum/accounts/abi/bind \
68+
github.com/ethereum/go-ethereum/crypto \
69+
github.com/joho/godotenv
70+
go get -t hedera-golang-example-project
71+
```
72+
73+
4. Get your Hedera testnet account hex encoded private key from the [Hedera Developer Portal](https://portal.hedera.com/register) and update the `.env.example` `OPERATOR_PRIVATE_KEY`
74+
75+
5. Copy `.env.example` to `.env`
76+
77+
6. Run the test script from the root directory of the project. The default network is set to "testnet."
78+
79+
```shell
80+
go test -v
81+
```
82+
83+
7. Run the following command to deploy the smart contract and run setGreeting / greet methods on it.
84+
```shell
85+
# builds the script
86+
go build .
87+
88+
# runs the script on mainnet
89+
./headera-golang-example-project --mainnet
90+
91+
# runs the script on testnet
92+
./headera-golang-example-project
93+
94+
# runs the script on previewnet
95+
./headera-golang-example-project --previewnet
96+
```

0 commit comments

Comments
 (0)