miguelmota / ethereum-development-with-go-book

📖 A little guide book on Ethereum Development with Go (golang)

Home Page:https://goethereumbook.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

abigen for complex contract example

huahuayu opened this issue · comments

Hi, for all the example within the book, the contract is simple. What if the contract is a complex one(with many libraries and helper contract in it)?

Let's say, uniswap StakingRewards contract https://etherscan.io/address/0xCA35e32e7926b96A9988f61d510E038108d8068e#code

Can you demonstrate how to interact with it? Many abi and bin file will generate, but specifically, I want interactive with the contractStakingRewards.

Also, I try to interact with TetherToken https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code

mkdir usdt
cd usdt
# touch TetherToken.sol and put code in it
solc --abi TetherToken.sol --out abi
solc --bin TetherToken.sol --out bin
abigen --bin=bin/TetherToken.bin --abi=abi/TetherToken.abi --pkg=usdt --out=usdt.go

then I get an error

Fatal: Failed to generate ABI binding: duplicated identifier "totalSupply"(normalized "TotalSupply"), use --alias for renaming

for the uniswap StakingRewards contract, I have invoke it successfuly

But for tether token, abigen error: use --alias for renaming I don't know how to solve it

Looks like the generated ABI has a totalSupply method as well as a _totalSupply method so the problem is the generator will pascal case the names and remove special characters so they both become normalized to TotalSupply which creates an ambiguity conflict.

[
  {
    "constant": true,
    "inputs": [],
    "name": "totalSupply",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "payable": false,
    "type": "function"
  },
  ...
  {
    "constant": true,
    "inputs": [],
    "name": "_totalSupply",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "payable": false,
    "type": "function"
  },
]

The solution is to use the --alias flag to change the name of one the ABI method names to something else so there's no conflict:

abigen --bin=bin/TetherToken.bin --abi=build/TetherToken.abi --pkg=usdt --out=usdt.go --alias _totalSupply=TotalSupply1

@miguelmota got it, thanks for the explain!