Currently implemented:
- ERC20: Token Standard
- ERC165: Standard Interface Detection
- ERC721: Non-Fungible Token Standard (NFT) with ERC721 Metadata and ERC721 Enumerable extensions.
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.16.3
abigen version 1.10.4-unstable
solc: 0.8.4+commit.c7e474f2.Emscripten.clang
In Go:
package main
import (
"fmt"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/metachris/erc721-go-bindings/erc165"
"github.com/metachris/erc721-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)
}
Get dependencies and run:
go mod tidy
go run .
Source: Erc721.sol
Build the contract:
# Install dependencies
yarn init -y
yarn add truffle @openzeppelin/contracts @chainsafe/truffle-plugin-abigen
yarn truffle compile
yarn truffle run abigen Erc20 Erc165 Erc721
abigen --bin=abigenBindings/bin/Erc20.bin --abi=abigenBindings/abi/Erc20.abi --pkg=erc20 --out=erc20/erc20.go
abigen --bin=abigenBindings/bin/Erc165.bin --abi=abigenBindings/abi/Erc165.abi --pkg=erc165 --out=erc165/erc165.go
abigen --bin=abigenBindings/bin/Erc721.bin --abi=abigenBindings/abi/Erc721.abi --pkg=erc721 --out=erc721/erc721.go