allsource-core 0.19.1

High-performance event store core built in Rust
Documentation
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use crate::{
    domain::value_objects::{ArticleId, CreatorId, Money, TenantId, TransactionId, WalletAddress},
    error::Result,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Blockchain network for the transaction
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Blockchain {
    /// Solana mainnet
    #[default]
    Solana,
    /// Base (Coinbase L2)
    Base,
    /// Polygon
    Polygon,
}

impl std::fmt::Display for Blockchain {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Blockchain::Solana => write!(f, "solana"),
            Blockchain::Base => write!(f, "base"),
            Blockchain::Polygon => write!(f, "polygon"),
        }
    }
}

/// Transaction status
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransactionStatus {
    /// Transaction is pending verification
    #[default]
    Pending,
    /// Transaction has been verified on-chain
    Confirmed,
    /// Transaction failed verification
    Failed,
    /// Transaction has been refunded
    Refunded,
    /// Transaction is disputed
    Disputed,
}

/// Domain Entity: Transaction
///
/// Represents a payment transaction for article access in the paywall system.
/// Transactions are immutable once confirmed - any changes create new events.
///
/// Domain Rules:
/// - Transaction must have a valid blockchain signature
/// - Amount must be positive and match article price
/// - Reader wallet must be valid
/// - Only pending transactions can be confirmed or failed
/// - Only confirmed transactions can be refunded or disputed
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transaction {
    id: TransactionId,
    tenant_id: TenantId,
    article_id: ArticleId,
    creator_id: CreatorId,
    reader_wallet: WalletAddress,
    amount: Money,
    platform_fee: Money,
    creator_amount: Money,
    blockchain: Blockchain,
    tx_signature: String,
    status: TransactionStatus,
    created_at: DateTime<Utc>,
    confirmed_at: Option<DateTime<Utc>>,
    refunded_at: Option<DateTime<Utc>>,
    metadata: serde_json::Value,
}

impl Transaction {
    /// Create a new pending transaction
    pub fn new(
        tenant_id: TenantId,
        article_id: ArticleId,
        creator_id: CreatorId,
        reader_wallet: WalletAddress,
        amount: Money,
        platform_fee_percentage: u64,
        blockchain: Blockchain,
        tx_signature: String,
    ) -> Result<Self> {
        Self::validate_amount(&amount)?;
        Self::validate_signature(&tx_signature)?;

        let platform_fee = amount.percentage(platform_fee_percentage);
        let creator_amount = (amount - platform_fee)?;

        Ok(Self {
            id: TransactionId::new(),
            tenant_id,
            article_id,
            creator_id,
            reader_wallet,
            amount,
            platform_fee,
            creator_amount,
            blockchain,
            tx_signature,
            status: TransactionStatus::Pending,
            created_at: Utc::now(),
            confirmed_at: None,
            refunded_at: None,
            metadata: serde_json::json!({}),
        })
    }

    /// Reconstruct transaction from storage (bypasses validation)
    #[allow(clippy::too_many_arguments)]
    pub fn reconstruct(
        id: TransactionId,
        tenant_id: TenantId,
        article_id: ArticleId,
        creator_id: CreatorId,
        reader_wallet: WalletAddress,
        amount: Money,
        platform_fee: Money,
        creator_amount: Money,
        blockchain: Blockchain,
        tx_signature: String,
        status: TransactionStatus,
        created_at: DateTime<Utc>,
        confirmed_at: Option<DateTime<Utc>>,
        refunded_at: Option<DateTime<Utc>>,
        metadata: serde_json::Value,
    ) -> Self {
        Self {
            id,
            tenant_id,
            article_id,
            creator_id,
            reader_wallet,
            amount,
            platform_fee,
            creator_amount,
            blockchain,
            tx_signature,
            status,
            created_at,
            confirmed_at,
            refunded_at,
            metadata,
        }
    }

    // Getters

    pub fn id(&self) -> &TransactionId {
        &self.id
    }

    pub fn tenant_id(&self) -> &TenantId {
        &self.tenant_id
    }

    pub fn article_id(&self) -> &ArticleId {
        &self.article_id
    }

    pub fn creator_id(&self) -> &CreatorId {
        &self.creator_id
    }

    pub fn reader_wallet(&self) -> &WalletAddress {
        &self.reader_wallet
    }

    pub fn amount(&self) -> &Money {
        &self.amount
    }

    pub fn amount_cents(&self) -> u64 {
        self.amount.amount()
    }

    pub fn platform_fee(&self) -> &Money {
        &self.platform_fee
    }

