mpl_bubblegum/generated/accounts/
voucher.rs1use crate::generated::types::LeafSchema;
9use borsh::BorshDeserialize;
10use borsh::BorshSerialize;
11use solana_program::pubkey::Pubkey;
12
13#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct Voucher {
16    pub discriminator: [u8; 8],
17    pub leaf_schema: LeafSchema,
18    pub index: u32,
19    #[cfg_attr(
20        feature = "serde",
21        serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
22    )]
23    pub merkle_tree: Pubkey,
24}
25
26impl Voucher {
27    pub fn create_pda(
28        merkle_tree: Pubkey,
29        nonce: u64,
30        bump: u8,
31    ) -> Result<solana_program::pubkey::Pubkey, solana_program::pubkey::PubkeyError> {
32        solana_program::pubkey::Pubkey::create_program_address(
33            &[
34                "voucher".as_bytes(),
35                merkle_tree.as_ref(),
36                nonce.to_string().as_ref(),
37                &[bump],
38            ],
39            &crate::MPL_BUBBLEGUM_ID,
40        )
41    }
42
43    pub fn find_pda(merkle_tree: &Pubkey, nonce: u64) -> (solana_program::pubkey::Pubkey, u8) {
44        solana_program::pubkey::Pubkey::find_program_address(
45            &[
46                "voucher".as_bytes(),
47                merkle_tree.as_ref(),
48                nonce.to_string().as_ref(),
49            ],
50            &crate::MPL_BUBBLEGUM_ID,
51        )
52    }
53
54    #[inline(always)]
55    pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
56        let mut data = data;
57        Self::deserialize(&mut data)
58    }
59}
60
61impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Voucher {
62    type Error = std::io::Error;
63
64    fn try_from(
65        account_info: &solana_program::account_info::AccountInfo<'a>,
66    ) -> Result<Self, Self::Error> {
67        let mut data: &[u8] = &(*account_info.data).borrow();
68        Self::deserialize(&mut data)
69    }
70}