From d7201f033f3a318a401352ca4e4dbeb0d6ec614c Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 20 Jan 2025 17:11:56 +0530 Subject: [PATCH] fix --- Cargo.toml | 3 +- README.md | 1 + .../sol-macro/examples/complex_returns.rs | 67 +++++-------------- examples/sol-macro/examples/decode_returns.rs | 10 +-- 4 files changed, 26 insertions(+), 55 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f994bc6..884f6f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,7 +95,7 @@ significant_drop_tightening = "allow" needless_return = "allow" [workspace.dependencies] -alloy = { version = "0.9", features = [ +alloy = { git = "https://github.com/alloy-rs/alloy", branch = "yash/core-patch", features = [ "eips", "full", "hyper", @@ -132,3 +132,4 @@ alloy-core = { git = "https://github.com/alloy-rs/core", branch = "yash/fix-fn-r alloy-sol-types = { git = "https://github.com/alloy-rs/core", branch = "yash/fix-fn-ret" } alloy-primitives = { git = "https://github.com/alloy-rs/core", branch = "yash/fix-fn-ret" } alloy-dyn-abi = { git = "https://github.com/alloy-rs/core", branch = "yash/fix-fn-ret" } +# alloy = { git = "https://github.com/alloy-rs/alloy", branch = "yash/core-patch" } diff --git a/README.md b/README.md index 9374e76..02a598c 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ This repository contains the following examples: - [x] [Contracts](./examples/contracts/examples/deploy_from_contract.rs) - [x] [Events and errors](./examples/sol-macro/examples/events_errors.rs) - [x] [Decode returns](./examples/sol-macro/examples/decode_returns.rs) + - [x] [Deconstructing complex return types](./examples/sol-macro/examples/complex_returns.rs) - [x] [Structs and enums](./examples/sol-macro/examples/structs_enums.rs) - [x] [User defined types](./examples/sol-macro/examples/user_defined_types.rs) - [x] Transactions diff --git a/examples/sol-macro/examples/complex_returns.rs b/examples/sol-macro/examples/complex_returns.rs index c674568..9e90dce 100644 --- a/examples/sol-macro/examples/complex_returns.rs +++ b/examples/sol-macro/examples/complex_returns.rs @@ -1,5 +1,5 @@ -//! Example showing how complex return values such as tuple, structs, etc. are returned from a call -//! to a contract using the `sol!` macro. +//! Example showing how to deconstruct complex return values such as tuple, structs, etc. are +//! returned from a call to a contract using the `sol!` macro. use alloy::{ hex, @@ -9,9 +9,6 @@ use alloy::{ }; use eyre::Result; -// Complex return demonstrating the new API that directly yields the values, enabling rust pattern -// matching and result destructuring. -// Note: The names of return are now ignored. sol! { function getNamedTuple() external view returns (uint256 a, uint256 b, uint256 c); function getUnamedTuple() external view returns (uint256, uint256, uint256); @@ -29,40 +26,25 @@ sol! { fn main() -> Result<()> { let data = vec![1, 2, 3].abi_encode_sequence(); - // Previously, the return struct would be of the form - // struct getNamedTupleReturn { - // a: U256, - // b: U256, - // c: U256 - // } - // Now the names are ignored, and results are yielded in the form of a tuple akin to the - // Solidity return param structure. - let (a, b, c) = getNamedTupleCall::abi_decode_returns(&data, true)?; + + // Return param names are retained as field names in the struct. + let getNamedTupleReturn { a, b, c } = getNamedTupleCall::abi_decode_returns(&data, true)?; assert_eq!(a, U256::from(1)); assert_eq!(b, U256::from(2)); assert_eq!(c, U256::from(3)); - // Previous return struct: - // struct getUnamedTupleReturn { - // _0: U256, - // _1: U256, - // _2: U256 - // } - let (a, b, c) = getUnamedTupleCall::abi_decode_returns(&data, true)?; + // Struct fields are named `_{index}` in case a return param is left unnamed. + let getUnamedTupleReturn { _0: a, _1: b, _2: c } = + getUnamedTupleCall::abi_decode_returns(&data, true)?; assert_eq!(a, U256::from(1)); assert_eq!(b, U256::from(2)); assert_eq!(c, U256::from(3)); - // Previous return struct: - // struct getPartialNamedTupleReturn { - // _0: U256, - // b: U256, - // _2: U256 - // } - // Now, the names are ignored and tuple yielded directly. - let (a, b, c) = getPartialNamedTupleCall::abi_decode_returns(&data, true)?; + // Indicates a case where only one of the return param is named and the rest are unnamed. + let getPartialNamedTupleReturn { _0: a, b, _2: c } = + getPartialNamedTupleCall::abi_decode_returns(&data, true)?; assert_eq!(a, U256::from(1)); assert_eq!(b, U256::from(2)); @@ -79,16 +61,9 @@ fn main() -> Result<()> { "0102030400000000000000000000000000000000000000000000000000000000" ); - // Previously, you the return struct would be of the of form: - // struct getStructWithBytesReturn { - // my_struct: MyStruct, - // _1: FixedBytes<32>, - // } - // Accessing the result would have the following DevX. - // let res = getStructWithBytesCall::abi_decode_returns(&data, true)?; - // let my_struct = res.my_struct; - // let bytes = res._1; - let (MyStruct { a, b, c }, bytes) = getStructWithBytesCall::abi_decode_returns(&data, true)?; + // Deconstruct a struct and bytes32 return value. + let getStructWithBytesReturn { my_struct: MyStruct { a, b, c }, _1 } = + getStructWithBytesCall::abi_decode_returns(&data, true)?; assert_eq!(a, U256::from(1)); assert_eq!(b, U256::from(2)); @@ -116,18 +91,12 @@ fn main() -> Result<()> { "0506070800000000000000000000000000000000000000000000000000000000" ); - // Previous return struct: - // struct getCompoundTupleStructReturn { - // _0: (MyStruct, FixedBytes<32>), - // _1: (MyStruct, FixedBytes<32>), - // } - // Accessing the result would have the following DevX. - // let res = getCompoundTupleStructCall::abi_decode_returns(&data, true)?; - // let (MyStruct { a, b, c }, bytes) = res._0; - // let (MyStruct { a: a2, b: b2, c: c2 }, bytes2) = res._1; - let ((MyStruct { a, b, c }, bytes), (MyStruct { a: a2, b: b2, c: c2 }, bytes2)) = + let getCompoundTupleStructReturn { _0, _1 } = getCompoundTupleStructCall::abi_decode_returns(&data, true)?; + let (MyStruct { a, b, c }, bytes) = _0; + let (MyStruct { a: a2, b: b2, c: c2 }, bytes2) = _1; + assert_eq!(a, U256::from(1)); assert_eq!(b, U256::from(2)); assert_eq!(c, U256::from(3)); diff --git a/examples/sol-macro/examples/decode_returns.rs b/examples/sol-macro/examples/decode_returns.rs index 81ddbd2..bbd3a14 100644 --- a/examples/sol-macro/examples/decode_returns.rs +++ b/examples/sol-macro/examples/decode_returns.rs @@ -17,7 +17,7 @@ sol!( ); fn main() -> Result<()> { - let (round_id, answer, started_at, updated_at, answered_in_round) = + let getRoundDataReturn { roundId, answer, startedAt, updatedAt, answeredInRound } = getRoundDataCall::abi_decode_returns( &hex!( "0000000000000000000000000000000000000000000000060000000000004716 @@ -29,11 +29,11 @@ fn main() -> Result<()> { true, )?; - assert_eq!(round_id, Uint::<80, 2>::from(110680464442257327894_u128)); + assert_eq!(roundId, Uint::<80, 2>::from(110680464442257327894_u128)); assert_eq!(answer, I256::from_dec_str("352098000000")?); - assert_eq!(started_at, U256::from(1718182523)); - assert_eq!(updated_at, U256::from(1718182523)); - assert_eq!(answered_in_round, Uint::<80, 2>::from(110680464442257327894_u128)); + assert_eq!(startedAt, U256::from(1718182523)); + assert_eq!(updatedAt, U256::from(1718182523)); + assert_eq!(answeredInRound, Uint::<80, 2>::from(110680464442257327894_u128)); Ok(()) }