Skip to content

internal/abi: verify struct to tuple mapping during parsing #239

@lmittmann

Description

@lmittmann

Idea

Extend the API of w3.NewFunc, w3.MustNewFunc, w3.NewEvent, w3.MustNewEvent with struct mappings. A struct mapping maps a Solidity struct (tuple) to a Go struct.

Benefits:

  • compact ABI definition
  • mapping verification at function/event-creation time

Challenges:

  • Go types like big.Int have an ambiguous mapping to Solidity types.
    • Possible solution: introduce a struct arg tag abitype that maps to the Solidity type
- func MustNewFunc(signature, returns string) *Func
+ func MustNewFunc(signature, returns string, types ...any) *Func

Example (Uniswap V4 Swap Function)

Potential Future API

funcSwap := w3.MustNewFunc("swap(PoolKey key, SwapParams params, bytes hookData)", "int256 delta", 
	PoolKey{}, SwapParams{},
)

// ABI binding for the PoolKey struct.
type PoolKey struct {
	Currency0   common.Address
	Currency1   common.Address
	Fee         *big.Int `abitype:"uint24"`
	TickSpacing *big.Int `abitype:"int24"`
	Hooks       common.Address
}

// ABI binding for the SwapParams struct.
type SwapParams struct {
	ZeroForOne        bool
	AmountSpecified   *big.Int `abitype:"int256"`
	SqrtPriceLimitX96 *big.Int `abitype:"uint160"`
}

Current API

funcSwap := w3.MustNewFunc(`swap(
	(address currency0, address currency1, uint24 fee, int24 tickSpacing, address hooks) key,
	(bool zeroForOne, int256 amountSpecified, uint160 sqrtPriceLimitX96) params,
	bytes hookData
)`, "int256 delta")

// ABI binding for the PoolKey struct.
type PoolKey struct {
	Currency0   common.Address
	Currency1   common.Address
	Fee         *big.Int
	TickSpacing *big.Int
	Hooks       common.Address
}

// ABI binding for the SwapParams struct.
type SwapParams struct {
	ZeroForOne        bool
	AmountSpecified   *big.Int
	SqrtPriceLimitX96 *big.Int
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions