1use steel::*;
2
3use crate::{
4 consts::{
5 BASIS_POINTS_DENOMINATOR, PAYMENT_STATE_ARBITRATED, PAYMENT_STATE_DISPUTED,
6 PAYMENT_STATE_FUNDED, PAYMENT_STATE_REFUNDED, PAYMENT_STATE_RELEASED,
7 },
8 error::EscrowError,
9 instruction::*,
10 state::{bank_pda, escrow_pda, payment_pda, sol_storage_pda, Payment},
11};
12
13pub struct EscrowSdk;
15
16impl EscrowSdk {
17 pub fn program_id() -> Pubkey {
19 crate::ID
20 }
21
22 pub fn bank_pda() -> (Pubkey, u8) {
24 bank_pda()
25 }
26
27 pub fn config_pda() -> (Pubkey, u8) {
29 crate::state::config_pda()
30 }
31
32 pub fn escrow_pda(mint: Pubkey) -> (Pubkey, u8) {
34 let (bank_pda, _) = Self::bank_pda();
35 escrow_pda(mint, bank_pda)
36 }
37
38 pub fn payment_pda(payment_uid: &str, bank: Pubkey) -> (Pubkey, u8) {
40 payment_pda(payment_uid, bank)
41 }
42
43 pub fn sol_storage_pda(mint: Pubkey, bank: Pubkey, escrow: Pubkey) -> (Pubkey, u8) {
45 sol_storage_pda(mint, bank, escrow)
46 }
47
48 pub fn associated_token_account(wallet: Pubkey, mint: Pubkey) -> Pubkey {
50 spl_associated_token_account::get_associated_token_address_with_program_id(
51 &wallet,
52 &mint,
53 &spl_token::ID,
54 )
55 }
56
57 pub fn escrow_token_account(mint: Pubkey) -> Pubkey {
59 let (escrow_pda, _) = Self::escrow_pda(mint);
60 spl_associated_token_account::get_associated_token_address_with_program_id(
61 &escrow_pda,
62 &mint,
63 &spl_token::ID,
64 )
65 }
66
67 pub fn initialize(signer: Pubkey, fee_bps: Option<u16>) -> Instruction {
73 let (bank_pda, _) = Self::bank_pda();
74 let (config_pda, _) = Self::config_pda();
75 Instruction {
76 program_id: Self::program_id(),
77 accounts: vec![
78 AccountMeta::new(signer, true),
79 AccountMeta::new(bank_pda, false),
80 AccountMeta::new(config_pda, false),
81 AccountMeta::new_readonly(solana_program::system_program::ID, false),
82 AccountMeta::new_readonly(solana_program::sysvar::rent::ID, false),
83 ],
84 data: Initialize {
85 fee_bps: (fee_bps.unwrap_or(100)).to_le_bytes(), _padding: [0; 6], }
88 .to_bytes(),
89 }
90 }
91
92 pub fn open_escrow(
100 signer: Pubkey,
101 payer: Pubkey,
102 mint: Pubkey,
103 min_payment_amount: u64,
104 max_payment_amount: u64,
105 fee_bps: u16,
106 ) -> Instruction {
107 let (bank_pda, _) = Self::bank_pda();
108 let (escrow_pda, _) = Self::escrow_pda(mint);
109 let escrow_tokens = if mint == Pubkey::default() {
110 escrow_pda
112 } else {
113 Self::escrow_token_account(mint)
115 };
116
117 let mut accounts = vec![
118 AccountMeta::new(signer, true),
119 AccountMeta::new(payer, true),
120 AccountMeta::new_readonly(bank_pda, false),
121 AccountMeta::new(escrow_pda, false),
122 AccountMeta::new(escrow_tokens, false),
123 AccountMeta::new_readonly(mint, false),
124 AccountMeta::new_readonly(solana_program::system_program::ID, false),
125 AccountMeta::new_readonly(spl_token::ID, false),
126 AccountMeta::new_readonly(spl_associated_token_account::ID, false),
127 AccountMeta::new_readonly(solana_program::sysvar::rent::ID, false),
128 ];
129
130 if mint == Pubkey::default() {
132 let (sol_storage_pda, _) = Self::sol_storage_pda(mint, bank_pda, escrow_pda);
133 accounts.push(AccountMeta::new(sol_storage_pda, false));
134 }
135
136 Instruction {
137 program_id: Self::program_id(),
138 accounts,
139 data: OpenEscrow {
140 min_payment_amount: min_payment_amount.to_le_bytes(),
141 max_payment_amount: max_payment_amount.to_le_bytes(),
142 fee_bps: fee_bps.to_le_bytes(),
143 _padding: [0; 6],
144 }
145 .to_bytes(),
146 }
147 }
148
149 fn sanitize_payment_uid(payment_uid: &str) -> [u8; 32] {
151 let mut uid_bytes = [0u8; 32];
152 let uid_str = payment_uid.replace('-', "");
153 let uid_bytes_str = uid_str.as_bytes();
154 let len = uid_bytes_str.len().min(32);
155 uid_bytes[..len].copy_from_slice(&uid_bytes_str[..len]);
156 uid_bytes
157 }
158
159 fn sanitize_order_num(order_num: &str) -> [u8; 32] {
161 let mut order_bytes = [0u8; 32];
162 let order_str = order_num.replace('-', ""); let order_bytes_str = order_str.as_bytes();
164 let len = order_bytes_str.len().min(32);
165 order_bytes[..len].copy_from_slice(&order_bytes_str[..len]);
166 order_bytes
167 }
168 pub fn fund_payment(
172 buyer: Pubkey,
173 buyer_tokens: Option<Pubkey>, seller: Pubkey,
175 mint: Pubkey,
176 amount: u64,
177 ttl_seconds: i64,
178 reference: Option<Pubkey>,
179 payment_uid: &str,
180 order_num: &str,
181 ) -> Instruction {
182 let (bank_pda, _) = Self::bank_pda();
183 let (config_pda, _) = Self::config_pda();
184 let (escrow_pda, _) = Self::escrow_pda(mint);
185 let (payment_pda, _) = crate::state::payment_pda(payment_uid, bank_pda);
186
187 let is_sol = mint == Pubkey::default();
188
189 let mut accounts = vec![
190 AccountMeta::new(buyer, true),
191 AccountMeta::new_readonly(bank_pda, false),
192 AccountMeta::new_readonly(config_pda, false),
193 AccountMeta::new(escrow_pda, false),
194 AccountMeta::new(payment_pda, false),
195 AccountMeta::new_readonly(mint, false),
196 ];
197
198 if is_sol {
199 let (sol_storage_pda, _) = Self::sol_storage_pda(mint, bank_pda, escrow_pda);
201 accounts.push(AccountMeta::new(sol_storage_pda, false));
202 accounts.push(AccountMeta::new_readonly(
203 solana_program::system_program::ID,
204 false,
205 ));
206 } else {
207 let buyer_tokens = buyer_tokens.expect("buyer_tokens required for SPL tokens");
209 let escrow_tokens = Self::escrow_token_account(mint);
210 accounts.push(AccountMeta::new(escrow_tokens, false));
211 accounts.push(AccountMeta::new(buyer_tokens, false));
212 accounts.push(AccountMeta::new_readonly(spl_token::ID, false));
213 accounts.push(AccountMeta::new_readonly(
214 solana_program::system_program::ID,
215 false,
216 ));
217 }
218
219 Instruction {
220 program_id: Self::program_id(),
221 accounts,
222 data: FundPayment {
223 seller,
224 mint,
225 amount: amount.to_le_bytes(),
226 ttl_seconds: ttl_seconds.to_le_bytes(),
227 reference: reference.unwrap_or_default(),
228 payment_uid: Self::sanitize_payment_uid(payment_uid),
229 order_num: Self::sanitize_order_num(order_num),
230 }
231 .to_bytes(),
232 }
233 }
234
235 pub fn release_payment(
239 caller: Pubkey,
240 seller_tokens: Option<Pubkey>, seller: Option<Pubkey>, mint: Pubkey,
243 payment_uid: &str,
244 ) -> Instruction {
245 let (bank_pda, _) = Self::bank_pda();
246 let (config_pda, _) = Self::config_pda();
247 let (escrow_pda, _) = Self::escrow_pda(mint);
248 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
249
250 let is_sol = mint == Pubkey::default();
251
252 let mut accounts = vec![
253 AccountMeta::new(caller, true),
254 AccountMeta::new_readonly(bank_pda, false),
255 AccountMeta::new_readonly(config_pda, false),
256 AccountMeta::new(escrow_pda, false),
257 AccountMeta::new(payment_pda, false),
258 AccountMeta::new_readonly(mint, false),
259 ];
260
261 if is_sol {
262 let (sol_storage_pda, _) = Self::sol_storage_pda(mint, bank_pda, escrow_pda);
264 accounts.push(AccountMeta::new(sol_storage_pda, false));
265 accounts.push(AccountMeta::new(
266 seller.expect("seller required for SOL"),
267 false,
268 ));
269 accounts.push(AccountMeta::new_readonly(
270 solana_program::system_program::ID,
271 false,
272 ));
273 } else {
274 let seller_tokens = seller_tokens.expect("seller_tokens required for SPL tokens");
276 let seller = seller.expect("seller account required for SPL tokens");
277 let escrow_tokens = Self::escrow_token_account(mint);
278 accounts.push(AccountMeta::new(escrow_tokens, false));
279 accounts.push(AccountMeta::new(seller_tokens, false));
280 accounts.push(AccountMeta::new(seller, false));
281 accounts.push(AccountMeta::new_readonly(spl_token::ID, false));
282 accounts.push(AccountMeta::new_readonly(
283 spl_associated_token_account::ID,
284 false,
285 ));
286 accounts.push(AccountMeta::new_readonly(
287 solana_program::system_program::ID,
288 false,
289 ));
290 }
291
292 Instruction {
293 program_id: Self::program_id(),
294 accounts,
295 data: ReleasePayment {}.to_bytes(),
296 }
297 }
298
299 pub fn refund_payment(
303 caller: Pubkey,
304 buyer_tokens: Option<Pubkey>, mint: Pubkey,
306 payment_uid: &str,
307 ) -> Instruction {
308 let (bank_pda, _) = Self::bank_pda();
309 let (config_pda, _) = Self::config_pda();
310 let (escrow_pda, _) = Self::escrow_pda(mint);
311 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
312
313 let is_sol = mint == Pubkey::default();
314
315 let mut accounts = vec![
316 AccountMeta::new(caller, true),
317 AccountMeta::new_readonly(bank_pda, false),
318 AccountMeta::new_readonly(config_pda, false),
319 AccountMeta::new(escrow_pda, false),
320 AccountMeta::new(payment_pda, false),
321 AccountMeta::new_readonly(mint, false),
322 ];
323
324 if is_sol {
325 let (sol_storage_pda, _) = Self::sol_storage_pda(mint, bank_pda, escrow_pda);
327 accounts.push(AccountMeta::new(sol_storage_pda, false));
328 accounts.push(AccountMeta::new(
329 buyer_tokens.expect("buyer required for SOL"),
330 false,
331 ));
332 accounts.push(AccountMeta::new_readonly(
333 solana_program::system_program::ID,
334 false,
335 ));
336 } else {
337 let buyer_tokens = buyer_tokens.expect("buyer_tokens required for SPL tokens");
339 let escrow_tokens = Self::escrow_token_account(mint);
340 accounts.push(AccountMeta::new(escrow_tokens, false));
341 accounts.push(AccountMeta::new(buyer_tokens, false));
342 accounts.push(AccountMeta::new_readonly(spl_token::ID, false));
343 }
344
345 Instruction {
346 program_id: Self::program_id(),
347 accounts,
348 data: RefundPayment {}.to_bytes(),
349 }
350 }
351
352 pub fn close_payment(buyer: Pubkey, mint: Pubkey, payment_uid: &str) -> Instruction {
354 let (bank_pda, _) = Self::bank_pda();
355 let (config_pda, _) = Self::config_pda();
356 let (escrow_pda, _) = Self::escrow_pda(mint);
357 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
358
359 Instruction {
360 program_id: Self::program_id(),
361 accounts: vec![
362 AccountMeta::new(buyer, true),
363 AccountMeta::new_readonly(bank_pda, false),
364 AccountMeta::new_readonly(config_pda, false),
365 AccountMeta::new(escrow_pda, false),
366 AccountMeta::new(payment_pda, false),
367 AccountMeta::new_readonly(solana_program::system_program::ID, false),
368 ],
369 data: ClosePayment {}.to_bytes(),
370 }
371 }
372
373 pub fn withdraw_fees(
377 authority: Pubkey,
378 beneficiary: Pubkey,
379 mint: Pubkey,
380 amount: u64,
381 ) -> Instruction {
382 let (bank_pda, _) = Self::bank_pda();
383 let (config_pda, _) = Self::config_pda();
384 let (escrow_pda, _) = Self::escrow_pda(mint);
385
386 let is_sol = mint == Pubkey::default();
387
388 let mut accounts = vec![
389 AccountMeta::new(authority, true),
390 AccountMeta::new_readonly(bank_pda, false),
391 AccountMeta::new_readonly(config_pda, false),
392 AccountMeta::new(escrow_pda, false),
393 AccountMeta::new(beneficiary, false),
394 ];
395
396 if is_sol {
397 let (sol_storage_pda, _) = Self::sol_storage_pda(mint, bank_pda, escrow_pda);
399 accounts.push(AccountMeta::new(sol_storage_pda, false));
400 accounts.push(AccountMeta::new_readonly(
401 solana_program::system_program::ID,
402 false,
403 ));
404 } else {
405 let escrow_tokens = Self::escrow_token_account(mint);
407 let beneficiary_tokens = Self::associated_token_account(beneficiary, mint);
408 accounts[4] = AccountMeta::new(escrow_tokens, false);
410 accounts.push(AccountMeta::new(beneficiary_tokens, false));
411 accounts.push(AccountMeta::new_readonly(mint, false));
412 accounts.push(AccountMeta::new_readonly(spl_token::ID, false));
413 }
414
415 Instruction {
416 program_id: Self::program_id(),
417 accounts,
418 data: WithdrawFees {
419 amount: amount.to_le_bytes(),
420 }
421 .to_bytes(),
422 }
423 }
424
425 pub fn confirm_fulfillment(buyer: Pubkey, mint: Pubkey, payment_uid: &str) -> Instruction {
426 let (bank_pda, _) = Self::bank_pda();
427 let (config_pda, _) = Self::config_pda();
428 let (escrow_pda, _) = Self::escrow_pda(mint);
429 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
430
431 Instruction {
432 program_id: Self::program_id(),
433 accounts: vec![
434 AccountMeta::new(buyer, true),
435 AccountMeta::new_readonly(bank_pda, false), AccountMeta::new_readonly(config_pda, false),
437 AccountMeta::new(escrow_pda, false), AccountMeta::new(payment_pda, false), ],
440 data: ConfirmFulfillment {}.to_bytes(),
441 }
442 }
443
444 pub fn dispute_payment(
446 disputer: Pubkey,
447 mint: Pubkey,
448 payment_uid: &str,
449 reason: u8,
450 description: Option<&str>,
451 ) -> Instruction {
452 let (bank_pda, _) = Self::bank_pda();
453 let (config_pda, _) = Self::config_pda();
454 let (escrow_pda, _) = Self::escrow_pda(mint);
455 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
456
457 let mut desc_bytes = [0u8; 64];
458 if let Some(desc) = description {
459 let desc_str = desc.as_bytes();
460 let len = desc_str.len().min(64);
461 desc_bytes[..len].copy_from_slice(&desc_str[..len]);
462 }
463
464 Instruction {
465 program_id: Self::program_id(),
466 accounts: vec![
467 AccountMeta::new(disputer, true),
468 AccountMeta::new_readonly(bank_pda, false),
469 AccountMeta::new_readonly(config_pda, false),
470 AccountMeta::new(escrow_pda, false),
471 AccountMeta::new(payment_pda, false),
472 AccountMeta::new_readonly(solana_program::system_program::ID, false),
473 AccountMeta::new_readonly(solana_program::sysvar::rent::ID, false),
474 ],
475 data: DisputePayment {
476 reason,
477 description: desc_bytes,
478 }
479 .to_bytes(),
480 }
481 }
482
483 pub fn arbitrate_payment(
485 arbitrator: Pubkey,
486 mint: Pubkey,
487 payment_uid: &str,
488 decision: u8,
489 buyer_amount: u64,
490 seller_amount: u64,
491 reason: u8,
492 description: Option<&str>,
493 ) -> Instruction {
494 let (bank_pda, _) = Self::bank_pda();
495 let (config_pda, _) = Self::config_pda();
496 let (escrow_pda, _) = Self::escrow_pda(mint);
497 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
498
499 let mut description_bytes = [0u8; 64];
500 if let Some(desc_str) = description {
501 let desc_bytes_str = desc_str.as_bytes();
502 let len = desc_bytes_str.len().min(64);
503 description_bytes[..len].copy_from_slice(&desc_bytes_str[..len]);
504 }
505
506 Instruction {
507 program_id: Self::program_id(),
508 accounts: vec![
509 AccountMeta::new_readonly(arbitrator, true),
510 AccountMeta::new_readonly(bank_pda, false),
511 AccountMeta::new_readonly(config_pda, false),
512 AccountMeta::new(escrow_pda, false),
513 AccountMeta::new(payment_pda, false),
514 AccountMeta::new_readonly(mint, false),
515 ],
516 data: ArbitratePayment {
517 buyer_amount: buyer_amount.to_le_bytes(),
518 seller_amount: seller_amount.to_le_bytes(),
519 decision,
520 reason,
521 description: description_bytes,
522 _padding: [0; 6],
523 }
524 .to_bytes(),
525 }
526 }
527
528 pub fn resolve_dispute(
532 resolver: Pubkey,
533 buyer_ata: Option<Pubkey>, seller_ata: Option<Pubkey>, arbitrator: Pubkey, mint: Pubkey,
537 payment_uid: &str,
538 decision: u8,
539 buyer_amount: u64,
540 seller_amount: u64,
541 ) -> Instruction {
542 let (bank_pda, _) = Self::bank_pda();
543 let (config_pda, _) = Self::config_pda();
544 let (escrow_pda, _) = Self::escrow_pda(mint);
545 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
546
547 let is_sol = mint == Pubkey::default();
548
549 let mut accounts = vec![
550 AccountMeta::new(resolver, true),
551 AccountMeta::new_readonly(bank_pda, false),
552 AccountMeta::new_readonly(config_pda, false),
553 AccountMeta::new(escrow_pda, false),
554 AccountMeta::new(payment_pda, false),
555 AccountMeta::new_readonly(mint, false),
556 ];
557
558 if is_sol {
559 let (sol_storage_pda, _) = Self::sol_storage_pda(mint, bank_pda, escrow_pda);
561 let buyer = buyer_ata.expect("buyer required for SOL");
562 let seller = seller_ata.expect("seller required for SOL");
563 accounts.push(AccountMeta::new(sol_storage_pda, false));
564 accounts.push(AccountMeta::new(buyer, false));
565 accounts.push(AccountMeta::new(seller, false));
566 accounts.push(AccountMeta::new_readonly(arbitrator, true));
567 accounts.push(AccountMeta::new_readonly(
568 solana_program::system_program::ID,
569 false,
570 ));
571 } else {
572 let buyer_ata = buyer_ata.expect("buyer_ata required for SPL tokens");
574 let seller_ata = seller_ata.expect("seller_ata required for SPL tokens");
575 let escrow_tokens = Self::escrow_token_account(mint);
576 accounts.push(AccountMeta::new(escrow_tokens, false));
577 accounts.push(AccountMeta::new(buyer_ata, false));
578 accounts.push(AccountMeta::new(seller_ata, false));
579 accounts.push(AccountMeta::new_readonly(arbitrator, true));
580 accounts.push(AccountMeta::new_readonly(spl_token::ID, false));
581 }
582
583 Instruction {
584 program_id: Self::program_id(),
585 accounts,
586 data: ResolveDispute {
587 decision,
588 buyer_amount: buyer_amount.to_le_bytes(),
589 seller_amount: seller_amount.to_le_bytes(),
590 _padding: [0; 7],
591 }
592 .to_bytes(),
593 }
594 }
595
596 pub fn extend_payment_ttl(
598 caller: Pubkey,
599 mint: Pubkey,
600 payment_uid: &str,
601 additional_seconds: i64,
602 ) -> Instruction {
603 let (bank_pda, _) = Self::bank_pda();
604 let (config_pda, _) = Self::config_pda();
605 let (escrow_pda, _) = Self::escrow_pda(mint);
606 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
607
608 Instruction {
609 program_id: Self::program_id(),
610 accounts: vec![
611 AccountMeta::new(caller, true),
612 AccountMeta::new_readonly(bank_pda, false), AccountMeta::new_readonly(config_pda, false),
614 AccountMeta::new_readonly(escrow_pda, false),
615 AccountMeta::new(payment_pda, false), AccountMeta::new_readonly(solana_program::system_program::ID, false),
617 AccountMeta::new_readonly(solana_program::sysvar::rent::ID, false),
618 ],
619 data: ExtendPaymentTTL {
620 additional_seconds: additional_seconds.to_le_bytes(),
621 }
622 .to_bytes(),
623 }
624 }
625
626 pub fn close_escrow(authority: Pubkey, recipient: Pubkey, mint: Pubkey) -> Instruction {
628 let (bank_pda, _) = Self::bank_pda();
629 let (escrow_pda, _) = Self::escrow_pda(mint);
630 let escrow_tokens = Self::escrow_token_account(mint);
631
632 Instruction {
633 program_id: Self::program_id(),
634 accounts: vec![
635 AccountMeta::new(authority, true),
636 AccountMeta::new_readonly(bank_pda, false),
637 AccountMeta::new(escrow_pda, false),
638 AccountMeta::new(escrow_tokens, false),
639 AccountMeta::new_readonly(mint, false),
640 AccountMeta::new_readonly(spl_token::ID, false),
641 AccountMeta::new(recipient, false),
642 ],
643 data: CloseEscrow {}.to_bytes(),
644 }
645 }
646
647 pub fn update_escrow_settings(
649 authority: Pubkey,
650 escrow: Pubkey,
651 fee_bps: u16,
652 min_payment_amount: u64,
653 max_payment_amount: u64,
654 ) -> Instruction {
655 let (bank_pda, _) = Self::bank_pda();
656 let (config_pda, _) = Self::config_pda();
657
658 Instruction {
659 program_id: Self::program_id(),
660 accounts: vec![
661 AccountMeta::new(authority, true),
662 AccountMeta::new_readonly(bank_pda, false), AccountMeta::new_readonly(config_pda, false),
664 AccountMeta::new(escrow, false), AccountMeta::new_readonly(solana_program::system_program::ID, false),
666 AccountMeta::new_readonly(solana_program::sysvar::rent::ID, false),
667 ],
668 data: UpdateEscrowSettings {
669 min_payment_amount: min_payment_amount.to_le_bytes(),
670 max_payment_amount: max_payment_amount.to_le_bytes(),
671 new_fee_bps: fee_bps.to_le_bytes(),
672 _padding: [0; 6],
673 }
674 .to_bytes(),
675 }
676 }
677
678 pub fn pause_escrow(authority: Pubkey, mint: Pubkey, pause: bool) -> Instruction {
680 let (bank_pda, _) = Self::bank_pda();
681 let (config_pda, _) = Self::config_pda();
682 let (escrow_pda, _) = Self::escrow_pda(mint);
683
684 Instruction {
685 program_id: Self::program_id(),
686 accounts: vec![
687 AccountMeta::new(authority, true),
688 AccountMeta::new_readonly(bank_pda, false), AccountMeta::new_readonly(config_pda, false),
690 AccountMeta::new(escrow_pda, false), AccountMeta::new_readonly(solana_program::system_program::ID, false),
692 AccountMeta::new_readonly(solana_program::sysvar::rent::ID, false),
693 ],
694 data: PauseEscrow {
695 pause: if pause { 1 } else { 0 },
696 }
697 .to_bytes(),
698 }
699 }
700
701 pub fn update_authority(current_authority: Pubkey, new_authority: Pubkey) -> Instruction {
703 let (bank_pda, _) = Self::bank_pda();
704
705 Instruction {
706 program_id: Self::program_id(),
707 accounts: vec![
708 AccountMeta::new(current_authority, true),
709 AccountMeta::new(bank_pda, false),
710 ],
711 data: UpdateAuthority { new_authority }.to_bytes(),
712 }
713 }
714
715 pub fn appeal_arbitration(
717 appellant: Pubkey,
718 mint: Pubkey,
719 payment_uid: &str,
720 reason: u8,
721 description: Option<&str>,
722 ) -> Instruction {
723 let (bank_pda, _) = Self::bank_pda();
724 let (config_pda, _) = Self::config_pda();
725 let (escrow_pda, _) = Self::escrow_pda(mint);
726 let (payment_pda, _) = crate::state::payment_pda(payment_uid, bank_pda);
727
728 let mut description_bytes = [0u8; 64];
729 if let Some(desc_str) = description {
730 let desc_bytes_str = desc_str.as_bytes();
731 let len = desc_bytes_str.len().min(64);
732 description_bytes[..len].copy_from_slice(&desc_bytes_str[..len]);
733 }
734
735 Instruction {
736 program_id: Self::program_id(),
737 accounts: vec![
738 AccountMeta::new(appellant, true),
739 AccountMeta::new_readonly(bank_pda, false),
740 AccountMeta::new_readonly(config_pda, false),
741 AccountMeta::new_readonly(escrow_pda, false),
742 AccountMeta::new(payment_pda, false),
743 ],
744 data: AppealArbitration {
745 description: description_bytes,
746 reason,
747 _padding: [0; 7],
748 }
749 .to_bytes(),
750 }
751 }
752
753 pub fn validate_payment_for_funding(payment: &Payment) -> Result<(), EscrowError> {
759 if payment.state != PAYMENT_STATE_FUNDED {
760 return Err(EscrowError::InvalidPaymentState);
761 }
762 Ok(())
763 }
764
765 pub fn validate_payment_for_release(payment: &Payment) -> Result<(), EscrowError> {
767 if payment.state != PAYMENT_STATE_FUNDED {
768 return Err(EscrowError::InvalidPaymentState);
769 }
770 Ok(())
771 }
772
773 pub fn validate_payment_for_refund(payment: &Payment) -> Result<(), EscrowError> {
775 if payment.state != PAYMENT_STATE_FUNDED {
776 return Err(EscrowError::InvalidPaymentState);
777 }
778 Ok(())
779 }
780
781 pub fn validate_payment_for_dispute(payment: &Payment) -> Result<(), EscrowError> {
783 if payment.state != PAYMENT_STATE_FUNDED {
784 return Err(EscrowError::InvalidPaymentState);
785 }
786 Ok(())
787 }
788
789 pub fn validate_payment_for_arbitration(payment: &Payment) -> Result<(), EscrowError> {
791 if payment.state != PAYMENT_STATE_DISPUTED {
792 return Err(EscrowError::InvalidPaymentState);
793 }
794 Ok(())
795 }
796
797 pub fn validate_payment_for_resolution(payment: &Payment) -> Result<(), EscrowError> {
799 if payment.state != PAYMENT_STATE_ARBITRATED {
800 return Err(EscrowError::InvalidPaymentState);
801 }
802 Ok(())
803 }
804
805 pub fn calculate_fee(amount: u64, fee_bps: u16) -> u64 {
811 let fee_bps = fee_bps as u128;
812 let amount = amount as u128;
813 let fee = (amount * fee_bps) / BASIS_POINTS_DENOMINATOR;
814 fee as u64
815 }
816
817 pub fn calculate_payout(amount: u64, fee_bps: u16) -> u64 {
819 let fee = Self::calculate_fee(amount, fee_bps);
820 amount.saturating_sub(fee)
821 }
822
823 pub fn is_payment_expired(payment: &Payment) -> bool {
825 let clock = solana_program::clock::Clock::get().unwrap_or_default();
826 clock.unix_timestamp > payment.expires_at
827 }
828
829 pub fn get_payment_state_string(state: u8) -> &'static str {
831 match state {
832 PAYMENT_STATE_FUNDED => "Funded",
833 PAYMENT_STATE_RELEASED => "Released",
834 PAYMENT_STATE_REFUNDED => "Refunded",
835 PAYMENT_STATE_DISPUTED => "Disputed",
836 PAYMENT_STATE_ARBITRATED => "Arbitrated",
837 _ => "Unknown",
838 }
839 }
840
841 pub fn get_escrow_state_string(paused: u8) -> &'static str {
843 match paused {
844 0 => "Active",
845 1 => "Paused",
846 _ => "Unknown",
847 }
848 }
849
850 pub fn get_payment_accounts(
856 buyer: Pubkey,
857 seller: Pubkey,
858 mint: Pubkey,
859 payment_uid: &str,
860 ) -> PaymentAccounts {
861 let (bank_pda, _) = Self::bank_pda();
862 let (escrow_pda, _) = Self::escrow_pda(mint);
863 let (payment_pda, _) = Self::payment_pda(payment_uid, bank_pda);
864 let escrow_tokens = Self::escrow_token_account(mint);
865
866 PaymentAccounts {
867 buyer,
868 seller,
869 mint,
870 bank: bank_pda,
871 escrow: escrow_pda,
872 payment: payment_pda,
873 escrow_tokens,
874 }
875 }
876
877 pub fn get_escrow_accounts(mint: Pubkey) -> EscrowAccounts {
879 let (bank_pda, _) = Self::bank_pda();
880 let (escrow_pda, _) = Self::escrow_pda(mint);
881 let escrow_tokens = Self::escrow_token_account(mint);
882
883 EscrowAccounts {
884 mint,
885 bank: bank_pda,
886 escrow: escrow_pda,
887 escrow_tokens,
888 }
889 }
890}
891
892#[derive(Debug, Clone)]
898pub struct PaymentAccounts {
899 pub buyer: Pubkey,
900 pub seller: Pubkey,
901 pub mint: Pubkey,
902 pub bank: Pubkey,
903 pub escrow: Pubkey,
904 pub payment: Pubkey,
905 pub escrow_tokens: Pubkey,
906}
907
908#[derive(Debug, Clone)]
910pub struct EscrowAccounts {
911 pub mint: Pubkey,
912 pub bank: Pubkey,
913 pub escrow: Pubkey,
914 pub escrow_tokens: Pubkey,
915}
916
917pub struct PaymentBuilder {
919 buyer: Option<Pubkey>,
920 buyer_tokens: Option<Pubkey>,
921 seller: Option<Pubkey>,
922 mint: Option<Pubkey>,
923 amount: Option<u64>,
924 ttl_seconds: Option<i64>,
925 reference: Option<Pubkey>,
926 payment_uid: Option<String>,
927 order_num: Option<String>,
928}
929
930impl PaymentBuilder {
931 pub fn new() -> Self {
932 Self {
933 buyer: None,
934 buyer_tokens: None,
935 seller: None,
936 mint: None,
937 amount: None,
938 ttl_seconds: None,
939 reference: None,
940 payment_uid: None,
941 order_num: None,
942 }
943 }
944
945 pub fn buyer(mut self, buyer: Pubkey) -> Self {
946 self.buyer = Some(buyer);
947 self
948 }
949
950 pub fn buyer_tokens(mut self, buyer_tokens: Pubkey) -> Self {
951 self.buyer_tokens = Some(buyer_tokens);
952 self
953 }
954
955 pub fn seller(mut self, seller: Pubkey) -> Self {
956 self.seller = Some(seller);
957 self
958 }
959
960 pub fn mint(mut self, mint: Pubkey) -> Self {
961 self.mint = Some(mint);
962 self
963 }
964
965 pub fn amount(mut self, amount: u64) -> Self {
966 self.amount = Some(amount);
967 self
968 }
969
970 pub fn ttl_seconds(mut self, ttl_seconds: i64) -> Self {
971 self.ttl_seconds = Some(ttl_seconds);
972 self
973 }
974
975 pub fn reference(mut self, reference: Pubkey) -> Self {
976 self.reference = Some(reference);
977 self
978 }
979
980 pub fn payment_uid(mut self, payment_uid: &str) -> Self {
981 self.payment_uid = Some(payment_uid.to_string());
982 self
983 }
984
985 pub fn order_num(mut self, order_num: &str) -> Self {
986 self.order_num = Some(order_num.to_string());
987 self
988 }
989
990 pub fn build(self) -> Result<Instruction, EscrowError> {
991 let buyer = self.buyer.ok_or(EscrowError::MissingRequiredField)?;
992 let buyer_tokens = self.buyer_tokens; let seller = self.seller.ok_or(EscrowError::MissingRequiredField)?;
994 let mint = self.mint.ok_or(EscrowError::MissingRequiredField)?;
995 let amount = self.amount.ok_or(EscrowError::MissingRequiredField)?;
996 let ttl_seconds = self.ttl_seconds.ok_or(EscrowError::MissingRequiredField)?;
997 let reference = self.reference;
998 let payment_uid = self.payment_uid.ok_or(EscrowError::MissingRequiredField)?;
999 let order_num = self.order_num.ok_or(EscrowError::MissingRequiredField)?;
1000
1001 Ok(EscrowSdk::fund_payment(
1002 buyer,
1003 buyer_tokens,
1004 seller,
1005 mint,
1006 amount,
1007 ttl_seconds,
1008 reference,
1009 &payment_uid,
1010 &order_num,
1011 ))
1012 }
1013}
1014
1015impl Default for PaymentBuilder {
1016 fn default() -> Self {
1017 Self::new()
1018 }
1019}
1020
1021pub struct EscrowBuilder {
1023 authority: Option<Pubkey>,
1024 funder: Option<Pubkey>,
1025 mint: Option<Pubkey>,
1026 min_payment_amount: Option<u64>,
1027 max_payment_amount: Option<u64>,
1028 fee_bps: Option<u16>,
1029}
1030
1031impl EscrowBuilder {
1032 pub fn new() -> Self {
1033 Self {
1034 authority: None,
1035 funder: None,
1036 mint: None,
1037 min_payment_amount: None,
1038 max_payment_amount: None,
1039 fee_bps: None,
1040 }
1041 }
1042
1043 pub fn authority(mut self, authority: Pubkey) -> Self {
1044 self.authority = Some(authority);
1045 self
1046 }
1047
1048 pub fn funder(mut self, funder: Pubkey) -> Self {
1049 self.funder = Some(funder);
1050 self
1051 }
1052
1053 pub fn mint(mut self, mint: Pubkey) -> Self {
1054 self.mint = Some(mint);
1055 self
1056 }
1057
1058 pub fn min_payment_amount(mut self, min_payment_amount: u64) -> Self {
1059 self.min_payment_amount = Some(min_payment_amount);
1060 self
1061 }
1062
1063 pub fn max_payment_amount(mut self, max_payment_amount: u64) -> Self {
1064 self.max_payment_amount = Some(max_payment_amount);
1065 self
1066 }
1067
1068 pub fn fee_bps(mut self, fee_bps: u16) -> Self {
1069 self.fee_bps = Some(fee_bps);
1070 self
1071 }
1072
1073 pub fn create_escrow(self) -> Result<Instruction, EscrowError> {
1074 let authority = self.authority.ok_or(EscrowError::MissingRequiredField)?;
1075 let funder = self.funder.ok_or(EscrowError::MissingRequiredField)?;
1076 let mint = self.mint.ok_or(EscrowError::MissingRequiredField)?;
1077 let min_payment_amount = self.min_payment_amount.unwrap_or(1); let max_payment_amount = self.max_payment_amount.unwrap_or(u64::MAX); let fee_bps = self.fee_bps.unwrap_or(50); Ok(EscrowSdk::open_escrow(
1082 authority,
1083 funder,
1084 mint,
1085 min_payment_amount,
1086 max_payment_amount,
1087 fee_bps,
1088 ))
1089 }
1090
1091 pub fn pause_escrow(self, paused: bool) -> Result<Instruction, EscrowError> {
1092 let authority = self.authority.ok_or(EscrowError::MissingRequiredField)?;
1093 let mint = self.mint.ok_or(EscrowError::MissingRequiredField)?;
1094
1095 Ok(EscrowSdk::pause_escrow(authority, mint, paused))
1096 }
1097}
1098
1099impl Default for EscrowBuilder {
1100 fn default() -> Self {
1101 Self::new()
1102 }
1103}
1104
1105pub mod tokens {
1111 use solana_program::pubkey::Pubkey;
1112
1113 pub const USDC: Pubkey =
1115 solana_program::pubkey!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
1116
1117 pub const USDT: Pubkey =
1119 solana_program::pubkey!("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB");
1120
1121 pub const WSOL: Pubkey = solana_program::pubkey!("So11111111111111111111111111111111111111112");
1123
1124 pub const MARS: Pubkey =
1126 solana_program::pubkey!("7RAV5UPRTzxn46kLeA8MiJsdNy9VKc5fip8FWEgTpTHh");
1127
1128 pub const MIRACLE: Pubkey =
1130 solana_program::pubkey!("Mirab4SFVff6sCuK48PPnSUj7PNpDDrBWY6FkJmuifG");
1131
1132 pub const TESTCOIN: Pubkey =
1134 solana_program::pubkey!("2gNCDGj8Xi9Zs7LNQTPWf4pfZvAM7UHusY4xhKNYg6W6");
1135}
1136
1137pub mod payment_states {
1139 use crate::consts::{
1140 PAYMENT_STATE_ARBITRATED, PAYMENT_STATE_DISPUTED, PAYMENT_STATE_FUNDED,
1141 PAYMENT_STATE_REFUNDED, PAYMENT_STATE_RELEASED,
1142 };
1143
1144 pub const FUNDED: u8 = PAYMENT_STATE_FUNDED;
1145 pub const RELEASED: u8 = PAYMENT_STATE_RELEASED;
1146 pub const REFUNDED: u8 = PAYMENT_STATE_REFUNDED;
1147 pub const DISPUTED: u8 = PAYMENT_STATE_DISPUTED;
1148 pub const ARBITRATED: u8 = PAYMENT_STATE_ARBITRATED;
1149}
1150
1151pub mod escrow_states {
1153 pub const ACTIVE: u8 = 0;
1154 pub const PAUSED: u8 = 1;
1155}