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 crate::nexus::generated::events::CLAIM_EVENT_DISCRIMINATOR;
use crate::nexus::generated::events::ClaimEvent;
use crate::nexus::generated::events::STAKE_EVENT_DISCRIMINATOR;
use crate::nexus::generated::events::StakeEvent;
use crate::nexus::generated::events::UNSTAKE_EVENT_DISCRIMINATOR;
use crate::nexus::generated::events::UnstakeEvent;
use borsh::BorshDeserialize;

/// Shared event-framing tag prepended to every framed event emitted by the
/// `send_nexus` program.
pub const ANCHOR_EVENT_CPI_DISCRIMINATOR: [u8; 8] =
	[228, 69, 165, 46, 81, 203, 154, 29];

/// Event kinds for the `send_nexus` program.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SendNexusEventKind {
	ClaimEvent,
	StakeEvent,
	UnstakeEvent,
}

/// Identifies a `send_nexus` event from the provided data.
///
/// A foreign `program_id` is a `None`, not an error: callers iterate whole transactions where
/// other programs are ordinary.
/// Data that lacks the shared CPI framing is rejected with a single compare.
pub fn identify_send_nexus_event(
	program_id: &solana_address::Address,
	data: &[u8],
) -> Option<SendNexusEventKind> {
	if program_id != &crate::nexus::generated::SEND_NEXUS_ID {
		return None;
	}
	if data.get(..ANCHOR_EVENT_CPI_DISCRIMINATOR.len())
		== Some(&ANCHOR_EVENT_CPI_DISCRIMINATOR[..])
	{
		if data.get(8..16) == Some(&CLAIM_EVENT_DISCRIMINATOR[..]) {
			return Some(SendNexusEventKind::ClaimEvent);
		}
		if data.get(8..16) == Some(&STAKE_EVENT_DISCRIMINATOR[..]) {
			return Some(SendNexusEventKind::StakeEvent);
		}
		if data.get(8..16) == Some(&UNSTAKE_EVENT_DISCRIMINATOR[..]) {
			return Some(SendNexusEventKind::UnstakeEvent);
		}
	}
	None
}

/// Parsed event variants for the `send_nexus` program.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SendNexusEvent {
	ClaimEvent(ClaimEvent),
	StakeEvent(StakeEvent),
	UnstakeEvent(UnstakeEvent),
}

/// Tries to parse a `send_nexus` event from the provided data.
///
/// `None` when [`identify_send_nexus_event`] finds nothing; `Some(Err(_))`
/// when one matches but fails to deserialize. Use [`Option::transpose`] to propagate failures
/// with `?`, or scan a batch:
///
/// ```ignore
/// let events: Vec<SendNexusEvent> = datas
///     .iter()
///     .filter_map(|data| try_parse_send_nexus_event(program_id, data))
///     .collect::<Result<_, _>>()?;
/// ```
pub fn try_parse_send_nexus_event(
	program_id: &solana_address::Address,
	data: &[u8],
) -> Option<Result<SendNexusEvent, std::io::Error>> {
	let event_kind = identify_send_nexus_event(program_id, data)?;
	Some(match event_kind {
		SendNexusEventKind::ClaimEvent => {
			// ANCHOR_EVENT_CPI_DISCRIMINATOR (8) + CLAIM_EVENT_DISCRIMINATOR (8)
			let mut data = &data[16..];
			ClaimEvent::deserialize(&mut data).map(SendNexusEvent::ClaimEvent)
		}
		SendNexusEventKind::StakeEvent => {
			// ANCHOR_EVENT_CPI_DISCRIMINATOR (8) + STAKE_EVENT_DISCRIMINATOR (8)
			let mut data = &data[16..];
			StakeEvent::deserialize(&mut data).map(SendNexusEvent::StakeEvent)
		}
		SendNexusEventKind::UnstakeEvent => {
			// ANCHOR_EVENT_CPI_DISCRIMINATOR (8) + UNSTAKE_EVENT_DISCRIMINATOR (8)
			let mut data = &data[16..];
			UnstakeEvent::deserialize(&mut data)
				.map(SendNexusEvent::UnstakeEvent)
		}
	})
}