use crate::types::ProtocolFee;
use crate::types::RewardInfo;
use crate::types::StaticParameters;
use crate::types::VariableParameters;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use solana_pubkey::Pubkey;
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LbPair {
pub discriminator: [u8; 8],
pub parameters: StaticParameters,
pub v_parameters: VariableParameters,
pub bump_seed: [u8; 1],
pub bin_step_seed: [u8; 2],
pub pair_type: u8,
pub active_id: i32,
pub bin_step: u16,
pub status: u8,
pub require_base_factor_seed: u8,
pub base_factor_seed: [u8; 2],
pub activation_type: u8,
pub creator_pool_on_off_control: u8,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub token_x_mint: Pubkey,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub token_y_mint: Pubkey,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub reserve_x: Pubkey,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub reserve_y: Pubkey,
pub protocol_fee: ProtocolFee,
pub padding1: [u8; 32],
pub reward_infos: [RewardInfo; 2],
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub oracle: Pubkey,
pub bin_array_bitmap: [u64; 16],
pub last_updated_at: i64,
pub padding2: [u8; 32],
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub pre_activation_swap_address: Pubkey,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub base_key: Pubkey,
pub activation_point: u64,
pub pre_activation_duration: u64,
pub padding3: [u8; 8],
pub padding4: u64,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub creator: Pubkey,
pub token_mint_x_program_flag: u8,
pub token_mint_y_program_flag: u8,
pub reserved: [u8; 22],
}
impl LbPair {
pub const LEN: usize = 904;
#[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_account_info::AccountInfo<'a>> for LbPair {
type Error = std::io::Error;
fn try_from(account_info: &solana_account_info::AccountInfo<'a>) -> Result<Self, Self::Error> {
let mut data: &[u8] = &(*account_info.data).borrow();
Self::deserialize(&mut data)
}
}
#[cfg(feature = "fetch")]
pub fn fetch_lb_pair(
rpc: &solana_client::rpc_client::RpcClient,
address: &solana_pubkey::Pubkey,
) -> Result<crate::shared::DecodedAccount<LbPair>, std::io::Error> {
let accounts = fetch_all_lb_pair(rpc, &[*address])?;
Ok(accounts[0].clone())
}
#[cfg(feature = "fetch")]
pub fn fetch_all_lb_pair(
rpc: &solana_client::rpc_client::RpcClient,
addresses: &[solana_pubkey::Pubkey],
) -> Result<Vec<crate::shared::DecodedAccount<LbPair>>, std::io::Error> {
let accounts = rpc
.get_multiple_accounts(addresses)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
let mut decoded_accounts: Vec<crate::shared::DecodedAccount<LbPair>> = Vec::new();
for i in 0..addresses.len() {
let address = addresses[i];
let account = accounts[i].as_ref().ok_or(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Account not found: {}", address),
))?;
let data = LbPair::from_bytes(&account.data)?;
decoded_accounts.push(crate::shared::DecodedAccount {
address,
account: account.clone(),
data,
});
}
Ok(decoded_accounts)
}
#[cfg(feature = "fetch")]
pub fn fetch_maybe_lb_pair(
rpc: &solana_client::rpc_client::RpcClient,
address: &solana_pubkey::Pubkey,
) -> Result<crate::shared::MaybeAccount<LbPair>, std::io::Error> {
let accounts = fetch_all_maybe_lb_pair(rpc, &[*address])?;
Ok(accounts[0].clone())
}
#[cfg(feature = "fetch")]
pub fn fetch_all_maybe_lb_pair(
rpc: &solana_client::rpc_client::RpcClient,
addresses: &[solana_pubkey::Pubkey],
) -> Result<Vec<crate::shared::MaybeAccount<LbPair>>, std::io::Error> {
let accounts = rpc
.get_multiple_accounts(addresses)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
let mut decoded_accounts: Vec<crate::shared::MaybeAccount<LbPair>> = Vec::new();
for i in 0..addresses.len() {
let address = addresses[i];
if let Some(account) = accounts[i].as_ref() {
let data = LbPair::from_bytes(&account.data)?;
decoded_accounts.push(crate::shared::MaybeAccount::Exists(
crate::shared::DecodedAccount {
address,
account: account.clone(),
data,
},
));
} else {
decoded_accounts.push(crate::shared::MaybeAccount::NotFound(address));
}
}
Ok(decoded_accounts)
}
#[cfg(feature = "anchor")]
impl anchor_lang::AccountDeserialize for LbPair {
fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
Ok(Self::deserialize(buf)?)
}
}
#[cfg(feature = "anchor")]
impl anchor_lang::AccountSerialize for LbPair {}
#[cfg(feature = "anchor")]
impl anchor_lang::Owner for LbPair {
fn owner() -> Pubkey {
crate::LB_CLMM_ID
}
}
#[cfg(feature = "anchor-idl-build")]
impl anchor_lang::IdlBuild for LbPair {}
#[cfg(feature = "anchor-idl-build")]
impl anchor_lang::Discriminator for LbPair {
const DISCRIMINATOR: [u8; 8] = [0; 8];
}