ethereum / go-ethereum

Go implementation of the Ethereum protocol
https://geth.ethereum.org
GNU Lesser General Public License v3.0
47.78k stars 20.23k forks source link

abigen to create an output type for contract functions rather than declaring return type directly #29689

Open crisog opened 7 months ago

crisog commented 7 months ago

Right now, the abigen-generated Go methods don't create/export the output types used for the functions, which makes it quite complex to use in a general way. The following is an example of how it looks now:

// GetMinipoolCountPerStatus is a free data retrieval call binding the contract method 0x3b5ecefa.
//
// Solidity: function getMinipoolCountPerStatus(uint256 _offset, uint256 _limit) view returns(uint256 initialisedCount, uint256 prelaunchCount, uint256 stakingCount, uint256 withdrawableCount, uint256 dissolvedCount)
func (_RocketMinipoolManager *RocketMinipoolManagerCaller) GetMinipoolCountPerStatus(opts *bind.CallOpts, _offset *big.Int, _limit *big.Int) (struct {
    InitialisedCount  *big.Int
    PrelaunchCount    *big.Int
    StakingCount      *big.Int
    WithdrawableCount *big.Int
    DissolvedCount    *big.Int
}, error) {
    var out []interface{}
    err := _RocketMinipoolManager.contract.Call(opts, &out, "getMinipoolCountPerStatus", _offset, _limit)

    outstruct := new(struct {
        InitialisedCount  *big.Int
        PrelaunchCount    *big.Int
        StakingCount      *big.Int
        WithdrawableCount *big.Int
        DissolvedCount    *big.Int
    })
    if err != nil {
        return *outstruct, err
    }

    outstruct.InitialisedCount = *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
    outstruct.PrelaunchCount = *abi.ConvertType(out[1], new(*big.Int)).(**big.Int)
    outstruct.StakingCount = *abi.ConvertType(out[2], new(*big.Int)).(**big.Int)
    outstruct.WithdrawableCount = *abi.ConvertType(out[3], new(*big.Int)).(**big.Int)
    outstruct.DissolvedCount = *abi.ConvertType(out[4], new(*big.Int)).(**big.Int)

    return *outstruct, err

}

Ideally, instead of putting the return type right in the return for the function, it would be great that a proper type gets created & exported like this:

type GetMinipoolCountPerStatusOutput struct {
    InitialisedCount  *big.Int
    PrelaunchCount    *big.Int
    StakingCount      *big.Int
    WithdrawableCount *big.Int
    DissolvedCount    *big.Int
}

func (_RocketMinipoolManager *RocketMinipoolManagerCaller) GetMinipoolCountPerStatus(opts *bind.CallOpts, _offset *big.Int, _limit *big.Int) (GetMinipoolCountPerStatusOutput, error) {
    var out []interface{}
    err := _RocketMinipoolManager.contract.Call(opts, &out, "getMinipoolCountPerStatus", _offset, _limit)

    outstruct := GetMinipoolCountPerStatusOutput{
        InitialisedCount: *abi.ConvertType(out[0], new(*big.Int)).(**big.Int),
        PrelaunchCount: *abi.ConvertType(out[1], new(*big.Int)).(**big.Int),
        StakingCount:  *abi.ConvertType(out[2], new(*big.Int)).(**big.Int),
        WithdrawableCount: *abi.ConvertType(out[3], new(*big.Int)).(**big.Int),
        DissolvedCount: *abi.ConvertType(out[4], new(*big.Int)).(**big.Int),
    }

    if err != nil {
        return outstruct, err
    }

    return outstruct, err
}
JuliusCaesar1370 commented 6 months ago

HBI

joey1123455 commented 5 months ago

HBI

hi Julius are you working on this

joey1123455 commented 2 months ago

I have started working on this