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 Counter {
pub key: Key,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub authority: Pubkey,
pub value: u32,
}
impl Counter {
pub const LEN: usize = 37;
pub const PREFIX: &'static [u8] = "counter".as_bytes();
pub fn create_pda(
authority: Pubkey,
bump: u8,
) -> Result<solana_program::pubkey::Pubkey, solana_program::pubkey::PubkeyError> {
solana_program::pubkey::Pubkey::create_program_address(
&["counter".as_bytes(), authority.as_ref(), &[bump]],
&crate::SOLANA_JONC_PROGRAM_COUNTER_ID,
)
}
pub fn find_pda(authority: &Pubkey) -> (solana_program::pubkey::Pubkey, u8) {
solana_program::pubkey::Pubkey::find_program_address(
&["counter".as_bytes(), authority.as_ref()],
&crate::SOLANA_JONC_PROGRAM_COUNTER_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 Counter {
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)
}
}
#[cfg(feature = "anchor")]
impl anchor_lang::AccountDeserialize for Counter {
fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
Ok(Self::deserialize(buf)?)
}
}
#[cfg(feature = "anchor")]
impl anchor_lang::AccountSerialize for Counter {}
#[cfg(feature = "anchor")]
impl anchor_lang::Owner for Counter {
fn owner() -> Pubkey {
crate::SOLANA_JONC_PROGRAM_COUNTER_ID
}
}
#[cfg(feature = "anchor-idl-build")]
impl anchor_lang::IdlBuild for Counter {}
#[cfg(feature = "anchor-idl-build")]
impl anchor_lang::Discriminator for Counter {
const DISCRIMINATOR: [u8; 8] = [0; 8];
}