Skip to main content

shadow_drive_user_staking/instructions/
migrate.rs

1use anchor_lang::prelude::*;
2use crate::instructions::initialize_account::*;
3use std::ops::{Deref, DerefMut};
4use std::mem::take;
5
6pub fn step1_handler(
7    ctx: Context<MigrateStep1>
8) -> Result<()> {
9
10    // Migrate all data into migration pda
11    ctx.accounts.migration.set_inner(ctx.accounts.storage_account.deref().clone());
12
13    Ok(())
14}
15
16pub fn step2_handler(
17    ctx: Context<MigrateStep2>
18) -> Result<()> {
19
20    // Migrate all data into migration pda
21    let migration: &mut StorageAccount = ctx.accounts.migration.deref_mut();
22    ctx.accounts.storage_account.set_inner(
23        StorageAccountV2 {
24            immutable: migration.immutable,
25            to_be_deleted: migration.to_be_deleted,
26            delete_request_epoch: migration.delete_request_epoch,
27            storage: migration.storage,
28            owner_1: migration.owner_1,
29            account_counter_seed: migration.account_counter_seed,
30            creation_time: migration.creation_time,
31            creation_epoch: migration.creation_epoch,
32            last_fee_epoch: migration.last_fee_epoch,
33            identifier: take(&mut migration.identifier),
34    });
35
36    Ok(())
37}
38
39#[derive(Accounts)]
40pub struct MigrateStep1<'info> {
41
42    /// Account to be migrated
43    #[account(
44        mut,
45        close = owner,
46        seeds = [
47            "storage-account".as_bytes(),
48            &storage_account.owner_1.key().to_bytes(),
49            &storage_account.account_counter_seed.to_le_bytes()
50        ],
51        bump,
52    )]
53    pub storage_account: Account<'info, StorageAccount>,
54
55    /// Migration helper PDA
56    #[account(
57        init,
58        space = calc_v1_storage(&storage_account.identifier),
59        payer = owner,
60        seeds = [
61            "migration-helper".as_bytes(),
62            &storage_account.key().to_bytes(),
63        ],
64        bump,
65    )]
66    pub migration: Account<'info, StorageAccount>,
67
68    /// User that is migrating
69    #[account(mut, constraint = storage_account.is_owner(owner.key()))]
70    pub owner: Signer<'info>,
71
72    pub system_program: Program<'info, System>,
73}
74
75#[derive(Accounts)]
76pub struct MigrateStep2<'info> {
77
78    /// New account
79    #[account(
80        init,
81        space = calc_v2_storage(&migration.identifier),
82        payer = owner,
83        seeds = [
84            "storage-account".as_bytes(),
85            &migration.owner_1.key().to_bytes(),
86            &migration.account_counter_seed.to_le_bytes()
87        ],
88        bump,
89    )]
90    pub storage_account: Account<'info, StorageAccountV2>,
91
92    /// Migration helper PDA
93    #[account(
94        mut,
95        close = owner,
96        seeds = [
97            "migration-helper".as_bytes(),
98            &storage_account.key().to_bytes(),
99        ],
100        bump,
101    )]
102    pub migration: Account<'info, StorageAccount>,
103
104    /// User that is migrating
105    #[account(mut, constraint = migration.is_owner(owner.key()))]
106    pub owner: Signer<'info>,
107
108    pub system_program: Program<'info, System>,
109}