Skip to main content

sla_escrow_api/state/
mod.rs

1mod bank;
2mod config;
3mod escrow;
4mod payment;
5
6pub use bank::*;
7pub use config::*;
8pub use escrow::*;
9pub use payment::*;
10
11use steel::*;
12
13use crate::consts::*;
14
15#[repr(u8)]
16#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
17pub enum EscrowAccount {
18    Bank = 100,
19    Config = 101,
20    Escrow = 102,
21    Payment = 103,
22}
23
24/// Derive the PDA of the bank account.
25pub fn bank_pda() -> (Pubkey, u8) {
26    Pubkey::find_program_address(&[BANK], &crate::id())
27}
28
29/// Derive the PDA of the config account.
30pub fn config_pda() -> (Pubkey, u8) {
31    Pubkey::find_program_address(&[CONFIG], &crate::id())
32}
33
34/// Derive the PDA of an escrow account.
35pub fn escrow_pda(mint: Pubkey, bank: Pubkey) -> (Pubkey, u8) {
36    Pubkey::find_program_address(&[ESCROW, mint.as_ref(), bank.as_ref()], &crate::id())
37}
38
39/// Derive the PDA of a payment account.
40pub fn payment_pda(payment_uid: &str, bank: Pubkey) -> (Pubkey, u8) {
41    // Convert UUID string to bytes, remove dashes, pad to 32 bytes
42    let mut uid_bytes = [0u8; 32];
43    let uid_str = payment_uid.replace('-', ""); // Remove UUID dashes
44    let uid_bytes_str = uid_str.as_bytes();
45    let len = uid_bytes_str.len().min(32);
46    uid_bytes[..len].copy_from_slice(&uid_bytes_str[..len]);
47
48    Pubkey::find_program_address(&[PAYMENT, &uid_bytes, bank.as_ref()], &crate::id())
49}
50
51/// Derive the PDA of a SOL storage account.
52/// This account holds SOL for escrows and can be used for transfers (no data, just lamports).
53pub fn sol_storage_pda(mint: Pubkey, bank: Pubkey, escrow: Pubkey) -> (Pubkey, u8) {
54    Pubkey::find_program_address(
55        &[SOL_STORAGE, mint.as_ref(), bank.as_ref(), escrow.as_ref()],
56        &crate::id(),
57    )
58}