|
| 1 | +/// Batch module – shared types for the SubTrackr batch operations contract. |
| 2 | +use soroban_sdk::{contracttype, String, Vec}; |
| 3 | +use subtrackr_types::SubscriptionId; |
| 4 | + |
| 5 | +// ── Subscription status ─────────────────────────────────────────────────────── |
| 6 | + |
| 7 | +#[contracttype] |
| 8 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 9 | +pub enum SubStatus { |
| 10 | + Active, |
| 11 | + Paused, |
| 12 | + Cancelled, |
| 13 | +} |
| 14 | + |
| 15 | +// ── Subscription record (lightweight on-chain representation) ───────────────── |
| 16 | + |
| 17 | +#[contracttype] |
| 18 | +#[derive(Clone)] |
| 19 | +pub struct SubRecord { |
| 20 | + pub exists: bool, |
| 21 | + pub status: SubStatus, |
| 22 | + pub charged: i128, |
| 23 | +} |
| 24 | + |
| 25 | +// ── Operation types ─────────────────────────────────────────────────────────── |
| 26 | + |
| 27 | +#[contracttype] |
| 28 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 29 | +pub enum OperationType { |
| 30 | + Create, |
| 31 | + Charge, |
| 32 | + Update, |
| 33 | + Cancel, |
| 34 | + Noop, |
| 35 | +} |
| 36 | + |
| 37 | +// ── Cancellation reasons ────────────────────────────────────────────────────── |
| 38 | + |
| 39 | +#[contracttype] |
| 40 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 41 | +pub enum CancelReason { |
| 42 | + UserRequested, |
| 43 | + PaymentFailed, |
| 44 | + Expired, |
| 45 | + Custom, |
| 46 | +} |
| 47 | + |
| 48 | +// ── Batch operation input ───────────────────────────────────────────────────── |
| 49 | + |
| 50 | +#[contracttype] |
| 51 | +#[derive(Clone)] |
| 52 | +pub struct BatchOperation { |
| 53 | + /// Ordered list of subscription IDs to process. |
| 54 | + pub subscription_ids: Vec<SubscriptionId>, |
| 55 | + /// Parallel i128 parameter per subscription (e.g. charge amount). |
| 56 | + pub params: Vec<i128>, |
| 57 | + /// Cancellation reasons aligned with subscription_ids for Cancel ops. |
| 58 | + pub cancel_reasons: Vec<CancelReason>, |
| 59 | + pub operation_type: OperationType, |
| 60 | +} |
| 61 | + |
| 62 | +// ── Per-operation result ────────────────────────────────────────────────────── |
| 63 | + |
| 64 | +#[contracttype] |
| 65 | +#[derive(Clone)] |
| 66 | +pub struct OperationResult { |
| 67 | + pub subscription_id: SubscriptionId, |
| 68 | + pub success: bool, |
| 69 | + /// Non-zero error code on failure. |
| 70 | + pub code: u32, |
| 71 | + /// Optional human-readable reason. |
| 72 | + pub reason: Option<String>, |
| 73 | +} |
| 74 | + |
| 75 | +// ── Batch-wide result ───────────────────────────────────────────────────────── |
| 76 | + |
| 77 | +#[contracttype] |
| 78 | +#[derive(Clone)] |
| 79 | +pub struct BatchResult { |
| 80 | + pub results: Vec<OperationResult>, |
| 81 | + pub state: BatchState, |
| 82 | + pub total_operations: u32, |
| 83 | + pub successful_operations: u32, |
| 84 | + pub failed_operations: u32, |
| 85 | + pub skipped_operations: u32, |
| 86 | + /// Whether all operations must succeed or all roll back. |
| 87 | + pub atomic: bool, |
| 88 | + /// True when an atomic batch was rolled back due to failure. |
| 89 | + pub rolled_back: bool, |
| 90 | +} |
| 91 | + |
| 92 | +// ── Batch execution state ───────────────────────────────────────────────────── |
| 93 | + |
| 94 | +#[contracttype] |
| 95 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 96 | +pub enum BatchState { |
| 97 | + Pending, |
| 98 | + Executing, |
| 99 | + Completed, |
| 100 | + PartiallyCompleted, |
| 101 | + Failed, |
| 102 | + RolledBack, |
| 103 | +} |
| 104 | + |
| 105 | +// ── Status summary returned to callers ─────────────────────────────────────── |
| 106 | + |
| 107 | +#[contracttype] |
| 108 | +#[derive(Clone)] |
| 109 | +pub struct BatchStatus { |
| 110 | + pub batch_id: u64, |
| 111 | + pub state: BatchState, |
| 112 | + pub total: u32, |
| 113 | + pub succeeded: u32, |
| 114 | + pub failed: u32, |
| 115 | +} |
| 116 | + |
| 117 | +// ── Optional filter for history queries ────────────────────────────────────── |
| 118 | + |
| 119 | +#[contracttype] |
| 120 | +#[derive(Clone)] |
| 121 | +pub struct BatchFilter { |
| 122 | + pub operation_type: Option<OperationType>, |
| 123 | + pub state: Option<BatchState>, |
| 124 | +} |
0 commit comments