Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion genesis-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ solana-hash = { workspace = true }
solana-inflation = { workspace = true }
solana-keypair = { workspace = true }
solana-logger = { workspace = true }
solana-native-token = { workspace = true }
solana-poh-config = { workspace = true }
solana-pubkey = { workspace = true }
solana-rent = { workspace = true }
Expand Down
19 changes: 8 additions & 11 deletions genesis-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use {
chrono::{TimeZone, Utc},
memmap2::Mmap,
solana_hash::Hash,
solana_native_token::lamports_to_sol_str,
solana_sha256_hasher::hash,
solana_shred_version::compute_shred_version,
std::{
Expand Down Expand Up @@ -236,7 +235,7 @@ impl fmt::Display for GenesisConfig {
{:?}\n\
{:?}\n\
{:?}\n\
Capitalization: {} SOL in {} accounts\n\
Capitalization: {} lamports in {} accounts\n\
Native instruction processors: {:#?}\n\
Rewards pool: {:#?}\n\
",
Expand All @@ -259,15 +258,13 @@ impl fmt::Display for GenesisConfig {
self.inflation,
self.rent,
self.fee_rate_governor,
lamports_to_sol_str(
self.accounts
.iter()
.map(|(pubkey, account)| {
assert!(account.lamports > 0, "{:?}", (pubkey, account));
account.lamports
})
.sum::<u64>()
),
self.accounts
.iter()
.map(|(pubkey, account)| {
assert!(account.lamports > 0, "{:?}", (pubkey, account));
account.lamports
})
.sum::<u64>(),
self.accounts.len(),
self.native_instruction_processors,
self.rewards_pools,
Expand Down
34 changes: 4 additions & 30 deletions native-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const LAMPORTS_PER_SOL_F64: f64 = LAMPORTS_PER_SOL as f64;
const SOL_DECIMALS: usize = 9;

/// Approximately convert fractional native tokens (lamports) into native tokens (SOL)
#[deprecated(since = "2.3.0", note = "lamports_to_sol_str")]
#[deprecated(
since = "2.3.0",
note = "solana_cli_output::display::build_balance_message"
)]
pub fn lamports_to_sol(lamports: u64) -> f64 {
lamports as f64 / LAMPORTS_PER_SOL_F64
}
Expand All @@ -21,16 +24,6 @@ pub fn sol_to_lamports(sol: f64) -> u64 {
(sol * LAMPORTS_PER_SOL_F64).round() as u64
}

/// Convert fractional native tokens (lamports) into native tokens (SOL)
pub fn lamports_to_sol_str(lamports: u64) -> String {
// Left-pad zeros to decimals + 1, so we at least have an integer zero
let mut s = format!("{:01$}", lamports, SOL_DECIMALS + 1);
// Add the decimal point (Sorry, "," locales!)
s.insert(s.len() - SOL_DECIMALS, '.');
let zeros_trimmed = s.trim_end_matches('0');
zeros_trimmed.trim_end_matches('.').to_string()
}

/// Convert native tokens (SOL) into fractional native tokens (lamports)
pub fn sol_str_to_lamports(sol_str: &str) -> Option<u64> {
if sol_str == "." {
Expand Down Expand Up @@ -153,25 +146,6 @@ mod tests {
);
}

#[test]
fn test_lamports_to_sol_str() {
assert_eq!("0", lamports_to_sol_str(0));
assert_eq!("0.000000001", lamports_to_sol_str(1));
assert_eq!("0.00000001", lamports_to_sol_str(10));
assert_eq!("0.0000001", lamports_to_sol_str(100));
assert_eq!("0.000001", lamports_to_sol_str(1000));
assert_eq!("0.00001", lamports_to_sol_str(10000));
assert_eq!("0.0001", lamports_to_sol_str(100000));
assert_eq!("0.001", lamports_to_sol_str(1000000));
assert_eq!("0.01", lamports_to_sol_str(10000000));
assert_eq!("0.1", lamports_to_sol_str(100000000));
assert_eq!("1", lamports_to_sol_str(1000000000));
assert_eq!("4.1", lamports_to_sol_str(4_100_000_000));
assert_eq!("8.2", lamports_to_sol_str(8_200_000_000));
assert_eq!("8.50228288", lamports_to_sol_str(8_502_282_880));
assert_eq!("18446744073.709551615", lamports_to_sol_str(u64::MAX));
}

#[test]
fn test_sol_str_to_lamports() {
assert_eq!(0, sol_str_to_lamports("0.0").unwrap());
Expand Down