Skip to content

Commit

Permalink
fix: encode_returns argument type and review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Jun 22, 2023
1 parent 63a8286 commit ea1b636
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/sol-macro/src/expand/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn expand_functions_enum(
.map(|function| r#type::params_min_data_size(cx, &function.arguments))
.max()
.unwrap();
let trt = Ident::new("SolCall", Span::call_site());
let trt = Ident::new("SolFunction", Span::call_site());
expand_call_like_enum(name, &variants, &types, min_data_len, trt, attrs)
}

Expand Down
10 changes: 4 additions & 6 deletions crates/sol-macro/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@ use syn::{Result, Token};
/// #(pub #argument_name: #argument_type,)*
/// }
///
/// impl SolCall for #{name}Call {
/// impl SolFunction for #{name}Call {
/// ...
/// }
///
/// pub struct #{name}Return {
/// #(pub #return_name: #return_type,)*
/// }
///
/// impl SolCall for #{name}Return {
/// impl SolFunction for #{name}Return {
/// ...
/// }
/// ```
pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenStream> {
let function_name = cx.function_name(function);
let call_name = cx.call_name(function_name);
let tokens = expand_call(cx, function, &call_name, &function.arguments)?;

Ok(tokens)
expand_call(cx, function, &call_name, &function.arguments)
}

fn expand_call(
Expand Down Expand Up @@ -80,7 +78,7 @@ fn expand_call(
#converts

#[automatically_derived]
impl ::alloy_sol_types::SolCall for #call_name {
impl ::alloy_sol_types::SolFunction for #call_name {
type Tuple<'a> = UnderlyingSolTuple<'a>;
type Token<'a> = <Self::Tuple<'a> as ::alloy_sol_types::SolType>::TokenType<'a>;

Expand Down
4 changes: 2 additions & 2 deletions crates/sol-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ pub use errors::{Error, Result};

mod types;
pub use types::{
data_type as sol_data, Encodable, EventTopic, Panic, PanicKind, Revert, SolCall, SolError,
SolEvent, SolStruct, SolType, TopicList,
data_type as sol_data, Encodable, EventTopic, Panic, PanicKind, Revert, SolError, SolEvent,
SolFunction, SolStruct, SolType, TopicList,
};

mod util;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{no_std_prelude::*, token::TokenSeq, Result, SolType};
use crate::{no_std_prelude::*, token::TokenSeq, Encodable, Result, SolType};

/// Solidity call (a tuple with a selector).
///
Expand All @@ -7,7 +7,7 @@ use crate::{no_std_prelude::*, token::TokenSeq, Result, SolType};
/// We do not recommend implementing this trait directly. Instead, we recommend
/// using the [`sol`][crate::sol] proc macro to parse a Solidity function
/// definition.
pub trait SolCall: Sized {
pub trait SolFunction: Sized {
/// The underlying tuple type which represents this type's arguments.
///
/// If this type has no arguments, this will be the unit type `()`.
Expand All @@ -21,7 +21,7 @@ pub trait SolCall: Sized {
/// If this type has no return values, this will be the unit type `()`.
type ReturnTuple<'a>: SolType<TokenType<'a> = Self::ReturnToken<'a>>;

/// The corresponding [TokenSeq] type.
/// The returns' corresponding [TokenSeq] type.
type ReturnToken<'a>: TokenSeq<'a>;

/// The function's ABI signature.
Expand All @@ -47,7 +47,7 @@ pub trait SolCall: Sized {
return size
}

<<Self as SolCall>::Tuple<'_> as SolType>::encoded_size(&self.to_rust())
<<Self as SolFunction>::Tuple<'_> as SolType>::encoded_size(&self.to_rust())
}

/// ABI decode this call's arguments from the given slice, **without** its
Expand Down Expand Up @@ -92,9 +92,12 @@ pub trait SolCall: Sized {
<Self::ReturnTuple<'_> as SolType>::decode(data, validate)
}

/// ABI encode the call's return values to the given buffer.
/// ABI encode the call's return values.
#[inline]
fn encode_returns_to(&self) -> Vec<u8> {
crate::encode(&self.tokenize())
fn encode_returns<'a, E>(e: &'a E) -> Vec<u8>
where
E: Encodable<Self::ReturnTuple<'a>>,
{
crate::encode(&e.to_tokens())
}
}
4 changes: 2 additions & 2 deletions crates/sol-types/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod call;
pub use call::SolCall;
mod function;
pub use function::SolFunction;

pub mod data_type;

Expand Down
2 changes: 1 addition & 1 deletion crates/sol-types/tests/doc_contracts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::{Address, U256};
use alloy_sol_types::{sol, SolCall};
use alloy_sol_types::{sol, SolFunction};
use hex_literal::hex;

sol! {
Expand Down
6 changes: 3 additions & 3 deletions crates/sol-types/tests/doc_function_like.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::{keccak256, U256};
use alloy_sol_types::{sol, SolCall, SolError};
use alloy_sol_types::{sol, SolError, SolFunction};
use hex_literal::hex;

// Unnamed arguments will be given a name based on their position,
Expand All @@ -18,7 +18,7 @@ sol! {
/// disambiguation, but the signature will remain the same.
///
/// E.g. if there are two functions named `foo`, the generated types will be
/// `foo_0Call` and `foo_1Call`, each of which will implement [`SolCall` ]
/// `foo_0Call` and `foo_1Call`, each of which will implement [`SolCall`]
/// with their respective signatures.
///
/// Both of these types will have the attributes of the function, like this
Expand Down Expand Up @@ -78,7 +78,7 @@ fn error() {
);
}

fn assert_call_signature<T: SolCall>(expected: &str) {
fn assert_call_signature<T: SolFunction>(expected: &str) {
assert_eq!(T::SIGNATURE, expected);
assert_eq!(T::SELECTOR, keccak256(expected)[..4]);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sol-types/tests/sol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::{keccak256, Address, U256};
use alloy_sol_types::{no_std_prelude::*, sol, SolCall, SolError, SolType};
use alloy_sol_types::{no_std_prelude::*, sol, SolError, SolFunction, SolType};

sol! {
struct MyStruct {
Expand Down

0 comments on commit ea1b636

Please sign in to comment.