forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_utilities.rs
More file actions
101 lines (75 loc) · 3.14 KB
/
Copy pathmath_utilities.rs
File metadata and controls
101 lines (75 loc) · 3.14 KB
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
//! Example of using math utilities to handle big numbers in 'wei' units.
use alloy::primitives::{
utils::{format_units, parse_units},
U256,
};
use eyre::Result;
fn main() -> Result<()> {
parse_units_example()?;
format_units_example()?;
Ok(())
}
/// dApps business logics handle big numbers in 'wei' units (i.e. sending transactions, on-chain
/// math, etc.). We provide convenient methods to map user inputs (usually in 'ether' or 'gwei')
/// into 'wei' format.
fn parse_units_example() -> Result<()> {
let pu = parse_units("1.0", "wei")?;
let num: U256 = pu.into();
assert_eq!(num, U256::from(1));
let pu = parse_units("1.0", "kwei")?;
let num: U256 = pu.into();
assert_eq!(num, U256::from(1000));
let pu = parse_units("1.0", "mwei")?;
let num: U256 = pu.into();
assert_eq!(num, U256::from(1000000));
let pu = parse_units("1.0", "gwei")?;
let num: U256 = pu.into();
assert_eq!(num, U256::from(1000000000));
let pu = parse_units("1.0", "szabo")?;
let num: U256 = pu.into();
assert_eq!(num, U256::from(1000000000000_u128));
let pu = parse_units("1.0", "finney")?;
let num: U256 = pu.into();
assert_eq!(num, U256::from(1000000000000000_u128));
let pu = parse_units("1.0", "ether")?;
let num: U256 = pu.into();
assert_eq!(num, U256::from(1000000000000000000_u128));
let pu = parse_units("1.0", 18)?;
let num: U256 = pu.into();
assert_eq!(num, U256::from(1000000000000000000_u128));
Ok(())
}
/// dApps business logics handle big numbers in 'wei' units (i.e. sending transactions, on-chain
/// math, etc.). On the other hand it is useful to convert big numbers into user readable formats
/// when displaying on a UI. Generally dApps display numbers in 'ether' and 'gwei' units,
/// respectively for displaying amounts and gas. The `format_units` function will format a big
/// number into a user readable string.
fn format_units_example() -> Result<()> {
// 1 ETHER = 10^18 WEI
let one_ether = U256::from(1000000000000000000_u128);
let num: String = format_units(one_ether, "wei")?;
assert_eq!(num, "1000000000000000000.0");
let num: String = format_units(one_ether, "gwei")?;
assert_eq!(num, "1000000000.000000000");
let num: String = format_units(one_ether, "ether")?;
assert_eq!(num, "1.000000000000000000");
// 1 GWEI = 10^9 WEI
let one_gwei = U256::from(1000000000_u128);
let num: String = format_units(one_gwei, 0)?;
assert_eq!(num, "1000000000.0");
let num: String = format_units(one_gwei, "wei")?;
assert_eq!(num, "1000000000.0");
let num: String = format_units(one_gwei, "kwei")?;
assert_eq!(num, "1000000.000");
let num: String = format_units(one_gwei, "mwei")?;
assert_eq!(num, "1000.000000");
let num: String = format_units(one_gwei, "gwei")?;
assert_eq!(num, "1.000000000");
let num: String = format_units(one_gwei, "szabo")?;
assert_eq!(num, "0.001000000000");
let num: String = format_units(one_gwei, "finney")?;
assert_eq!(num, "0.000001000000000");
let num: String = format_units(one_gwei, "ether")?;
assert_eq!(num, "0.000000001000000000");
Ok(())
}