@@ -4,7 +4,7 @@ use clap::{
4
4
} ;
5
5
use console:: Emoji ;
6
6
use solana_account_decoder:: {
7
- parse_token:: { TokenAccountType , UiAccountState , UiTokenAmount } ,
7
+ parse_token:: { TokenAccountType , UiAccountState } ,
8
8
UiAccountData ,
9
9
} ;
10
10
use solana_clap_utils:: {
@@ -424,32 +424,41 @@ fn command_transfer(
424
424
mint_address : Option < Pubkey > ,
425
425
mint_decimals : Option < u8 > ,
426
426
) -> 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
428
429
. rpc_client
429
430
. get_token_account_balance ( & sender)
430
431
. map_err ( |err| {
431
432
format ! (
432
433
"Error: Failed to get token balance of sender address {}: {}" ,
433
434
sender, err
434
435
)
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) ;
438
446
439
447
println ! (
440
448
"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
442
452
) ;
443
- if ui_amount > sender_balance {
453
+
454
+ if transfer_balance > sender_balance {
444
455
return Err ( format ! (
445
456
"Error: Sender has insufficient funds, current balance is {}" ,
446
- sender_balance
457
+ sender_token_amount . real_number_string_trimmed ( )
447
458
)
448
459
. into ( ) ) ;
449
460
}
450
461
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) ;
453
462
let mut instructions = vec ! [ ] ;
454
463
455
464
let mut recipient_token_account = recipient;
@@ -523,7 +532,7 @@ fn command_transfer(
523
532
& recipient_token_account,
524
533
& config. owner ,
525
534
& config. multisigner_pubkeys ,
526
- amount ,
535
+ transfer_balance ,
527
536
decimals,
528
537
) ?) ;
529
538
Ok ( Some ( (
@@ -726,11 +735,22 @@ fn command_close(config: &Config, account: Pubkey, destination: Pubkey) -> Comma
726
735
. rpc_client
727
736
. get_token_account ( & account) ?
728
737
. 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
+ } ) ?;
729
748
730
- if !source_account. is_native && source_account . token_amount . ui_amount > 0. 0 {
749
+ if !source_account. is_native && source_amount > 0 {
731
750
return Err ( format ! (
732
751
"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( )
734
754
)
735
755
. into ( ) ) ;
736
756
}
@@ -750,19 +770,19 @@ fn command_balance(config: &Config, address: Pubkey) -> CommandResult {
750
770
let balance = config. rpc_client . get_token_account_balance ( & address) ?;
751
771
752
772
if config. verbose {
753
- println ! ( "ui amount: {}" , balance. ui_amount ) ;
773
+ println ! ( "ui amount: {}" , balance. real_number_string_trimmed ( ) ) ;
754
774
println ! ( "decimals: {}" , balance. decimals) ;
755
775
println ! ( "amount: {}" , balance. amount) ;
756
776
} else {
757
- println ! ( "{}" , balance. ui_amount ) ;
777
+ println ! ( "{}" , balance. real_number_string_trimmed ( ) ) ;
758
778
}
759
779
Ok ( None )
760
780
}
761
781
762
782
fn command_supply ( config : & Config , address : Pubkey ) -> CommandResult {
763
783
let supply = config. rpc_client . get_token_supply ( & address) ?;
764
784
765
- println ! ( "{}" , supply. ui_amount ) ;
785
+ println ! ( "{}" , supply. real_number_string_trimmed ( ) ) ;
766
786
Ok ( None )
767
787
}
768
788
@@ -805,14 +825,16 @@ fn command_accounts(config: &Config, token: Option<Pubkey>) -> CommandResult {
805
825
if token. is_some ( ) {
806
826
println ! (
807
827
"{:<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
809
831
)
810
832
} else {
811
833
println ! (
812
834
"{:<44} {:<44} {}{}" ,
813
835
address,
814
836
ui_token_account. mint,
815
- ui_token_account. token_amount. ui_amount ,
837
+ ui_token_account. token_amount. real_number_string_trimmed ( ) ,
816
838
maybe_frozen
817
839
)
818
840
}
@@ -865,6 +887,11 @@ fn command_gc(config: &Config) -> CommandResult {
865
887
. pubkey
866
888
. parse :: < Pubkey > ( )
867
889
. 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) ) ;
868
895
869
896
let close_authority =
870
897
ui_token_account. close_authority . map_or ( config. owner , |s| {
@@ -876,10 +903,7 @@ fn command_gc(config: &Config) -> CommandResult {
876
903
entry. insert (
877
904
token_account,
878
905
(
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,
883
907
ui_token_account. token_amount . decimals ,
884
908
frozen,
885
909
close_authority,
@@ -954,36 +978,13 @@ fn command_gc(config: &Config) -> CommandResult {
954
978
Ok ( Some ( ( lamports_needed, instructions) ) )
955
979
}
956
980
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
-
980
981
fn command_account_info ( config : & Config , address : Pubkey ) -> CommandResult {
981
982
let account = config. rpc_client . get_token_account ( & address) ?. unwrap ( ) ;
982
983
println ! ( ) ;
983
984
println_name_value ( "Address:" , & address. to_string ( ) ) ;
984
985
println_name_value (
985
986
"Balance:" ,
986
- & stringify_ui_token_amount_trimmed ( & account. token_amount ) ,
987
+ & account. token_amount . real_number_string_trimmed ( ) ,
987
988
) ;
988
989
let mint = format ! (
989
990
"{}{}" ,
@@ -997,10 +998,7 @@ fn command_account_info(config: &Config, address: Pubkey) -> CommandResult {
997
998
println ! ( "Delegation:" ) ;
998
999
println_name_value ( " Delegate:" , delegate) ;
999
1000
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 ( ) ) ;
1004
1002
} else {
1005
1003
println_name_value ( "Delegation:" , "" ) ;
1006
1004
}
0 commit comments