@@ -688,38 +688,68 @@ fn command_thaw(config: &Config, account: Pubkey, mint_address: Option<Pubkey>)
688
688
Ok ( Some ( ( 0 , vec ! [ instructions] ) ) )
689
689
}
690
690
691
- fn command_wrap ( config : & Config , sol : f64 , account : Pubkey ) -> CommandResult {
691
+ fn command_wrap ( config : & Config , sol : f64 , account : Option < Pubkey > ) -> CommandResult {
692
692
let lamports = sol_to_lamports ( sol) ;
693
- println ! ( "Wrapping {} SOL into {}" , sol, account) ;
694
693
695
- let instructions = vec ! [
696
- system_instruction:: create_account(
697
- & config. owner,
698
- & account,
699
- lamports,
700
- Account :: LEN as u64 ,
701
- & spl_token:: id( ) ,
702
- ) ,
703
- initialize_account(
704
- & spl_token:: id( ) ,
705
- & account,
706
- & native_mint:: id( ) ,
707
- & config. owner,
708
- ) ?,
709
- ] ;
694
+ let instructions = if let Some ( account) = account {
695
+ println ! ( "Wrapping {} SOL into {}" , sol, account) ;
696
+ vec ! [
697
+ system_instruction:: create_account(
698
+ & config. owner,
699
+ & account,
700
+ lamports,
701
+ Account :: LEN as u64 ,
702
+ & spl_token:: id( ) ,
703
+ ) ,
704
+ initialize_account(
705
+ & spl_token:: id( ) ,
706
+ & account,
707
+ & native_mint:: id( ) ,
708
+ & config. owner,
709
+ ) ?,
710
+ ]
711
+ } else {
712
+ let account = get_associated_token_address ( & config. owner , & native_mint:: id ( ) ) ;
713
+
714
+ if !config. sign_only {
715
+ if let Some ( account_data) = config
716
+ . rpc_client
717
+ . get_account_with_commitment ( & account, config. rpc_client . commitment ( ) ) ?
718
+ . value
719
+ {
720
+ if account_data. owner != system_program:: id ( ) {
721
+ return Err ( format ! ( "Error: Account already exists: {}" , account) . into ( ) ) ;
722
+ }
723
+ }
724
+ }
725
+
726
+ println ! ( "Wrapping {} SOL into {}" , sol, account) ;
727
+ vec ! [
728
+ system_instruction:: transfer( & config. owner, & account, lamports) ,
729
+ create_associated_token_account( & config. fee_payer, & config. owner, & native_mint:: id( ) ) ,
730
+ ]
731
+ } ;
710
732
if !config. sign_only {
711
733
check_owner_balance ( config, lamports) ?;
712
734
}
713
735
Ok ( Some ( ( 0 , vec ! [ instructions] ) ) )
714
736
}
715
737
716
- fn command_unwrap ( config : & Config , address : Pubkey ) -> CommandResult {
738
+ fn command_unwrap ( config : & Config , address : Option < Pubkey > ) -> CommandResult {
739
+ let use_associated_account = address. is_none ( ) ;
740
+ let address =
741
+ address. unwrap_or_else ( || get_associated_token_address ( & config. owner , & native_mint:: id ( ) ) ) ;
717
742
println ! ( "Unwrapping {}" , address) ;
718
743
if !config. sign_only {
719
- println ! (
720
- " Amount: {} SOL" ,
721
- lamports_to_sol( config. rpc_client. get_balance( & address) ?) ,
722
- ) ;
744
+ let lamports = config. rpc_client . get_balance ( & address) ?;
745
+ if lamports == 0 {
746
+ if use_associated_account {
747
+ return Err ( "No wrapped SOL in associated account; did you mean to specify an auxiliary address?" . to_string ( ) . into ( ) ) ;
748
+ } else {
749
+ return Err ( format ! ( "No wrapped SOL in {}" , address) . into ( ) ) ;
750
+ }
751
+ }
752
+ println ! ( " Amount: {} SOL" , lamports_to_sol( lamports) , ) ;
723
753
}
724
754
println ! ( " Recipient: {}" , & config. owner) ;
725
755
@@ -1577,6 +1607,12 @@ fn main() {
1577
1607
. required ( true )
1578
1608
. help ( "Amount of SOL to wrap" ) ,
1579
1609
)
1610
+ . arg (
1611
+ Arg :: with_name ( "create_aux_account" )
1612
+ . takes_value ( false )
1613
+ . long ( "create-aux-account" )
1614
+ . help ( "Wrap SOL in an auxillary account instead of associated token account" ) ,
1615
+ )
1580
1616
. nonce_args ( true )
1581
1617
. offline_args ( ) ,
1582
1618
)
@@ -1589,8 +1625,8 @@ fn main() {
1589
1625
. value_name ( "TOKEN_ACCOUNT_ADDRESS" )
1590
1626
. takes_value ( true )
1591
1627
. index ( 1 )
1592
- . required ( true )
1593
- . help ( "The address of the token account to unwrap ") ,
1628
+ . help ( "The address of the auxiliary token account to unwrap \
1629
+ [default: associated token account for --owner] ") ,
1594
1630
)
1595
1631
. arg ( multisig_signer_arg ( ) )
1596
1632
. nonce_args ( true )
@@ -2050,15 +2086,18 @@ fn main() {
2050
2086
}
2051
2087
( "wrap" , Some ( arg_matches) ) => {
2052
2088
let amount = value_t_or_exit ! ( arg_matches, "amount" , f64 ) ;
2053
- let ( signer, account) = new_throwaway_signer ( ) ;
2054
- let account = account. unwrap ( ) ;
2055
- bulk_signers. push ( signer) ;
2089
+ let account = if arg_matches. is_present ( "create_aux_account" ) {
2090
+ let ( signer, account) = new_throwaway_signer ( ) ;
2091
+ bulk_signers. push ( signer) ;
2092
+ account
2093
+ } else {
2094
+ // No need to add a signer when creating an associated token account
2095
+ None
2096
+ } ;
2056
2097
command_wrap ( & config, amount, account)
2057
2098
}
2058
2099
( "unwrap" , Some ( arg_matches) ) => {
2059
- let address = pubkey_of_signer ( arg_matches, "address" , & mut wallet_manager)
2060
- . unwrap ( )
2061
- . unwrap ( ) ;
2100
+ let address = pubkey_of_signer ( arg_matches, "address" , & mut wallet_manager) . unwrap ( ) ;
2062
2101
command_unwrap ( & config, address)
2063
2102
}
2064
2103
( "approve" , Some ( arg_matches) ) => {
0 commit comments