mpl_testing_utils/utils/
external_price.rs

1use crate::utils::*;
2
3use mpl_token_vault;
4use solana_program::{borsh::try_from_slice_unchecked, system_instruction};
5use solana_program_test::*;
6use solana_sdk::{
7    pubkey::Pubkey, signature::Signer, signer::keypair::Keypair, transaction::Transaction,
8};
9
10#[derive(Debug)]
11pub struct ExternalPrice {
12    pub keypair: Keypair,
13    pub price_mint: Keypair,
14}
15
16impl Default for ExternalPrice {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl ExternalPrice {
23    pub fn new() -> Self {
24        ExternalPrice {
25            keypair: Keypair::new(),
26            price_mint: Keypair::new(),
27        }
28    }
29
30    pub async fn get_data(
31        &self,
32        context: &mut ProgramTestContext,
33    ) -> mpl_token_vault::state::ExternalPriceAccount {
34        let account = get_account(context, &self.keypair.pubkey()).await;
35        try_from_slice_unchecked(&account.data).unwrap()
36    }
37
38    pub async fn update(
39        &self,
40        context: &mut ProgramTestContext,
41        price_per_share: u64,
42        price_mint: &Pubkey,
43        allowed_to_combine: bool,
44    ) -> Result<(), BanksClientError> {
45        let tx = Transaction::new_signed_with_payer(
46            &[
47                mpl_token_vault::instruction::create_update_external_price_account_instruction(
48                    mpl_token_vault::id(),
49                    self.keypair.pubkey(),
50                    price_per_share,
51                    *price_mint,
52                    allowed_to_combine,
53                ),
54            ],
55            Some(&context.payer.pubkey()),
56            &[&context.payer, &self.keypair],
57            context.last_blockhash,
58        );
59
60        context.banks_client.process_transaction(tx).await
61    }
62
63    pub async fn create(&self, context: &mut ProgramTestContext) -> Result<(), BanksClientError> {
64        create_mint(
65            context,
66            &self.price_mint,
67            &context.payer.pubkey(),
68            Some(&context.payer.pubkey()),
69        )
70        .await?;
71
72        let rent = context.banks_client.get_rent().await.unwrap();
73        let tx = Transaction::new_signed_with_payer(
74            &[system_instruction::create_account(
75                &context.payer.pubkey(),
76                &self.keypair.pubkey(),
77                rent.minimum_balance(mpl_token_vault::state::MAX_EXTERNAL_ACCOUNT_SIZE),
78                mpl_token_vault::state::MAX_EXTERNAL_ACCOUNT_SIZE as u64,
79                &mpl_token_vault::id(),
80            )],
81            Some(&context.payer.pubkey()),
82            &[&context.payer, &self.keypair],
83            context.last_blockhash,
84        );
85
86        context.banks_client.process_transaction(tx).await
87    }
88}