Skip to content

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

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

Open
lmittmann opened this issue Mar 20, 2025 · 0 comments · May be fixed by #248
Open

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

lmittmann opened this issue Mar 20, 2025 · 0 comments · May be fixed by #248
Assignees
Labels
enhancement New feature or request

Comments

@lmittmann
Copy link
Owner

lmittmann commented Mar 20, 2025

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
}
@lmittmann lmittmann added the enhancement New feature or request label Mar 20, 2025
@lmittmann lmittmann self-assigned this Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant