@@ -4,7 +4,7 @@ use clap::{
44} ;
55use console:: Emoji ;
66use solana_account_decoder:: {
7- parse_token:: { TokenAccountType , UiAccountState , UiTokenAmount } ,
7+ parse_token:: { TokenAccountType , UiAccountState } ,
88 UiAccountData ,
99} ;
1010use solana_clap_utils:: {
@@ -424,32 +424,41 @@ fn command_transfer(
424424 mint_address : Option < Pubkey > ,
425425 mint_decimals : Option < u8 > ,
426426) -> CommandResult {
427- let sender_balance = config
427+ let ( mint_pubkey, decimals) = resolve_mint_info ( config, & sender, mint_address, mint_decimals) ?;
428+ let sender_token_amount = config
428429 . rpc_client
429430 . get_token_account_balance ( & sender)
430431 . map_err ( |err| {
431432 format ! (
432433 "Error: Failed to get token balance of sender address {}: {}" ,
433434 sender, err
434435 )
435- } ) ?
436- . ui_amount ;
437- let ui_amount = ui_amount. unwrap_or ( sender_balance) ;
436+ } ) ?;
437+ let sender_balance = sender_token_amount. amount . parse :: < u64 > ( ) . map_err ( |err| {
438+ format ! (
439+ "Token account {} balance could not be parsed: {}" ,
440+ sender, err
441+ )
442+ } ) ?;
443+ let transfer_balance = ui_amount
444+ . map ( |ui_amount| spl_token:: ui_amount_to_amount ( ui_amount, decimals) )
445+ . unwrap_or ( sender_balance) ;
438446
439447 println ! (
440448 "Transfer {} tokens\n Sender: {}\n Recipient: {}" ,
441- ui_amount, sender, recipient
449+ spl_token:: amount_to_ui_amount( transfer_balance, decimals) ,
450+ sender,
451+ recipient
442452 ) ;
443- if ui_amount > sender_balance {
453+
454+ if transfer_balance > sender_balance {
444455 return Err ( format ! (
445456 "Error: Sender has insufficient funds, current balance is {}" ,
446- sender_balance
457+ sender_token_amount . real_number_string_trimmed ( )
447458 )
448459 . into ( ) ) ;
449460 }
450461
451- let ( mint_pubkey, decimals) = resolve_mint_info ( config, & sender, mint_address, mint_decimals) ?;
452- let amount = spl_token:: ui_amount_to_amount ( ui_amount, decimals) ;
453462 let mut instructions = vec ! [ ] ;
454463
455464 let mut recipient_token_account = recipient;
@@ -523,7 +532,7 @@ fn command_transfer(
523532 & recipient_token_account,
524533 & config. owner ,
525534 & config. multisigner_pubkeys ,
526- amount ,
535+ transfer_balance ,
527536 decimals,
528537 ) ?) ;
529538 Ok ( Some ( (
@@ -726,11 +735,22 @@ fn command_close(config: &Config, account: Pubkey, destination: Pubkey) -> Comma
726735 . rpc_client
727736 . get_token_account ( & account) ?
728737 . ok_or_else ( || format ! ( "Could not find token account {}" , account) ) ?;
738+ let source_amount = source_account
739+ . token_amount
740+ . amount
741+ . parse :: < u64 > ( )
742+ . map_err ( |err| {
743+ format ! (
744+ "Token account {} balance could not be parsed: {}" ,
745+ account, err
746+ )
747+ } ) ?;
729748
730- if !source_account. is_native && source_account . token_amount . ui_amount > 0. 0 {
749+ if !source_account. is_native && source_amount > 0 {
731750 return Err ( format ! (
732751 "Account {} still has {} tokens; empty the account in order to close it." ,
733- account, source_account. token_amount. ui_amount
752+ account,
753+ source_account. token_amount. real_number_string_trimmed( )
734754 )
735755 . into ( ) ) ;
736756 }
@@ -750,19 +770,19 @@ fn command_balance(config: &Config, address: Pubkey) -> CommandResult {
750770 let balance = config. rpc_client . get_token_account_balance ( & address) ?;
751771
752772 if config. verbose {
753- println ! ( "ui amount: {}" , balance. ui_amount ) ;
773+ println ! ( "ui amount: {}" , balance. real_number_string_trimmed ( ) ) ;
754774 println ! ( "decimals: {}" , balance. decimals) ;
755775 println ! ( "amount: {}" , balance. amount) ;
756776 } else {
757- println ! ( "{}" , balance. ui_amount ) ;
777+ println ! ( "{}" , balance. real_number_string_trimmed ( ) ) ;
758778 }
759779 Ok ( None )
760780}
761781
762782fn command_supply ( config : & Config , address : Pubkey ) -> CommandResult {
763783 let supply = config. rpc_client . get_token_supply ( & address) ?;
764784
765- println ! ( "{}" , supply. ui_amount ) ;
785+ println ! ( "{}" , supply. real_number_string_trimmed ( ) ) ;
766786 Ok ( None )
767787}
768788
@@ -805,14 +825,16 @@ fn command_accounts(config: &Config, token: Option<Pubkey>) -> CommandResult {
805825 if token. is_some ( ) {
806826 println ! (
807827 "{:<44} {}{}" ,
808- address, ui_token_account. token_amount. ui_amount, maybe_frozen
828+ address,
829+ ui_token_account. token_amount. real_number_string_trimmed( ) ,
830+ maybe_frozen
809831 )
810832 } else {
811833 println ! (
812834 "{:<44} {:<44} {}{}" ,
813835 address,
814836 ui_token_account. mint,
815- ui_token_account. token_amount. ui_amount ,
837+ ui_token_account. token_amount. real_number_string_trimmed ( ) ,
816838 maybe_frozen
817839 )
818840 }
@@ -865,6 +887,11 @@ fn command_gc(config: &Config) -> CommandResult {
865887 . pubkey
866888 . parse :: < Pubkey > ( )
867889 . unwrap_or_else ( |err| panic ! ( "Invalid token account: {}" , err) ) ;
890+ let token_amount = ui_token_account
891+ . token_amount
892+ . amount
893+ . parse :: < u64 > ( )
894+ . unwrap_or_else ( |err| panic ! ( "Invalid token amount: {}" , err) ) ;
868895
869896 let close_authority =
870897 ui_token_account. close_authority . map_or ( config. owner , |s| {
@@ -876,10 +903,7 @@ fn command_gc(config: &Config) -> CommandResult {
876903 entry. insert (
877904 token_account,
878905 (
879- spl_token:: ui_amount_to_amount (
880- ui_token_account. token_amount . ui_amount ,
881- ui_token_account. token_amount . decimals ,
882- ) ,
906+ token_amount,
883907 ui_token_account. token_amount . decimals ,
884908 frozen,
885909 close_authority,
@@ -954,36 +978,13 @@ fn command_gc(config: &Config) -> CommandResult {
954978 Ok ( Some ( ( lamports_needed, instructions) ) )
955979}
956980
957- fn stringify_ui_token_amount ( amount : & UiTokenAmount ) -> String {
958- let decimals = amount. decimals as usize ;
959- if decimals > 0 {
960- let amount = u64:: from_str ( & amount. amount ) . unwrap ( ) ;
961-
962- // Left-pad zeros to decimals + 1, so we at least have an integer zero
963- let mut s = format ! ( "{:01$}" , amount, decimals + 1 ) ;
964-
965- // Add the decimal point (Sorry, "," locales!)
966- s. insert ( s. len ( ) - decimals, '.' ) ;
967- s
968- } else {
969- amount. amount . clone ( )
970- }
971- }
972-
973- fn stringify_ui_token_amount_trimmed ( amount : & UiTokenAmount ) -> String {
974- let s = stringify_ui_token_amount ( amount) ;
975- let zeros_trimmed = s. trim_end_matches ( '0' ) ;
976- let decimal_trimmed = zeros_trimmed. trim_end_matches ( '.' ) ;
977- decimal_trimmed. to_string ( )
978- }
979-
980981fn command_account_info ( config : & Config , address : Pubkey ) -> CommandResult {
981982 let account = config. rpc_client . get_token_account ( & address) ?. unwrap ( ) ;
982983 println ! ( ) ;
983984 println_name_value ( "Address:" , & address. to_string ( ) ) ;
984985 println_name_value (
985986 "Balance:" ,
986- & stringify_ui_token_amount_trimmed ( & account. token_amount ) ,
987+ & account. token_amount . real_number_string_trimmed ( ) ,
987988 ) ;
988989 let mint = format ! (
989990 "{}{}" ,
@@ -997,10 +998,7 @@ fn command_account_info(config: &Config, address: Pubkey) -> CommandResult {
997998 println ! ( "Delegation:" ) ;
998999 println_name_value ( " Delegate:" , delegate) ;
9991000 let allowance = account. delegated_amount . as_ref ( ) . unwrap ( ) ;
1000- println_name_value (
1001- " Allowance:" ,
1002- & stringify_ui_token_amount_trimmed ( & allowance) ,
1003- ) ;
1001+ println_name_value ( " Allowance:" , & allowance. real_number_string_trimmed ( ) ) ;
10041002 } else {
10051003 println_name_value ( "Delegation:" , "" ) ;
10061004 }
0 commit comments