Skip to content

Commit

Permalink
feat: implement abi2sol
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Aug 13, 2023
1 parent 5f39812 commit 1541e22
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 308 deletions.
90 changes: 70 additions & 20 deletions crates/json-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ 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")]
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 @@ -77,30 +104,53 @@ impl JsonAbi {
}
}

/// Creates constructor call builder.
pub const fn constructor(&self) -> Option<&Constructor> {
self.constructor.as_ref()
/// 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 a `uint8` UDVT, since no
/// other information about the enum is present in the ABI itself.
#[inline]
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")]
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);

out.push_str("interface ");
out.push_str(name);
out.push('{');
if len != 0 {
crate::to_sol::ToSol::to_sol(self, out);
}
out.push('}');
}

/// 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)
/// Creates constructor call builder.
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 1541e22

Please sign in to comment.