Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(json-abi): fallback to tuple types for nested params in to_sol #354

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions crates/json-abi/src/to_sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ impl ToSol for Param {
self.internal_type.as_ref(),
false,
&self.name,
&self.components,
out,
);
}
Expand All @@ -353,6 +354,7 @@ impl ToSol for EventParam {
self.internal_type.as_ref(),
self.indexed,
&self.name,
&self.components,
out,
);
}
Expand All @@ -363,17 +365,48 @@ fn param<'a>(
internal_type: Option<&'a InternalType>,
indexed: bool,
name: &str,
components: &[Param],
out: &mut String,
) {
if let Some(it) = internal_type {
match it {
type_name = match it {
InternalType::AddressPayable(_) => "address payable",
InternalType::Contract(_) => "address",
InternalType::Struct { ty, .. }
| InternalType::Enum { ty, .. }
| InternalType::Other { ty, .. } => type_name = ty,
_ => {}
}
| InternalType::Other { ty, .. } => ty,
};
};
out.push_str(type_name);

match type_name.strip_prefix("tuple") {
// This condition is met only for JSON ABIs emitted by Solc 0.4.X which don't contain
// `internalType` fields and instead all structs are emitted as unnamed tuples.
// See https://github.com/alloy-rs/core/issues/349
Some(rest) if rest.is_empty() || rest.starts_with('[') => {
// note: this does not actually emit valid Solidity because there are no inline
// tuple types `(T, U, V, ...)`, but it's valid for `sol!`.
out.push('(');
for (i, component) in components.iter().enumerate() {
if i > 0 {
out.push_str(", ");
}
param(
&component.ty,
component.internal_type.as_ref(), // this is probably always None
false,
"", // don't emit names in types
&component.components,
out,
);
}
out.push(')');
// could be array sizes
out.push_str(rest);
}
// primitive type
_ => out.push_str(type_name),
}

if indexed {
out.push_str(" indexed");
}
Expand Down
Loading