1- use std:: collections:: BTreeMap ;
21use std:: mem:: size_of;
32
43use colored:: Colorize ;
@@ -30,14 +29,16 @@ pub fn print_book(sdk: &SDKClient, market: &Pubkey, book: &Ladder) -> anyhow::Re
3029 ) )
3130 } ) ;
3231 let price_precision: usize = get_precision (
33- 10_u64 . pow ( meta. quote_decimals ) / meta. tick_size_in_quote_atoms_per_base_unit ,
32+ 10_u64 . pow ( meta. quote_decimals ) * meta. raw_base_units_per_base_unit as u64
33+ / meta. tick_size_in_quote_atoms_per_base_unit ,
3434 ) ;
35- let size_precision: usize = get_precision ( meta. num_base_lots_per_base_unit ) ;
35+ let size_precision: usize =
36+ get_precision ( meta. num_base_lots_per_base_unit / meta. raw_base_units_per_base_unit as u64 ) ;
3637 let bid_strings = bids
3738 . into_iter ( )
3839 . map ( |( price, size) | {
39- let p = format ! ( "{:.1$}" , price, price_precision) ;
40- let s = format ! ( "{:.1$}" , size, size_precision) . green ( ) ;
40+ let p = format_float ( price, price_precision) ;
41+ let s = format_float ( size, size_precision) . green ( ) ;
4142 ( s, p)
4243 } )
4344 . collect :: < Vec < _ > > ( ) ;
@@ -48,8 +49,8 @@ pub fn print_book(sdk: &SDKClient, market: &Pubkey, book: &Ladder) -> anyhow::Re
4849 . into_iter ( )
4950 . rev ( )
5051 . map ( |( price, size) | {
51- let p = format ! ( "{:.1$}" , price, price_precision) ;
52- let s = format ! ( "{:.1$}" , size, size_precision) . red ( ) ;
52+ let p = format_float ( price, price_precision) ;
53+ let s = format_float ( size, size_precision) . red ( ) ;
5354 ( p, s)
5455 } )
5556 . collect :: < Vec < _ > > ( ) ;
@@ -81,19 +82,19 @@ pub fn print_book(sdk: &SDKClient, market: &Pubkey, book: &Ladder) -> anyhow::Re
8182}
8283
8384pub fn get_precision ( mut target : u64 ) -> usize {
84- let mut prime_factors = BTreeMap :: new ( ) ;
85- let mut candidate = 2 ;
86- while target > 1 {
87- if target % candidate == 0 {
88- * prime_factors. entry ( candidate) . or_insert ( 0 ) += 1 ;
89- target /= candidate;
90- } else {
91- candidate += 1 ;
92- }
85+ let mut fives = 0 ;
86+ let mut twos = 0 ;
87+ let initial = target;
88+ while target > 0 && target % 5 == 0 {
89+ target /= 5 ;
90+ fives += 1 ;
91+ }
92+ while target > 0 && target % 2 == 0 {
93+ target /= 2 ;
94+ twos += 1 ;
9395 }
94- let precision =
95- ( * prime_factors. get ( & 2 ) . unwrap_or ( & 0 ) ) . max ( * prime_factors. get ( & 5 ) . unwrap_or ( & 0 ) ) ;
96- if precision == 0 {
96+ let precision = twos. max ( fives) ;
97+ if precision == 0 && initial != 0 {
9798 // In the off chance that the target does not have 2 or 5 as a prime factor,
9899 // we'll just return a precision of 3 decimals.
99100 3
@@ -102,6 +103,21 @@ pub fn get_precision(mut target: u64) -> usize {
102103 }
103104}
104105
106+ pub fn format_float ( float : f64 , precision : usize ) -> String {
107+ if precision > 3 && float. abs ( ) < 1.0 {
108+ // Use scientific notation for small numbers
109+ format ! ( "{:.1$e}" , float, 3 )
110+ } else if float > 1e9 {
111+ let prefix = format_float ( float / 1e9 , 3 ) ;
112+ format ! ( "{}B" , prefix)
113+ } else if float > 1e6 {
114+ let prefix = format_float ( float / 1e6 , 3 ) ;
115+ format ! ( "{}M" , prefix)
116+ } else {
117+ format ! ( "{:.1$}" , float, precision)
118+ }
119+ }
120+
105121pub fn print_market_summary_data ( market_pubkey : & Pubkey , header : & MarketHeader ) {
106122 let base_pubkey = header. base_params . mint_key ;
107123 let quote_pubkey = header. quote_params . mint_key ;
0 commit comments