1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use super::{create_mint, create_token_account, ExternalPrice, Metadata};
use mpl_token_vault::{instruction, state::PREFIX};
use solana_program::{pubkey::Pubkey, system_instruction};
use solana_program_test::*;
use solana_sdk::{
    signature::{Keypair, Signer},
    transaction::Transaction,
    transport,
};

pub struct Vault {
    pub keypair: Keypair,
    pub mint: Keypair,
    pub redeem_treasury: Keypair,
    pub fraction_treasury: Keypair,
}

impl Default for Vault {
    fn default() -> Self {
        Self::new()
    }
}

impl Vault {
    pub fn new() -> Self {
        Vault {
            keypair: Keypair::new(),
            mint: Keypair::new(),
            redeem_treasury: Keypair::new(),
            fraction_treasury: Keypair::new(),
        }
    }

    pub async fn add_token_to_inactive_vault(
        &self,
        context: &mut ProgramTestContext,
        amount: u64,
        metadata: &Metadata,
    ) -> transport::Result<(Pubkey, Pubkey)> {
        let vault_pubkey = self.keypair.pubkey();
        let metaplex_token_vault_id = mpl_token_vault::id();

        let store = Keypair::new();
        let token_mint_pubkey = metadata.mint.pubkey();

        let seeds = &[
            PREFIX.as_bytes(),
            vault_pubkey.as_ref(),
            token_mint_pubkey.as_ref(),
        ];
        let (safety_deposit_box, _) = Pubkey::find_program_address(seeds, &metaplex_token_vault_id);
        let seeds = &[
            PREFIX.as_bytes(),
            metaplex_token_vault_id.as_ref(),
            vault_pubkey.as_ref(),
        ];
        let (authority, _) = Pubkey::find_program_address(seeds, &metaplex_token_vault_id);
        create_token_account(context, &store, &token_mint_pubkey, &authority).await?;

        let tx = Transaction::new_signed_with_payer(
            &[instruction::create_add_token_to_inactive_vault_instruction(
                mpl_token_vault::id(),
                safety_deposit_box,
                metadata.token.pubkey(),
                store.pubkey(),
                self.keypair.pubkey(),
                context.payer.pubkey(),
                context.payer.pubkey(),
                context.payer.pubkey(),
                amount,
            )],
            Some(&context.payer.pubkey()),
            &[&context.payer, &context.payer, &context.payer],
            context.last_blockhash,
        );
        context.banks_client.process_transaction(tx).await?;

        Ok((safety_deposit_box, store.pubkey()))
    }

    pub async fn activate(
        &self,
        context: &mut ProgramTestContext,
        number_of_shares: u64,
    ) -> Result<(), BanksClientError> {
        let metaplex_token_vault_id = mpl_token_vault::id();
        let vault_pubkey = self.keypair.pubkey();

        let seeds = &[
            PREFIX.as_bytes(),
            metaplex_token_vault_id.as_ref(),
            vault_pubkey.as_ref(),
        ];
        let (authority, _) = Pubkey::find_program_address(seeds, &metaplex_token_vault_id);

        let tx = Transaction::new_signed_with_payer(
            &[instruction::create_activate_vault_instruction(
                mpl_token_vault::id(),
                self.keypair.pubkey(),
                self.mint.pubkey(),
                self.fraction_treasury.pubkey(),
                authority,
                context.payer.pubkey(),
                number_of_shares,
            )],
            Some(&context.payer.pubkey()),
            &[&context.payer],
            context.last_blockhash,
        );

        context.banks_client.process_transaction(tx).await
    }

    pub async fn combine(
        &self,
        context: &mut ProgramTestContext,
        external_price: &ExternalPrice,
    ) -> Result<(), BanksClientError> {
        let outstanding_token_account = Keypair::new();
        let paying_token_account = Keypair::new();

        let metaplex_token_vault_id = mpl_token_vault::id();
        let vault_pubkey = self.keypair.pubkey();

        create_token_account(
            context,
            &outstanding_token_account,
            &self.mint.pubkey(),
            &context.payer.pubkey(),
        )
        .await?;
        create_token_account(
            context,
            &paying_token_account,
            &external_price.price_mint.pubkey(),
            &context.payer.pubkey(),
        )
        .await?;

        let seeds = &[
            PREFIX.as_bytes(),
            metaplex_token_vault_id.as_ref(),
            vault_pubkey.as_ref(),
        ];
        let (authority, _) = Pubkey::find_program_address(seeds, &metaplex_token_vault_id);

        let tx = Transaction::new_signed_with_payer(
            &[instruction::create_combine_vault_instruction(
                mpl_token_vault::id(),
                self.keypair.pubkey(),
                outstanding_token_account.pubkey(),
                paying_token_account.pubkey(),
                self.mint.pubkey(),
                self.fraction_treasury.pubkey(),
                self.redeem_treasury.pubkey(),
                context.payer.pubkey(),
                context.payer.pubkey(),
                context.payer.pubkey(),
                authority,
                external_price.keypair.pubkey(),
            )],
            Some(&context.payer.pubkey()),
            &[&context.payer, &context.payer, &context.payer],
            context.last_blockhash,
        );

        context.banks_client.process_transaction(tx).await
    }

    pub async fn create(
        &self,
        context: &mut ProgramTestContext,
        external_price: &ExternalPrice,
    ) -> Result<(), BanksClientError> {
        let metaplex_token_vault_id = mpl_token_vault::id();
        let vault_pubkey = self.keypair.pubkey();

        let seeds = &[
            PREFIX.as_bytes(),
            metaplex_token_vault_id.as_ref(),
            vault_pubkey.as_ref(),
        ];
        let (authority, _) = Pubkey::find_program_address(seeds, &metaplex_token_vault_id);

        create_mint(context, &self.mint, &authority, Some(&authority)).await?;
        create_token_account(
            context,
            &self.redeem_treasury,
            &external_price.price_mint.pubkey(),
            &authority,
        )
        .await?;
        create_token_account(
            context,
            &self.fraction_treasury,
            &self.mint.pubkey(),
            &authority,
        )
        .await?;

        let rent = context.banks_client.get_rent().await.unwrap();
        let tx = Transaction::new_signed_with_payer(
            &[
                system_instruction::create_account(
                    &context.payer.pubkey(),
                    &self.keypair.pubkey(),
                    rent.minimum_balance(mpl_token_vault::state::MAX_VAULT_SIZE),
                    mpl_token_vault::state::MAX_VAULT_SIZE as u64,
                    &mpl_token_vault::id(),
                ),
                instruction::create_init_vault_instruction(
                    mpl_token_vault::id(),
                    self.mint.pubkey(),
                    self.redeem_treasury.pubkey(),
                    self.fraction_treasury.pubkey(),
                    self.keypair.pubkey(),
                    context.payer.pubkey(),
                    external_price.keypair.pubkey(),
                    false,
                ),
            ],
            Some(&context.payer.pubkey()),
            &[&context.payer, &context.payer, &self.keypair],
            context.last_blockhash,
        );

        context.banks_client.process_transaction(tx).await
    }
}