/eth-go-bindings

Go bindings for Ethereum smart contracts: ERC20, ERC165 and ERC721, ERC777, ERC1155

Primary LanguageGoMIT LicenseMIT

Go bindings for Ethereum smart contracts

Currently implemented:

Example usage: examples/erc20-erc721.go

Notes:

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

Quickstart

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)
}

Smart contracts & building them

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