Skip to main content

sla_escrow_api/
instruction.rs

1use solana_program::pubkey::Pubkey;
2use steel::*;
3
4#[repr(u8)]
5#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
6pub enum EscrowInstruction {
7    // Public Instructions (No Authority Required)
8    FundPayment = 0, // Creates payment account and funds it atomically (SOL or SPL)
9    ReleasePayment = 1, // Release tokens to seller (SOL or SPL)
10    RefundPayment = 2, // Refund tokens to buyer (SOL or SPL)
11    ClosePayment = 3, // Close payment account
12    ExtendPaymentTTL = 4, // Extend payment expiration
13    SubmitDelivery = 5, // Seller submits delivery payload hash
14    ConfirmOracle = 6, // Oracle confirms fulfillment
15
16    // Admin Instructions (Require Authority) — continuous range 100–107
17    Initialize = 100,
18    UpdateAuthority = 101, // Two-step: now proposes authority (create AuthorityTransfer PDA)
19    UpdateConfig = 102,
20    OpenEscrow = 103,
21    PauseEscrow = 104,
22    UpdateEscrowSettings = 105,
23    CloseEscrow = 106,
24    WithdrawFees = 107,    // Withdraw accumulated protocol fees (SOL or SPL)
25    AcceptAuthority = 108, // Two-step: new authority accepts proposed transfer
26    CancelAuthorityProposal = 109, // Two-step: current authority cancels pending proposal
27}
28
29#[repr(C)]
30#[derive(Clone, Copy, Debug, Pod, Zeroable)]
31pub struct Initialize {
32    pub fee_bps: [u8; 2],  // Use byte array to avoid endianness issues
33    pub _padding: [u8; 6], // Pad to 8 bytes for alignment
34}
35
36#[repr(C)]
37#[derive(Clone, Copy, Debug, Pod, Zeroable)]
38pub struct UpdateAuthority {
39    pub new_authority: Pubkey,
40}
41
42#[repr(C)]
43#[derive(Clone, Copy, Debug, Pod, Zeroable)]
44pub struct FundPayment {
45    pub seller: Pubkey,           // 32 bytes
46    pub mint: Pubkey,             // 32 bytes
47    pub oracle_authority: Pubkey, // 32 bytes
48    pub payment_uid: [u8; 32],    // 32 bytes
49    pub sla_hash: [u8; 32],       // 32 bytes
50    pub amount: [u8; 8],          // 8 bytes
51    pub ttl_seconds: [u8; 8],     // 8 bytes
52}
53
54#[repr(C)]
55#[derive(Clone, Copy, Debug, Pod, Zeroable)]
56pub struct ReleasePayment {}
57
58#[repr(C)]
59#[derive(Clone, Copy, Debug, Pod, Zeroable)]
60pub struct RefundPayment {}
61
62#[repr(C)]
63#[derive(Clone, Copy, Debug, Pod, Zeroable)]
64pub struct SubmitDelivery {
65    pub delivery_hash: [u8; 32], // 32 bytes - hashed AI delivery payload
66}
67
68#[repr(C)]
69#[derive(Clone, Copy, Debug, Pod, Zeroable)]
70pub struct ClosePayment {}
71
72#[repr(C)]
73#[derive(Clone, Copy, Debug, Pod, Zeroable)]
74pub struct WithdrawFees {
75    pub amount: [u8; 8], // 8 bytes - amount to withdraw (0 = withdraw all)
76}
77
78#[repr(C)]
79#[derive(Clone, Copy, Debug, Pod, Zeroable)]
80pub struct ExtendPaymentTTL {
81    pub additional_seconds: [u8; 8], // Additional TTL in seconds
82}
83
84#[repr(C)]
85#[derive(Clone, Copy, Debug, Pod, Zeroable)]
86pub struct CloseEscrow {}
87
88#[repr(C)]
89#[derive(Clone, Copy, Debug, Pod, Zeroable)]
90pub struct UpdateEscrowSettings {
91    pub min_payment_amount: [u8; 8], // 8 bytes
92    pub max_payment_amount: [u8; 8], // 8 bytes
93    pub min_fee_amount: [u8; 8],     // 8 bytes
94    pub new_fee_bps: [u8; 2],        // 2 bytes
95    pub new_oracle_fee_bps: [u8; 2], // 2 bytes - u16::MAX means unchanged
96    pub _padding: [u8; 4],           // 4 bytes padding
97}
98
99#[repr(C)]
100#[derive(Clone, Copy, Debug, Pod, Zeroable)]
101pub struct PauseEscrow {
102    pub pause: u8, // 0=Unpause, 1=Pause
103}
104
105#[repr(C)]
106#[derive(Clone, Copy, Debug, Pod, Zeroable)]
107pub struct UpdateConfig {
108    pub closure_delay_seconds: [u8; 8], // 8 bytes - Closure delay for all final states
109    pub refund_cooldown_seconds: [u8; 8], // 8 bytes - Refund cooldown for buyer-initiated refunds
110    pub delivery_cutoff_seconds: [u8; 8], // 8 bytes - Min seconds before expires_at for delivery
111                                        // Total: 8+8+8 = 24 bytes (already aligned)
112}
113
114#[repr(C)]
115#[derive(Clone, Copy, Debug, Pod, Zeroable)]
116pub struct OpenEscrow {
117    pub min_payment_amount: [u8; 8], // 8 bytes
118    pub max_payment_amount: [u8; 8], // 8 bytes
119    pub min_fee_amount: [u8; 8],     // 8 bytes
120    pub fee_bps: [u8; 2],            // 2 bytes - u16::MAX (65535) means use Bank's default fee
121    pub oracle_fee_bps: [u8; 2],     // 2 bytes - oracle tip bps (0 = disabled)
122    pub _padding: [u8; 4],           // 4 bytes padding
123}
124
125#[repr(C)]
126#[derive(Clone, Copy, Debug, Pod, Zeroable)]
127pub struct ConfirmOracle {
128    pub delivery_hash: [u8; 32], // 32 bytes - must match Payment.delivery_hash (set at SubmitDelivery)
129    pub resolution_hash: [u8; 32], // 32 bytes - oracle's attestation digest (opaque to program; stored on Payment for audit)
130    pub resolution_reason: [u8; 2], // 2 bytes - LE u16 reason code (see ResolutionReason enum)
131    pub resolution_state: u8,      // 1 byte  - 1: Approved, 2: Rejected
132    pub _padding: [u8; 5],         // 5 bytes padding
133}
134
135#[repr(C)]
136#[derive(Clone, Copy, Debug, Pod, Zeroable)]
137pub struct AcceptAuthority {}
138
139#[repr(C)]
140#[derive(Clone, Copy, Debug, Pod, Zeroable)]
141pub struct CancelAuthorityProposal {}
142
143instruction!(EscrowInstruction, FundPayment);
144instruction!(EscrowInstruction, ReleasePayment);
145instruction!(EscrowInstruction, RefundPayment);
146instruction!(EscrowInstruction, ClosePayment);
147instruction!(EscrowInstruction, ExtendPaymentTTL);
148instruction!(EscrowInstruction, SubmitDelivery);
149instruction!(EscrowInstruction, ConfirmOracle);
150
151instruction!(EscrowInstruction, Initialize);
152instruction!(EscrowInstruction, OpenEscrow);
153instruction!(EscrowInstruction, UpdateAuthority);
154instruction!(EscrowInstruction, AcceptAuthority);
155instruction!(EscrowInstruction, CancelAuthorityProposal);
156instruction!(EscrowInstruction, WithdrawFees);
157instruction!(EscrowInstruction, CloseEscrow);
158instruction!(EscrowInstruction, UpdateEscrowSettings);
159instruction!(EscrowInstruction, PauseEscrow);
160instruction!(EscrowInstruction, UpdateConfig);