2021-10-29 12:29:35 +00:00
|
|
|
//! This is a basic representation of a contract ABI that does no post processing but contains the
|
|
|
|
//! raw content of the ABI.
|
2021-08-16 07:29:44 +00:00
|
|
|
|
|
|
|
#![allow(missing_docs)]
|
2021-09-30 08:27:24 +00:00
|
|
|
use serde::{
|
2021-10-10 08:31:34 +00:00
|
|
|
de::{MapAccess, SeqAccess, Visitor},
|
2021-09-30 08:27:24 +00:00
|
|
|
Deserialize, Deserializer, Serialize,
|
|
|
|
};
|
2021-08-16 07:29:44 +00:00
|
|
|
|
|
|
|
/// Contract ABI as a list of items where each item can be a function, constructor or event
|
2022-02-23 10:46:52 +00:00
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
|
|
#[serde(transparent)]
|
2021-09-30 08:27:24 +00:00
|
|
|
pub struct RawAbi(Vec<Item>);
|
|
|
|
|
|
|
|
impl IntoIterator for RawAbi {
|
|
|
|
type Item = Item;
|
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RawAbiVisitor;
|
|
|
|
|
|
|
|
impl<'de> Visitor<'de> for RawAbiVisitor {
|
|
|
|
type Value = RawAbi;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
formatter.write_str("a sequence or map with `abi` key")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: SeqAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
|
|
|
|
while let Some(element) = seq.next_element()? {
|
|
|
|
vec.push(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(RawAbi(vec))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: MapAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut vec = None;
|
|
|
|
|
|
|
|
while let Some(key) = map.next_key::<String>()? {
|
|
|
|
if key == "abi" {
|
|
|
|
vec = Some(RawAbi(map.next_value::<Vec<Item>>()?));
|
|
|
|
} else {
|
|
|
|
map.next_value::<serde::de::IgnoredAny>()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vec.ok_or_else(|| serde::de::Error::missing_field("abi"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for RawAbi {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
deserializer.deserialize_any(RawAbiVisitor)
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 07:29:44 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Item {
|
|
|
|
#[serde(default)]
|
|
|
|
pub inputs: Vec<Component>,
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
|
|
pub state_mutability: Option<String>,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub type_field: String,
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
|
|
pub name: Option<String>,
|
|
|
|
#[serde(default)]
|
|
|
|
pub outputs: Vec<Component>,
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:26:21 +00:00
|
|
|
/// Either an input/output or a nested component of an input/output
|
2021-08-16 07:29:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Component {
|
2021-10-29 12:29:35 +00:00
|
|
|
#[serde(rename = "internalType", default, skip_serializing_if = "Option::is_none")]
|
2021-08-16 07:29:44 +00:00
|
|
|
pub internal_type: Option<String>,
|
2022-02-22 18:26:21 +00:00
|
|
|
#[serde(default)]
|
2021-08-16 07:29:44 +00:00
|
|
|
pub name: String,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub type_field: String,
|
|
|
|
#[serde(default)]
|
|
|
|
pub components: Vec<Component>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-09-30 08:27:24 +00:00
|
|
|
|
2021-08-16 07:29:44 +00:00
|
|
|
#[test]
|
|
|
|
fn can_parse_raw_abi() {
|
|
|
|
const VERIFIER_ABI: &str = include_str!("../../tests/solidity-contracts/verifier_abi.json");
|
|
|
|
let _ = serde_json::from_str::<RawAbi>(VERIFIER_ABI).unwrap();
|
|
|
|
}
|
2021-09-30 08:27:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_hardhat_raw_abi() {
|
|
|
|
const VERIFIER_ABI: &str =
|
|
|
|
include_str!("../../tests/solidity-contracts/verifier_abi_hardhat.json");
|
|
|
|
let _ = serde_json::from_str::<RawAbi>(VERIFIER_ABI).unwrap();
|
|
|
|
}
|
2022-02-22 18:26:21 +00:00
|
|
|
|
|
|
|
/// due to ethabi's limitations some may be stripped when ethers-solc generates the abi, such as
|
|
|
|
/// the name of the component
|
|
|
|
#[test]
|
|
|
|
fn can_parse_ethers_solc_generated_abi() {
|
|
|
|
let s = r#"[{"type":"function","name":"greet","inputs":[{"internalType":"struct Greeter.Stuff","name":"stuff","type":"tuple","components":[{"type":"bool"}]}],"outputs":[{"internalType":"struct Greeter.Stuff","name":"","type":"tuple","components":[{"type":"bool"}]}],"stateMutability":"view"}]"#;
|
|
|
|
let _ = serde_json::from_str::<RawAbi>(s).unwrap();
|
|
|
|
}
|
2021-08-16 07:29:44 +00:00
|
|
|
}
|