Skip to content

Commit

Permalink
Implement Serialize for Abi
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeRosa committed Mar 11, 2022
1 parent af84b60 commit 9e12430
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 30 deletions.
77 changes: 74 additions & 3 deletions src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Error, Result};
use ethereum_types::H256;
use serde::{de::Visitor, Deserialize};
use serde::{de::Visitor, Deserialize, Serialize};

use crate::{params::Param, DecodedParams, Event, Value};

Expand Down Expand Up @@ -102,6 +102,72 @@ impl std::str::FromStr for Abi {
}
}

impl Serialize for Abi {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut entries = vec![];

if let Some(c) = &self.constructor {
entries.push(AbiEntry {
type_: String::from("constructor"),
name: None,
inputs: Some(c.inputs.clone()),
outputs: None,
state_mutability: Some(StateMutability::NonPayable),
anonymous: None,
});
}

for f in &self.functions {
entries.push(AbiEntry {
type_: String::from("function"),
name: Some(f.name.clone()),
inputs: Some(f.inputs.clone()),
outputs: Some(f.outputs.clone()),
state_mutability: Some(f.state_mutability),
anonymous: None,
});
}

for e in &self.events {
entries.push(AbiEntry {
type_: String::from("event"),
name: Some(e.name.clone()),
inputs: Some(e.inputs.clone()),
outputs: None,
state_mutability: None,
anonymous: Some(e.anonymous),
});
}

if self.has_receive {
entries.push(AbiEntry {
type_: String::from("receive"),
name: None,
inputs: None,
outputs: None,
state_mutability: Some(StateMutability::Payable),
anonymous: None,
});
}

if self.has_fallback {
entries.push(AbiEntry {
type_: String::from("fallback"),
name: None,
inputs: None,
outputs: None,
state_mutability: Some(StateMutability::Payable),
anonymous: None,
});
}

entries.serialize(serializer)
}
}

impl<'de> Deserialize<'de> for Abi {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -181,7 +247,7 @@ impl Function {
}

/// Available state mutability values for functions and constructors.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum StateMutability {
/// Specified to not read the blockchain state.
Expand All @@ -194,15 +260,20 @@ pub enum StateMutability {
Payable,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AbiEntry {
#[serde(rename = "type")]
type_: String,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
inputs: Option<Vec<Param>>,
#[serde(skip_serializing_if = "Option::is_none")]
outputs: Option<Vec<Param>>,
#[serde(skip_serializing_if = "Option::is_none")]
state_mutability: Option<StateMutability>,
#[serde(skip_serializing_if = "Option::is_none")]
anonymous: Option<bool>,
}

Expand Down
Loading

0 comments on commit 9e12430

Please sign in to comment.