1use crate::primitives::{Address, Hash, Timestamp};
7use crate::principal_chain::PrincipalChain;
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct SettlementRequest {
16 pub request_id: String,
18 pub provider: Address,
20 pub customer: Address,
22 pub service_type: ServiceType,
24 pub payment_intent: PaymentIntent,
26 pub amount: u64,
28 pub service_proof: ServiceProof,
30 pub timestamp: Timestamp,
32 pub deadline: Option<Timestamp>,
34}
35
36impl SettlementRequest {
37 pub fn new(
39 provider: Address,
40 customer: Address,
41 service_type: ServiceType,
42 amount: u64,
43 service_proof: ServiceProof,
44 ) -> Self {
45 Self {
46 request_id: uuid::Uuid::new_v4().to_string(),
47 provider,
48 customer,
49 service_type,
50 payment_intent: PaymentIntent::Immediate,
51 amount,
52 service_proof,
53 timestamp: Timestamp::now(),
54 deadline: None,
55 }
56 }
57
58 pub fn with_payment_intent(mut self, intent: PaymentIntent) -> Self {
60 self.payment_intent = intent;
61 self
62 }
63
64 pub fn with_deadline(mut self, deadline: Timestamp) -> Self {
66 self.deadline = Some(deadline);
67 self
68 }
69
70 pub fn is_expired(&self) -> bool {
72 if let Some(deadline) = self.deadline {
73 Timestamp::now() > deadline
74 } else {
75 false
76 }
77 }
78}
79
80#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
82pub struct SettlementReceipt {
83 pub receipt_id: String,
85 pub request_id: String,
87 pub transaction_hash: Hash,
89 pub provider: Address,
91 pub customer: Address,
93 pub service_type: ServiceType,
95 pub amount: u64,
97 pub status: SettlementStatus,
99 pub settled_at: Timestamp,
101 pub principal_chain: PrincipalChain,
108 pub metadata: HashMap<String, String>,
110}
111
112impl SettlementReceipt {
113 #[allow(clippy::too_many_arguments)]
121 pub fn new(
122 request_id: String,
123 transaction_hash: Hash,
124 provider: Address,
125 customer: Address,
126 service_type: ServiceType,
127 amount: u64,
128 status: SettlementStatus,
129 principal_chain: PrincipalChain,
130 ) -> Self {
131 Self {
132 receipt_id: uuid::Uuid::new_v4().to_string(),
133 request_id,
134 transaction_hash,
135 provider,
136 customer,
137 service_type,
138 amount,
139 status,
140 settled_at: Timestamp::now(),
141 principal_chain,
142 metadata: HashMap::new(),
143 }
144 }
145
146 pub fn add_metadata(&mut self, key: String, value: String) {
148 self.metadata.insert(key, value);
149 }
150}
151
152#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
154pub enum SettlementStatus {
155 Pending,
157 Completed,
159 Failed,
161 Disputed,
163 Refunded,
165}
166
167#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
169#[serde(tag = "type", content = "details")]
170pub enum ServiceType {
171 ModelInference {
173 model_id: String,
175 tokens: u32,
177 },
178 TeeComputation {
180 computation_id: String,
182 compute_units: u64,
184 },
185 Storage {
187 data_size: u64,
189 duration: u64,
191 },
192 AgentExecution {
194 agent_id: String,
196 task_id: String,
198 },
199 DataService {
201 service_id: String,
203 volume: u64,
205 },
206 Bridge {
208 transfer_id: String,
210 amount: u64,
212 },
213 HttpPayment {
215 protocol: String,
217 resource: String,
219 },
220 Custom {
222 name: String,
224 parameters: HashMap<String, String>,
226 },
227}
228
229impl ServiceType {
230 pub fn type_name(&self) -> &str {
232 match self {
233 Self::ModelInference { .. } => "ModelInference",
234 Self::TeeComputation { .. } => "TeeComputation",
235 Self::Storage { .. } => "Storage",
236 Self::AgentExecution { .. } => "AgentExecution",
237 Self::DataService { .. } => "DataService",
238 Self::Bridge { .. } => "Bridge",
239 Self::HttpPayment { .. } => "HttpPayment",
240 Self::Custom { .. } => "Custom",
241 }
242 }
243}
244
245#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
247pub enum PaymentIntent {
248 Immediate,
250 Deferred,
252 OnDelivery,
254 Escrow,
256 Subscription,
258}
259
260#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
262pub struct ServiceProof {
263 pub proof_type: ProofType,
265 pub proof_data: Vec<u8>,
267 pub signatures: Vec<ProofSignature>,
269 pub attestation: Option<Vec<u8>>,
271}
272
273impl ServiceProof {
274 pub fn new(proof_type: ProofType, proof_data: Vec<u8>) -> Self {
276 Self {
277 proof_type,
278 proof_data,
279 signatures: Vec::new(),
280 attestation: None,
281 }
282 }
283
284 pub fn add_signature(&mut self, signature: ProofSignature) {
286 self.signatures.push(signature);
287 }
288
289 pub fn with_attestation(mut self, attestation: Vec<u8>) -> Self {
291 self.attestation = Some(attestation);
292 self
293 }
294}
295
296#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
298pub enum ProofType {
299 Cryptographic,
301 TeeAttestation,
303 MultiParty,
305 Merkle,
307 ZeroKnowledge,
309 Oracle,
311}
312
313#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
315pub struct ProofSignature {
316 pub signer: Address,
318 pub signature: Vec<u8>,
320 pub role: SignerRole,
322}
323
324#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
326pub enum SignerRole {
327 Provider,
329 Consumer,
331 Verifier,
333 Oracle,
335}
336
337#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
339pub struct EscrowConfig {
340 pub escrow_address: Address,
342 pub amount: u64,
344 pub release_conditions: ReleaseConditions,
346 pub timeout: Timestamp,
348}
349
350#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
352pub enum ReleaseConditions {
353 ProviderSignature,
355 ConsumerSignature,
357 BothSignatures,
359 VerifierSignature,
361 Timeout,
363 Custom { condition: String },
365}