Skip to content

Commit 549f59f

Browse files
Added Amount mod
1 parent bdf10de commit 549f59f

4 files changed

Lines changed: 112 additions & 4 deletions

File tree

examples/get_balance.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use wavesplatform::node::{Node, MAINNET_URL};
2+
use wavesplatform::util::Amount;
23

34
#[tokio::main]
45
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -9,13 +10,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
910
.get_balance("3PEktVux2RhchSN63DsDo4b4mz4QqzKSeDv")
1011
.await?;
1112

12-
println!("Balance: {} WAVES", result.balance());
13+
let balance = Amount::from_wavelet(result.balance());
14+
15+
println!("Balance: {} WAVES", balance);
1316

1417
let result = node
1518
.get_balance_details("3PEktVux2RhchSN63DsDo4b4mz4QqzKSeDv")
1619
.await?;
1720

18-
println!("Regular balance: {} WAVES", result.regular());
21+
let balance = Amount::from_wavelet(result.regular());
22+
23+
println!("Regular balance: {} WAVES", balance);
1924

2025
Ok(())
2126
}

src/node.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl<'a> Node<'a> {
3434
/// Get the regular balance in WAVES at a given address
3535
/// ```no_run
3636
/// use wavesplatform::node::{Node, MAINNET_URL};
37+
/// use wavesplatform::util::Amount;
3738
///
3839
/// #[tokio::main]
3940
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -43,7 +44,9 @@ impl<'a> Node<'a> {
4344
/// .get_balance("3PEktVux2RhchSN63DsDo4b4mz4QqzKSeDv")
4445
/// .await?;
4546
///
46-
/// println!("Balance: {} WAVES", result.balance());
47+
/// let balance = Amount::from_wavelet(result.balance());
48+
///
49+
/// println!("Balance: {} WAVES", balance);
4750
///
4851
/// Ok(())
4952
/// }
@@ -62,6 +65,7 @@ impl<'a> Node<'a> {
6265
/// Get the available, regular, generating, and effective balance
6366
/// ```no_run
6467
/// use wavesplatform::node::{Node, MAINNET_URL};
68+
/// use wavesplatform::util::Amount;
6569
///
6670
/// #[tokio::main]
6771
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -71,7 +75,9 @@ impl<'a> Node<'a> {
7175
/// .get_balance_details("3PEktVux2RhchSN63DsDo4b4mz4QqzKSeDv")
7276
/// .await?;
7377
///
74-
/// println!("Regular balance: {} WAVES", result.regular());
78+
/// let balance = Amount::from_wavelet(result.regular());
79+
///
80+
/// println!("Regular balance: {} WAVES", balance);
7581
///
7682
/// Ok(())
7783
/// }

src/util.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
mod amount;
2+
13
use curve25519_dalek::montgomery::MontgomeryPoint;
24
use ed25519_dalek::*;
35

6+
pub use amount::*;
7+
48
/// Signature verify function
59
pub fn sig_verify(
610
message: &[u8],

src/util/amount.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::fmt;
2+
use std::str::FromStr;
3+
4+
/// The number of decimal places (decimals) for WAVES is 8.
5+
const DECIMALS: usize = 8;
6+
7+
/// The [`Amount`] type can be used to express Waves amounts that supports conversion to various denominations.
8+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9+
pub struct Amount(u64);
10+
11+
impl Amount {
12+
/// The zero amount.
13+
pub const ZERO: Amount = Amount(0);
14+
/// Exactly one WAVELET.
15+
pub const ONE_WAVELET: Amount = Amount(1);
16+
/// Exactly one WAVES.
17+
pub const ONE_WAVES: Amount = Amount(100_000_000);
18+
19+
/// Create an [`Amount`] with WAVELET precision and the given number of WAVELET.
20+
pub fn from_wavelet(wavelet: u64) -> Amount {
21+
Amount(wavelet)
22+
}
23+
24+
/// The maximum value of an [`Amount`].
25+
pub fn max_value() -> Amount {
26+
Amount(u64::max_value())
27+
}
28+
29+
/// The minimum value of an [`Amount`].
30+
pub fn min_value() -> Amount {
31+
Amount(u64::min_value())
32+
}
33+
34+
/// Get the number of WAVELET in this [`Amount`].
35+
pub fn as_wavelet(self) -> u64 {
36+
self.0
37+
}
38+
39+
/// Express this [`Amount`] as a floating-point value in WAVES.
40+
pub fn as_waves(self) -> f64 {
41+
let real = format!("{:0width$}", self.0, width = DECIMALS);
42+
43+
if real.len() == DECIMALS {
44+
let result = format!("0.{}", &real[real.len() - DECIMALS..]);
45+
f64::from_str(&result).unwrap()
46+
} else {
47+
let result = format!(
48+
"{}.{}",
49+
&real[0..(real.len() - DECIMALS)],
50+
&real[real.len() - DECIMALS..]
51+
);
52+
f64::from_str(&result).unwrap()
53+
}
54+
}
55+
}
56+
57+
impl Default for Amount {
58+
fn default() -> Self {
59+
Amount::ZERO
60+
}
61+
}
62+
63+
impl fmt::Display for Amount {
64+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65+
write!(f, "{}", self.as_waves())
66+
}
67+
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
73+
#[test]
74+
fn test() {
75+
let wavelet = 1_000;
76+
let balance = Amount::from_wavelet(wavelet);
77+
assert_eq!(balance.as_waves(), 0.00001);
78+
79+
let wavelet = 1_000_000;
80+
let balance = Amount::from_wavelet(wavelet);
81+
assert_eq!(balance.as_waves(), 0.01);
82+
83+
let wavelet = 1_000_000_000;
84+
let balance = Amount::from_wavelet(wavelet);
85+
assert_eq!(balance.as_waves(), 10.0);
86+
}
87+
88+
#[test]
89+
fn test_one_waves() {
90+
let one_waves = Amount::ONE_WAVES;
91+
assert_eq!(one_waves.as_waves(), 1.0);
92+
}
93+
}

0 commit comments

Comments
 (0)