forked from QuickLendX/quicklendx-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.rs
More file actions
449 lines (414 loc) · 13.3 KB
/
Copy pathtypes.rs
File metadata and controls
449 lines (414 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Core data types for the QuickLendX protocol.
//!
//! This module defines the persistent data structures stored in the blockchain.
//! All types are designed for Soroban compatibility using `#[contracttype]`.
//!
//! Key design principles:
//! - Direct storage optimization: minimal nesting for frequently accessed fields
//! - Future-proofing: use of optional fields and versioned enums
//! - Type safety: strong typing for status and categories
//! - Addresses are used for identity to leverage Soroban's built-in access control
use crate::DisputeResolution as OtherDisputeResolution;
use soroban_sdk::{contracttype, Address, BytesN, String, Vec};
/// Invoice status enumeration representing the lifecycle of an invoice
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InvoiceStatus {
Pending,
Verified,
Funded,
Paid,
Defaulted,
Cancelled,
Refunded,
}
impl InvoiceStatus {
pub fn is_terminal(&self) -> bool {
matches!(
self,
InvoiceStatus::Paid
| InvoiceStatus::Defaulted
| InvoiceStatus::Cancelled
| InvoiceStatus::Refunded
)
}
}
/// Invoice lock state controlled by admin holds.
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InvoiceLock {
None,
Frozen,
}
impl InvoiceLock {
pub fn is_locked(&self) -> bool {
*self != Self::None
}
}
/// Bid status enumeration
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BidStatus {
Placed,
Accepted,
Withdrawn,
Expired,
Cancelled,
}
/// Investment status enumeration tracking the lifecycle of investor positions.
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InvestmentStatus {
/// The investment is active, funded, and tracked in the active investment index.
/// This is the only non-terminal state.
Active,
/// Investment funds were withdrawn by the investor (terminal status).
Withdrawn,
/// Investment completed successfully, and the investor has received their payouts (terminal status).
Completed,
/// The associated invoice defaulted due to non-payment, triggering default/insurance logic (terminal status).
Defaulted,
/// Investment was refunded due to invoice cancellation (terminal status).
Refunded,
}
/// Dispute status enumeration
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DisputeStatus {
None,
Disputed,
UnderReview,
Resolved,
}
/// Dispute resolution outcome enumeration
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DisputeResolution {
None,
FavorBusiness,
FavorInvestor,
Split,
Dismissed,
}
impl DisputeResolution {
pub fn code(self) -> u32 {
match self {
Self::None => 0,
Self::FavorBusiness => 1,
Self::FavorInvestor => 2,
Self::Split => 3,
Self::Dismissed => 4,
}
}
}
/// Invoice category for classification
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InvoiceCategory {
Services,
Goods,
Consulting,
Logistics,
Products,
Manufacturing,
Technology,
Healthcare,
Other,
}
/// Line item record for invoice metadata
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LineItemRecord(pub String, pub u32, pub i128, pub i128);
/// Payment record for invoice history
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PaymentRecord {
pub amount: i128,
pub payer: Address,
pub timestamp: u64,
pub transaction_id: String,
}
/// Dispute data structure
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Dispute {
pub created_by: Address,
pub created_at: u64,
pub reason: String,
pub evidence: String,
pub resolution: String,
pub resolved_by: Address,
pub resolved_at: u64,
pub resolution_outcome: DisputeResolution,
}
/// Invoice rating structure
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InvoiceRating {
pub rater: Address,
pub score: u32, // 1-5
pub comment: String,
pub timestamp: u64,
}
/// Freeze reason enumeration representing why a business invoice was frozen
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BusinessFreezeReason {
/// Generic administrative freeze (admin's discretion)
AdminAction,
/// Business KYC was rejected or revoked
KYCRejected,
/// Legal or compliance policy violation
ComplianceViolation,
/// Fraud or suspicious activity detected
SuspiciousActivity,
/// Court order or legal hold applied
LegalHold,
}
/// Freeze record stored alongside the frozen flag on an invoice
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FreezeInfo {
pub reason: BusinessFreezeReason,
pub frozen_by: Address,
pub frozen_at: u64,
}
/// Core Invoice data structure
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Invoice {
pub id: BytesN<32>,
pub business: Address,
pub amount: i128,
pub currency: Address,
pub due_date: u64,
pub status: InvoiceStatus,
pub created_at: u64,
pub description: String,
/// Optional customer display name. Max length enforced: `MAX_NAME_LENGTH`.
pub metadata_customer_name: Option<String>,
/// Optional customer address. Max length enforced: `MAX_ADDRESS_LENGTH`.
pub metadata_customer_address: Option<String>,
/// Optional tax identifier. Max length enforced: `MAX_TAX_ID_LENGTH`.
pub metadata_tax_id: Option<String>,
/// Optional free-text notes. Max length enforced: `MAX_NOTES_LENGTH`.
pub metadata_notes: Option<String>,
pub metadata_line_items: Vec<LineItemRecord>,
pub category: InvoiceCategory,
pub tags: Vec<String>,
pub funded_amount: i128,
pub funded_at: Option<u64>,
pub investor: Option<Address>,
pub settled_at: Option<u64>,
pub average_rating: Option<u32>,
pub total_ratings: u32,
pub ratings: Vec<InvoiceRating>,
pub dispute_status: DisputeStatus,
pub dispute: Dispute,
pub total_paid: i128,
pub payment_history: Vec<PaymentRecord>,
pub origination_fee_bps: Option<u32>,
}
pub const RATINGS_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
/// Versioned ratings snapshot for off-chain indexers and downstream contracts.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RatingsSnapshot {
pub schema_version: u32,
pub invoice_id: BytesN<32>,
pub average_rating: Option<u32>,
pub total_ratings: u32,
pub highest_rating: Option<u32>,
pub lowest_rating: Option<u32>,
pub ledger_sequence: u32,
}
/// Input type for a single invoice within a `store_invoices_batch` call.
///
/// Bundles every per-invoice field so the batch entrypoint can accept a
/// `Vec<InvoiceInput>` with a single-argument list, keeping the public ABI
/// clean and future-proof (adding optional metadata requires a new struct
/// version rather than a new variadic argument list).
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InvoiceInput {
/// Invoice face value in the smallest currency unit (must be > 0).
pub amount: i128,
/// Token contract address for the invoice currency.
pub currency: Address,
/// Unix timestamp by which the invoice must be settled (must be in the future).
pub due_date: u64,
/// Human-readable invoice description (max `MAX_DESCRIPTION_LENGTH` bytes).
pub description: String,
/// Invoice category (Services, Products, etc.).
pub category: InvoiceCategory,
/// Optional searchable tags (max `MAX_INVOICE_TAGS` entries, each max 50 bytes).
pub tags: Vec<String>,
}
/// Helper struct for metadata updates
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InvoiceMetadata {
/// Customer display name. Trimmed and validated to be non-empty. Max: `MAX_NAME_LENGTH`.
pub customer_name: String,
/// Customer address. Max: `MAX_ADDRESS_LENGTH`.
pub customer_address: String,
/// Tax identifier (free-form). Max: `MAX_TAX_ID_LENGTH`.
pub tax_id: String,
/// Line items vector. Each item's description validated against `MAX_DESCRIPTION_LENGTH`.
/// The vector length is bounded by `MAX_METADATA_LINE_ITEMS` in verification.
pub line_items: Vec<LineItemRecord>,
/// Free-form notes. Max: `MAX_NOTES_LENGTH`.
pub notes: String,
}
// Invoice logic is implemented in crate::invoice module.
/// Bid data structure
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Bid {
pub bid_id: BytesN<32>,
pub invoice_id: BytesN<32>,
pub investor: Address,
pub bid_amount: i128,
pub expected_return: i128,
pub timestamp: u64,
pub status: BidStatus,
pub expiration_timestamp: u64,
}
/// Investment data structure
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Investment {
pub investment_id: BytesN<32>,
pub invoice_id: BytesN<32>,
pub investor: Address,
pub amount: i128,
pub funded_at: u64,
pub status: InvestmentStatus,
pub insurance: Vec<InsuranceCoverage>,
}
/// Insurance coverage record
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InsuranceCoverage {
pub provider: Address,
pub coverage_percentage: u32,
pub coverage_amount: i128,
pub premium_amount: i128,
pub active: bool,
}
/// Platform fee configuration
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PlatformFeeConfig {
pub fee_bps: u32,
pub treasury_address: Option<Address>,
pub updated_at: u64,
pub updated_by: Address,
}
/// Search relevance rank for invoice search results
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum SearchRank {
Other,
PartialMatch,
ExactId,
}
/// A single search result entry
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SearchResult {
pub invoice_id: BytesN<32>,
pub rank: SearchRank,
pub created_at: u64,
}
/// Report returned by paginated admin rebuild helpers.
///
/// The rebuild is paginated and resumable. Each helper documents its completion
/// signal and what it counts as `reindexed`.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RebuildReport {
/// Number of invoice IDs examined in this page.
pub scanned: u32,
/// Number of records repaired or rewritten by the rebuild helper.
pub reindexed: u32,
/// Offset to pass on the next call.
pub next_offset: u32,
}
/// Report returned by paginated admin prune helpers.
///
/// The prune is paginated and resumable. Pass `next_offset` as the `offset`
/// on the next call. The last page is reached when `next_offset` stops advancing.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PruneReport {
/// Number of invoice IDs examined in this page.
pub scanned: u32,
/// Number of invoices actually pruned (deleted).
pub pruned: u32,
/// Offset to pass on the next call.
pub next_offset: u32,
}
/// Typed reason for freezing a business entity or its invoices.
///
/// Stored alongside the freeze state to provide an audit trail and enable
/// targeted unfreeze logic. An admin must supply one of these variants
/// when freezing; a bare boolean is no longer sufficient.
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BusinessFreezeReason {
/// Suspected fraudulent invoice submission or business identity.
FraudSuspected,
/// Business failed or failed ongoing KYC/AML compliance checks.
ComplianceViolation,
/// Active or resolved dispute requiring the business to be frozen
/// until resolution.
Dispute,
/// Business requested a voluntary freeze (e.g., for internal audit).
Voluntary,
/// Admin-initiated freeze for an unspecified or catch-all reason.
AdminAction,
}
impl BusinessFreezeReason {
/// Returns a short human-readable label for event logging.
pub fn label(&self) -> &'static str {
match self {
Self::FraudSuspected => "fraud_suspected",
Self::ComplianceViolation => "compliance_violation",
Self::Dispute => "dispute",
Self::Voluntary => "voluntary",
Self::AdminAction => "admin_action",
}
}
}
/// Typed reason for freezing an investor account.
///
/// Symmetric with [`BusinessFreezeReason`] — every freeze must carry a
/// typed reason so that audit logs and unfreeze workflows can operate on
/// structured data rather than opaque booleans.
#[contracttype]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InvestorFreezeReason {
/// Investor engaged in suspicious or fraudulent bid/investment activity.
FraudSuspected,
/// Investor failed or failed ongoing KYC/AML compliance checks.
ComplianceViolation,
/// Active dispute involving the investor's positions.
Dispute,
/// Investor requested a voluntary freeze.
Voluntary,
/// Admin-initiated freeze for an unspecified or catch-all reason.
AdminAction,
}
impl InvestorFreezeReason {
/// Returns a short human-readable label for event logging.
pub fn label(&self) -> &'static str {
match self {
Self::FraudSuspected => "fraud_suspected",
Self::ComplianceViolation => "compliance_violation",
Self::Dispute => "dispute",
Self::Voluntary => "voluntary",
Self::AdminAction => "admin_action",
}
}
}