Skip to content

Commit

Permalink
dedup code
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Aug 29, 2023
1 parent f97b339 commit b5c098d
Showing 1 changed file with 31 additions and 57 deletions.
88 changes: 31 additions & 57 deletions crates/dyn-abi/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::{DynSolType, Result};
use alloc::vec::Vec;
use alloy_json_abi::{EventParam, Param};
use alloy_json_abi::{EventParam, InternalType, Param};
use alloy_sol_type_parser::{
Error as TypeStrError, RootType, TupleSpecifier, TypeSpecifier, TypeStem,
};
Expand Down Expand Up @@ -123,72 +123,46 @@ impl ResolveSolType for TypeSpecifier<'_> {

impl ResolveSolType for Param {
fn resolve(&self) -> Result<DynSolType> {
let ty = TypeSpecifier::parse(&self.ty)?;

// type is simple, and we can resolve it via the specifier
if self.is_simple_type() {
return ty.resolve()
}

// type is complex
let tuple = self
.components
.iter()
.map(Self::resolve)
.collect::<Result<Vec<_>, _>>()?;

#[cfg(feature = "eip712")]
let resolved = if let Some((_, name)) = self.internal_type().and_then(|i| i.as_struct()) {
DynSolType::CustomStruct {
// skip array sizes, since we have them already from parsing `ty`
name: name.split('[').next().unwrap().into(),
prop_names: self.components.iter().map(|c| c.name.clone()).collect(),
tuple,
}
} else {
DynSolType::Tuple(tuple)
};

#[cfg(not(feature = "eip712"))]
let resolved = DynSolType::Tuple(tuple);

Ok(resolved.array_wrap_from_iter(ty.sizes))
resolve_param(&self.ty, &self.components, self.internal_type())
}
}

impl ResolveSolType for EventParam {
fn resolve(&self) -> Result<DynSolType> {
let ty = TypeSpecifier::try_from(self.ty.as_str()).expect("always valid");
resolve_param(&self.ty, &self.components, self.internal_type())
}
}

// type is simple, and we can resolve it via the specifier
if self.is_simple_type() {
return ty.resolve()
}
fn resolve_param(ty: &str, components: &[Param], _it: Option<&InternalType>) -> Result<DynSolType> {
let ty = TypeSpecifier::parse(ty)?;

// type is complex. First extract the tuple of inner types
let tuple = self
.components
.iter()
.map(|c| c.resolve())
.collect::<Result<Vec<_>, _>>()?;
// type is simple, and we can resolve it via the specifier
if components.is_empty() {
return ty.resolve()
}

// if we have a struct specifier, we can use it to get the name of the
// struct
#[cfg(feature = "eip712")]
{
let prop_names = self.components.iter().map(|c| c.name.clone()).collect();
if let Some(spec) = self.struct_specifier() {
return Ok(DynSolType::CustomStruct {
name: spec.stem.span().into(),
prop_names,
tuple,
}
.array_wrap_from_iter(spec.sizes.iter().copied()))
}
// type is complex
let tuple = components
.iter()
.map(Param::resolve)
.collect::<Result<Vec<_>, _>>()?;

#[cfg(feature = "eip712")]
let resolved = if let Some((_, name)) = _it.and_then(|i| i.as_struct()) {
DynSolType::CustomStruct {
// skip array sizes, since we have them already from parsing `ty`
name: name.split('[').next().unwrap().into(),
prop_names: components.iter().map(|c| c.name.clone()).collect(),
tuple,
}
} else {
DynSolType::Tuple(tuple)
};

Ok(DynSolType::Tuple(tuple).array_wrap_from_iter(ty.sizes.iter().copied()))
}
#[cfg(not(feature = "eip712"))]
let resolved = DynSolType::Tuple(tuple);

Ok(resolved.array_wrap_from_iter(ty.sizes))
}

macro_rules! deref_impl {
Expand Down

0 comments on commit b5c098d

Please sign in to comment.