shadow_drive_sdk/client/
migrate.rs1use anchor_lang::{system_program, 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, derived_addresses, models::*};
11
12impl<T> ShadowDriveClient<T>
13where
14 T: Signer,
15{
16 pub async fn migrate(
45 &self,
46 storage_account_key: &Pubkey,
47 ) -> ShadowDriveResult<(ShdwDriveResponse, ShdwDriveResponse)> {
48 let step_1_response = self.migrate_step_1(storage_account_key).await?;
49 let step_2_response = self.migrate_step_2(storage_account_key).await?;
50 Ok((step_1_response, step_2_response))
51 }
52
53 pub async fn migrate_step_1(
56 &self,
57 storage_account_key: &Pubkey,
58 ) -> ShadowDriveResult<ShdwDriveResponse> {
59 let wallet_pubkey = self.wallet.pubkey();
60 let (migration, _) = derived_addresses::migration_helper(storage_account_key);
61
62 let accounts = shdw_drive_accounts::MigrateStep1 {
63 storage_account: *storage_account_key,
64 migration,
65 owner: wallet_pubkey,
66 system_program: system_program::ID,
67 };
68
69 let args = shdw_drive_instructions::MigrateStep1 {};
70
71 let instruction = Instruction {
72 program_id: PROGRAM_ADDRESS,
73 accounts: accounts.to_account_metas(None),
74 data: args.data(),
75 };
76
77 let mut txn = Transaction::new_with_payer(&[instruction], Some(&wallet_pubkey));
78 txn.try_sign(
79 &[&self.wallet],
80 self.rpc_client.get_latest_blockhash().await?,
81 )?;
82 let txn_result = self.rpc_client.send_and_confirm_transaction(&txn).await?;
83
84 Ok(ShdwDriveResponse {
85 txid: txn_result.to_string(),
86 })
87 }
88 pub async fn migrate_step_2(
91 &self,
92 storage_account_key: &Pubkey,
93 ) -> ShadowDriveResult<ShdwDriveResponse> {
94 let wallet_pubkey = self.wallet.pubkey();
95 let (migration, _) = derived_addresses::migration_helper(storage_account_key);
96
97 let accounts = shdw_drive_accounts::MigrateStep2 {
98 storage_account: *storage_account_key,
99 migration,
100 owner: wallet_pubkey,
101 system_program: system_program::ID,
102 };
103
104 let args = shdw_drive_instructions::MigrateStep2 {};
105
106 let instruction = Instruction {
107 program_id: PROGRAM_ADDRESS,
108 accounts: accounts.to_account_metas(None),
109 data: args.data(),
110 };
111
112 let mut txn = Transaction::new_with_payer(&[instruction], Some(&wallet_pubkey));
113 txn.try_sign(
114 &[&self.wallet],
115 self.rpc_client.get_latest_blockhash().await?,
116 )?;
117 let txn_result = self.rpc_client.send_and_confirm_transaction(&txn).await?;
118
119 Ok(ShdwDriveResponse {
120 txid: txn_result.to_string(),
121 })
122 }
123}