Currently implemented:
- ERC20: Token Standard
- ERC165: Standard Interface Detection, several interface IDs
- ERC721: Non-Fungible Token Standard (NFT) with ERC721 Metadata and ERC721 Enumerable extensions.
- ERC777: Token Standard
- ERC1155: Multi Token Standard
Example usage: examples/erc20-erc721.go
Notes:
- Inspired by github.com/fxfactorial/defi-abigen (which has bindings for Aave, Chainlink price feed, Compound, Erc20, Onesplit and Uniswap)
- Based on OpenZeppelin contracts
Versions used to build the bindings:
go1.18.4
solc: 0.8.15+commit.e14f2714.Darwin.appleclang
abigen version 1.10.13-unstable
go-ethereum: v1.10.21
Accessing an ERC-721 (NFT) smart contract in Go:
package main
import (
"fmt"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/metachris/eth-go-bindings/erc165"
"github.com/metachris/eth-go-bindings/erc721"
)
func main() {
// Connect to a geth node (when using Infura, you need to use your own API key)
conn, err := ethclient.Dial("https://mainnet.infura.io/v3/API_KEY")
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
// Instantiate the ERC721 contract for Uniswap V3: Positions NFT
address := common.HexToAddress("0xC36442b4a4522E871399CD717aBDD847Ab11FE88")
token, err := erc721.NewErc721(address, conn)
if err != nil {
log.Fatalf("Failed to instantiate a Token contract: %v", err)
}
// Access token properties
name, err := token.Name(nil)
if err != nil {
log.Fatalf("Failed to retrieve token name: %v", err)
}
fmt.Println("Token name:", name)
// Invoke the ERC165 SupportsInterface method
supportsMetadata, err := token.SupportsInterface(nil, erc165.InterfaceIdErc721Metadata)
if err != nil {
log.Fatalf("Failed to retrieve supportsInterface: %v", err)
}
fmt.Println("Supports ERC721Metadata extension:", supportsMetadata)
}
Sources: https://github.com/metachris/eth-go-bindings/tree/master/contracts
Building:
# Install dependencies
yarn init -y
yarn add truffle @openzeppelin/contracts @chainsafe/truffle-plugin-abigen
# Compile and create ABIs
make clean
make bindings