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
use crate::utils::*;

use mpl_token_vault;
use solana_program::{borsh::try_from_slice_unchecked, system_instruction};
use solana_program_test::*;
use solana_sdk::{
    pubkey::Pubkey, signature::Signer, signer::keypair::Keypair, transaction::Transaction,
};

#[derive(Debug)]
pub struct ExternalPrice {
    pub keypair: Keypair,
    pub price_mint: Keypair,
}

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

impl ExternalPrice {
    pub fn new() -> Self {
        ExternalPrice {
            keypair: Keypair::new(),
            price_mint: Keypair::new(),
        }
    }

    pub async fn get_data(
        &self,
        context: &mut ProgramTestContext,
    ) -> mpl_token_vault::state::ExternalPriceAccount {
        let account = get_account(context, &self.keypair.pubkey()).await;
        try_from_slice_unchecked(&account.data).unwrap()
    }

    pub async fn update(
        &self,
        context: &mut ProgramTestContext,
        price_per_share: u64,
        price_mint: &Pubkey,
        allowed_to_combine: bool,
    ) -> Result<(), BanksClientError> {
        let tx = Transaction::new_signed_with_payer(
            &[
                mpl_token_vault::instruction::create_update_external_price_account_instruction(
                    mpl_token_vault::id(),
                    self.keypair.pubkey(),
                    price_per_share,
                    *price_mint,
                    allowed_to_combine,
                ),
            ],
            Some(&context.payer.pubkey()),
            &[&context.payer, &self.keypair],
            context.last_blockhash,
        );

        context.banks_client.process_transaction(tx).await
    }

    pub async fn create(&self, context: &mut ProgramTestContext) -> Result<(), BanksClientError> {
        create_mint(
            context,
            &self.price_mint,
            &context.payer.pubkey(),
            Some(&context.payer.pubkey()),
        )
        .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_EXTERNAL_ACCOUNT_SIZE),
                mpl_token_vault::state::MAX_EXTERNAL_ACCOUNT_SIZE as u64,
                &mpl_token_vault::id(),
            )],
            Some(&context.payer.pubkey()),
            &[&context.payer, &self.keypair],
            context.last_blockhash,
        );

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