//! 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 crate::generated::types::MarketMaker;
use crate::generated::types::PoolToken;
use borsh::BorshSerialize;
use borsh::BorshDeserialize;


#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TunaSpotPosition {
pub discriminator: [u8; 8],
/// Struct version
pub version: u16,
/// Bump seed for the tuna position account
pub bump: [u8; 1],
/// The authority address used for managing the position
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub authority: Pubkey,
/// Liquidity pool address this position belongs to
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub pool: Pubkey,
/// The mint address for token A
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub mint_a: Pubkey,
/// The mint address for token B
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]
pub mint_b: Pubkey,
/// Market maker (Orca, Fusion)
pub market_maker: MarketMaker,
/// Position token: A (LONG, B is borrowed), B (SHORT, A is borrowed)
pub position_token: PoolToken,
/// The token used as collateral.
pub collateral_token: PoolToken,
/// Position options (unused)
pub flags: u32,
/// The total amount of the position in token A (long) or B (short).
pub amount: u64,
/// The amount of shares borrowed by the user from vault B (long) or A (short).
pub loan_shares: u64,
/// The amount of funds borrowed by the user from vault B (long) or A (short). Doesn't include accrued interest.
pub loan_funds: u64,
/// Position entry sqrt price.
pub entry_sqrt_price: u128,
/// Position lower limit order sqrt price (stop loss for a LONG position).
pub lower_limit_order_sqrt_price: u128,
/// Position upper limit order sqrt price (take profit for a LONG position).
pub upper_limit_order_sqrt_price: u128,
/// Reserved
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
pub reserved: [u8; 128],
}


pub const TUNA_SPOT_POSITION_DISCRIMINATOR: [u8; 8] = [118, 56, 124, 104, 178, 205, 174, 77];

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

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

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

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

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

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

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