    pub fn platform_fee_cents(&self) -> u64 {
        self.platform_fee.amount()
    }

    pub fn creator_amount(&self) -> &Money {
        &self.creator_amount
    }

    pub fn creator_amount_cents(&self) -> u64 {
        self.creator_amount.amount()
    }

    pub fn blockchain(&self) -> Blockchain {
        self.blockchain
    }

    pub fn tx_signature(&self) -> &str {
        &self.tx_signature
    }

    pub fn status(&self) -> TransactionStatus {
        self.status
    }

    pub fn created_at(&self) -> DateTime<Utc> {
        self.created_at
    }

    pub fn confirmed_at(&self) -> Option<DateTime<Utc>> {
        self.confirmed_at
    }

    pub fn refunded_at(&self) -> Option<DateTime<Utc>> {
        self.refunded_at
    }

    pub fn metadata(&self) -> &serde_json::Value {
        &self.metadata
    }

    // Domain behavior methods

    /// Check if transaction is confirmed
    pub fn is_confirmed(&self) -> bool {
        self.status == TransactionStatus::Confirmed
    }

    /// Check if transaction is pending
    pub fn is_pending(&self) -> bool {
        self.status == TransactionStatus::Pending
    }

    /// Check if transaction can grant access (confirmed and not refunded)
    pub fn grants_access(&self) -> bool {
        self.status == TransactionStatus::Confirmed
    }

    /// Confirm the transaction (payment verified on-chain)
    pub fn confirm(&mut self) -> Result<()> {
        if self.status != TransactionStatus::Pending {
            return Err(crate::error::AllSourceError::ValidationError(format!(
                "Cannot confirm transaction with status {:?}",
                self.status
            )));
        }

        self.status = TransactionStatus::Confirmed;
        self.confirmed_at = Some(Utc::now());
        Ok(())
    }

    /// Mark transaction as failed
    pub fn fail(&mut self, reason: &str) -> Result<()> {
        if self.status != TransactionStatus::Pending {
            return Err(crate::error::AllSourceError::ValidationError(format!(
                "Cannot fail transaction with status {:?}",
                self.status
            )));
        }

        self.status = TransactionStatus::Failed;
        self.metadata["failure_reason"] = serde_json::json!(reason);
        Ok(())
    }

    /// Refund the transaction
    pub fn refund(&mut self, refund_tx_signature: &str) -> Result<()> {
        if self.status != TransactionStatus::Confirmed {
            return Err(crate::error::AllSourceError::ValidationError(
                "Can only refund confirmed transactions".to_string(),
            ));
        }

        self.status = TransactionStatus::Refunded;
        self.refunded_at = Some(Utc::now());
        self.metadata["refund_tx_signature"] = serde_json::json!(refund_tx_signature);
        Ok(())
    }

    /// Mark transaction as disputed
    pub fn dispute(&mut self, reason: &str) -> Result<()> {
        if self.status != TransactionStatus::Confirmed {
            return Err(crate::error::AllSourceError::ValidationError(
                "Can only dispute confirmed transactions".to_string(),
            ));
        }

        self.status = TransactionStatus::Disputed;
        self.metadata["dispute_reason"] = serde_json::json!(reason);
        self.metadata["disputed_at"] = serde_json::json!(Utc::now().to_rfc3339());
        Ok(())
    }

    /// Resolve a dispute (back to confirmed)
    pub fn resolve_dispute(&mut self, resolution: &str) -> Result<()> {
        if self.status != TransactionStatus::Disputed {
            return Err(crate::error::AllSourceError::ValidationError(
                "Can only resolve disputed transactions".to_string(),
            ));
        }

        self.status = TransactionStatus::Confirmed;
        self.metadata["dispute_resolution"] = serde_json::json!(resolution);
        self.metadata["resolved_at"] = serde_json::json!(Utc::now().to_rfc3339());
        Ok(())
    }

    /// Get explorer URL for the transaction
    pub fn explorer_url(&self) -> String {
        match self.blockchain {
            Blockchain::Solana => {
                format!("https://solscan.io/tx/{}", self.tx_signature)
            }
            Blockchain::Base => {
                format!("https://basescan.org/tx/{}", self.tx_signature)
            }
            Blockchain::Polygon => {
                format!("https://polygonscan.com/tx/{}", self.tx_signature)
            }
        }
    }

    // Validation

    fn validate_amount(amount: &Money) -> Result<()> {
        if amount.is_zero() {
            return Err(crate::error::AllSourceError::ValidationError(
                "Transaction amount must be positive".to_string(),
            ));
        }
        Ok(())
    }

