Sitelet https://github.com/njelich/examples/commit/66f5eed2b40a965deba6a8c1a51f9867a427239f
Skip to content

Commit 66f5eed

Browse files
authored
feat: sol marco derive attributes (alloy-rs#207)
1 parent c75a08e commit 66f5eed

5 files changed

Lines changed: 197 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ This repository contains the following examples:
8888
- [x] [Decode returns](./examples/sol-macro/examples/decode_returns.rs)
8989
- [x] [Structs and enums](./examples/sol-macro/examples/structs_enums.rs)
9090
- [x] [User defined types](./examples/sol-macro/examples/user_defined_types.rs)
91+
- [x] [`all_derives` attribute](./examples/sol-macro/examples/all_derives.rs)
92+
- [x] [`extra_derives` attribute](./examples/sol-macro/examples/extra_derives.rs)
9193
- [x] Transactions
9294
- [x] [Decode input](./examples/transactions/examples/decode_input.rs)
9395
- [x] [Encode and decode EIP-1559 transaction](./examples/transactions/examples/encode_decode_eip1559.rs)

‎examples/sol-macro/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ alloy.workspace = true
1818
eyre.workspace = true
1919
futures-util.workspace = true
2020
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
21+
serde.workspace = true
22+
serde_json.workspace = true
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
[
2+
{
3+
"type": "function",
4+
"name": "colors",
5+
"inputs": [
6+
{
7+
"name": "",
8+
"type": "address",
9+
"internalType": "address"
10+
}
11+
],
12+
"outputs": [
13+
{
14+
"name": "r",
15+
"type": "uint8",
16+
"internalType": "uint8"
17+
},
18+
{
19+
"name": "g",
20+
"type": "uint8",
21+
"internalType": "uint8"
22+
},
23+
{
24+
"name": "b",
25+
"type": "uint8",
26+
"internalType": "uint8"
27+
}
28+
],
29+
"stateMutability": "view"
30+
},
31+
{
32+
"type": "function",
33+
"name": "getColor",
34+
"inputs": [
35+
{
36+
"name": "user",
37+
"type": "address",
38+
"internalType": "address"
39+
}
40+
],
41+
"outputs": [
42+
{
43+
"name": "",
44+
"type": "tuple",
45+
"internalType": "struct Colors.Color",
46+
"components": [
47+
{
48+
"name": "r",
49+
"type": "uint8",
50+
"internalType": "uint8"
51+
},
52+
{
53+
"name": "g",
54+
"type": "uint8",
55+
"internalType": "uint8"
56+
},
57+
{
58+
"name": "b",
59+
"type": "uint8",
60+
"internalType": "uint8"
61+
}
62+
]
63+
}
64+
],
65+
"stateMutability": "view"
66+
},
67+
{
68+
"type": "function",
69+
"name": "getColorAsTuple",
70+
"inputs": [
71+
{
72+
"name": "user",
73+
"type": "address",
74+
"internalType": "address"
75+
}
76+
],
77+
"outputs": [
78+
{
79+
"name": "",
80+
"type": "uint8",
81+
"internalType": "uint8"
82+
},
83+
{
84+
"name": "",
85+
"type": "uint8",
86+
"internalType": "uint8"
87+
},
88+
{
89+
"name": "",
90+
"type": "uint8",
91+
"internalType": "uint8"
92+
}
93+
],
94+
"stateMutability": "view"
95+
},
96+
{
97+
"type": "function",
98+
"name": "setColor",
99+
"inputs": [
100+
{
101+
"name": "r",
102+
"type": "uint8",
103+
"internalType": "uint8"
104+
},
105+
{
106+
"name": "g",
107+
"type": "uint8",
108+
"internalType": "uint8"
109+
},
110+
{
111+
"name": "b",
112+
"type": "uint8",
113+
"internalType": "uint8"
114+
}
115+
],
116+
"outputs": [],
117+
"stateMutability": "nonpayable"
118+
}
119+
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! This example demonstrates the `all_derives` attribute in the `sol!` macro.
2+
//!
3+
//! The `all_derives` attribute enables the derivation of standard Rust traits.
4+
use alloy::{
5+
primitives::{Address, U256},
6+
sol,
7+
};
8+
use std::hash::{DefaultHasher, Hash, Hasher};
9+
10+
sol! (
11+
#![sol(all_derives)]
12+
// The `all_derives` attribute enables derivation of std rust traits such as
13+
// `Default`, `Debug`, `PartialEq`, `Eq`, and `Hash`.
14+
struct Foo {
15+
uint256 a;
16+
uint64 b;
17+
address c;
18+
}
19+
);
20+
21+
fn main() {
22+
// `Default` is derived.
23+
let foo = Foo::default();
24+
let foo_bar = Foo { a: U256::from(1), b: 2, c: Address::with_last_byte(1) };
25+
26+
let mut foo_list = vec![foo.clone(), foo, foo_bar];
27+
28+
// `Debug` derived as well.
29+
println!("Initial foo_list: {:?}", foo_list);
30+
31+
// `PartialEq` is derived, enabling us to apply `.dedup()`.
32+
foo_list.dedup();
33+
assert_eq!(foo_list.len(), 2);
34+
35+
// `Hash` is derived, enabling us to apply `.hash()`.
36+
let baz = Foo { a: U256::from(1), b: 2, c: Address::with_last_byte(1) };
37+
let mut hasher = DefaultHasher::default();
38+
baz.hash(&mut hasher);
39+
let hash = hasher.finish();
40+
println!("Hash of baz: {hash}");
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! This example shows how to apply trait derivations globally by specifying the path.
2+
//!
3+
//! While the `all_derives` attribute is useful for deriving standard Rust traits such as Debug,
4+
//! PartialEq, Default etc., the `extra_derives` attribute allows us to derive other useful traits
5+
//! by specifying their path.
6+
//!
7+
//! In this example, we'll derive `serde::Serialize` and `serde::Deserialize` for the types defined
8+
//! in the `sol!` macro.
9+
10+
use alloy::sol;
11+
12+
sol!(
13+
// `all_derives` - derives standard Rust traits.
14+
#![sol(all_derives)]
15+
// `extra_derives` - derives additional traits by specifying their path.
16+
#![sol(extra_derives(serde::Serialize, serde::Deserialize))]
17+
Colors,
18+
"examples/abi/Colors.json",
19+
);
20+
21+
fn main() -> eyre::Result<()> {
22+
let mut color_struct = Colors::Color::default();
23+
color_struct.r = 255;
24+
25+
// serde::Serialize is derived for types passed to the `sol!` macro.
26+
let json = serde_json::to_string_pretty(&color_struct)?;
27+
println!("{json}");
28+
29+
// serde::Deserialize is derived for all types in the abi.
30+
let deserialized: Colors::Color = serde_json::from_str(&json)?;
31+
println!("{:?}", deserialized);
32+
Ok(())
33+
}

0 commit comments

Comments
 (0)