@@ -3,7 +3,8 @@ use solana_client::rpc_config::RpcSendTransactionConfig;
3
3
use solana_sdk:: { commitment_config:: CommitmentLevel , compute_budget:: ComputeBudgetInstruction } ;
4
4
use solend_program:: {
5
5
instruction:: {
6
- liquidate_obligation_and_redeem_reserve_collateral, refresh_obligation, refresh_reserve,
6
+ liquidate_obligation_and_redeem_reserve_collateral, redeem_reserve_collateral,
7
+ refresh_obligation, refresh_reserve,
7
8
} ,
8
9
state:: Obligation ,
9
10
} ;
@@ -282,6 +283,27 @@ fn main() {
282
283
. help ( "amount of ctokens to withdraw" ) ,
283
284
)
284
285
)
286
+ . subcommand (
287
+ SubCommand :: with_name ( "redeem-collateral" )
288
+ . about ( "Redeem ctokens for tokens" )
289
+ // @TODO: use is_valid_signer
290
+ . arg (
291
+ Arg :: with_name ( "redeem-reserve" )
292
+ . long ( "redeem-reserve" )
293
+ . value_name ( "RESERVE_PUBKEY" )
294
+ . takes_value ( true )
295
+ . required ( true )
296
+ . help ( "reserve pubkey" ) ,
297
+ )
298
+ . arg (
299
+ Arg :: with_name ( "collateral-amount" )
300
+ . long ( "redeem-amount" )
301
+ . value_name ( "AMOUNT" )
302
+ . takes_value ( true )
303
+ . required ( true )
304
+ . help ( "amount of ctokens to redeem" ) ,
305
+ )
306
+ )
285
307
. subcommand (
286
308
SubCommand :: with_name ( "add-reserve" )
287
309
. about ( "Add a reserve to a lending market" )
@@ -771,6 +793,12 @@ fn main() {
771
793
772
794
command_withdraw_collateral ( & config, obligation, withdraw_reserve, collateral_amount)
773
795
}
796
+ ( "redeem-collateral" , Some ( arg_matches) ) => {
797
+ let redeem_reserve = pubkey_of ( arg_matches, "redeem-reserve" ) . unwrap ( ) ;
798
+ let collateral_amount = value_of ( arg_matches, "collateral-amount" ) . unwrap ( ) ;
799
+
800
+ command_redeem_collateral ( & config, & redeem_reserve, collateral_amount)
801
+ }
774
802
( "add-reserve" , Some ( arg_matches) ) => {
775
803
let lending_market_owner_keypair =
776
804
keypair_of ( arg_matches, "lending_market_owner" ) . unwrap ( ) ;
@@ -986,6 +1014,51 @@ fn command_create_lending_market(
986
1014
Ok ( ( ) )
987
1015
}
988
1016
1017
+ #[ allow( clippy:: too_many_arguments) ]
1018
+ fn command_redeem_collateral (
1019
+ config : & Config ,
1020
+ redeem_reserve_pubkey : & Pubkey ,
1021
+ collateral_amount : u64 ,
1022
+ ) -> CommandResult {
1023
+ let redeem_reserve = {
1024
+ let data = config
1025
+ . rpc_client
1026
+ . get_account ( redeem_reserve_pubkey)
1027
+ . unwrap ( ) ;
1028
+ Reserve :: unpack ( & data. data ) . unwrap ( )
1029
+ } ;
1030
+
1031
+ let source_ata =
1032
+ get_or_create_associated_token_address ( config, & redeem_reserve. collateral . mint_pubkey ) ;
1033
+ let dest_ata =
1034
+ get_or_create_associated_token_address ( config, & redeem_reserve. liquidity . mint_pubkey ) ;
1035
+
1036
+ let recent_blockhash = config. rpc_client . get_latest_blockhash ( ) ?;
1037
+ let transaction = Transaction :: new (
1038
+ & vec ! [ config. fee_payer. as_ref( ) ] ,
1039
+ Message :: new_with_blockhash (
1040
+ & [ redeem_reserve_collateral (
1041
+ config. lending_program_id ,
1042
+ collateral_amount,
1043
+ source_ata,
1044
+ dest_ata,
1045
+ * redeem_reserve_pubkey,
1046
+ redeem_reserve. collateral . mint_pubkey ,
1047
+ redeem_reserve. liquidity . supply_pubkey ,
1048
+ redeem_reserve. lending_market ,
1049
+ config. fee_payer . pubkey ( ) ,
1050
+ ) ] ,
1051
+ Some ( & config. fee_payer . pubkey ( ) ) ,
1052
+ & recent_blockhash,
1053
+ ) ,
1054
+ recent_blockhash,
1055
+ ) ;
1056
+
1057
+ send_transaction ( config, transaction) ?;
1058
+
1059
+ Ok ( ( ) )
1060
+ }
1061
+
989
1062
#[ allow( clippy:: too_many_arguments) ]
990
1063
fn command_withdraw_collateral (
991
1064
config : & Config ,
@@ -1658,7 +1731,7 @@ fn send_transaction(
1658
1731
CommitmentConfig :: confirmed ( ) ,
1659
1732
RpcSendTransactionConfig {
1660
1733
preflight_commitment : Some ( CommitmentLevel :: Processed ) ,
1661
- skip_preflight : false ,
1734
+ skip_preflight : true ,
1662
1735
encoding : None ,
1663
1736
max_retries : None ,
1664
1737
} ,
@@ -1687,11 +1760,9 @@ fn quote_currency_of(matches: &ArgMatches<'_>, name: &str) -> Option<[u8; 32]> {
1687
1760
fn get_or_create_associated_token_address ( config : & Config , mint : & Pubkey ) -> Pubkey {
1688
1761
let ata = get_associated_token_address ( & config. fee_payer . pubkey ( ) , mint) ;
1689
1762
1690
- if let Err ( e) = config. rpc_client . get_account ( & ata) {
1691
- println ! ( "{:?}" , e) ;
1692
-
1693
- // create the ata
1763
+ if config. rpc_client . get_account ( & ata) . is_err ( ) {
1694
1764
println ! ( "Creating ATA for mint {:?}" , mint) ;
1765
+
1695
1766
let recent_blockhash = config. rpc_client . get_latest_blockhash ( ) . unwrap ( ) ;
1696
1767
let transaction = Transaction :: new (
1697
1768
& vec ! [ config. fee_payer. as_ref( ) ] ,
0 commit comments