-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
23 lines (19 loc) · 751 Bytes
/
utils.rs
File metadata and controls
23 lines (19 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::time::{SystemTime, UNIX_EPOCH};
use web3::types::U256;
pub fn get_nstime() -> u64 {
let dur = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
// The correct way to calculate the current time is
// `dur.as_secs() * 1_000_000_000 + dur.subsec_nanos() as u64`
// But this is faster, and the difference in terms of entropy is
// negligible (log2(10^9) == 29.9).
dur.as_secs() << 30 | dur.subsec_nanos() as u64
}
pub fn wei_to_eth(wei_val: U256) -> f64 {
let res = wei_val.as_u128() as f64;
res / 1_000_000_000_000_000_000.0
}
pub fn eth_to_wei(eth_val: f64) -> U256 {
let result = eth_val * 1_000_000_000_000_000_000.0;
let result = result as u128;
U256::from(result)
}