universalsettle-api 0.2.1

X402-inspired settlement program for any token on Solana
Documentation
mod authority_transfer;
mod config;
mod fee_shard;
mod split_vault;

pub use authority_transfer::*;
pub use config::*;
pub use fee_shard::*;
pub use split_vault::*;

use steel::*;

use crate::consts::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum SettlementAccount {
    Config = 0,
    SplitVault = 1,
    AuthorityTransfer = 2,
    FeeShard = 3,
}

/// Derive the PDA of the config account.
pub fn config_pda() -> (Pubkey, u8) {
    Pubkey::find_program_address(&[CONFIG], &crate::id())
}

/// Derive the PDA of the SplitVault account
pub fn split_vault_pda(seller: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[VAULT, seller.as_ref()], &crate::id())
}

/// Derive the PDA of the SOL storage account for a SplitVault.
pub fn vault_sol_storage_pda(vault_pda: Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[SOL_STORAGE, vault_pda.as_ref()], &crate::id())
}

/// Derive the PDA of an authority transfer proposal.
pub fn authority_transfer_pda(config: Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[AUTHORITY_TRANSFER, config.as_ref()], &crate::id())
}

/// Derive the PDA of a FeeShard account.
pub fn fee_shard_pda(index: u64) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[FEE_SHARD, &index.to_le_bytes()], &crate::id())
}

/// Derive the PDA of the SOL storage for a FeeShard.
pub fn fee_shard_sol_storage_pda(shard_pda: Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[FEE_SHARD_SOL, shard_pda.as_ref()], &crate::id())
}

/// Derive a shard index from a seller pubkey and shard count.
pub fn derive_shard_index(seller: &Pubkey, shard_count: u8) -> u64 {
    if shard_count == 0 {
        return 0;
    }
    (seller.as_ref()[0] % shard_count) as u64
}