use alloc::collections::{btree_map::Values, BTreeMap};
#[cfg(feature = "serde")]
use core::fmt;
use core::iter::Flatten;
#[cfg(feature = "full-serde")]
use std::io;
#[cfg(feature = "serde")]
use serde::{
de::{SeqAccess, Visitor},
ser::SerializeSeq,
Deserialize, Deserializer, Serialize, Serializer,
};
#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
#[cfg(feature = "serde")]
use crate::operation::Operation;
use crate::{error::Error as AbiError, errors, Constructor, Error, Event, Function};
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Contract {
pub constructor: Option<Constructor>,
pub functions: BTreeMap<String, Vec<Function>>,
pub events: BTreeMap<String, Vec<Event>>,
pub errors: BTreeMap<String, Vec<AbiError>>,
pub receive: bool,
pub fallback: bool,
}
#[cfg(feature = "serde")]
impl<'a> Deserialize<'a> for Contract {
fn deserialize<D>(deserializer: D) -> Result<Contract, D::Error>
where
D: Deserializer<'a>,
{
deserializer.deserialize_any(ContractVisitor)
}
}
#[cfg(feature = "serde")]
struct ContractVisitor;
#[cfg(feature = "serde")]
impl<'a> Visitor<'a> for ContractVisitor {
type Value = Contract;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("valid abi spec file")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'a>,
{
let mut result = Contract::default();
while let Some(operation) = seq.next_element()? {
match operation {
Operation::Constructor(constructor) => {
result.constructor = Some(constructor);
}
Operation::Function(func) => {
result.functions.entry(func.name.clone()).or_default().push(func);
}
Operation::Event(event) => {
result.events.entry(event.name.clone()).or_default().push(event);
}
Operation::Error(error) => {
result.errors.entry(error.name.clone()).or_default().push(error);
}
Operation::Fallback => {
result.fallback = true;
}
Operation::Receive => {
result.receive = true;
}
}
}
Ok(result)
}
}
#[cfg(feature = "serde")]
impl Serialize for Contract {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[derive(Serialize)]
#[serde(tag = "type")]
enum OperationRef<'a> {
#[serde(rename = "constructor")]
Constructor(&'a Constructor),
#[serde(rename = "function")]
Function(&'a Function),
#[serde(rename = "event")]
Event(&'a Event),
#[serde(rename = "error")]
Error(&'a AbiError),
#[serde(rename = "fallback")]
Fallback,
#[serde(rename = "receive")]
Receive,
}
let mut seq = serializer.serialize_seq(None)?;
if let Some(constructor) = &self.constructor {
seq.serialize_element(&OperationRef::Constructor(constructor))?;
}
for functions in self.functions.values() {
for function in functions {
seq.serialize_element(&OperationRef::Function(function))?;
}
}
for events in self.events.values() {
for event in events {
seq.serialize_element(&OperationRef::Event(event))?;
}
}
for errors in self.errors.values() {
for error in errors {
seq.serialize_element(&OperationRef::Error(error))?;
}
}
if self.receive {
seq.serialize_element(&OperationRef::Receive)?;
}
if self.fallback {
seq.serialize_element(&OperationRef::Fallback)?;
}
seq.end()
}
}
impl Contract {
#[cfg(feature = "full-serde")]
pub fn load<T: io::Read>(reader: T) -> errors::Result<Self> {
serde_json::from_reader(reader).map_err(From::from)
}
pub fn constructor(&self) -> Option<&Constructor> {
self.constructor.as_ref()
}
pub fn function(&self, name: &str) -> errors::Result<&Function> {
self.functions.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn event(&self, name: &str) -> errors::Result<&Event> {
self.events.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn error(&self, name: &str) -> errors::Result<&AbiError> {
self.errors.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn events_by_name(&self, name: &str) -> errors::Result<&Vec<Event>> {
self.events.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn functions_by_name(&self, name: &str) -> errors::Result<&Vec<Function>> {
self.functions.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn errors_by_name(&self, name: &str) -> errors::Result<&Vec<AbiError>> {
self.errors.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn functions(&self) -> Functions {
Functions(self.functions.values().flatten())
}
pub fn events(&self) -> Events {
Events(self.events.values().flatten())
}
pub fn errors(&self) -> AbiErrors {
AbiErrors(self.errors.values().flatten())
}
}
pub struct Functions<'a>(Flatten<Values<'a, String, Vec<Function>>>);
impl<'a> Iterator for Functions<'a> {
type Item = &'a Function;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct Events<'a>(Flatten<Values<'a, String, Vec<Event>>>);
impl<'a> Iterator for Events<'a> {
type Item = &'a Event;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct AbiErrors<'a>(Flatten<Values<'a, String, Vec<AbiError>>>);
impl<'a> Iterator for AbiErrors<'a> {
type Item = &'a AbiError;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
#[cfg(all(test, feature = "serde"))]
#[allow(deprecated)]
mod test {
#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
use alloc::collections::BTreeMap;
use core::iter::FromIterator;
use crate::{tests::assert_ser_de, AbiError, Constructor, Contract, Event, EventParam, Function, Param, ParamType};
#[test]
fn empty() {
let json = "[]";
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::new(),
events: BTreeMap::new(),
errors: BTreeMap::new(),
receive: false,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn constructor() {
let json = r#"
[
{
"type": "constructor",
"inputs": [
{
"name":"a",
"type":"address"
}
]
}
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: Some(Constructor {
inputs: vec![Param { name: "a".to_string(), kind: ParamType::Address, internal_type: None }]
}),
functions: BTreeMap::new(),
events: BTreeMap::new(),
errors: BTreeMap::new(),
receive: false,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn functions() {
let json = r#"
[
{
"type": "function",
"name": "foo",
"inputs": [
{
"name":"a",
"type":"address"
}
],
"outputs": [
{
"name": "res",
"type":"address"
}
]
},
{
"type": "function",
"name": "bar",
"inputs": [],
"outputs": []
}
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::from_iter(vec![
(
"foo".to_string(),
vec![Function {
name: "foo".to_string(),
inputs: vec![Param {
name: "a".to_string(),
kind: ParamType::Address,
internal_type: None,
}],
outputs: vec![Param {
name: "res".to_string(),
kind: ParamType::Address,
internal_type: None,
}],
constant: None,
state_mutability: Default::default(),
}]
),
(
"bar".to_string(),
vec![Function {
name: "bar".to_string(),
inputs: vec![],
outputs: vec![],
constant: None,
state_mutability: Default::default(),
}]
),
]),
events: BTreeMap::new(),
errors: BTreeMap::new(),
receive: false,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn functions_overloads() {
let json = r#"
[
{
"type": "function",
"name": "foo",
"inputs": [
{
"name":"a",
"type":"address"
}
],
"outputs": [
{
"name": "res",
"type":"address"
}
]
},
{
"type": "function",
"name": "foo",
"inputs": [],
"outputs": []
}
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::from_iter(vec![(
"foo".to_string(),
vec![
Function {
name: "foo".to_string(),
inputs: vec![Param {
name: "a".to_string(),
kind: ParamType::Address,
internal_type: None,
}],
outputs: vec![Param {
name: "res".to_string(),
kind: ParamType::Address,
internal_type: None,
}],
constant: None,
state_mutability: Default::default(),
},
Function {
name: "foo".to_string(),
inputs: vec![],
outputs: vec![],
constant: None,
state_mutability: Default::default(),
},
]
)]),
events: BTreeMap::new(),
errors: BTreeMap::new(),
receive: false,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn events() {
let json = r#"
[
{
"type": "event",
"name": "foo",
"inputs": [
{
"name":"a",
"type":"address"
}
],
"anonymous": false
},
{
"type": "event",
"name": "bar",
"inputs": [
{
"name":"a",
"type":"address",
"indexed": true
}
],
"anonymous": false
}
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::new(),
events: BTreeMap::from_iter(vec![
(
"foo".to_string(),
vec![Event {
name: "foo".to_string(),
inputs: vec![EventParam {
name: "a".to_string(),
kind: ParamType::Address,
indexed: false,
}],
anonymous: false,
}]
),
(
"bar".to_string(),
vec![Event {
name: "bar".to_string(),
inputs: vec![EventParam { name: "a".to_string(), kind: ParamType::Address, indexed: true }],
anonymous: false,
}]
),
]),
errors: BTreeMap::new(),
receive: false,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn events_overload() {
let json = r#"
[
{
"type": "event",
"name": "foo",
"inputs": [
{
"name":"a",
"type":"address"
}
],
"anonymous": false
},
{
"type": "event",
"name": "foo",
"inputs": [
{
"name":"a",
"type":"address",
"indexed": true
}
],
"anonymous": false
}
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::new(),
events: BTreeMap::from_iter(vec![(
"foo".to_string(),
vec![
Event {
name: "foo".to_string(),
inputs: vec![EventParam {
name: "a".to_string(),
kind: ParamType::Address,
indexed: false,
}],
anonymous: false,
},
Event {
name: "foo".to_string(),
inputs: vec![EventParam { name: "a".to_string(), kind: ParamType::Address, indexed: true }],
anonymous: false,
},
]
)]),
errors: BTreeMap::new(),
receive: false,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn errors() {
let json = r#"
[
{
"type": "error",
"inputs": [
{
"name": "available",
"type": "uint256"
},
{
"name": "required",
"type": "address"
}
],
"name": "foo"
},
{
"type": "error",
"inputs": [
{
"name": "a",
"type": "uint256"
},
{
"name": "b",
"type": "address"
}
],
"name": "bar"
}
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::new(),
events: BTreeMap::new(),
errors: BTreeMap::from_iter(vec![
(
"foo".to_string(),
vec![AbiError {
name: "foo".to_string(),
inputs: vec![
Param {
name: "available".to_string(),
kind: ParamType::Uint(256),
internal_type: None,
},
Param { name: "required".to_string(), kind: ParamType::Address, internal_type: None }
],
}]
),
(
"bar".to_string(),
vec![AbiError {
name: "bar".to_string(),
inputs: vec![
Param { name: "a".to_string(), kind: ParamType::Uint(256), internal_type: None },
Param { name: "b".to_string(), kind: ParamType::Address, internal_type: None }
],
}]
),
]),
receive: false,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn errors_overload() {
let json = r#"
[
{
"type": "error",
"inputs": [
{
"name": "a",
"type": "uint256"
}
],
"name": "foo"
},
{
"type": "error",
"inputs": [
{
"name": "a",
"type": "uint256"
},
{
"name": "b",
"type": "address"
}
],
"name": "foo"
}
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::new(),
events: BTreeMap::new(),
errors: BTreeMap::from_iter(vec![(
"foo".to_string(),
vec![
AbiError {
name: "foo".to_string(),
inputs: vec![Param {
name: "a".to_string(),
kind: ParamType::Uint(256),
internal_type: None,
}],
},
AbiError {
name: "foo".to_string(),
inputs: vec![
Param { name: "a".to_string(), kind: ParamType::Uint(256), internal_type: None },
Param { name: "b".to_string(), kind: ParamType::Address, internal_type: None }
],
},
]
),]),
receive: false,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn receive() {
let json = r#"
[
{ "type": "receive" }
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::new(),
events: BTreeMap::new(),
errors: BTreeMap::new(),
receive: true,
fallback: false,
}
);
assert_ser_de(&deserialized);
}
#[test]
fn fallback() {
let json = r#"
[
{ "type": "fallback" }
]
"#;
let deserialized: Contract = serde_json::from_str(json).unwrap();
assert_eq!(
deserialized,
Contract {
constructor: None,
functions: BTreeMap::new(),
events: BTreeMap::new(),
errors: BTreeMap::new(),
receive: false,
fallback: true,
}
);
assert_ser_de(&deserialized);
}
}