Skip to main content

solmail_program/
lib.rs

1//! SolMail On-Chain Program
2//!
3//! A Solana program using Pinocchio for efficient payment handling:
4//! - Payment Escrow: Hold SOL/USDC until mail delivery is confirmed
5//! - Prepaid Credits: Deposit funds, spend on mail orders
6
7#![no_std]
8
9extern crate alloc;
10
11use pinocchio::{
12    account_info::AccountInfo,
13    entrypoint,
14    program_error::ProgramError,
15    pubkey::Pubkey,
16    ProgramResult,
17};
18
19pub mod constants;
20pub mod error;
21pub mod instructions;
22pub mod state;
23
24// Declare program entrypoint
25entrypoint!(process_instruction);
26
27/// Program entrypoint
28///
29/// Instruction discriminators:
30/// - 0: create_escrow - Create a new payment escrow
31/// - 1: confirm_delivery - Oracle confirms delivery, releases funds
32/// - 2: refund_escrow - Refund after timeout
33/// - 3: deposit_credits - Add prepaid credits
34/// - 4: spend_credits - Oracle deducts credits for mail order
35/// - 5: withdraw_credits - User withdraws unused credits
36pub fn process_instruction(
37    program_id: &Pubkey,
38    accounts: &[AccountInfo],
39    instruction_data: &[u8],
40) -> ProgramResult {
41    if instruction_data.is_empty() {
42        return Err(ProgramError::InvalidInstructionData);
43    }
44
45    let instruction = instruction_data[0];
46    let data = &instruction_data[1..];
47
48    match instruction {
49        // Escrow instructions
50        0 => instructions::escrow::create::process(program_id, accounts, data),
51        1 => instructions::escrow::confirm::process(program_id, accounts, data),
52        2 => instructions::escrow::refund::process(program_id, accounts, data),
53
54        // Credits instructions
55        3 => instructions::credits::deposit::process(program_id, accounts, data),
56        4 => instructions::credits::spend::process(program_id, accounts, data),
57        5 => instructions::credits::withdraw::process(program_id, accounts, data),
58
59        _ => Err(ProgramError::InvalidInstructionData),
60    }
61}