shadow_drive_sdk/client/redeem_rent.rs
1use anchor_lang::{InstructionData, ToAccountMetas};
2use shadow_drive_user_staking::accounts as shdw_drive_accounts;
3use shadow_drive_user_staking::instruction as shdw_drive_instructions;
4use solana_sdk::{
5 instruction::Instruction, pubkey::Pubkey, signer::Signer, transaction::Transaction,
6};
7
8use super::ShadowDriveClient;
9
10use crate::{constants::PROGRAM_ADDRESS, models::*};
11
12impl<T> ShadowDriveClient<T>
13where
14 T: Signer,
15{
16 /// Reclaims the Solana rent from any on-chain file accounts. Older versions of the Shadow Drive used to create accounts for uploaded files.
17 ///
18 /// * `storage_account_key` - The public key of the [`StorageAccount`](crate::models::StorageAccount) that contained the deleted file.
19 /// * `file_account_key` - The public key of the File account to be closed.
20 ///
21 /// # Example
22 ///
23 /// ```
24 /// # use shadow_drive_rust::{ShadowDriveClient, derived_addresses::storage_account};
25 /// # use solana_client::rpc_client::RpcClient;
26 /// # use solana_sdk::{
27 /// # pubkey::Pubkey,
28 /// # signature::Keypair,
29 /// # signer::{keypair::read_keypair_file, Signer},
30 /// # };
31 /// # use std::str::FromStr;
32 /// #
33 /// # let keypair = read_keypair_file(KEYPAIR_PATH).expect("failed to load keypair at path");
34 /// # let user_pubkey = keypair.pubkey();
35 /// # let rpc_client = RpcClient::new("https://ssc-dao.genesysgo.net");
36 /// # let shdw_drive_client = ShadowDriveClient::new(keypair, rpc_client);
37 /// # let (storage_account_key, _) = storage_account(&user_pubkey, 0);
38 /// # let file_account_key = Pubkey::from_str("ACbwxy6KEqLPKXBMbYXp48F8dPchbbKEcQEbmcCSZe31").unwrap();
39 /// #
40 ///let redeem_rent_response = shdw_drive_client
41 /// .redeem_rent(&storage_account_key, &file_account_key)
42 /// .await?;
43 /// ```
44 pub async fn redeem_rent(
45 &self,
46 storage_account_key: &Pubkey,
47 file_account_key: &Pubkey,
48 ) -> ShadowDriveResult<ShdwDriveResponse> {
49 let wallet_pubkey = self.wallet.pubkey();
50
51 let accounts = shdw_drive_accounts::RedeemRent {
52 storage_account: *storage_account_key,
53 file: *file_account_key,
54 owner: wallet_pubkey,
55 };
56
57 let args = shdw_drive_instructions::RedeemRent {};
58
59 let instruction = Instruction {
60 program_id: PROGRAM_ADDRESS,
61 accounts: accounts.to_account_metas(None),
62 data: args.data(),
63 };
64
65 let mut txn = Transaction::new_with_payer(&[instruction], Some(&wallet_pubkey));
66 txn.try_sign(
67 &[&self.wallet],
68 self.rpc_client.get_latest_blockhash().await?,
69 )?;
70 let txn_result = self.rpc_client.send_and_confirm_transaction(&txn).await?;
71
72 Ok(ShdwDriveResponse {
73 txid: txn_result.to_string(),
74 })
75 }
76}