Skip to main content

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    DisputePayment = 4, // Dispute a payment
13    ResolveDispute = 5, // Resolve a dispute (SOL or SPL)
14    ExtendPaymentTTL = 6, // Extend payment expiration
15    ConfirmFulfillment = 7, // Buyer confirms order fulfillment
16    AppealArbitration = 8, // Appeal arbitration decision
17
18    // Admin Instructions (Require Authority)
19    Initialize = 100,
20    UpdateAuthority = 101,
21    UpdateConfig = 102,
22    OpenEscrow = 103,
23    PauseEscrow = 104,
24    UpdateEscrowSettings = 105,
25    CloseEscrow = 106,
26    ArbitratePayment = 107, // Arbitrate payment (SOL or SPL)
27    WithdrawFees = 108,     // Withdraw fees (SOL or SPL)
28}
29
30#[repr(C)]
31#[derive(Clone, Copy, Debug, Pod, Zeroable)]
32pub struct Initialize {
33    pub fee_bps: [u8; 2],  // Use byte array to avoid endianness issues
34    pub _padding: [u8; 6], // Pad to 8 bytes for alignment
35}
36
37#[repr(C)]
38#[derive(Clone, Copy, Debug, Pod, Zeroable)]
39pub struct UpdateAuthority {
40    pub new_authority: Pubkey,
41}
42
43#[repr(C)]
44#[derive(Clone, Copy, Debug, Pod, Zeroable)]
45pub struct FundPayment {
46    pub seller: Pubkey,        // 32 bytes
47    pub mint: Pubkey,          // 32 bytes
48    pub amount: [u8; 8],       // 8 bytes
49    pub ttl_seconds: [u8; 8],  // 8 bytes
50    pub reference: Pubkey,     // 32 bytes
51    pub payment_uid: [u8; 32], // 32 bytes - payment unique identifier
52    pub order_num: [u8; 32],   // 32 bytes - order number
53                               // Total: 32+32+8+8+32+32+32 = 176 bytes, need 0 bytes padding for 176-byte alignment
54}
55
56#[repr(C)]
57#[derive(Clone, Copy, Debug, Pod, Zeroable)]
58pub struct ReleasePayment {}
59
60#[repr(C)]
61#[derive(Clone, Copy, Debug, Pod, Zeroable)]
62pub struct RefundPayment {}
63
64#[repr(C)]
65#[derive(Clone, Copy, Debug, Pod, Zeroable)]
66pub struct ClosePayment {}
67
68#[repr(C)]
69#[derive(Clone, Copy, Debug, Pod, Zeroable)]
70pub struct WithdrawFees {
71    pub amount: [u8; 8], // 8 bytes - amount to withdraw (0 = withdraw all)
72}
73
74// New Public Instructions
75#[repr(C)]
76#[derive(Clone, Copy, Debug, Pod, Zeroable)]
77pub struct DisputePayment {
78    pub reason: u8, // 1 byte
79    pub description: [u8; 64], // 64 bytes
80                    // Total: 65 bytes (no padding needed - already aligned)
81}
82
83#[repr(C)]
84#[derive(Clone, Copy, Debug, Pod, Zeroable)]
85pub struct ResolveDispute {
86    pub buyer_amount: [u8; 8],  // 8 bytes
87    pub seller_amount: [u8; 8], // 8 bytes
88    pub decision: u8,           // 1 byte
89    // Total: 8+8+1 = 17 bytes, need 7 bytes padding for 24-byte alignment
90    pub _padding: [u8; 7], // 7 bytes padding
91}
92
93#[repr(C)]
94#[derive(Clone, Copy, Debug, Pod, Zeroable)]
95pub struct ExtendPaymentTTL {
96    pub additional_seconds: [u8; 8], // Additional TTL in seconds
97}
98
99// New Admin Instructions
100#[repr(C)]
101#[derive(Clone, Copy, Debug, Pod, Zeroable)]
102pub struct ArbitratePayment {
103    pub buyer_amount: [u8; 8],  // 8 bytes
104    pub seller_amount: [u8; 8], // 8 bytes
105    pub decision: u8,           // 1 byte
106    pub reason: u8,             // 1 byte - arbitration reason category
107    pub description: [u8; 64],  // 64 bytes - optional detailed explanation
108    // Total: 8+8+1+1+64 = 82 bytes, need 6 bytes padding for 88-byte alignment
109    pub _padding: [u8; 6], // 6 bytes padding
110}
111
112#[repr(C)]
113#[derive(Clone, Copy, Debug, Pod, Zeroable)]
114pub struct CloseEscrow {}
115
116#[repr(C)]
117#[derive(Clone, Copy, Debug, Pod, Zeroable)]
118pub struct UpdateEscrowSettings {
119    pub min_payment_amount: [u8; 8], // 8 bytes
120    pub max_payment_amount: [u8; 8], // 8 bytes
121    pub new_fee_bps: [u8; 2],        // 2 bytes
122    // Total: 8+8+2 = 18 bytes, need 6 bytes padding for 24-byte alignment
123    pub _padding: [u8; 6], // 6 bytes padding
124}
125
126#[repr(C)]
127#[derive(Clone, Copy, Debug, Pod, Zeroable)]
128pub struct PauseEscrow {
129    pub pause: u8, // 0=Unpause, 1=Pause
130}
131
132#[repr(C)]
133#[derive(Clone, Copy, Debug, Pod, Zeroable)]
134pub struct UpdateConfig {
135    pub appeal_window_seconds: [u8; 8], // 8 bytes - Appeal window duration
136    pub closure_delay_seconds: [u8; 8], // 8 bytes - Closure delay for all final states
137    pub max_appeals: u8,                // 1 byte - Maximum appeals allowed
138    // Total: 8+8+1 = 17 bytes, need 7 bytes padding for 24-byte alignment
139    pub _padding: [u8; 7], // 7 bytes padding
140}
141
142#[repr(C)]
143#[derive(Clone, Copy, Debug, Pod, Zeroable)]
144pub struct ConfirmFulfillment {}
145
146#[repr(C)]
147#[derive(Clone, Copy, Debug, Pod, Zeroable)]
148pub struct AppealArbitration {
149    pub description: [u8; 64], // 64 bytes - Appeal reason description
150    pub reason: u8,            // 1 byte - category (like DisputePayment)
151    pub _padding: [u8; 7],     // 7 bytes padding
152}
153
154#[repr(C)]
155#[derive(Clone, Copy, Debug, Pod, Zeroable)]
156pub struct OpenEscrow {
157    pub min_payment_amount: [u8; 8], // 8 bytes
158    pub max_payment_amount: [u8; 8], // 8 bytes
159    pub fee_bps: [u8; 2],            // 2 bytes - u16::MAX (65535) means use Bank's default fee
160    // Total: 8+8+2 = 18 bytes, need 6 bytes padding for 24-byte alignment
161    pub _padding: [u8; 6], // 6 bytes padding
162}
163
164instruction!(EscrowInstruction, FundPayment);
165instruction!(EscrowInstruction, ReleasePayment);
166instruction!(EscrowInstruction, RefundPayment);
167instruction!(EscrowInstruction, ClosePayment);
168instruction!(EscrowInstruction, DisputePayment);
169instruction!(EscrowInstruction, ResolveDispute);
170instruction!(EscrowInstruction, ExtendPaymentTTL);
171instruction!(EscrowInstruction, ConfirmFulfillment);
172instruction!(EscrowInstruction, AppealArbitration);
173
174instruction!(EscrowInstruction, Initialize);
175instruction!(EscrowInstruction, OpenEscrow);
176instruction!(EscrowInstruction, UpdateAuthority);
177instruction!(EscrowInstruction, WithdrawFees);
178instruction!(EscrowInstruction, ArbitratePayment);
179instruction!(EscrowInstruction, CloseEscrow);
180instruction!(EscrowInstruction, UpdateEscrowSettings);
181instruction!(EscrowInstruction, PauseEscrow);
182instruction!(EscrowInstruction, UpdateConfig);