Categories
Cryptocurrency Ethereum Mastering Development

Abigen doesn’t create free data retrieval call for public state variable

I’m looking at an example contract using go-ethereum. In the book, when a go file is created in Store.sol through abigen, the code that automatically gets the Version() string is generated. However, although I tried to follow it as it is in my work environment, only the function that returns transactionOpts is created as follows, and the function that returns Version, which is the actual public variable, is not created. I can’t get a sense of what’s wrong.

https://github.com/miguelmota/ethereum-development-with-go-book/blob/master/code/contracts/Store.sol

pragma solidity ^0.4.24;

contract Store {
  event ItemSet(bytes32 key, bytes32 value);

  string public version;
  mapping (bytes32 => bytes32) public items;

  constructor(string _version) public {
    version = _version;
  }

  function setItem(bytes32 key, bytes32 value) external {
    items[key] = value;
    emit ItemSet(key, value);
  }
}

https://goethereumbook.org/smart-contract-read/

func main() {
    client, err := ethclient.Dial("https://rinkeby.infura.io")
    if err != nil {
        log.Fatal(err)
    }

    address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
    instance, err := store.NewStore(address, client)
    if err != nil {
        log.Fatal(err)
    }

    version, err := instance.Version(nil) // RETURN String!!
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(version) // "1.0"
}
// Version is a free data retrieval call binding the contract method 0x54fd4d50.
//
// Solidity: function version() constant returns(string)
func (_Store *StoreSession) Version() (string, error) {
    return _Store.Contract.Version(&_Store.CallOpts)
}

In My Enviroment When i generate Store.go by using abigen

func main(){
    client, err := ethclient.Dial("http://127.0.0.1:8545")
    instance, err := store.NewStore(common.HexToAddress("0xDe5cbCb781AC84039f0317AE45Ac45D9945fc488"), client)
    if err != nil{
        log.Fatal(err)
    }
    log.Println("Instance Loaded Success")

    version, err := instance.Version(nil) // => this returns bind.TransactionOpts!
    if err != nil{
        log.Println(err)
    }
    fmt.Println(version)
}

My environment is as follows:

  • geth version : 1.9.0
  • abigen version : 1.9.0
  • solc version : 0.6.0

How can I get a free data retrieval call for public state variable like that book?

Leave a Reply

Your email address will not be published. Required fields are marked *