Skip to content

Commit

Permalink
feat: implement abi2sol (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Aug 23, 2023
1 parent 406c07b commit 89764b7
Show file tree
Hide file tree
Showing 5 changed files with 477 additions and 308 deletions.
92 changes: 71 additions & 21 deletions crates/json-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ pub struct JsonAbi {
}

impl JsonAbi {
/// Creates an empty ABI object.
#[inline]
pub fn new() -> Self {
Self::default()
}

/// Parse the ABI json from a `str`. This is a convenience wrapper around
/// [`serde_json::from_str`].
#[cfg(feature = "serde_json")]
#[inline]
pub fn from_json_str(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}

/// Loads contract from json
#[cfg(all(feature = "std", feature = "serde_json"))]
pub fn load<T: std::io::Read>(mut reader: T) -> Result<Self, serde_json::Error> {
// https://docs.rs/serde_json/latest/serde_json/fn.from_reader.html
// serde_json docs recommend buffering the whole reader to a string
// This also prevents a borrowing issue when deserializing from a reader
let mut json = String::with_capacity(1024);
reader
.read_to_string(&mut json)
.map_err(serde_json::Error::io)?;

Self::from_json_str(&json)
}

/// Returns the total number of items (of any type).
pub fn len(&self) -> usize {
self.constructor.is_some() as usize
Expand Down Expand Up @@ -83,32 +111,54 @@ impl JsonAbi {
}
}

/// Creates constructor call builder.
/// Formats this JSON ABI as a Solidity interface.
///
/// The order of the definitions is not guaranteed.
///
/// Generates:
///
/// ```solidity
/// interface <name> {
/// <enums>...
/// <UDVTs>...
/// <structs>...
/// <errors>...
/// <events>...
/// <fallback>
/// <receive>
/// <functions>...
/// }
/// ```
///
/// Note that enums are going to be identical to `uint8` UDVTs, since no
/// other information about enums is present in the ABI.
#[inline]
pub const fn constructor(&self) -> Option<&Constructor> {
self.constructor.as_ref()
pub fn to_sol(&self, name: &str) -> String {
let mut out = String::new();
self.to_sol_raw(name, &mut out);
out
}

/// Parse the ABI json from a `str`. This is a convenience wrapper around
/// [`serde_json::from_str`].
#[cfg(feature = "serde_json")]
#[inline]
pub fn from_json_str(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
/// Formats this JSON ABI as a Solidity interface into the given string.
///
/// See [`to_sol`](JsonAbi::to_sol) for more information.
pub fn to_sol_raw(&self, name: &str, out: &mut String) {
let len = self.len();
out.reserve((len + 1) * 128);

/// Loads contract from json
#[cfg(all(feature = "std", feature = "serde_json"))]
pub fn load<T: std::io::Read>(mut reader: T) -> Result<Self, serde_json::Error> {
// https://docs.rs/serde_json/latest/serde_json/fn.from_reader.html
// serde_json docs recommend buffering the whole reader to a string
// This also prevents a borrowing issue when deserializing from a reader
let mut json = String::with_capacity(1024);
reader
.read_to_string(&mut json)
.map_err(serde_json::Error::io)?;
out.push_str("interface ");
out.push_str(name);
out.push('{');
if len != 0 {
crate::to_sol::ToSol::to_sol(self, out);
}
out.push('}');
}

Self::from_json_str(&json)
/// Creates constructor call builder.
#[inline]
pub const fn constructor(&self) -> Option<&Constructor> {
self.constructor.as_ref()
}

/// Gets all the functions with the given name.
Expand Down
2 changes: 2 additions & 0 deletions crates/json-abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub use param::{EventParam, Param};
mod internal_type;
pub use internal_type::InternalType;

mod to_sol;

pub(crate) mod utils;

pub use alloy_sol_type_parser as parser;
Expand Down
Loading

0 comments on commit 89764b7

Please sign in to comment.