gemachain_program/
native_token.rs

1#![allow(clippy::integer_arithmetic)]
2/// There are 10^9 carats in one GEMA
3pub const CARATS_PER_GEMA: u64 = 1_000_000_000;
4
5/// Approximately convert fractional native tokens (carats) into native tokens (GEMA)
6pub fn carats_to_gema(carats: u64) -> f64 {
7    carats as f64 / CARATS_PER_GEMA as f64
8}
9
10/// Approximately convert native tokens (GEMA) into fractional native tokens (carats)
11pub fn gema_to_carats(nub: f64) -> u64 {
12    (nub * CARATS_PER_GEMA as f64) as u64
13}
14
15use std::fmt::{Debug, Display, Formatter, Result};
16pub struct Gema(pub u64);
17
18impl Gema {
19    fn write_in_gema(&self, f: &mut Formatter) -> Result {
20        write!(
21            f,
22            "GM {}.{:09}",
23            self.0 / CARATS_PER_GEMA,
24            self.0 % CARATS_PER_GEMA
25        )
26    }
27}
28
29impl Display for Gema {
30    fn fmt(&self, f: &mut Formatter) -> Result {
31        self.write_in_gema(f)
32    }
33}
34
35impl Debug for Gema {
36    fn fmt(&self, f: &mut Formatter) -> Result {
37        self.write_in_gema(f)
38    }
39}