ABI - Application Binary Interface. Go developers can then use the bindings to interact with the contract from their Go application without having to deal directly with data encoding and decoding. An ABI is generated when a contract is compiled.
Ethereum smart contracts have a schema that defines its functions and returns types as a JSON file. This JSON file is known as an Application Binary Interface, or ABI.
- The ABI acts as a specification for precisely how to encode data sent to a contract and how to decode the data the contract sends back.
- The ABI is the only essential piece of information required to generate Go bindings.
- Go developers can then use the bindings to interact with the contract from their Go application without having to deal directly with data encoding and decoding.
Geth includes a source code generator called abigen that can convert Ethereum ABI definitions into easy-to-use, type-safe Go packages. With a valid Go development environment set up and the go-ethereum repository checked out correctly, abigen can be built as follows:
go install github.com/ethereum/go-ethereum/cmd/abigen@latest
there are two recommend method to get ABI file of specify Contract.
- For exsisting Contract, go to eterscan, and search contract name like Uniswap V3 Factory, then type
Crtl + F
to findContract ABI
. - Using development framework like
Foundry
,Hardhat
,Brownie
or online IDERemix
. More detail command for DEFI contract abigen can be show below.
- Clone Uniswap V3 repo
git clone git@github.com:sugarshop/uniswap-v3-foundry.git
- complie contract using
Foundry
command
forge build
-
ABI file will be generated in out/UniswapV3Factory.sol/UniswapV3Factory.json and out/UniswapV3Pool.sol/UniswapV3Pool.json
-
update Go version and install abigen
go install github.com/ethereum/go-ethereum/cmd/abigen@latest
- abigen for Factory and Pool Contract
abigen --abi abi/uniswap/UniswapV3Factory.abi --pkg factory --type Factory --out contracts/uniswap/Factory.go
abigen --abi abi/uniswap/UniswapV3Pool.abi --pkg pool --type Pool --out contracts/uniswap/Pool.go