sendfun-sdk 3.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 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 PartnerMetadata {
	pub discriminator: [u8; 8],
	pub version: u8,
	pub bump: u8,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub partner: Address,
	pub name: String,
	pub note: String,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub added_at: i64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub updated_at: i64,
	#[cfg_attr(
		feature = "serde",
		serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
	)]
	pub platform_config: Address,
	pub reserved: [u8; 32],
}

pub const PARTNER_METADATA_DISCRIMINATOR: [u8; 8] =
	[68, 68, 130, 19, 16, 209, 98, 156];

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

	/// Decodes an account read off-chain after checking its owner and discriminator.
	pub fn from_account(
		owner: &solana_address::Address,
		data: &[u8],
	) -> Result<Self, std::io::Error> {
		if *owner != crate::nexus::generated::SEND_NEXUS_ID {
			return Err(std::io::Error::new(
				std::io::ErrorKind::InvalidData,
				format!(
					"invalid account owner: {owner}, expected {}",
					crate::nexus::generated::SEND_NEXUS_ID
				),
			));
		}
		Self::from_bytes(data)
	}

	/// `Ok(None)` for an account that is not created yet: the system program owns it with no data (an address someone sent lamports to).
	pub fn from_account_maybe(
		owner: &solana_address::Address,
		data: &[u8],
	) -> Result<Option<Self>, std::io::Error> {
		if data.is_empty() && *owner == super::SYSTEM_PROGRAM_ID {
			return Ok(None);
		}
		Self::from_account(owner, data).map(Some)
	}
}

impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>>
	for PartnerMetadata
{
	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 = "anchor")]
impl anchor_lang::AccountDeserialize for PartnerMetadata {
	fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
		if buf.len() < PARTNER_METADATA_DISCRIMINATOR.len()
			|| buf[..PARTNER_METADATA_DISCRIMINATOR.len()]
				!= PARTNER_METADATA_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 PartnerMetadata {}

#[cfg(feature = "anchor")]
impl anchor_lang::Owner for PartnerMetadata {
	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 PartnerMetadata {}

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