ethabi_next/
contract.rs

1// Copyright 2015-2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::{errors, operation::Operation, Constructor, Error, Event, Function};
10use serde::{
11	de::{SeqAccess, Visitor},
12	Deserialize, Deserializer,
13};
14use std::{
15	collections::{hash_map::Values, HashMap},
16	fmt, io,
17	iter::Flatten,
18};
19
20/// API building calls to contracts ABI.
21#[derive(Clone, Debug, PartialEq)]
22pub struct Contract {
23	/// Contract constructor.
24	pub constructor: Option<Constructor>,
25	/// Contract functions.
26	pub functions: HashMap<String, Vec<Function>>,
27	/// Contract events, maps signature to event.
28	pub events: HashMap<String, Vec<Event>>,
29	/// Contract has receive function.
30	pub receive: bool,
31	/// Contract has fallback function.
32	pub fallback: bool,
33}
34
35impl<'a> Deserialize<'a> for Contract {
36	fn deserialize<D>(deserializer: D) -> Result<Contract, D::Error>
37	where
38		D: Deserializer<'a>,
39	{
40		deserializer.deserialize_any(ContractVisitor)
41	}
42}
43
44struct ContractVisitor;
45
46impl<'a> Visitor<'a> for ContractVisitor {
47	type Value = Contract;
48
49	fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
50		formatter.write_str("valid abi spec file")
51	}
52
53	fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
54	where
55		A: SeqAccess<'a>,
56	{
57		let mut result = Contract {
58			constructor: None,
59			functions: HashMap::default(),
60			events: HashMap::default(),
61			receive: true,
62			fallback: false,
63		};
64
65		while let Some(operation) = seq.next_element()? {
66			match operation {
67				Operation::Constructor(constructor) => {
68					result.constructor = Some(constructor);
69				}
70				Operation::Function(func) => {
71					result.functions.entry(func.name.clone()).or_default().push(func);
72				}
73				Operation::Event(event) => {
74					result.events.entry(event.name.clone()).or_default().push(event);
75				}
76				Operation::Fallback => {
77					result.fallback = true;
78				}
79				Operation::Receive => {
80					result.receive = true;
81				}
82			}
83		}
84
85		Ok(result)
86	}
87}
88
89impl Contract {
90	/// Loads contract from json.
91	pub fn load<T: io::Read>(reader: T) -> errors::Result<Self> {
92		serde_json::from_reader(reader).map_err(From::from)
93	}
94
95	/// Creates constructor call builder.
96	pub fn constructor(&self) -> Option<&Constructor> {
97		self.constructor.as_ref()
98	}
99
100	/// Get the function named `name`, the first if there are overloaded
101	/// versions of the same function.
102	pub fn function(&self, name: &str) -> errors::Result<&Function> {
103		self.functions.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
104	}
105
106	/// Get the contract event named `name`, the first if there are multiple.
107	pub fn event(&self, name: &str) -> errors::Result<&Event> {
108		self.events.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
109	}
110
111	/// Get all contract events named `name`.
112	pub fn events_by_name(&self, name: &str) -> errors::Result<&Vec<Event>> {
113		self.events.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
114	}
115
116	/// Get all functions named `name`.
117	pub fn functions_by_name(&self, name: &str) -> errors::Result<&Vec<Function>> {
118		self.functions.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
119	}
120
121	/// Iterate over all functions of the contract in arbitrary order.
122	pub fn functions(&self) -> Functions {
123		Functions(self.functions.values().flatten())
124	}
125
126	/// Iterate over all events of the contract in arbitrary order.
127	pub fn events(&self) -> Events {
128		Events(self.events.values().flatten())
129	}
130}
131
132/// Contract functions iterator.
133pub struct Functions<'a>(Flatten<Values<'a, String, Vec<Function>>>);
134
135impl<'a> Iterator for Functions<'a> {
136	type Item = &'a Function;
137
138	fn next(&mut self) -> Option<Self::Item> {
139		self.0.next()
140	}
141}
142
143/// Contract events iterator.
144pub struct Events<'a>(Flatten<Values<'a, String, Vec<Event>>>);
145
146impl<'a> Iterator for Events<'a> {
147	type Item = &'a Event;
148
149	fn next(&mut self) -> Option<Self::Item> {
150		self.0.next()
151	}
152}