forked from Liquifact/Liquifact-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettlement.rs
More file actions
2784 lines (2504 loc) · 87.1 KB
/
Copy pathsettlement.rs
File metadata and controls
2784 lines (2504 loc) · 87.1 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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Settlement and withdrawal tests for the LiquiFact escrow contract.
//!
//! Covers the full `withdraw` surface (happy path, wrong-status guards, legal-hold
//! block, idempotency, event emission, and terminal status assertion) as well as
//! the `settle` → `claim_investor_payout` flow, maturity gates, and dust-sweep
//! integration that belong in the same lifecycle module.
//!
//! # State model recap (ADR-001)
//! ```text
//! 0 (open) ──fund──▶ 1 (funded) ──settle──▶ 2 (settled)
//! └────withdraw───▶ 3 (withdrawn)
//! ```
//! `withdraw` and `settle` are mutually exclusive; both require `status == 1`.
//!
//! # Test organisation
//! Each test builds its own `Env` via the shared `setup` / `default_init` helpers
//! defined in `escrow/src/test.rs`. No cross-test state is shared.
#[cfg(test)]
use super::{
assert_contract_error, default_init, deploy, deploy_with_id, free_addresses,
install_stellar_asset_token, setup, StellarTestToken, MAX_DUST_SWEEP_AMOUNT, TARGET,
};
use crate::{
EscrowError, EscrowSettled, InvoiceEscrow, LiquifactEscrow, SettlementReadiness, YieldTier,
};
use soroban_sdk::{
testutils::{Address as _, Events, Ledger as _},
token::StellarAssetClient,
Address, Env, Event, String, Vec as SorobanVec,
};
// ──────────────────────────────────────────────────────────────────────────────
// Helpers
// ──────────────────────────────────────────────────────────────────────────────
/// Bring an escrow to `status == 1` (funded) by depositing exactly `TARGET`
/// from a single investor, then return the investor address.
fn fund_to_target(client: &super::LiquifactEscrowClient<'_>, env: &Env) -> Address {
let investor = Address::generate(env);
client.fund(&investor, &TARGET);
investor
}
fn setup_claim_env<'a>(
env: &'a Env,
invoice_id: &str,
target: i128,
yield_bps: i64,
) -> (
super::LiquifactEscrowClient<'a>,
StellarTestToken<'a>,
Address,
Address,
) {
env.mock_all_auths();
let token = install_stellar_asset_token(env);
let (contract_id, client) = deploy_with_id(env);
let admin = Address::generate(env);
let sme = Address::generate(env);
let treasury = Address::generate(env);
client.init(
&admin,
&String::from_str(env, invoice_id),
&sme,
&target,
&yield_bps,
&0u64,
&token.id,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
(client, token, contract_id, treasury)
}
/// Set up an escrow backed by a real Stellar asset contract (SAC), fund it to
/// target, and mint `TARGET` tokens into the escrow contract so `withdraw()` can
/// actually transfer them. Returns `(client, sme, sac_admin_client)`.
fn setup_funded_with_token<'a>(
env: &'a Env,
) -> (
super::LiquifactEscrowClient<'a>,
Address,
StellarAssetClient<'a>,
) {
env.mock_all_auths();
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 = super::LiquifactEscrowClient::new(env, &escrow_id);
let admin = Address::generate(env);
let sme = Address::generate(env);
let treasury = Address::generate(env);
client.init(
&admin,
&soroban_sdk::String::from_str(env, "INV_TOK"),
&sme,
&TARGET,
&800i64,
&0u64,
&token_id,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
// Mint tokens to investor so fund() can transfer them into the escrow.
let investor = Address::generate(env);
sac_admin.mint(&investor, &TARGET);
client.fund(&investor, &TARGET);
// The tokens are now in the escrow from the fund() transfer — no extra mint needed for withdraw().
(client, sme, sac_admin)
}
/// Bring an escrow to `status == 2` (settled) and return the investor address.
fn settle_escrow(client: &super::LiquifactEscrowClient<'_>, env: &Env) -> Address {
let investor = fund_to_target(client, env);
client.settle();
investor
}
// ──────────────────────────────────────────────────────────────────────────────
// `withdraw` — happy path
// ──────────────────────────────────────────────────────────────────────────────
/// Status must become 3 after a successful `withdraw`.
///
/// This is the primary assertion required by the task description.
#[test]
fn withdraw_sets_status_to_three() {
let env = Env::default();
env.mock_all_auths();
let (client, _sme, _sac) = setup_funded_with_token(&env);
client.withdraw();
let escrow = client.get_escrow();
assert_eq!(
escrow.status, 3u32,
"status must be 3 (withdrawn) after withdraw"
);
}
/// `withdraw` must require SME auth.
///
/// In `mock_all_auths` environments the check always passes; this test
/// documents the expected signer so a future auth-audit can grep for it.
#[test]
fn withdraw_requires_sme_auth() {
let env = Env::default();
env.mock_all_auths();
let (client, _sme, _sac) = setup_funded_with_token(&env);
// Passes because test env mocks all auth. The assertion is on the *call*
// succeeding for the correct signer (sme), not an impostor.
client.withdraw();
// Verify state changed — confirming it was sme who triggered the path.
assert_eq!(client.get_escrow().status, 3u32);
}
/// After `withdraw` the funded_amount and funding_target remain intact —
/// `withdraw` transitions state and transfers tokens, but does not zero accounting fields.
#[test]
fn withdraw_preserves_accounting_fields() {
let env = Env::default();
env.mock_all_auths();
let (client, _sme, _sac) = setup_funded_with_token(&env);
client.withdraw();
let escrow = client.get_escrow();
assert_eq!(
escrow.funded_amount, TARGET,
"funded_amount must not be wiped by withdraw"
);
assert_eq!(
escrow.funding_target, TARGET,
"funding_target must not be mutated by withdraw"
);
}
/// `withdraw` emits an `SmeWithdrew` event.
#[test]
fn withdraw_emits_event() {
let env = Env::default();
env.mock_all_auths();
let (client, _sme, _sac) = setup_funded_with_token(&env);
client.withdraw();
// At least one event must be emitted in the transaction.
let contract_events = env.events().all();
let events = contract_events.events();
assert!(
!events.is_empty(),
"withdraw must emit at least one contract event"
);
}
// ──────────────────────────────────────────────────────────────────────────────
// `withdraw` — wrong-status guards
// ──────────────────────────────────────────────────────────────────────────────
/// `withdraw` on an `open` (status 0) escrow must panic.
///
/// The escrow has not been funded; `withdraw` requires `status == 1`.
#[test]
#[should_panic]
fn withdraw_on_open_escrow_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
// No funding — status is still 0.
client.withdraw();
}
/// `withdraw` on an already-settled (status 2) escrow must panic.
///
/// Once `settle` has been called the escrow is terminal in the settlement path;
/// `withdraw` must not be able to re-label it.
#[test]
#[should_panic]
fn withdraw_on_settled_escrow_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
settle_escrow(&client, &env);
// status == 2 — withdraw must be rejected.
client.withdraw();
}
/// `withdraw` called twice on the same escrow must panic on the second call.
///
/// Once status reaches 3 (withdrawn) it is terminal; no forward transition
/// exists from 3, so a second `withdraw` must be rejected.
#[test]
#[should_panic]
fn withdraw_twice_panics() {
let env = Env::default();
env.mock_all_auths();
let (client, _sme, _sac) = setup_funded_with_token(&env);
client.withdraw(); // first call — succeeds, status → 3
client.withdraw(); // second call — must panic (status == 3, not 1)
}
/// `settle` cannot be called after `withdraw` (status 3 is terminal).
#[test]
#[should_panic]
fn settle_after_withdraw_panics() {
let env = Env::default();
env.mock_all_auths();
let (client, _sme, _sac) = setup_funded_with_token(&env);
client.withdraw(); // status → 3
client.settle(); // must panic — settle requires status == 1
}
/// `fund` cannot be called after `withdraw` (status 3 is terminal).
#[test]
#[should_panic]
fn fund_after_withdraw_panics() {
let env = Env::default();
env.mock_all_auths();
let (client, _sme, _sac) = setup_funded_with_token(&env);
client.withdraw(); // status → 3
let late_investor = Address::generate(&env);
client.fund(&late_investor, &10_000_000_000_i128); // must panic — fund requires status == 0
}
// ──────────────────────────────────────────────────────────────────────────────
// `withdraw` — legal-hold block (ADR-004)
// ──────────────────────────────────────────────────────────────────────────────
/// `withdraw` must be blocked while a legal hold is active.
///
/// Per ADR-004 the hold freezes `withdraw` regardless of escrow status.
#[test]
#[should_panic]
fn withdraw_blocked_by_legal_hold() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
fund_to_target(&client, &env);
client.set_legal_hold(&true);
// Status is 1 but hold is active — must panic.
client.withdraw();
}
/// `withdraw` must succeed after a legal hold is cleared.
///
/// Verifies that `clear_legal_hold` (or `set_legal_hold(false)`) fully lifts
/// the block and the escrow can proceed to `status == 3`.
#[test]
fn withdraw_succeeds_after_hold_cleared() {
let env = Env::default();
env.mock_all_auths();
let (client, _sme, _sac) = setup_funded_with_token(&env);
client.set_legal_hold(&true);
client.set_legal_hold(&false);
client.withdraw();
assert_eq!(client.get_escrow().status, 3u32);
}
// ──────────────────────────────────────────────────────────────────────────────
// Investor claim idempotency and per-investor isolation
// ──────────────────────────────────────────────────────────────────────────────
#[test]
fn test_claim_investor_twice_is_idempotent() {
let env = Env::default();
let (client, token, contract_id, _treasury) =
setup_claim_env(&env, "IDEMP001", 1_000i128, 400i64);
let investor = Address::generate(&env);
token.stellar.mint(&investor, &1_000i128);
client.fund(&investor, &1_000i128);
token.stellar.mint(&contract_id, &40i128); // extra yield portion only
client.settle();
// First claim - should succeed and set the claimed marker
client.claim_investor_payout(&investor);
assert!(client.is_investor_claimed(&investor));
// Second claim - should be idempotent (no-op, does not panic)
client.claim_investor_payout(&investor);
assert!(client.is_investor_claimed(&investor));
}
#[test]
#[should_panic]
fn test_claim_by_non_investor_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
let stranger = Address::generate(&env);
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "STR001"),
&sme,
&1_000i128,
&400i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
// Escrow settled but stranger never funded
let investor = Address::generate(&env);
client.fund(&investor, &1_000i128);
client.settle();
client.claim_investor_payout(&stranger);
}
#[test]
fn test_clashing_investors_have_independent_claims() {
let env = Env::default();
let (client, token, contract_id, _treasury) =
setup_claim_env(&env, "CLASH001", 2_000i128, 400i64);
let inv_a = Address::generate(&env);
let inv_b = Address::generate(&env);
token.stellar.mint(&inv_a, &1_000i128);
token.stellar.mint(&inv_b, &1_000i128);
client.fund(&inv_a, &1_000i128);
client.fund(&inv_b, &1_000i128);
token.stellar.mint(&contract_id, &80i128); // extra yield
client.settle();
client.claim_investor_payout(&inv_a);
assert!(client.is_investor_claimed(&inv_a));
assert!(!client.is_investor_claimed(&inv_b));
client.claim_investor_payout(&inv_b);
assert!(client.is_investor_claimed(&inv_b));
}
/// `set_legal_hold` must be admin-only; a non-admin cannot place a hold.
#[test]
#[should_panic]
fn legal_hold_set_by_non_admin_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
env.mock_all_auths_allowing_non_root_auth(); // stricter auth mode
env.mock_auths(&[]);
default_init(&client, &env, &admin, &sme);
// `sme` is not the admin — must panic.
client.set_legal_hold(&true);
}
// ──────────────────────────────────────────────────────────────────────────────
// `settle` path — complementary coverage ensuring mutual exclusivity
// ──────────────────────────────────────────────────────────────────────────────
/// `settle` transitions status from 1 to 2.
#[test]
fn settle_sets_status_to_two() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
fund_to_target(&client, &env);
client.settle();
assert_eq!(client.get_escrow().status, 2u32);
}
/// `settle` is blocked while a legal hold is active.
#[test]
#[should_panic]
fn settle_blocked_by_legal_hold() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
fund_to_target(&client, &env);
client.set_legal_hold(&true);
client.settle();
}
#[test]
#[should_panic]
fn test_claim_blocked_until_commitment_ledger_time() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let inv = Address::generate(&env);
let (tok, treasury) = free_addresses(&env);
client.init(
&admin,
&String::from_str(&env, "LOCK001"),
&sme,
&1_000i128,
&400i64,
&0u64,
&tok,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
client.fund_with_commitment(&inv, &1_000i128, &500u64);
client.settle();
client.claim_investor_payout(&inv);
}
#[test]
fn test_claim_succeeds_after_commitment_and_settle() {
let env = Env::default();
let (client, token, contract_id, _treasury) =
setup_claim_env(&env, "COMMIT1", 1_000i128, 400i64);
let inv = Address::generate(&env);
token.stellar.mint(&inv, &1_000i128);
client.fund_with_commitment(&inv, &1_000i128, &100u64);
token.stellar.mint(&contract_id, &40i128); // extra yield
client.settle();
env.ledger().set_timestamp(150);
client.claim_investor_payout(&inv);
assert!(client.is_investor_claimed(&inv));
}
#[test]
fn test_claim_gating_exact_timestamp() {
let env = Env::default();
let (client, token, contract_id, _treasury) = setup_claim_env(&env, "GATE1", 1_000i128, 400i64);
let inv = Address::generate(&env);
env.ledger().set_timestamp(1000);
let lock_duration = 500u64;
token.stellar.mint(&inv, &1_000i128);
client.fund_with_commitment(&inv, &1_000i128, &lock_duration);
token.stellar.mint(&contract_id, &40i128); // extra yield
client.settle();
let expiry = 1000 + lock_duration;
// 1 second before expiry
env.ledger().set_timestamp(expiry - 1);
let err = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
client.claim_investor_payout(&inv);
}));
assert!(err.is_err(), "Claim should be blocked 1s before expiry");
// Exact expiry
env.ledger().set_timestamp(expiry);
client.claim_investor_payout(&inv);
assert!(client.is_investor_claimed(&inv));
}
#[test]
fn test_claim_gating_with_multiple_investors() {
let env = Env::default();
let (client, token, contract_id, _treasury) = setup_claim_env(&env, "GATE2", 2_000i128, 400i64);
let inv1 = Address::generate(&env);
let inv2 = Address::generate(&env);
env.ledger().set_timestamp(1000);
token.stellar.mint(&inv1, &1_000i128);
token.stellar.mint(&inv2, &1_000i128);
client.fund_with_commitment(&inv1, &1_000i128, &100u64); // Expiry 1100
client.fund_with_commitment(&inv2, &1_000i128, &200u64); // Expiry 1200
token.stellar.mint(&contract_id, &80i128); // extra yield
client.settle();
env.ledger().set_timestamp(1150);
// inv1 can claim
client.claim_investor_payout(&inv1);
assert!(client.is_investor_claimed(&inv1));
// inv2 still blocked
let err = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
client.claim_investor_payout(&inv2);
}));
assert!(err.is_err(), "inv2 should still be blocked at 1150");
env.ledger().set_timestamp(1200);
client.claim_investor_payout(&inv2);
assert!(client.is_investor_claimed(&inv2));
}
/// Cost baseline: settle after funding.
#[test]
fn test_cost_baseline_settle() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
let investor = Address::generate(&env);
client.init(
&admin,
&String::from_str(&env, "INV103b"),
&sme,
&TARGET,
&800i64,
&50_000u64, // maturity after setup's default timestamp of 12345
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
client.fund(&investor, &TARGET);
env.ledger().set_timestamp(50_001);
let settled = client.settle();
assert_eq!(settled.status, 2);
}
/// `settle` called twice must panic on the second call.
#[test]
#[should_panic]
fn settle_twice_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
fund_to_target(&client, &env);
client.settle();
client.settle(); // status == 2, must panic
}
// ──────────────────────────────────────────────────────────────────────────────
// Maturity gate — settle is time-gated when `maturity > 0`; bypass when 0
// ──────────────────────────────────────────────────────────────────────────────
/// `settle` succeeds immediately when `maturity == 0` regardless of ledger time.
#[test]
fn settle_with_maturity_zero_succeeds_immediately() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let (token, treasury) = free_addresses(&env);
client.init(
&admin,
&String::from_str(&env, "INV_MAT_001"),
&sme,
&TARGET,
&800i64,
&0u64,
&token,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
assert!(
!client.has_maturity_lock(),
"maturity == 0 must be surfaced as no maturity lock"
);
assert!(!client.get_escrow_summary().has_maturity_lock);
fund_to_target(&client, &env);
env.ledger().with_mut(|l| l.timestamp = 1);
let settled = client.settle();
assert_eq!(settled.status, 2);
assert_eq!(settled.maturity, 0);
}
/// `settle` with `maturity > 0` must trap one second before the configured
/// validator-observed ledger timestamp and must not mutate the funded state.
#[test]
fn settle_one_second_before_maturity_traps_and_preserves_state() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let (token, treasury) = free_addresses(&env);
let maturity: u64 = 20_000;
client.init(
&admin,
&String::from_str(&env, "INV_MAT_003"),
&sme,
&TARGET,
&800i64,
&maturity,
&token,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
fund_to_target(&client, &env);
let snapshot_before = client.get_funding_close_snapshot();
env.ledger().with_mut(|l| l.timestamp = maturity - 1);
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
client.settle();
}));
assert!(
result.is_err(),
"settle must trap before the inclusive maturity boundary"
);
assert_eq!(
client.get_escrow().status,
1,
"pre-maturity settlement attempt must leave escrow funded"
);
assert_eq!(
client.get_funding_close_snapshot(),
snapshot_before,
"pre-maturity settlement attempt must not mutate snapshot state"
);
}
/// `settle` with `maturity > 0` succeeds at exactly the maturity timestamp.
#[test]
fn settle_at_maturity_succeeds() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let (token, treasury) = free_addresses(&env);
let maturity: u64 = 20_000;
client.init(
&admin,
&String::from_str(&env, "INV_MAT_002"),
&sme,
&TARGET,
&800i64,
&maturity,
&token,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
assert!(
client.has_maturity_lock(),
"positive maturity must be surfaced as an active maturity lock"
);
assert!(client.get_escrow_summary().has_maturity_lock);
fund_to_target(&client, &env);
env.ledger().with_mut(|l| l.timestamp = maturity);
let settled = client.settle();
assert_eq!(settled.status, 2);
assert_eq!(settled.maturity, maturity);
}
/// `settle` must panic if SME auth is not provided.
#[test]
#[should_panic]
fn settle_requires_sme_auth() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
fund_to_target(&client, &env);
env.mock_auths(&[]); // clear mocks — auth will fail
client.settle();
}
/// `settle` on open (status 0) escrow must panic.
#[test]
#[should_panic]
fn settle_on_open_escrow_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
// No funding — status is still 0.
client.settle();
}
/// `settle` on withdrawn (status 3) escrow must panic.
#[test]
#[should_panic]
fn settle_on_withdrawn_escrow_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
fund_to_target(&client, &env);
client.withdraw(); // status → 3
client.settle();
}
/// `sweep_terminal_dust` must reject open/funded escrows before terminal state.
// HostError wraps contract panic; expected substring not matched in outer message.
#[ignore = "HostError wraps contract panic; expected substring not matched"]
#[test]
#[should_panic(expected = "dust sweep only in terminal states (settled, withdrawn, or cancelled)")]
fn sweep_terminal_dust_before_terminal_state_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
let investor = settle_escrow(&client, &env);
client.claim_investor_payout(&investor);
let contract_events = env.events().all();
let events = contract_events.events();
assert!(
!events.is_empty(),
"claim must emit InvestorPayoutClaimed event"
);
}
/// `claim_investor_payout` must be blocked while a legal hold is active.
#[test]
#[should_panic]
fn claim_investor_payout_blocked_by_legal_hold() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
let investor = settle_escrow(&client, &env);
client.set_legal_hold(&true);
client.claim_investor_payout(&investor); // must panic
}
/// `claim_investor_payout` must fail before `settle` (status != 2).
#[test]
#[should_panic]
fn claim_investor_payout_before_settle_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
default_init(&client, &env, &admin, &sme);
let investor = fund_to_target(&client, &env);
client.claim_investor_payout(&investor);
}
/// An investor that did not participate cannot claim.
#[test]
#[should_panic]
fn claim_investor_payout_non_participant_panics() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
env.mock_all_auths_allowing_non_root_auth();
env.mock_auths(&[]);
default_init(&client, &env, &admin, &sme);
settle_escrow(&client, &env);
let stranger = Address::generate(&env);
client.claim_investor_payout(&stranger);
}
// ──────────────────────────────────────────────────────────────────────────────
// Terminal dust sweep
// ──────────────────────────────────────────────────────────────────────────────
// Uses `token.stellar`/`escrow_id` not in scope (deploy not deploy_with_id).
#[cfg(any())]
#[test]
fn test_sweep_terminal_dust_after_settle_transfers_to_treasury() {
let env = Env::default();
env.mock_all_auths();
let token = install_stellar_asset_token(&env);
let (contract_id, client) = deploy_with_id(&env);
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let (_tok, treasury) = free_addresses(&env);
let maturity = 5000u64;
client.init(
&admin,
&String::from_str(&env, "SW001"),
&sme,
&TARGET,
&100i64,
&maturity,
&token.id,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
let investor = Address::generate(&env);
client.fund(&investor, &1_000i128);
client.settle();
token.stellar.mint(&contract_id, &5_000i128);
let before_t = token.token.balance(&treasury);
let swept = client.sweep_terminal_dust(&5_000i128);
assert_eq!(swept, 5_000i128);
assert_eq!(token.token.balance(&tre), before_t + 5_000i128);
}
// Uses `token.stellar`/`escrow_id` not in scope.
#[cfg(any())]
#[test]
fn test_sweep_terminal_dust_after_withdraw_and_ledger_tick() {
let env = Env::default();
env.mock_all_auths();
let token = install_stellar_asset_token(&env);
let (contract_id, client) = deploy_with_id(&env);
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let (_tok, treasury) = free_addresses(&env);
let maturity = 5000u64;
client.init(
&admin,
&String::from_str(&env, "SW002"),
&sme,
&TARGET,
&100i64,
&maturity,
&token.id,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
let investor = Address::generate(&env);
client.fund(&investor, &1_000i128);
client.withdraw();
env.ledger()
.set_sequence_number(env.ledger().sequence() + 10);
token.stellar.mint(&contract_id, &333i128);
let swept = client.sweep_terminal_dust(&333i128);
assert_eq!(swept, 333i128);
}
// HostError wraps contract panic; expected substring not matched.
#[ignore = "HostError wraps contract panic; expected substring not matched"]
#[test]
#[should_panic]
fn test_sweep_rejected_when_open() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
let investor = Address::generate(&env);
client.init(
&admin,
&String::from_str(&env, "SW003"),
&sme,
&TARGET,
&100i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
client.fund(&investor, &1_000i128);
client.settle();
client.claim_investor_payout(&investor);
assert!(client.is_investor_claimed(&investor));
}
#[test]
#[should_panic]
fn test_sweep_blocked_under_legal_hold() {
let env = Env::default();
let (client, admin, sme) = setup(&env);
let investor = Address::generate(&env);
client.init(
&admin,
&String::from_str(&env, "SW004"),
&sme,
&1_000i128,
&100i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),