Skip to content

internal/abi: support struct to tuple mapping during parsing#248

Merged
lmittmann merged 23 commits intomainfrom
lmittmann/issue239
Jun 27, 2025
Merged

internal/abi: support struct to tuple mapping during parsing#248
lmittmann merged 23 commits intomainfrom
lmittmann/issue239

Conversation

@lmittmann
Copy link
Owner

@lmittmann lmittmann commented Apr 4, 2025

This PR improves ABI parsing for functions with tuples. To ABI encode or decode tuple values they need to be

  1. written out in the function signature ((uint256 tupleField0, address tupleField1)),
  2. and the tuple needs to be mapped to a Go struct type Tuple struct { TupleField0 *big.Int, TupleField1 common.Address }.

This PR makes the Go Tuple definition usable in the function Signature, which simplifies the function definition.

Example: Uniswap V4 swap-function

Old

var 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")

type PoolKey struct {
    Currency0   common.Address
    Currency1   common.Address
    Fee         *big.Int 
    TickSpacing *big.Int
    Hooks       common.Addresss
}

type SwapParams struct {
    ZeroForOne        bool
    AmountSpecified   *big.Int
    SqrtPriceLimitX96 *big.Int 
}

New

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


type PoolKey struct {
    Currency0   common.Address
    Currency1   common.Address
    Fee         *big.Int `abitype:"uint24"`
    TickSpacing *big.Int `abitype:"int24"`
    Hooks       common.Addresss
}

type SwapParams struct {
    ZeroForOne        bool
    AmountSpecified   *big.Int `abitype:"int256"`
    SqrtPriceLimitX96 *big.Int `abitype:"uint160"`
}

@lmittmann lmittmann linked an issue Apr 4, 2025 that may be closed by this pull request
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jun 2, 2025

Deploying w3 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 32eca41
Status: ✅  Deploy successful!
Preview URL: https://09bd20e1.w3-7ji.pages.dev
Branch Preview URL: https://lmittmann-issue239.w3-7ji.pages.dev

View logs

@lmittmann lmittmann marked this pull request as ready for review June 26, 2025 10:14
@lmittmann lmittmann merged commit 8f6097d into main Jun 27, 2025
3 checks passed
@lmittmann lmittmann deleted the lmittmann/issue239 branch June 27, 2025 08:59
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.

internal/abi: verify struct to tuple mapping during parsing

1 participant