Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Jan 20, 2025
1 parent 46372da commit d7201f0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 55 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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" }
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 18 additions & 49 deletions examples/sol-macro/examples/complex_returns.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
10 changes: 5 additions & 5 deletions examples/sol-macro/examples/decode_returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(())
}

0 comments on commit d7201f0

Please sign in to comment.