use crate::generated::types::AllowedDistributor;
use crate::generated::types::DistributionType;
use crate::generated::types::Key;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use solana_program::pubkey::Pubkey;
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Distribution {
pub key: Key,
pub bump: u8,
pub distribution_type: DistributionType,
pub subsidize_receipts: bool,
pub allowed_distributor: AllowedDistributor,
pub padding: [u8; 2],
pub tree_height: u8,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub authority: Pubkey,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub mint: Pubkey,
pub merkle_root: [u8; 32],
pub start_time: i64,
pub end_time: i64,
pub total_claimants: u64,
pub total_amount: u64,
pub claim_count: u64,
pub claim_amount: u64,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub seed: Pubkey,
pub name: [u8; 32],
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub permissioned_distributor: Pubkey,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
pub reserved: [u8; 128],
}
impl Distribution {
pub const LEN: usize = 376;
pub const PREFIX: &'static [u8] = "distribution".as_bytes();
pub fn create_pda(
mint: Pubkey,
seed: Pubkey,
bump: u8,
) -> Result<solana_program::pubkey::Pubkey, solana_program::pubkey::PubkeyError> {
solana_program::pubkey::Pubkey::create_program_address(
&[
"distribution".as_bytes(),
mint.as_ref(),
seed.as_ref(),
&[bump],
],
&crate::MPL_DISTRO_ID,
)
}
pub fn find_pda(mint: &Pubkey, seed: &Pubkey) -> (solana_program::pubkey::Pubkey, u8) {
solana_program::pubkey::Pubkey::find_program_address(
&["distribution".as_bytes(), mint.as_ref(), seed.as_ref()],
&crate::MPL_DISTRO_ID,
)
}
#[inline(always)]
pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
let mut data = data;
Self::deserialize(&mut data)
}
}
impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Distribution {
type Error = std::io::Error;
fn try_from(
account_info: &solana_program::account_info::AccountInfo<'a>,
) -> Result<Self, Self::Error> {
let mut data: &[u8] = &(*account_info.data).borrow();
Self::deserialize(&mut data)
}
}