Skip to content

Commit

Permalink
feat(sol!)!: gen unit/tuple structs for call types with 0/1 param
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Feb 19, 2025
1 parent a5ffce7 commit dbb72b9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
25 changes: 20 additions & 5 deletions crates/sol-macro-expander/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS
let call_tuple = expand_tuple_types(parameters.types(), cx).0;
let return_tuple = expand_tuple_types(returns.types(), cx).0;

let converts = expand_from_into_tuples(&call_name, parameters, cx, true);
let converts = expand_from_into_tuples(&call_name, parameters, cx, false);
let return_converts = expand_from_into_tuples(&return_name, returns, cx, true);

let signature = cx.function_signature(function);
let selector = crate::utils::selector(&signature);
let tokenize_impl = expand_tokenize(parameters, cx, true);
let tokenize_impl = expand_tokenize(parameters, cx, false);

let call_doc = docs.then(|| {
let selector = hex::encode_prefixed(selector.array.as_slice());
Expand Down Expand Up @@ -97,16 +97,31 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS
}
});

let call_struct = if parameters.is_empty() {
quote! {
pub struct #call_name;
}
} else if parameters.len() == 1 {
let ty = cx.expand_rust_type(&parameters[0].ty);
quote! {
pub struct #call_name(pub #ty);
}
} else {
quote! {
pub struct #call_name {
#(#call_fields),*
}
}
};

let alloy_sol_types = &cx.crates.sol_types;

let tokens = quote! {
#(#call_attrs)*
#call_doc
#[allow(non_camel_case_types, non_snake_case, clippy::pub_underscore_fields)]
#[derive(Clone)]
pub struct #call_name {
#(#call_fields),*
}
#call_struct

#(#return_attrs)*
#return_doc
Expand Down
6 changes: 3 additions & 3 deletions crates/sol-macro/doctests/function_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ fn function() {
let _ = overloaded_0Call {};
assert_call_signature::<overloaded_0Call>("overloaded()");

let _ = overloaded_1Call { _0: U256::from(1) };
let _ = overloaded_1Call(U256::from(1));
assert_call_signature::<overloaded_1Call>("overloaded(uint256)");

let _ = overloaded_2Call { _0: "hello".into() };
let _ = overloaded_2Call("hello".into());
assert_call_signature::<overloaded_2Call>("overloaded(string)");

// Exactly the same as `function variableGetter(uint256) returns (bool)`.
let _ = variableGetterCall { k: U256::from(2) };
let _ = variableGetterCall(U256::from(2));
assert_call_signature::<variableGetterCall>("variableGetter(uint256)");
let _ = variableGetterReturn { v: false };
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sol-types/tests/macros/sol/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn large_array() {
"../json-abi/tests/abi/LargeArray.json"
);

let call = LargeArray::callWithLongArrayCall { longArray: [0; 128] };
let call = LargeArray::callWithLongArrayCall([0; 128]);
let _ = format!("{call:#?}");

assert_eq!(LargeArray::callWithLongArrayCall::SIGNATURE, "callWithLongArray(uint64[128])");
Expand Down
28 changes: 14 additions & 14 deletions crates/sol-types/tests/macros/sol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ fn getter_names() {
let _ = Getters::valueCall {};
let _ = Getters::valueReturn { value: String::new() };

let _ = Getters::arrayCall { _0: U256::ZERO };
let _ = Getters::arrayCall(U256::ZERO);
let _ = Getters::arrayReturn { _0: String::new() };

let _ = Getters::mapCall { _0: B256::ZERO };
let _ = Getters::mapCall(B256::ZERO);
let _ = Getters::mapReturn { _0: String::new() };

let _ = Getters::mapWithNamesCall { k: B256::ZERO };
let _ = Getters::mapWithNamesCall(B256::ZERO);
let _ = Getters::mapWithNamesReturn { v: String::new() };

let _ = Getters::nestedMapWithNamesCall { k1: B256::ZERO, k2: U256::ZERO };
Expand Down Expand Up @@ -497,17 +497,17 @@ fn rust_keywords() {
bytes32 box;
}

function mod(address impl) returns (bool is, bool fn);
function mod(address impl, address some) returns (bool is, bool fn);
}
}
use r#dyn::*;

let _ = r#const { r#unsafe: true, r#box: Default::default() };
let m = modCall { r#impl: Address::ZERO };
let m = modCall { r#impl: Address::ZERO, some: Address::ZERO };
let _ = dynCalls::r#mod(m);
let _ = modReturn { is: true, r#fn: false };
assert_eq!(r#const::NAME, "const");
assert_eq!(modCall::SIGNATURE, "mod(address)");
assert_eq!(modCall::SIGNATURE, "mod(address,address)");
}

#[test]
Expand Down Expand Up @@ -543,7 +543,7 @@ fn most_rust_keywords() {
assert_eq!($raw::NAME, stringify!($kw));
assert_ne!($raw::NAME, stringify!($raw));
assert_eq!(<[<$kw Call>]>::SIGNATURE, concat!(stringify!($kw), "(bytes1)"));
let _ = [<$kw Call>] { $raw: [0u8; 1].into() };
let _ = [<$kw Call>]([0u8; 1].into());
assert_eq!(error::$raw::SIGNATURE, concat!(stringify!($kw), "(bytes2)"));
let _ = error::$raw([0u8; 2].into());
assert_eq!(event::$raw::SIGNATURE, concat!(stringify!($kw), "(bytes3)"));
Expand Down Expand Up @@ -914,16 +914,16 @@ fn contract_derive_default() {
sol! {
#[derive(Debug, Default)]
contract MyContract {
function f1();
function f2();
function f1(address);
function f2(address b);
event e1();
event e2();
error c();
}
}

let MyContract::f1Call {} = MyContract::f1Call::default();
let MyContract::f2Call {} = MyContract::f2Call::default();
let MyContract::f1Call(_) = MyContract::f1Call::default();
let MyContract::f2Call(_) = MyContract::f2Call::default();
let MyContract::e1 {} = MyContract::e1::default();
let MyContract::e2 {} = MyContract::e2::default();
#[allow(clippy::default_constructed_unit_structs)]
Expand Down Expand Up @@ -995,11 +995,11 @@ fn regression_overloads() {
}
}

let _ = Vm::getNonce_0Call { account: Address::ZERO };
let _ = Vm::getNonce_0Call(Address::ZERO);
let _ = Vm::getNonce_0Return { nonce: 0 };
assert_eq!(Vm::getNonce_0Call::SIGNATURE, "getNonce(address)");

let _ = Vm::getNonce_1Call { wallet: Vm::Wallet { stuff: U256::ZERO } };
let _ = Vm::getNonce_1Call(Vm::Wallet { stuff: U256::ZERO });
let _ = Vm::getNonce_1Return { nonce: 0 };
assert_eq!(Vm::getNonce_1Call::SIGNATURE, "getNonce((uint256))");
}
Expand All @@ -1015,7 +1015,7 @@ fn normal_paths() {
function func(I.S memory stuff);
}

let _ = funcCall { stuff: I::S { x: U256::ZERO } };
let _ = funcCall(I::S { x: U256::ZERO });
}

#[test]
Expand Down

0 comments on commit dbb72b9

Please sign in to comment.