sendfun-sdk 1.1.0

Rust SDK for send.fun programs
Documentation
#![allow(
	clippy::all,
	clippy::pedantic,
	clippy::nursery,
	clippy::expect_used,
	clippy::unwrap_used,
	clippy::indexing_slicing,
	clippy::allow_attributes,
	clippy::allow_attributes_without_reason,
	clippy::mem_forget,
	unfulfilled_lint_expectations,
	unexpected_cfgs,
	unused_imports
)]

//! 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 crate::dex::generated::types::PoolStatus;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use solana_address::Address;

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Pool {
	pub discriminator: [u8; 8],
	pub version: u8,
	pub bump: u8,
	pub status: PoolStatus,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub base_mint: Address,
	pub base_decimals: u8,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub quote_mint: Address,
	pub quote_decimals: u8,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub coin_creator: Address,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub creator_fee_config: Address,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub base_vault: Address,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub quote_vault: Address,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub lp_mint: Address,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub lp_supply: u64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub base_reserves: u64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub quote_reserves: u64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub quote_ledger: u64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub created_at: i64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub protocol_owed: u64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub creator_owed: u64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub platform_config: Address,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::Bytes>")
	)]
	pub reserved: [u8; 64],
}

pub const POOL_DISCRIMINATOR: [u8; 8] = [241, 154, 109, 4, 17, 177, 109, 188];

impl Pool {
	pub const LEN: usize = 389;

	#[inline(always)]
	pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
		if data.get(..POOL_DISCRIMINATOR.len()) != Some(&POOL_DISCRIMINATOR[..])
		{
			return Err(std::io::Error::new(
				std::io::ErrorKind::InvalidData,
				"invalid account discriminator",
			));
		}
		let mut data = data;
		Self::deserialize(&mut data)
	}
}

impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Pool {
	type Error = std::io::Error;

	fn try_from(
		account_info: &solana_program::account_info::AccountInfo<'a>,
	) -> Result<Self, Self::Error> {
		if account_info.owner != &crate::dex::generated::SEND_DEX_ID {
			return Err(std::io::Error::new(
				std::io::ErrorKind::InvalidData,
				"invalid account owner",
			));
		}
		let data: &[u8] = &(*account_info.data).borrow();
		Self::from_bytes(data)
	}
}

#[cfg(feature = "fetch")]
pub fn fetch_pool(
	rpc: &solana_rpc_client::rpc_client::RpcClient,
	address: &solana_address::Address,
) -> Result<crate::dex::generated::shared::DecodedAccount<Pool>, std::io::Error>
{
	let accounts = fetch_all_pool(rpc, &[*address])?;
	Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_pool(
	rpc: &solana_rpc_client::rpc_client::RpcClient,
	addresses: &[solana_address::Address],
) -> Result<
	Vec<crate::dex::generated::shared::DecodedAccount<Pool>>,
	std::io::Error,
> {
	let accounts = rpc
		.get_multiple_accounts(addresses)
		.map_err(|e| std::io::Error::other(e.to_string()))?;
	let mut decoded_accounts: Vec<
		crate::dex::generated::shared::DecodedAccount<Pool>,
	> = Vec::new();
	for i in 0..addresses.len() {
		let address = addresses[i];
		let account = accounts[i].as_ref().ok_or(std::io::Error::other(
			format!("Account not found: {address}"),
		))?;
		if account.owner != crate::dex::generated::SEND_DEX_ID {
			return Err(std::io::Error::other(format!(
				"Invalid owner for account: {address}"
			)));
		}
		let data = Pool::from_bytes(&account.data)?;
		decoded_accounts.push(crate::dex::generated::shared::DecodedAccount {
			address,
			account: account.clone(),
			data,
		});
	}
	Ok(decoded_accounts)
}

#[cfg(feature = "fetch")]
pub fn fetch_maybe_pool(
	rpc: &solana_rpc_client::rpc_client::RpcClient,
	address: &solana_address::Address,
) -> Result<crate::dex::generated::shared::MaybeAccount<Pool>, std::io::Error> {
	let accounts = fetch_all_maybe_pool(rpc, &[*address])?;
	Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_maybe_pool(
	rpc: &solana_rpc_client::rpc_client::RpcClient,
	addresses: &[solana_address::Address],
) -> Result<
	Vec<crate::dex::generated::shared::MaybeAccount<Pool>>,
	std::io::Error,
> {
	let accounts = rpc
		.get_multiple_accounts(addresses)
		.map_err(|e| std::io::Error::other(e.to_string()))?;
	let mut decoded_accounts: Vec<
		crate::dex::generated::shared::MaybeAccount<Pool>,
	> = Vec::new();
	for i in 0..addresses.len() {
		let address = addresses[i];
		if let Some(account) = accounts[i].as_ref() {
			if account.owner != crate::dex::generated::SEND_DEX_ID {
				return Err(std::io::Error::other(format!(
					"Invalid owner for account: {address}"
				)));
			}
			let data = Pool::from_bytes(&account.data)?;
			decoded_accounts.push(
				crate::dex::generated::shared::MaybeAccount::Exists(
					crate::dex::generated::shared::DecodedAccount {
						address,
						account: account.clone(),
						data,
					},
				),
			);
		} else {
			decoded_accounts.push(
				crate::dex::generated::shared::MaybeAccount::NotFound(address),
			);
		}
	}
	Ok(decoded_accounts)
}

#[cfg(feature = "anchor")]
impl anchor_lang::AccountDeserialize for Pool {
	fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
		if buf.len() < POOL_DISCRIMINATOR.len()
			|| buf[..POOL_DISCRIMINATOR.len()] != POOL_DISCRIMINATOR[..]
		{
			return Err(
				anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch
					.into(),
			);
		}
		Self::try_deserialize_unchecked(buf)
	}

	fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
		Ok(Self::deserialize(buf)?)
	}
}

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

#[cfg(feature = "anchor")]
impl anchor_lang::Owner for Pool {
	fn owner() -> anchor_lang::solana_program::pubkey::Pubkey {
		anchor_lang::solana_program::pubkey::Pubkey::from(
			crate::dex::generated::SEND_DEX_ID.to_bytes(),
		)
	}
}

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

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