    fn validate_signature(signature: &str) -> Result<()> {
        if signature.is_empty() {
            return Err(crate::error::AllSourceError::InvalidInput(
                "Transaction signature cannot be empty".to_string(),
            ));
        }

        // Solana signatures are 88 characters base58
        // Allow some flexibility for different blockchains
        if signature.len() < 40 || signature.len() > 128 {
            return Err(crate::error::AllSourceError::InvalidInput(format!(
                "Invalid transaction signature length: {}",
                signature.len()
            )));
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::value_objects::Currency;

    const VALID_WALLET: &str = "9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM";
    const VALID_SIGNATURE: &str =
        "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9g8eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9g";

    fn test_tenant_id() -> TenantId {
        TenantId::new("test-tenant".to_string()).unwrap()
    }

    fn test_article_id() -> ArticleId {
        ArticleId::new("test-article".to_string()).unwrap()
    }

    fn test_creator_id() -> CreatorId {
        CreatorId::new()
    }

    fn test_wallet() -> WalletAddress {
        WalletAddress::new(VALID_WALLET.to_string()).unwrap()
    }

    #[test]
    fn test_create_transaction() {
        let transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100), // $1.00
            7,                     // 7% fee
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        );

        assert!(transaction.is_ok());
        let transaction = transaction.unwrap();

        assert_eq!(transaction.amount_cents(), 100);
        assert_eq!(transaction.platform_fee_cents(), 7); // 7%
        assert_eq!(transaction.creator_amount_cents(), 93); // 100 - 7
        assert_eq!(transaction.status(), TransactionStatus::Pending);
        assert!(transaction.is_pending());
    }

    #[test]
    fn test_reject_zero_amount() {
        let result = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::zero(Currency::USD),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_reject_empty_signature() {
        let result = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            String::new(),
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_confirm_transaction() {
        let mut transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        assert!(transaction.is_pending());
        assert!(!transaction.grants_access());

        let result = transaction.confirm();
        assert!(result.is_ok());
        assert!(transaction.is_confirmed());
        assert!(transaction.grants_access());
        assert!(transaction.confirmed_at().is_some());
    }

    #[test]
    fn test_cannot_confirm_confirmed() {
        let mut transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        transaction.confirm().unwrap();
        let result = transaction.confirm();
        assert!(result.is_err());
    }

    #[test]
    fn test_fail_transaction() {
        let mut transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        let result = transaction.fail("Insufficient funds");
        assert!(result.is_ok());
        assert_eq!(transaction.status(), TransactionStatus::Failed);
    }

    #[test]
    fn test_refund_transaction() {
        let mut transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        // Must confirm first
        transaction.confirm().unwrap();

        let result =
            transaction.refund("refund_tx_sig_12345678901234567890123456789012345678901234");
        assert!(result.is_ok());
        assert_eq!(transaction.status(), TransactionStatus::Refunded);
        assert!(transaction.refunded_at().is_some());
        assert!(!transaction.grants_access());
    }

    #[test]
    fn test_cannot_refund_pending() {
        let mut transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        let result = transaction.refund("refund_sig");
        assert!(result.is_err());
    }

    #[test]
    fn test_dispute_transaction() {
        let mut transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        transaction.confirm().unwrap();
        let result = transaction.dispute("Content not delivered");
        assert!(result.is_ok());
        assert_eq!(transaction.status(), TransactionStatus::Disputed);
    }

    #[test]
    fn test_resolve_dispute() {
        let mut transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        transaction.confirm().unwrap();
        transaction.dispute("Content not delivered").unwrap();

        let result = transaction.resolve_dispute("Content was delivered, access restored");
        assert!(result.is_ok());
        assert_eq!(transaction.status(), TransactionStatus::Confirmed);
    }

    #[test]
    fn test_explorer_url() {
        let transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        let url = transaction.explorer_url();
        assert!(url.contains("solscan.io"));
        assert!(url.contains(VALID_SIGNATURE));
    }

    #[test]
    fn test_serde_serialization() {
        let transaction = Transaction::new(
            test_tenant_id(),
            test_article_id(),
            test_creator_id(),
            test_wallet(),
            Money::usd_cents(100),
            7,
            Blockchain::Solana,
            VALID_SIGNATURE.to_string(),
        )
        .unwrap();

        let json = serde_json::to_string(&transaction);
        assert!(json.is_ok());

        let deserialized: Transaction = serde_json::from_str(&json.unwrap()).unwrap();
        assert_eq!(deserialized.amount_cents(), 100);
    }
}