This guide provides tutorials for using Geth to deploy and interact with smart contracts.
We can use solc
or tools like Hardhat or Foundry to generate the ABI and BIN files for our contracts.
- Download
solc
using solc-select:git clone https://github.com/crytic/solc-select.git cd solc-select ./solc-select install
- Install the desired Solidity version:
solc-select use 0.8.26 --always-install
- Navigate to the
contract
folder and run the command to generate the ABI and BIN files:solc --abi --bin -o build Storage.sol
- Install the
go-ethereum
package, which is required for theabigen
tool:go get github.com/ethereum/go-ethereum
- Install the
abigen
tool:go install github.com/ethereum/go-ethereum/cmd/abigen@latest
- Create a folder named
storage
for our Go package:mkdir storage cd storage
- Run the
abigen
command in thestorage
folder to generate the Go files for thestorage
package:abigen --abi=../contract/build/Storage.abi --bin=../contract/build/Storage.bin --pkg=storage --out=storage.go
The generated storage.go
file provides the DeployStorage()
function to deploy the contract, as well as functions to interact with it.
To call the store()
function in the Storage
contract, use the generated Store()
function in the storage.go
file.
Refer to the example in storage/storage_test.go
for contract deployment and interaction.
In this test, we use Anvil to start a local ethereum testnet node. Ensure you have Foundry installed before running the test:
make test
- Testcontainers is used for the tests. so we need to start docker before running the tests.
- The Testcontainers fetches the the foundry docker image from
ghcr.io/foundry-rs/foundry
. The fetch may fail with errorno matching manifest for linux/arm64/v8 in the manifest list entries
on Apple Silicon chips. You may need to manully pull the image withdocker pull ghcr.io/foundry-rs/foundry --platform linux/x86_64
first if you are on a Apple Silicon Chip and encountered the error. - Run
make clean
if you encounter any Go module or package load errors.