Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(abigen): support parse transaction input bytes readable go struct in abigen #27361

Closed
wants to merge 5 commits into from

Conversation

fenghaojiang
Copy link

@fenghaojiang fenghaojiang commented May 26, 2023

feature: support parse transaction input bytes readable go struct abigen

It's easy to use abigen to generate code that help you parse the event log and call the payable method with the transactor.

But parsing the transaction call data is missing. You can only send a transaction with a transactor. This PR aims to generate codes that help developer parse transaction calldata.

abigen can generate code to parse the transaction call data.
example

package bin

import (
	"context"
	"fmt"
	"testing"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
)

func TestOnParse(t *testing.T) {
	cli, err := ethclient.Dial("https://rpc.ankr.com/polygon")
	if err != nil {
		t.Fatal(err)
	}

	collectTx, _, err := cli.TransactionByHash(context.Background(),
		common.HexToHash("0x741146cce64d873cfe82ade413651de355a6db92f992e5bfc4e1c58d92f5dd5b"))
	if err != nil {
		t.Fatal(err)
	}

	increaseLiquidityTx, _, err := cli.TransactionByHash(context.Background(),
		common.HexToHash("0x645ff650d7bfb9a74f573af474b9ebee48c3fadc7dac67257a8da6b55c71f338"))

	collectInfo, err := ParseCollect(collectTx.Data())
	if err != nil {
		t.Fatal(err)
	}

	increaseLiquidityInfo, err := ParseIncreaseLiquidity(increaseLiquidityTx.Data())
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("%+v\n", collectInfo)
	fmt.Printf("%+v\n", increaseLiquidityInfo)
}

Method ParseRenew is generate by abigen.

Here is the output of above codes:
&{Param_params:{TokenId:+894123 Recipient:0x48B8e4ed457da9B64c33Ee50Fd5490614833A37D Amount0Max:+340282366920938463463374607431768211455 Amount1Max:+340282366920938463463374607431768211455}}
&{Param_params:{TokenId:+891904 Amount0Desired:+331092 Amount1Desired:+0 Amount0Min:+331092 Amount1Min:+0 Deadline:+1685112789}}

@fenghaojiang fenghaojiang changed the title feature: support parse transaction input bytes readable go struct abigen feature(abigen): support parse transaction input bytes readable go struct abigen May 26, 2023
@fenghaojiang fenghaojiang changed the title feature(abigen): support parse transaction input bytes readable go struct abigen feature(abigen): support parse transaction input bytes readable go struct in abigen May 26, 2023
@fenghaojiang
Copy link
Author

Here is the code generated by new abigen:

Solidity: function collect((uint256,address,uint128,uint128) params) payable returns(uint256 amount0, uint256 amount1)

// CollectParams is an auto generated read-only Go binding of transcaction calldata params
type CollectParams struct {
	Param_params INonfungiblePositionManagerCollectParams
}

// Parse Collect method from calldata of a transaction
//
// Solidity: function collect((uint256,address,uint128,uint128) params) payable returns(uint256 amount0, uint256 amount1)
func ParseCollect(calldata []byte) (*CollectParams, error) {
	if len(calldata) <= 4 {
		return nil, fmt.Errorf("invalid calldata input")
	}

	_abi, err := UniswapMetaData.GetAbi()
	if err != nil {
		return nil, fmt.Errorf("failed to get abi of registry metadata:%w", err)
	}

	out, err := _abi.Methods["collect"].Inputs.Unpack(calldata[4:])
	if err != nil {
		return nil, fmt.Errorf("failed to unpack collect params data: %w", err)
	}

	var paramsResult = new(CollectParams)
	value := reflect.ValueOf(paramsResult).Elem()

	if value.NumField() != len(out) {
		return nil, fmt.Errorf("failed to match calldata with param field number")
	}

	out0 := *abi.ConvertType(out[0], new(INonfungiblePositionManagerCollectParams)).(*INonfungiblePositionManagerCollectParams)

	return &CollectParams{
		Param_params: out0,
	}, nil
}

@s1na
Copy link
Contributor

s1na commented May 26, 2023

I'm working on a v2 of abigen #26782 and this feature is one of the building blocks.

@fenghaojiang
Copy link
Author

fenghaojiang commented May 26, 2023

I'm working on a v2 of abigen #26782 and this feature is one of the building blocks.

Nice. Your code looks neat. Should I merge my commits into your branch? And what I should do next?

@fenghaojiang
Copy link
Author

This PR is non-intrusive.

@fjl
Copy link
Contributor

fjl commented Jun 19, 2023

As discussed in Discord, we would prefer to ship abigen v2 (#26782) rather than adding more features into abigen v1.

@fjl fjl closed this Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants