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,
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}
26
27#[repr(C)]
28#[derive(Clone, Copy, Debug, Pod, Zeroable)]
29pub struct Initialize {
30    pub fee_bps: [u8; 2],  // Use byte array to avoid endianness issues
31    pub _padding: [u8; 6], // Pad to 8 bytes for alignment
32}
33
34#[repr(C)]
35#[derive(Clone, Copy, Debug, Pod, Zeroable)]
36pub struct UpdateAuthority {
37    pub new_authority: Pubkey,
38}
39
40#[repr(C)]
41#[derive(Clone, Copy, Debug, Pod, Zeroable)]
42pub struct FundPayment {
43    pub seller: Pubkey,           // 32 bytes
44    pub mint: Pubkey,             // 32 bytes
45    pub oracle_authority: Pubkey, // 32 bytes
46    pub payment_uid: [u8; 32],    // 32 bytes
47    pub sla_hash: [u8; 32],       // 32 bytes
48    pub amount: [u8; 8],          // 8 bytes
49    pub ttl_seconds: [u8; 8],     // 8 bytes
50}
51
52#[repr(C)]
53#[derive(Clone, Copy, Debug, Pod, Zeroable)]
54pub struct ReleasePayment {}
55
56#[repr(C)]
57#[derive(Clone, Copy, Debug, Pod, Zeroable)]
58pub struct RefundPayment {}
59
60#[repr(C)]
61#[derive(Clone, Copy, Debug, Pod, Zeroable)]
62pub struct SubmitDelivery {
63    pub delivery_hash: [u8; 32], // 32 bytes - hashed AI delivery payload
64}
65
66#[repr(C)]
67#[derive(Clone, Copy, Debug, Pod, Zeroable)]
68pub struct ClosePayment {}
69
70#[repr(C)]
71#[derive(Clone, Copy, Debug, Pod, Zeroable)]
72pub struct WithdrawFees {
73    pub amount: [u8; 8], // 8 bytes - amount to withdraw (0 = withdraw all)
74}
75
76#[repr(C)]
77#[derive(Clone, Copy, Debug, Pod, Zeroable)]
78pub struct ExtendPaymentTTL {
79    pub additional_seconds: [u8; 8], // Additional TTL in seconds
80}
81
82#[repr(C)]
83#[derive(Clone, Copy, Debug, Pod, Zeroable)]
84pub struct CloseEscrow {}
85
86#[repr(C)]
87#[derive(Clone, Copy, Debug, Pod, Zeroable)]
88pub struct UpdateEscrowSettings {
89    pub min_payment_amount: [u8; 8], // 8 bytes
90    pub max_payment_amount: [u8; 8], // 8 bytes
91    pub min_fee_amount: [u8; 8],     // 8 bytes
92    pub new_fee_bps: [u8; 2],        // 2 bytes
93    pub new_oracle_fee_bps: [u8; 2], // 2 bytes - u16::MAX means unchanged
94    pub _padding: [u8; 4],           // 4 bytes padding
95}
96
97#[repr(C)]
98#[derive(Clone, Copy, Debug, Pod, Zeroable)]
99pub struct PauseEscrow {
100    pub pause: u8, // 0=Unpause, 1=Pause
101}
102
103#[repr(C)]
104#[derive(Clone, Copy, Debug, Pod, Zeroable)]
105pub struct UpdateConfig {
106    pub closure_delay_seconds: [u8; 8], // 8 bytes - Closure delay for all final states
107    pub refund_cooldown_seconds: [u8; 8], // 8 bytes - Refund cooldown for buyer-initiated refunds
108                                        // Total: 8+8 = 16 bytes (already aligned)
109}
110
111#[repr(C)]
112#[derive(Clone, Copy, Debug, Pod, Zeroable)]
113pub struct OpenEscrow {
114    pub min_payment_amount: [u8; 8], // 8 bytes
115    pub max_payment_amount: [u8; 8], // 8 bytes
116    pub min_fee_amount: [u8; 8],     // 8 bytes
117    pub fee_bps: [u8; 2],            // 2 bytes - u16::MAX (65535) means use Bank's default fee
118    pub oracle_fee_bps: [u8; 2],     // 2 bytes - oracle tip bps (0 = disabled)
119    pub _padding: [u8; 4],           // 4 bytes padding
120}
121
122#[repr(C)]
123#[derive(Clone, Copy, Debug, Pod, Zeroable)]
124pub struct ConfirmOracle {
125    pub delivery_hash: [u8; 32], // 32 bytes
126
127    pub resolution_state: u8, // 1 byte - 1: Approved/Completed, 2: Rejected/Refundable
128    pub _padding: [u8; 7],    // 7 bytes margin
129}
130
131instruction!(EscrowInstruction, FundPayment);
132instruction!(EscrowInstruction, ReleasePayment);
133instruction!(EscrowInstruction, RefundPayment);
134instruction!(EscrowInstruction, ClosePayment);
135instruction!(EscrowInstruction, ExtendPaymentTTL);
136instruction!(EscrowInstruction, SubmitDelivery);
137instruction!(EscrowInstruction, ConfirmOracle);
138
139instruction!(EscrowInstruction, Initialize);
140instruction!(EscrowInstruction, OpenEscrow);
141instruction!(EscrowInstruction, UpdateAuthority);
142instruction!(EscrowInstruction, WithdrawFees);
143instruction!(EscrowInstruction, CloseEscrow);
144instruction!(EscrowInstruction, UpdateEscrowSettings);
145instruction!(EscrowInstruction, PauseEscrow);
146instruction!(EscrowInstruction, UpdateConfig);