forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoding_json_abi.rs
69 lines (56 loc) · 2.03 KB
/
decoding_json_abi.rs
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
//! Example for deserializing ABI using `json_abi`.
use alloy::json_abi::JsonAbi;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Get the contract abi.
let path = std::env::current_dir()?.join("examples/advanced/examples/abi/SimpleLending.json");
let contents = std::fs::read(path)?;
let abi: JsonAbi = serde_json::from_slice(&contents)?;
// Print deserialized ABI components
println!("Deserialized ABI:");
// Constructor
if let Some(constructor) = &abi.constructor {
println!("\n>> Constructor:");
println!(" Inputs: {:?}", constructor.inputs);
println!(" State mutability: {:?}", constructor.state_mutability);
}
println!("\n=========\n");
// Functions
println!("Functions:");
for (name, functions) in &abi.functions {
println!("\n>> {}:", name);
for function in functions {
println!(" Inputs: {:?}", function.inputs);
println!(" Outputs: {:?}", function.outputs);
println!(" State mutability: {:?}", function.state_mutability);
}
}
println!("\n=========\n");
// Events
println!("Events:");
for (name, events) in &abi.events {
println!("\n>> {}:", name);
for event in events {
println!(" Inputs: {:?}", event.inputs);
println!(" Anonymous: {}", event.anonymous);
}
}
println!("\n=========\n");
// Errors
println!("Errors:");
for (name, errors) in &abi.errors {
println!(">> {}:", name);
for error in errors {
println!(" Inputs: {:?}", error.inputs);
}
}
println!("\n=========\n");
// Example of working with a specific function
if let Some(add_collateral) = abi.functions.get("addCollateral").and_then(|f| f.first()) {
println!("Example: addCollateral() function exists!");
println!("Inputs:");
for input in &add_collateral.inputs {
println!(" Name: {}, Type: {}", input.name, input.ty);
}
}
Ok(())
}