//! This code was AUTOGENERATED using the codama library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun codama to update it.
//!
//! <https://github.com/codama-idl/codama>
//!

use solana_pubkey::Pubkey;
use borsh::BorshSerialize;
use borsh::BorshDeserialize;


#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TunaConfig {
pub discriminator: [u8; 8],
/// Struct version.
pub version: u16,
/// Bump seed for the vaults config account.
pub bump: u8,
/// Current update authority address for both the TunaConfig and the Vault PDAs themselves - can be updated.
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub admin_authority: Pubkey,
/// Fee recipient address.
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub fee_recipient: Pubkey,
/// Owner of the program. Can change ownership and set the admin of the program.
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub owner_authority: Pubkey,
/// Maximum allowed swap slippage percentage.
pub max_swap_slippage: u32,
/// Maximum allowed percentage of leftovers.
pub max_percentage_of_leftovers: u32,
/// Suspends lending deposits in case of emergency.
pub suspend_lending_deposits: bool,
/// Suspends lending withdrawals in case of emergency.
pub suspend_lending_withdrawals: bool,
/// Suspends adding liquidity to positions in case of emergency.
pub suspend_add_liquidity: bool,
/// Suspends removing liquidity, claiming fees and closing positions in case of emergency.
pub suspend_remove_liquidity: bool,
/// Liquidation bot wallet.
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub liquidator_authority: Pubkey,
/// Maximum allowed oracle price deviation in percent.
pub oracle_price_deviation_threshold: u32,
/// The default protocol fee rate.
pub default_protocol_fee_rate: u16,
/// The default liquidation fee rate.
pub default_liquidation_fee_rate: u32,
/// The default rebalance fee rate.
pub default_rebalance_fee_rate: u32,
/// Oracle price update authority address
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub oracle_price_update_authority: Pubkey,
/// Reserved
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
pub reserved: [u8; 134],
}


pub const TUNA_CONFIG_DISCRIMINATOR: [u8; 8] = [124, 149, 24, 7, 195, 168, 153, 58];

impl TunaConfig {
      pub const LEN: usize = 331;
  
  
  
  #[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 TunaConfig {
  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_tuna_config(
  rpc: &solana_client::rpc_client::RpcClient,
  address: &solana_pubkey::Pubkey,
) -> Result<crate::shared::DecodedAccount<TunaConfig>, std::io::Error> {
  let accounts = fetch_all_tuna_config(rpc, &[*address])?;
  Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_tuna_config(
  rpc: &solana_client::rpc_client::RpcClient,
  addresses: &[solana_pubkey::Pubkey],
) -> Result<Vec<crate::shared::DecodedAccount<TunaConfig>>, 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<TunaConfig>> = 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 = TunaConfig::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_tuna_config(
  rpc: &solana_client::rpc_client::RpcClient,
  address: &solana_pubkey::Pubkey,
) -> Result<crate::shared::MaybeAccount<TunaConfig>, std::io::Error> {
    let accounts = fetch_all_maybe_tuna_config(rpc, &[*address])?;
    Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_maybe_tuna_config(
  rpc: &solana_client::rpc_client::RpcClient,
  addresses: &[solana_pubkey::Pubkey],
) -> Result<Vec<crate::shared::MaybeAccount<TunaConfig>>, 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<TunaConfig>> = Vec::new();
    for i in 0..addresses.len() {
      let address = addresses[i];
      if let Some(account) = accounts[i].as_ref() {
        let data = TunaConfig::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 TunaConfig {
      fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
        Ok(Self::deserialize(buf)?)
      }
  }

  #[cfg(feature = "anchor")]
  impl anchor_lang::AccountSerialize for TunaConfig {}

  #[cfg(feature = "anchor")]
  impl anchor_lang::Owner for TunaConfig {
      fn owner() -> Pubkey {
        crate::TUNA_ID
      }
  }

  #[cfg(feature = "anchor-idl-build")]
  impl anchor_lang::IdlBuild for TunaConfig {}

  
  #[cfg(feature = "anchor-idl-build")]
  impl anchor_lang::Discriminator for TunaConfig {
    const DISCRIMINATOR: &[u8] = &[0; 8];
  }

