@@ -855,35 +855,95 @@ func formatNumber(num uint64) string {
855855 return string (result )
856856}
857857
858- // formatBaseFee formats base fee in wei to human-readable format
858+ // formatBaseFee formats base fee in wei to human-readable format using appropriate SI units
859859func formatBaseFee (baseFee * big.Int ) string {
860860 if baseFee == nil || baseFee .Cmp (big .NewInt (0 )) == 0 {
861861 return "0"
862862 }
863863
864- // Convert to gwei (wei / 1e9)
865- gwei := new (big.Int ).Div (baseFee , big .NewInt (1000000000 ))
864+ // Define units with their divisors, names, thresholds, and decimal precision
865+ type unit struct {
866+ divisor * big.Int
867+ name string
868+ threshold * big.Int
869+ decimals int
870+ }
871+
872+ units := []unit {
873+ {new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (18 ), nil ), "ether" , new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (18 ), nil ), 3 }, // 10^18
874+ {new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (15 ), nil ), "milli" , new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (15 ), nil ), 3 }, // 10^15 milliether
875+ {new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (12 ), nil ), "micro" , new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (12 ), nil ), 3 }, // 10^12 microether
876+ {new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (9 ), nil ), "gwei" , new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (9 ), nil ), 3 }, // 10^9 gwei
877+ {new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (6 ), nil ), "mwei" , new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (6 ), nil ), 3 }, // 10^6 megawei
878+ {new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (3 ), nil ), "kwei" , new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (3 ), nil ), 3 }, // 10^3 kilowei
879+ {big .NewInt (1 ), "wei" , big .NewInt (1 ), 0 }, // wei (no decimals)
880+ }
881+
882+ // Find the appropriate unit (largest unit where value >= threshold)
883+ for _ , u := range units {
884+ if baseFee .Cmp (u .threshold ) >= 0 {
885+ if u .name == "wei" {
886+ // For wei, add thousand separators for values >= 1,000
887+ weiStr := baseFee .String ()
888+ if len (weiStr ) > 3 {
889+ return fmt .Sprintf ("%s wei" , addThousandSeparators (weiStr ))
890+ }
891+ return fmt .Sprintf ("%s wei" , weiStr )
892+ }
866893
867- // If less than 1 gwei, show in wei
868- if gwei .Cmp (big .NewInt (0 )) == 0 {
869- return fmt .Sprintf ("%s wei" , baseFee .String ())
894+ // Convert to the selected unit using big.Float for precision
895+ value := new (big.Float ).SetInt (baseFee )
896+ divisor := new (big.Float ).SetInt (u .divisor )
897+ result := new (big.Float ).Quo (value , divisor )
898+
899+ // Format with appropriate precision
900+ formatStr := fmt .Sprintf ("%%.%df %%s" , u .decimals )
901+ resultFloat , _ := result .Float64 ()
902+
903+ // Remove trailing zeros from decimal representation
904+ formatted := fmt .Sprintf (formatStr , resultFloat , u .name )
905+ return removeTrailingZeros (formatted )
906+ }
870907 }
871908
872- // Format with thousand separators
873- gweiStr := gwei .String ()
874- if len (gweiStr ) <= 3 {
875- return fmt .Sprintf ("%s gwei" , gweiStr )
909+ // Fallback (should never reach here)
910+ return baseFee .String () + " wei"
911+ }
912+
913+ // addThousandSeparators adds commas to a numeric string for readability
914+ func addThousandSeparators (numStr string ) string {
915+ if len (numStr ) <= 3 {
916+ return numStr
876917 }
877918
878- // Add commas for readability
879919 var result []rune
880- for i , char := range gweiStr {
881- if i > 0 && (len (gweiStr )- i )% 3 == 0 {
920+ for i , char := range numStr {
921+ if i > 0 && (len (numStr )- i )% 3 == 0 {
882922 result = append (result , ',' )
883923 }
884924 result = append (result , char )
885925 }
886- return fmt .Sprintf ("%s gwei" , string (result ))
926+ return string (result )
927+ }
928+
929+ // removeTrailingZeros removes trailing zeros from decimal numbers in formatted strings
930+ func removeTrailingZeros (formatted string ) string {
931+ // Split on space to separate number from unit
932+ parts := strings .Split (formatted , " " )
933+ if len (parts ) != 2 {
934+ return formatted
935+ }
936+
937+ numberPart := parts [0 ]
938+ unitPart := parts [1 ]
939+
940+ // If it contains a decimal point, remove trailing zeros
941+ if strings .Contains (numberPart , "." ) {
942+ numberPart = strings .TrimRight (numberPart , "0" )
943+ numberPart = strings .TrimRight (numberPart , "." )
944+ }
945+
946+ return numberPart + " " + unitPart
887947}
888948
889949// formatGasPercentage calculates and formats gas usage percentage
0 commit comments