escrow-api 0.2.0

Signer-Payer Payment Escrow Program
Documentation
use solana_program::pubkey::Pubkey;
use steel::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum EscrowInstruction {
    // Public Instructions (No Authority Required)
    FundPayment = 0, // Creates payment account and funds it atomically (SOL or SPL)
    ReleasePayment = 1, // Release tokens to seller (SOL or SPL)
    RefundPayment = 2, // Refund tokens to buyer (SOL or SPL)
    ClosePayment = 3, // Close payment account
    DisputePayment = 4, // Dispute a payment
    ResolveDispute = 5, // Resolve a dispute (SOL or SPL)
    ExtendPaymentTTL = 6, // Extend payment expiration
    ConfirmFulfillment = 7, // Buyer confirms order fulfillment
    AppealArbitration = 8, // Appeal arbitration decision

    // Admin Instructions (Require Authority)
    Initialize = 100,
    UpdateAuthority = 101,
    UpdateConfig = 102,
    OpenEscrow = 103,
    PauseEscrow = 104,
    UpdateEscrowSettings = 105,
    CloseEscrow = 106,
    ArbitratePayment = 107, // Arbitrate payment (SOL or SPL)
    WithdrawFees = 108,     // Withdraw fees (SOL or SPL)
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Initialize {
    pub fee_bps: [u8; 2],  // Use byte array to avoid endianness issues
    pub _padding: [u8; 6], // Pad to 8 bytes for alignment
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct UpdateAuthority {
    pub new_authority: Pubkey,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct FundPayment {
    pub seller: Pubkey,        // 32 bytes
    pub mint: Pubkey,          // 32 bytes
    pub amount: [u8; 8],       // 8 bytes
    pub ttl_seconds: [u8; 8],  // 8 bytes
    pub reference: Pubkey,     // 32 bytes
    pub payment_uid: [u8; 32], // 32 bytes - payment unique identifier
    pub order_num: [u8; 32],   // 32 bytes - order number
                               // Total: 32+32+8+8+32+32+32 = 176 bytes, need 0 bytes padding for 176-byte alignment
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ReleasePayment {}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct RefundPayment {}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ClosePayment {}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct WithdrawFees {
    pub amount: [u8; 8], // 8 bytes - amount to withdraw (0 = withdraw all)
}

// New Public Instructions
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct DisputePayment {
    pub reason: u8, // 1 byte
    pub description: [u8; 64], // 64 bytes
                    // Total: 65 bytes (no padding needed - already aligned)
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ResolveDispute {
    pub buyer_amount: [u8; 8],  // 8 bytes
    pub seller_amount: [u8; 8], // 8 bytes
    pub decision: u8,           // 1 byte
    // Total: 8+8+1 = 17 bytes, need 7 bytes padding for 24-byte alignment
    pub _padding: [u8; 7], // 7 bytes padding
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ExtendPaymentTTL {
    pub additional_seconds: [u8; 8], // Additional TTL in seconds
}

// New Admin Instructions
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ArbitratePayment {
    pub buyer_amount: [u8; 8],  // 8 bytes
    pub seller_amount: [u8; 8], // 8 bytes
    pub decision: u8,           // 1 byte
    pub reason: u8,             // 1 byte - arbitration reason category
    pub description: [u8; 64],  // 64 bytes - optional detailed explanation
    // Total: 8+8+1+1+64 = 82 bytes, need 6 bytes padding for 88-byte alignment
    pub _padding: [u8; 6], // 6 bytes padding
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct CloseEscrow {}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct UpdateEscrowSettings {
    pub min_payment_amount: [u8; 8], // 8 bytes
    pub max_payment_amount: [u8; 8], // 8 bytes
    pub new_fee_bps: [u8; 2],        // 2 bytes
    // Total: 8+8+2 = 18 bytes, need 6 bytes padding for 24-byte alignment
    pub _padding: [u8; 6], // 6 bytes padding
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct PauseEscrow {
    pub pause: u8, // 0=Unpause, 1=Pause
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct UpdateConfig {
    pub appeal_window_seconds: [u8; 8], // 8 bytes - Appeal window duration
    pub closure_delay_seconds: [u8; 8], // 8 bytes - Closure delay for all final states
    pub max_appeals: u8,                // 1 byte - Maximum appeals allowed
    // Total: 8+8+1 = 17 bytes, need 7 bytes padding for 24-byte alignment
    pub _padding: [u8; 7], // 7 bytes padding
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ConfirmFulfillment {}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct AppealArbitration {
    pub description: [u8; 64], // 64 bytes - Appeal reason description
    pub reason: u8,            // 1 byte - category (like DisputePayment)
    pub _padding: [u8; 7],     // 7 bytes padding
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct OpenEscrow {
    pub min_payment_amount: [u8; 8], // 8 bytes
    pub max_payment_amount: [u8; 8], // 8 bytes
    pub fee_bps: [u8; 2],            // 2 bytes - u16::MAX (65535) means use Bank's default fee
    // Total: 8+8+2 = 18 bytes, need 6 bytes padding for 24-byte alignment
    pub _padding: [u8; 6], // 6 bytes padding
}

instruction!(EscrowInstruction, FundPayment);
instruction!(EscrowInstruction, ReleasePayment);
instruction!(EscrowInstruction, RefundPayment);
instruction!(EscrowInstruction, ClosePayment);
instruction!(EscrowInstruction, DisputePayment);
instruction!(EscrowInstruction, ResolveDispute);
instruction!(EscrowInstruction, ExtendPaymentTTL);
instruction!(EscrowInstruction, ConfirmFulfillment);
instruction!(EscrowInstruction, AppealArbitration);

instruction!(EscrowInstruction, Initialize);
instruction!(EscrowInstruction, OpenEscrow);
instruction!(EscrowInstruction, UpdateAuthority);
instruction!(EscrowInstruction, WithdrawFees);
instruction!(EscrowInstruction, ArbitratePayment);
instruction!(EscrowInstruction, CloseEscrow);
instruction!(EscrowInstruction, UpdateEscrowSettings);
instruction!(EscrowInstruction, PauseEscrow);
instruction!(EscrowInstruction, UpdateConfig);