sla-escrow-api 0.2.8

SLA-Escrow: Service Level Agreement Enforcer for AI Agents
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
    ExtendPaymentTTL = 4, // Extend payment expiration
    SubmitDelivery = 5, // Seller submits delivery payload hash
    ConfirmOracle = 6, // Oracle confirms fulfillment

    // Admin Instructions (Require Authority) — continuous range 100–107
    Initialize = 100,
    UpdateAuthority = 101, // Two-step: now proposes authority (create AuthorityTransfer PDA)
    UpdateConfig = 102,
    OpenEscrow = 103,
    PauseEscrow = 104,
    UpdateEscrowSettings = 105,
    CloseEscrow = 106,
    WithdrawFees = 107,    // Withdraw accumulated protocol fees (SOL or SPL)
    AcceptAuthority = 108, // Two-step: new authority accepts proposed transfer
    CancelAuthorityProposal = 109, // Two-step: current authority cancels pending proposal
}

#[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 oracle_authority: Pubkey, // 32 bytes
    pub payment_uid: [u8; 32],    // 32 bytes
    pub sla_hash: [u8; 32],       // 32 bytes
    pub amount: [u8; 8],          // 8 bytes
    pub ttl_seconds: [u8; 8],     // 8 bytes
}

#[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 SubmitDelivery {
    pub delivery_hash: [u8; 32], // 32 bytes - hashed AI delivery payload
}

#[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)
}

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

#[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 min_fee_amount: [u8; 8],     // 8 bytes
    pub new_fee_bps: [u8; 2],        // 2 bytes
    pub new_oracle_fee_bps: [u8; 2], // 2 bytes - u16::MAX means unchanged
    pub _padding: [u8; 4],           // 4 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 closure_delay_seconds: [u8; 8], // 8 bytes - Closure delay for all final states
    pub refund_cooldown_seconds: [u8; 8], // 8 bytes - Refund cooldown for buyer-initiated refunds
    pub delivery_cutoff_seconds: [u8; 8], // 8 bytes - Min seconds before expires_at for delivery
                                        // Total: 8+8+8 = 24 bytes (already aligned)
}

#[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 min_fee_amount: [u8; 8],     // 8 bytes
    pub fee_bps: [u8; 2],            // 2 bytes - u16::MAX (65535) means use Bank's default fee
    pub oracle_fee_bps: [u8; 2],     // 2 bytes - oracle tip bps (0 = disabled)
    pub _padding: [u8; 4],           // 4 bytes padding
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ConfirmOracle {
    pub delivery_hash: [u8; 32], // 32 bytes - must match Payment.delivery_hash (set at SubmitDelivery)
    pub resolution_hash: [u8; 32], // 32 bytes - oracle's attestation digest (opaque to program; stored on Payment for audit)
    pub resolution_reason: [u8; 2], // 2 bytes - LE u16 reason code (see ResolutionReason enum)
    pub resolution_state: u8,      // 1 byte  - 1: Approved, 2: Rejected
    pub _padding: [u8; 5],         // 5 bytes padding
}

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

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

instruction!(EscrowInstruction, FundPayment);
instruction!(EscrowInstruction, ReleasePayment);
instruction!(EscrowInstruction, RefundPayment);
instruction!(EscrowInstruction, ClosePayment);
instruction!(EscrowInstruction, ExtendPaymentTTL);
instruction!(EscrowInstruction, SubmitDelivery);
instruction!(EscrowInstruction, ConfirmOracle);

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