sendfun-sdk 1.0.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::nexus::generated::types::AuthPlatformEntry;
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 GlobalConfig {
	pub discriminator: [u8; 8],
	pub version: u8,
	pub bump: u8,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub authority: Address,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub operator: Address,
	pub auth_platforms: Vec<AuthPlatformEntry>,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::Bytes>")
	)]
	pub reserved: [u8; 64],
}

pub const GLOBAL_CONFIG_DISCRIMINATOR: [u8; 8] =
	[149, 8, 156, 202, 160, 252, 176, 217];

impl GlobalConfig {
	#[inline(always)]
	pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
		if data.get(..GLOBAL_CONFIG_DISCRIMINATOR.len())
			!= Some(&GLOBAL_CONFIG_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 GlobalConfig
{
	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::nexus::generated::SEND_NEXUS_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_global_config(
	rpc: &solana_rpc_client::rpc_client::RpcClient,
	address: &solana_address::Address,
) -> Result<
	crate::nexus::generated::shared::DecodedAccount<GlobalConfig>,
	std::io::Error,
> {
	let accounts = fetch_all_global_config(rpc, &[*address])?;
	Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_global_config(
	rpc: &solana_rpc_client::rpc_client::RpcClient,
	addresses: &[solana_address::Address],
) -> Result<
	Vec<crate::nexus::generated::shared::DecodedAccount<GlobalConfig>>,
	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::nexus::generated::shared::DecodedAccount<GlobalConfig>,
	> = 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::nexus::generated::SEND_NEXUS_ID {
			return Err(std::io::Error::other(format!(
				"Invalid owner for account: {address}"
			)));
		}
		let data = GlobalConfig::from_bytes(&account.data)?;
		decoded_accounts.push(
			crate::nexus::generated::shared::DecodedAccount {
				address,
				account: account.clone(),
				data,
			},
		);
	}
	Ok(decoded_accounts)
}

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

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

#[cfg(feature = "anchor")]
impl anchor_lang::AccountDeserialize for GlobalConfig {
	fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
		if buf.len() < GLOBAL_CONFIG_DISCRIMINATOR.len()
			|| buf[..GLOBAL_CONFIG_DISCRIMINATOR.len()]
				!= GLOBAL_CONFIG_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 GlobalConfig {}

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

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

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