use anchor_lang::prelude::*;
use anchor_spl::{token::TokenAccount, token_interface};
use crate::utils::pubkey::optional_address;
pub use gmsol_utils::token_config::TokensCollector;
#[zero_copy]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TokenAndAccount {
token: Pubkey,
account: Pubkey,
}
impl TokenAndAccount {
pub fn init(&mut self, account: &Account<TokenAccount>) {
self.token = account.mint;
self.account = account.key();
}
pub fn init_with_interface(
&mut self,
account: &InterfaceAccount<token_interface::TokenAccount>,
) {
self.token = account.mint;
self.account = account.key();
}
pub fn token(&self) -> Option<Pubkey> {
optional_address(&self.token).copied()
}
pub fn account(&self) -> Option<Pubkey> {
optional_address(&self.account).copied()
}
pub fn token_and_account(&self) -> Option<(Pubkey, Pubkey)> {
let token = self.token()?;
let account = self.account()?;
Some((token, account))
}
}