-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathbasic_workflow.rs
More file actions
917 lines (881 loc) · 29.5 KB
/
Copy pathbasic_workflow.rs
File metadata and controls
917 lines (881 loc) · 29.5 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
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
//! # karis-ky Escrow SDK Examples
//!
//! This file demonstrates common patterns for interacting with the karis-ky
//! LiquifactEscrow contract on the Stellar/Soroban network.
//!
//! ## Covered Patterns
//!
//! - **init**: Deploy and initialize an invoice escrow
//! - **fund**: Fund as an investor (simple funding)
//! - **fund_with_commitment**: Fund with a lock period for tiered yield
//! - **settle**: Settle the escrow as the SME
//! - **claim_investor_payout**: Claim payout as an investor after settlement
//! - **withdraw**: SME pulls funded liquidity
//!
//! ## Error Handling
//!
//! All entrypoints emit typed [`EscrowError`] codes (see docs/escrow-error-messages.md).
//! SDKs should branch on the numeric error code rather than parsing panic strings.
//!
//! ## Prerequisites
//!
//! ```bash
//! cargo build -p karis_ky_escrow
//! ```
//!
//! ---
use karis_ky_escrow::{
EscrowError, InvoiceEscrow, LiquifactEscrow, LiquifactEscrowClient, YieldTier,
};
// ═══════════════════════════════════════════════════════════════════════════════
// Real compilable example functions — run with:
// cargo test -p karis_ky_escrow -- --nocapture
// ═══════════════════════════════════════════════════════════════════════════════
/// Example: Initialize escrow, fund as investor, settle as SME, claim as investor.
/// This is the core lifecycle demonstrating all four required patterns.
#[cfg(test)]
#[test]
fn example_basic_workflow_init_fund_settle_claim() {
use soroban_sdk::{testutils::Address as _, Address, Env, String};
let env = Env::default();
env.mock_all_auths();
// Step 1: Deploy and initialize the escrow
let escrow_id = env.register(LiquifactEscrow, ());
let client = LiquifactEscrowClient::new(&env, &escrow_id);
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let funding_token = Address::generate(&env);
let treasury = Address::generate(&env);
let invoice_amount: i128 = 1_000_000_000;
let yield_bps: i64 = 500;
let escrow = client.init(
&admin,
&String::from_str(&env, "EX-001"),
&sme,
&invoice_amount,
&yield_bps,
&0u64,
&funding_token,
&None, &treasury, &None, &None, &None, &None,
&None, &None, &None, &None, &None, &None,
);
assert_eq!(escrow.status, 0);
// Step 2: Fund as investor
let funded = client.fund(&investor, &invoice_amount);
assert_eq!(funded.status, 1);
assert_eq!(client.get_contribution(&investor), invoice_amount);
// Step 3: Settle as SME
let settled = client.settle();
assert_eq!(settled.status, 2);
// Step 4: Claim as investor
client.claim_investor_payout(&investor);
assert!(client.is_investor_claimed(&investor));
// Idempotent: second claim is a no-op
client.claim_investor_payout(&investor);
assert!(client.is_investor_claimed(&investor));
}
/// Example: Initialize with yield-bearing token, fund, settle, claim.
#[cfg(test)]
#[test]
fn example_yield_token_workflow() {
use soroban_sdk::{testutils::Address as _, Address, Env, String};
let env = Env::default();
env.mock_all_auths();
let client = LiquifactEscrowClient::new(&env, &env.register(LiquifactEscrow, ()));
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let base_token = Address::generate(&env);
let yield_token = Address::generate(&env);
let treasury = Address::generate(&env);
let escrow = client.init(
&admin,
&String::from_str(&env, "YIELD-EX"),
&sme,
&1_000_000_000i128,
&400i64,
&0u64,
&base_token,
&None, &treasury, &None, &None, &None, &None,
&None, &None,
&Some(yield_token.clone()), // yield_token
&None, // oracle_contract
&None, // nft_contract
);
assert_eq!(client.get_yield_token(), Some(yield_token.clone()));
client.fund(&investor, &1_000_000_000i128);
client.settle();
client.claim_investor_payout(&investor);
assert!(client.is_investor_claimed(&investor));
}
/// Example: Initialize with NFT contract, settle to mint settlement NFT.
#[cfg(test)]
#[test]
fn example_nft_mint_workflow() {
use soroban_sdk::{testutils::Address as _, Address, Env, String};
let env = Env::default();
env.mock_all_auths();
let client = LiquifactEscrowClient::new(&env, &env.register(LiquifactEscrow, ()));
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let token = Address::generate(&env);
let treasury = Address::generate(&env);
let nft_contract = Address::generate(&env);
client.init(
&admin,
&String::from_str(&env, "NFT-EX"),
&sme,
&1_000_000_000i128,
&400i64,
&0u64,
&token,
&None, &treasury, &None, &None, &None, &None,
&None, &None,
&None, // yield_token
&None, // oracle_contract
&Some(nft_contract.clone()), // nft_contract
);
assert_eq!(client.get_nft_contract(), Some(nft_contract.clone()));
client.fund(&investor, &1_000_000_000i128);
let settled = client.settle();
assert_eq!(settled.status, 2);
client.claim_investor_payout(&investor);
}
/// # Example 1: Basic Escrow Lifecycle
///
/// This example demonstrates the complete happy path:
/// 1. Admin initializes an escrow for an invoice
/// 2. Investor funds the escrow to reach the target
/// 3. SME settles the escrow after maturity
/// 4. Investor claims their payout
///
/// ```rust
/// fn example_basic_workflow() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// // --- Step 1: Deploy and initialize the escrow ---
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let investor = Address::generate(&env);
/// let funding_token = Address::generate(&env);
/// let treasury = Address::generate(&env);
///
/// let invoice_amount: i128 = 1_000_000_000; // 1000 USDC (6 decimals)
/// let yield_bps: i64 = 500; // 5.00% annualized yield
/// let maturity: u64 = env.ledger().timestamp() + 30 * 24 * 60 * 60; // 30 days
///
/// let escrow = client.init(
/// &admin,
/// &String::from_str(&env, "INV-2024-001"),
/// &sme,
/// &invoice_amount,
/// &yield_bps,
/// &maturity,
/// &funding_token,
/// &None, // registry
/// &treasury,
/// &None, // yield_tiers
/// &None, // min_contribution
/// &None, // max_unique_investors
/// &None, // max_per_investor
/// &None, // legal_hold_clear_delay
/// &None, // funding_deadline
/// &None, // yield_slippage_threshold
/// &None, // yield_token
/// &None, // oracle_contract
/// &None, // nft_contract
/// );
///
/// assert_eq!(escrow.status, 0); // Open
/// assert_eq!(escrow.invoice_id, soroban_sdk::symbol_short!("INV-2024-001"));
///
/// // --- Step 2: Investor funds the escrow ---
/// let funded_event = client.fund(&investor, &invoice_amount);
/// assert_eq!(funded_event.status, 1); // Now funded
///
/// let contribution = client.get_contribution(&investor);
/// assert_eq!(contribution, invoice_amount);
///
/// // --- Step 3: Advance time past maturity and settle ---
/// env.ledger().with_mut(|l| l.timestamp = maturity + 1);
///
/// let settled = client.settle();
/// assert_eq!(settled.status, 2); // Settled
///
/// // --- Step 4: Investor claims payout ---
/// client.claim_investor_payout(&investor);
/// assert!(client.is_investor_claimed(&investor));
///
/// // Verify the second claim is idempotent (no-op)
/// client.claim_investor_payout(&investor);
/// assert!(client.is_investor_claimed(&investor));
/// }
/// ```
/// # Example 2: Escrow with Tiered Yield
///
/// This example shows how to configure tiered yield rates that reward
/// investors who commit to longer lock periods.
///
/// ```rust
/// fn example_tiered_yield() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String, Vec};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let investor_short = Address::generate(&env);
/// let investor_long = Address::generate(&env);
/// let token = Address::generate(&env);
/// let treasury = Address::generate(&env);
///
/// // Configure tiered yield:
/// // - Base: 4% (400 bps)
/// // - Tier 1: 5% for 30-day lock
/// // - Tier 2: 7% for 90-day lock
/// let yield_tiers = Vec::from_array(
/// &env,
/// [
/// YieldTier {
/// min_lock_secs: 30 * 24 * 60 * 60,
/// yield_bps: 500,
/// },
/// YieldTier {
/// min_lock_secs: 90 * 24 * 60 * 60,
/// yield_bps: 700,
/// },
/// ],
/// );
///
/// client.init(
/// &admin,
/// &String::from_str(&env, "INV-TIER-001"),
/// &sme,
/// &2_000_000_000i128,
/// &400i64, // base yield 4%
/// &0u64, // no maturity lock
/// &token,
/// &None,
/// &treasury,
/// &Some(yield_tiers),
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None, // yield_token
/// &None, // oracle_contract
/// &None, // nft_contract
/// );
///
/// // Investor with short lock (30 days) gets Tier 1 yield (5%)
/// client.fund_with_commitment(
/// &investor_short,
/// &1_000_000_000i128,
/// &(30 * 24 * 60 * 60u64),
/// );
///
/// // Investor with long lock (90 days) gets Tier 2 yield (7%)
/// client.fund_with_commitment(
/// &investor_long,
/// &1_000_000_000i128,
/// &(90 * 24 * 60 * 60u64),
/// );
///
/// client.settle();
///
/// // Claims respect individual lock periods
/// let elapsed: u64 = 30 * 24 * 60 * 60;
/// env.ledger().with_mut(|l| l.timestamp = elapsed);
///
/// // Short-lock investor can claim after 30 days
/// client.claim_investor_payout(&investor_short);
/// assert!(client.is_investor_claimed(&investor_short));
///
/// // Long-lock investor is still locked at 30 days
/// // claim_investor_payout would panic with InvestorCommitmentLockNotExpired
/// let still_locked = !client.is_investor_claimed(&investor_long);
/// assert!(still_locked);
///
/// // Advance to 90 days — now both can claim
/// env.ledger().with_mut(|l| l.timestamp = 90 * 24 * 60 * 60);
/// client.claim_investor_payout(&investor_long);
/// assert!(client.is_investor_claimed(&investor_long));
/// }
/// ```
/// # Example 3: SME Withdraws Liquidity
///
/// When the SME wants to pull funded liquidity directly (without settlement),
/// they call `withdraw()`.
///
/// ```rust
/// fn example_sme_withdraw() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
/// use soroban_sdk::token::StellarAssetClient;
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// // Use a real Stellar asset token for withdrawal testing
/// let sac = env.register_stellar_asset_contract_v2(Address::generate(&env));
/// let token_id = sac.address();
/// let sac_admin = StellarAssetClient::new(&env, &token_id);
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let investor = Address::generate(&env);
/// let treasury = Address::generate(&env);
///
/// let target: i128 = 500_000_000;
///
/// client.init(
/// &admin,
/// &String::from_str(&env, "INV-WD-001"),
/// &sme,
/// &target,
/// &300i64,
/// &0u64,
/// &token_id,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None, // yield_token
/// &None, // oracle_contract
/// &None, // nft_contract
/// );
///
/// // Fund the escrow
/// client.fund(&investor, &target);
///
/// // Mint tokens into escrow so withdraw can actually transfer
/// sac_admin.mint(&escrow_id, &target);
///
/// // SME withdraws — status goes to 3 (withdrawn)
/// let escrow = client.withdraw();
/// assert_eq!(escrow.status, 3);
///
/// // Verify tokens were transferred to SME
/// // (In production, check the token balance of sme_address)
/// }
/// ```
/// # Example 4: Multi-Investor Pro-Rata Payout Calculation
///
/// Off-chain pro-rata share calculation using the funding close snapshot.
///
/// ```rust
/// fn example_pro_rata_calculation() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let inv_a = Address::generate(&env);
/// let inv_b = Address::generate(&env);
/// let inv_c = Address::generate(&env);
/// let token = Address::generate(&env);
/// let treasury = Address::generate(&env);
///
/// let target: i128 = 3_000_000_000;
///
/// client.init(
/// &admin,
/// &String::from_str(&env, "PRO-RATA-01"),
/// &sme,
/// &target,
/// &500i64,
/// &0u64,
/// &token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None, // yield_token
/// &None, // oracle_contract
/// &None, // nft_contract
/// );
///
/// // Three investors contribute different amounts
/// client.fund(&inv_a, &1_500_000_000i128); // 50%
/// client.fund(&inv_b, &1_000_000_000i128); // 33.3%
/// client.fund(&inv_c, &500_000_000i128); // 16.7%
///
/// client.settle();
///
/// // Read the funding close snapshot for pro-rata denominator
/// let snapshot = client.get_funding_close_snapshot().unwrap();
/// let total_principal = snapshot.total_principal; // 3_000_000_000
/// let yield_bps = client.get_escrow().yield_bps;
///
/// // Off-chain pro-rata calculation:
/// // For investor A: share = 1_500_000_000 / 3_000_000_000 = 0.5 (50%)
/// // Payout = share * total_principal * (1 + yield_bps / 10000)
/// let total_pool: i128 =
/// total_principal + (total_principal * yield_bps as i128 / 10_000);
///
/// // Each investor's payout = contribution / total_principal * total_pool
/// let payout_a = (client.get_contribution(&inv_a) * total_pool) / total_principal;
/// let payout_b = (client.get_contribution(&inv_b) * total_pool) / total_principal;
/// let payout_c = (client.get_contribution(&inv_c) * total_pool) / total_principal;
///
/// // Sum of payouts must not exceed pool (within rounding)
/// let payout_sum = payout_a + payout_b + payout_c;
/// assert!(payout_sum <= total_pool + 2); // Allow ±2 for integer rounding
///
/// // Investors can now claim
/// client.claim_investor_payout(&inv_a);
/// client.claim_investor_payout(&inv_b);
/// client.claim_investor_payout(&inv_c);
/// }
/// ```
/// # Example 5: Escrow with Yield-Bearing Token (e.g., aUSDC)
///
/// When `yield_token` is configured, integrations should wrap base tokens
/// into yield-bearing tokens during funding and unwrap at settlement.
///
/// ```rust
/// fn example_yield_token_escrow() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let investor = Address::generate(&env);
/// let base_token = Address::generate(&env); // USDC
/// let yield_token = Address::generate(&env); // aUSDC (yield-bearing wrapper)
/// let treasury = Address::generate(&env);
///
/// // Init with yield-bearing token configuration
/// let escrow = client.init(
/// &admin,
/// &String::from_str(&env, "YIELD-001"),
/// &sme,
/// &1_000_000_000i128,
/// &400i64,
/// &0u64,
/// &base_token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &Some(yield_token.clone()), // yield_token
/// &None, // oracle_contract
/// &None, // nft_contract
/// );
///
/// // Verify the yield token is set
/// assert_eq!(client.get_yield_token(), Some(yield_token.clone()));
///
/// // Integration layer: wrap base token → yield token
/// // (e.g., deposit USDC → receive aUSDC)
/// // This is performed off-chain or via a separate contract call.
///
/// client.fund(&investor, &1_000_000_000i128);
/// client.settle();
///
/// // Integration layer: unwrap yield token → base token + yield
/// // (e.g., redeem aUSDC → USDC + accrued interest)
/// // The YieldUnwrapped event is emitted during settlement.
///
/// client.claim_investor_payout(&investor);
/// }
/// ```
/// # Example 6: Oracle-Verified Settlement
///
/// When `oracle_contract` is configured, settlement requires oracle-verified
/// invoice payment proof before finalizing.
///
/// ```rust
/// fn example_oracle_settlement() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let investor = Address::generate(&env);
/// let funding_token = Address::generate(&env);
/// let treasury = Address::generate(&env);
/// let oracle = Address::generate(&env); // Stellar price oracle contract
///
/// // Init with oracle contract for price-feed verification
/// client.init(
/// &admin,
/// &String::from_str(&env, "ORACLE-001"),
/// &sme,
/// &1_000_000_000i128,
/// &400i64,
/// &0u64,
/// &funding_token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None, // yield_token
/// &Some(oracle.clone()), // oracle_contract
/// &None, // nft_contract
/// );
///
/// // Verify the oracle is set
/// assert_eq!(client.get_oracle_contract(), Some(oracle.clone()));
///
/// client.fund(&investor, &1_000_000_000i128);
///
/// // Integration layer query pattern:
/// // 1. Query oracle for invoice payment verification
/// // 2. Confirm payment in real-world currency matches invoice
/// // 3. Settle the escrow (oracle verified)
///
/// client.settle();
///
/// // OracleSettlementVerified event is emitted during settlement.
/// client.claim_investor_payout(&investor);
/// }
/// ```
/// # Example 7: Settlement NFT Minting
///
/// When `nft_contract` is configured, settlement triggers NFT minting
/// representing the settled invoice. The SME can use this NFT as
/// collateral or proof of settlement.
///
/// ```rust
/// fn example_settlement_nft() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let investor = Address::generate(&env);
/// let funding_token = Address::generate(&env);
/// let treasury = Address::generate(&env);
/// let nft_contract = Address::generate(&env); // SEP-41 NFT contract
///
/// // Init with NFT contract for settlement NFT minting
/// client.init(
/// &admin,
/// &String::from_str(&env, "NFT-001"),
/// &sme,
/// &1_000_000_000i128,
/// &400i64,
/// &0u64,
/// &funding_token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None, // yield_token
/// &None, // oracle_contract
/// &Some(nft_contract.clone()), // nft_contract
/// );
///
/// // Verify the NFT contract is set
/// assert_eq!(client.get_nft_contract(), Some(nft_contract.clone()));
///
/// client.fund(&investor, &1_000_000_000i128);
/// client.settle();
///
/// // SettlementNftMinted event is emitted during settlement.
/// // The event includes: invoice_id, settlement_date, yield_paid_bps
/// // Third-party contracts can query the NFT metadata.
///
/// client.claim_investor_payout(&investor);
/// }
/// ```
/// # Example 8: Error Handling Patterns
///
/// Demonstrates how to handle typed errors when interacting with the escrow.
///
/// ```rust
/// fn example_error_handling() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let token = Address::generate(&env);
/// let treasury = Address::generate(&env);
///
/// // --- Error pattern: Attempt to settle before funding ---
/// client.init(
/// &admin,
/// &String::from_str(&env, "ERR-001"),
/// &sme,
/// &1_000_000_000i128,
/// &400i64,
/// &0u64,
/// &token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// );
///
/// // settle() on an open escrow panics with SettlementNotFunded (code 121)
/// // Use try_settle() to handle the error gracefully:
/// let result = client.try_settle();
/// assert!(result.is_err());
///
/// // --- Error pattern: Double initialization ---
/// // init() on an already-initialized escrow panics with EscrowAlreadyInitialized (code 3)
/// let reinit_result = client.try_init(
/// &admin,
/// &String::from_str(&env, "ERR-001-DUP"),
/// &sme,
/// &500_000_000i128,
/// &400i64,
/// &0u64,
/// &token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// );
/// assert!(reinit_result.is_err());
///
/// // --- Error pattern: Read before initialization ---
/// // get_escrow() panics with EscrowNotInitialized (code 20) on a fresh contract
/// let fresh_id = env.register(LiquifactEscrow, ());
/// let fresh_client = LiquifactEscrowClient::new(&env, &fresh_id);
/// let get_result = fresh_client.try_get_escrow();
/// assert!(get_result.is_err());
///
/// // --- Error pattern: Invoice ID validation ---
/// // Invalid invoice IDs (empty, too long, bad chars) panic with
/// // InvoiceIdInvalidLength (code 4) or InvoiceIdInvalidCharset (code 5)
/// let fresh_id2 = env.register(LiquifactEscrow, ());
/// let fresh_client2 = LiquifactEscrowClient::new(&env, &fresh_id2);
/// let bad_id_result = fresh_client2.try_init(
/// &admin,
/// &String::from_str(&env, ""), // empty — invalid
/// &sme,
/// &1_000_000_000i128,
/// &400i64,
/// &0u64,
/// &token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// );
/// assert!(bad_id_result.is_err());
/// }
/// ```
/// # Example 9: Escrow Summary Query
///
/// Demonstrates the `get_escrow_summary` entrypoint which bundles multiple
/// read-only values for off-chain indexers and client rendering.
///
/// ```rust
/// fn example_escrow_summary() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let investor = Address::generate(&env);
/// let token = Address::generate(&env);
/// let treasury = Address::generate(&env);
///
/// client.init(
/// &admin,
/// &String::from_str(&env, "SUM-001"),
/// &sme,
/// &2_000_000_000i128,
/// &600i64,
/// &0u64,
/// &token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// );
///
/// client.fund(&investor, &2_000_000_000i128);
/// client.settle();
///
/// // Get comprehensive escrow state in a single call
/// let summary = client.get_escrow_summary();
///
/// assert_eq!(summary.escrow.status, 2);
/// assert_eq!(summary.schema_version, 6);
/// assert!(!summary.legal_hold);
/// assert_eq!(summary.unique_funder_count, 1);
/// // summary.yield_token, summary.oracle_contract, summary.nft_contract
/// // are all None when not configured.
/// }
/// ```
/// # Example 10: Funding with Deadline
///
/// Demonstrates funding deadline enforcement — after the deadline,
/// new deposits are rejected.
///
/// ```rust
/// fn example_funding_deadline() {
/// use soroban_sdk::{testutils::Address as _, Address, Env, String};
///
/// let env = Env::default();
/// env.mock_all_auths();
///
/// let escrow_id = env.register(LiquifactEscrow, ());
/// let client = LiquifactEscrowClient::new(&env, &escrow_id);
///
/// let admin = Address::generate(&env);
/// let sme = Address::generate(&env);
/// let investor = Address::generate(&env);
/// let token = Address::generate(&env);
/// let treasury = Address::generate(&env);
///
/// let now = env.ledger().timestamp();
/// let deadline = now + 7 * 24 * 60 * 60; // 7 days from now
///
/// client.init(
/// &admin,
/// &String::from_str(&env, "DEADLINE-01"),
/// &sme,
/// &1_000_000_000i128,
/// &400i64,
/// &0u64,
/// &token,
/// &None,
/// &treasury,
/// &None,
/// &None,
/// &None,
/// &None,
/// &None,
/// &Some(deadline), // funding deadline
/// &None,
/// &None,
/// &None,
/// &None,
/// );
///
/// // Fund before deadline — succeeds
/// client.fund(&investor, &500_000_000i128);
///
/// // Advance past deadline
/// env.ledger().with_mut(|l| l.timestamp = deadline + 1);
///
/// // Fund after deadline — panics with FundingDeadlinePassed (code 164)
/// let late_investor = Address::generate(&env);
/// let late_result = client.try_fund(&late_investor, &500_000_000i128);
/// assert!(late_result.is_err());
/// }
/// ```
fn main() {
println!("karis-ky Escrow SDK Examples");
println!("===========================");
println!();
println!("This file contains example code demonstrating common patterns");
println!("for interacting with the LiquifactEscrow contract.");
println!();
println!("Run with: cargo test -p karis_ky_escrow");
println!();
println!("For detailed documentation, see:");
println!(" - docs/escrow-data-model.md");
println!(" - docs/escrow-events.md");
println!(" - docs/escrow-security-checklist.md");
println!(" - README.md");
}