solana_native_token/
lib.rs

1//! Definitions for the native SOL token and its fractional lamports.
2
3#![allow(clippy::arithmetic_side_effects)]
4
5/// There are 10^9 lamports in one SOL
6pub const LAMPORTS_PER_SOL: u64 = 1_000_000_000;
7
8/// Approximately convert fractional native tokens (lamports) into native tokens (SOL)
9pub fn lamports_to_sol(lamports: u64) -> f64 {
10    lamports as f64 / LAMPORTS_PER_SOL as f64
11}
12
13/// Approximately convert native tokens (SOL) into fractional native tokens (lamports)
14pub fn sol_to_lamports(sol: f64) -> u64 {
15    (sol * LAMPORTS_PER_SOL as f64) as u64
16}
17
18use std::fmt::{Debug, Display, Formatter, Result};
19pub struct Sol(pub u64);
20
21impl Sol {
22    fn write_in_sol(&self, f: &mut Formatter) -> Result {
23        write!(
24            f,
25            "◎{}.{:09}",
26            self.0 / LAMPORTS_PER_SOL,
27            self.0 % LAMPORTS_PER_SOL
28        )
29    }
30}
31
32impl Display for Sol {
33    fn fmt(&self, f: &mut Formatter) -> Result {
34        self.write_in_sol(f)
35    }
36}
37
38impl Debug for Sol {
39    fn fmt(&self, f: &mut Formatter) -> Result {
40        self.write_in_sol(f)
41    }
42}