1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! The encoding/decoding support for [`contract abi`](https://docs.soliditylang.org/en/develop/abi-spec.html).
//!
//! This module provide to function to encode/decode rust value:
//! - [`from_abi`]: serialize rust object into solidity(contract) abi format.
//! - [`to_abi`]: deserialize rust object from solidity(contract) abi format.
//!
//! By design, these two functions cannot distinguish between the underlying reference types of json values,
//! and therefore cannot pass [`serde_json::Value`] to them. Of course you could look at [`eip712`](crate::eip::eip712) and provide
//! additional type definition parameters to enable dynamic transforming, but that's a piece of the design goal that
//! isn't a concern for `reweb3`.
//!
//! # Static
//!
//! reweb3 only supports "contract abi" static encoder/decoder via [`serde`] framework:
//!
//! ```no_run
//! # fn main() {
//! # use ::serde::{Deserialize, Serialize};
//! # use serde_json::json;
//! # use reweb3::primitives::Address;
//! # use reweb3::abi::*;
//! #[derive(Debug, Serialize, Deserialize, PartialEq)]
//! struct Person {
//! pub name: String,
//! pub wallet: Address,
//! }
//!
//! #[derive(Debug, Serialize, Deserialize, PartialEq)]
//! struct Mail {
//! pub from: Person,
//! pub to: Person,
//! pub contents: String,
//! }
//!
//! let json = json!({
//! "from": {
//! "name": "Cow",
//! "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
//! },
//! "to": {
//! "name": "Bob",
//! "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
//! },
//! "contents": "Hello, Bob!"
//! });
//!
//! let mail: Mail = serde_json::from_value(json).unwrap();
//!
//! let mail_decoded: Mail = from_abi(to_abi(&mail).unwrap()).unwrap();
//!
//! assert_eq!(mail_decoded, mail);
//!
//! let seq: Vec<u32> = from_abi(to_abi(&vec![1, 1, 1]).unwrap()).unwrap();
//!
//! assert_eq!(seq, vec![1, 1, 1]);
//! # }
//! ```
pub use *;
pub use *;