forked from QuickLendX/quicklendx-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_test_file35.sh
More file actions
478 lines (408 loc) · 18.6 KB
/
Copy pathfix_test_file35.sh
File metadata and controls
478 lines (408 loc) · 18.6 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
#!/bin/bash
cat << 'INNER_EOF' > quicklendx-contracts/src/test_platform_metrics_reconciliation.rs
//! Reconciliation tests for PlatformMetrics.
//!
//! These tests independently recompute platform aggregates from the
//! underlying invoice and investment records and assert equality with
//! AnalyticsCalculator::calculate_platform_metrics.
//!
//! Invariant:
//! Derived analytics must equal independently reconstructed state.
//!
//! Rounding rules are intentionally mirrored from analytics.rs so that
//! aggregate drift, counting bugs, or denominator regressions fail loudly.
#![cfg(test)]
extern crate alloc;
use crate::analytics::{AnalyticsCalculator, PlatformMetrics};
use crate::contract::{QuickLendXContract, QuickLendXContractClient};
use crate::investment::InvestmentStorage;
use crate::storage::InvoiceStorage;
use crate::types::{Investment, InvestmentStatus, InvoiceCategory, InvoiceStatus};
use soroban_sdk::{
testutils::{Address as _, Ledger, MockAuth, MockAuthInvoke},
Address, Env, String, Vec, BytesN, IntoVal
};
// --- helpers ----------------------------------------------------------------
fn setup(env: &Env) -> (QuickLendXContractClient<'_>, Address, Address, Address) {
let contract_id = env.register(QuickLendXContract, ());
let client = QuickLendXContractClient::new(env, &contract_id);
let admin = Address::generate(env);
let business = Address::generate(env);
// Proper initialization with explicit auth per user instructions
let currency_whitelist = Address::generate(env);
let fees_treasury = Address::generate(env);
env.mock_auths(&[
MockAuth {
address: &admin,
invoke: &MockAuthInvoke {
contract: &contract_id,
fn_name: "init",
args: (&admin, ¤cy_whitelist, &fees_treasury).into_val(env),
sub_invokes: &[],
},
},
]);
client.init(&admin, ¤cy_whitelist, &fees_treasury);
// Note: mock_auths resets after the call, so we don't have mock_all_auths lingering.
(client, contract_id, admin, business)
}
fn call_update_invoice_status(
env: &Env,
client: &QuickLendXContractClient<'_>,
contract_id: &Address,
admin: &Address,
invoice_id: &BytesN<32>,
status: InvoiceStatus,
) {
env.mock_auths(&[
MockAuth {
address: admin,
invoke: &MockAuthInvoke {
contract: contract_id,
fn_name: "update_invoice_status",
args: (invoice_id.clone(), status.clone()).into_val(env),
sub_invokes: &[],
},
},
]);
client.update_invoice_status(invoice_id, &status);
}
fn upload(
env: &Env,
client: &QuickLendXContractClient,
business: &Address,
amount: i128,
desc: &str,
) -> BytesN<32> {
let currency = Address::generate(env);
let due_date = env.ledger().timestamp() + 86_400;
env.mock_all_auths(); // store_invoice requires business auth
let res = client.store_invoice(
business,
&amount,
¤cy,
&due_date,
&String::from_str(env, desc),
&InvoiceCategory::Services,
&Vec::new(env),
);
env.mock_auths(&[]); // clear mock auths
res
}
fn store_investment(
env: &Env,
contract_id: &Address,
invoice_id: &BytesN<32>,
investor: &Address,
amount: i128,
status: InvestmentStatus,
) -> BytesN<32> {
env.as_contract(contract_id, || {
let investment_id = InvestmentStorage::generate_unique_investment_id(env);
let investment = Investment {
investment_id: investment_id.clone(),
invoice_id: invoice_id.clone(),
investor: investor.clone(),
amount,
funded_at: env.ledger().timestamp(),
status,
insurance: Vec::new(env),
};
InvestmentStorage::store_investment(env, &investment);
investment_id
})
}
// --- oracle calculator ------------------------------------------------------
struct IndependentMetrics {
total_invoices: u32,
total_investments: u32,
total_volume: i128,
average_invoice_amount: i128,
average_investment_amount: i128,
default_rate: i128,
success_rate: i128,
}
fn compute_independent_metrics(env: &Env, contract_id: &Address) -> IndependentMetrics {
env.as_contract(contract_id, || {
let mut all_invoices = alloc::vec::Vec::new();
let pending = InvoiceStorage::get_invoices_by_status(env, InvoiceStatus::Pending);
for id in pending.iter() {
if let Some(inv) = InvoiceStorage::get_invoice(env, &id) {
all_invoices.push(inv);
}
}
let verified = InvoiceStorage::get_invoices_by_status(env, InvoiceStatus::Verified);
for id in verified.iter() {
if let Some(inv) = InvoiceStorage::get_invoice(env, &id) {
all_invoices.push(inv);
}
}
let funded = InvoiceStorage::get_invoices_by_status(env, InvoiceStatus::Funded);
for id in funded.iter() {
if let Some(inv) = InvoiceStorage::get_invoice(env, &id) {
all_invoices.push(inv);
}
}
let paid = InvoiceStorage::get_invoices_by_status(env, InvoiceStatus::Paid);
for id in paid.iter() {
if let Some(inv) = InvoiceStorage::get_invoice(env, &id) {
all_invoices.push(inv);
}
}
let defaulted = InvoiceStorage::get_invoices_by_status(env, InvoiceStatus::Defaulted);
for id in defaulted.iter() {
if let Some(inv) = InvoiceStorage::get_invoice(env, &id) {
all_invoices.push(inv);
}
}
let total_invoices = all_invoices.len() as u32;
let expected_total_volume: i128 = all_invoices.iter().map(|i| i.amount).sum();
let total_investments = (funded.len() + paid.len() + defaulted.len()) as u32;
let expected_average_invoice_amount = if total_invoices > 0 {
// integer division truncates toward zero
expected_total_volume / (total_invoices as i128)
} else {
0
};
let mut expected_total_invested = 0i128;
for inv in all_invoices.iter() {
if inv.status == InvoiceStatus::Funded || inv.status == InvoiceStatus::Paid || inv.status == InvoiceStatus::Defaulted {
if let Some(investment) = InvestmentStorage::get_investment_by_invoice(env, &inv.id) {
expected_total_invested += investment.amount;
}
}
}
let expected_average_investment_amount = if total_investments > 0 {
// integer division truncates toward zero
expected_total_invested / (total_investments as i128)
} else {
0
};
// denominator is total_investments, scaled by 10000
let expected_default_rate = if total_investments > 0 {
((defaulted.len() as u32).saturating_mul(10_000)) / total_investments
} else {
0
} as i128;
let expected_success_rate = if total_investments > 0 {
((paid.len() as u32).saturating_mul(10_000)) / total_investments
} else {
0
} as i128;
IndependentMetrics {
total_invoices,
total_investments,
total_volume: expected_total_volume,
average_invoice_amount: expected_average_invoice_amount,
average_investment_amount: expected_average_investment_amount,
default_rate: expected_default_rate,
success_rate: expected_success_rate,
}
})
}
// --- tests ------------------------------------------------------------------
#[test]
fn test_platform_metrics_reconcile_with_independent_sum() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000u64);
let (client, contract_id, admin, business) = setup(&env);
let investor = Address::generate(&env);
// Create a variety of invoices to populate the fixture
// 1. Pending invoice
upload(&env, &client, &business, 1_000, "inv_pending");
// 2. Active (Funded) invoice
let inv_active = upload(&env, &client, &business, 2_500, "inv_active");
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_active, InvoiceStatus::Verified);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_active, InvoiceStatus::Funded);
store_investment(&env, &contract_id, &inv_active, &investor, 2_500, InvestmentStatus::Active);
// 3. Completed (Paid) invoice
let inv_completed = upload(&env, &client, &business, 3_333, "inv_completed");
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_completed, InvoiceStatus::Verified);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_completed, InvoiceStatus::Funded);
store_investment(&env, &contract_id, &inv_completed, &investor, 3_333, InvestmentStatus::Completed);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_completed, InvoiceStatus::Paid);
// 4. Defaulted invoice
let inv_defaulted = upload(&env, &client, &business, 7_777, "inv_defaulted");
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_defaulted, InvoiceStatus::Verified);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_defaulted, InvoiceStatus::Funded);
store_investment(&env, &contract_id, &inv_defaulted, &investor, 7_777, InvestmentStatus::Defaulted);
// Mark as defaulted involves grace period checks. Let's advance ledger to avoid OperationNotAllowed.
env.ledger().set_timestamp(env.ledger().timestamp() + 10_000_000u64);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_defaulted, InvoiceStatus::Defaulted);
// 5. Cancelled invoice (should not be included in these metrics)
let inv_cancelled = upload(&env, &client, &business, 5_000, "inv_cancelled");
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv_cancelled, InvoiceStatus::Cancelled);
// 6. Rejected invoice (doesn't exist in our enum, skip)
// Independently compute metrics
let expected = compute_independent_metrics(&env, &contract_id);
// Ask the contract to compute metrics
let metrics = env.as_contract(&contract_id, || {
AnalyticsCalculator::calculate_platform_metrics(&env).unwrap()
});
// Reconcile
assert_eq!(metrics.total_invoices, expected.total_invoices);
assert_eq!(metrics.total_investments, expected.total_investments);
assert_eq!(metrics.total_volume, expected.total_volume);
assert_eq!(metrics.average_invoice_amount, expected.average_invoice_amount);
assert_eq!(metrics.average_investment_amount, expected.average_investment_amount);
assert_eq!(metrics.default_rate, expected.default_rate);
assert_eq!(metrics.success_rate, expected.success_rate);
}
#[test]
fn test_default_rate_matches_fixture_ratio() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000u64);
let (client, contract_id, admin, business) = setup(&env);
let investor = Address::generate(&env);
// Create 3 funded invoices, default 1 of them. Expected rate: 3333 bps.
let inv1 = upload(&env, &client, &business, 1_000, "inv1");
let inv2 = upload(&env, &client, &business, 1_000, "inv2");
let inv3 = upload(&env, &client, &business, 1_000, "inv3");
for inv in [&inv1, &inv2, &inv3] {
call_update_invoice_status(&env, &client, &contract_id, &admin, inv, InvoiceStatus::Verified);
call_update_invoice_status(&env, &client, &contract_id, &admin, inv, InvoiceStatus::Funded);
store_investment(&env, &contract_id, inv, &investor, 1_000, InvestmentStatus::Active);
}
env.ledger().set_timestamp(env.ledger().timestamp() + 10_000_000u64);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv1, InvoiceStatus::Defaulted);
let metrics = env.as_contract(&contract_id, || {
AnalyticsCalculator::calculate_platform_metrics(&env).unwrap()
});
let expected = compute_independent_metrics(&env, &contract_id);
// default_rate uses integer truncation (scaled by 10_000)
// 1 / 3 * 10000 = 3333
assert_eq!(expected.default_rate, 3333);
assert_eq!(metrics.default_rate, expected.default_rate);
}
#[test]
fn test_success_rate_matches_fixture_ratio() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000u64);
let (client, contract_id, admin, business) = setup(&env);
let investor = Address::generate(&env);
// Create 3 funded invoices, pay 2 of them. Expected rate: 6666 bps.
let inv1 = upload(&env, &client, &business, 1_000, "inv1");
let inv2 = upload(&env, &client, &business, 1_000, "inv2");
let inv3 = upload(&env, &client, &business, 1_000, "inv3");
for inv in [&inv1, &inv2, &inv3] {
call_update_invoice_status(&env, &client, &contract_id, &admin, inv, InvoiceStatus::Verified);
call_update_invoice_status(&env, &client, &contract_id, &admin, inv, InvoiceStatus::Funded);
store_investment(&env, &contract_id, inv, &investor, 1_000, InvestmentStatus::Active);
}
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv1, InvoiceStatus::Paid);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv2, InvoiceStatus::Paid);
let metrics = env.as_contract(&contract_id, || {
AnalyticsCalculator::calculate_platform_metrics(&env).unwrap()
});
let expected = compute_independent_metrics(&env, &contract_id);
// success_rate is expressed in basis points (scaled by 10_000)
// 2 / 3 * 10000 = 6666
assert_eq!(expected.success_rate, 6666);
assert_eq!(metrics.success_rate, expected.success_rate);
}
#[test]
fn test_average_invoice_amount_rounding() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000u64);
let (client, contract_id, _admin, business) = setup(&env);
// Upload 3 invoices total sum = 1000
upload(&env, &client, &business, 333, "inv1");
upload(&env, &client, &business, 333, "inv2");
upload(&env, &client, &business, 334, "inv3");
let metrics = env.as_contract(&contract_id, || {
AnalyticsCalculator::calculate_platform_metrics(&env).unwrap()
});
let expected = compute_independent_metrics(&env, &contract_id);
// integer division truncates toward zero
// 1000 / 3 = 333
assert_eq!(expected.average_invoice_amount, 333);
assert_eq!(metrics.average_invoice_amount, expected.average_invoice_amount);
}
#[test]
fn test_average_investment_amount_rounding() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000u64);
let (client, contract_id, admin, business) = setup(&env);
let investor = Address::generate(&env);
// 1000 total invested across 6 investments
let amounts = [166, 166, 166, 167, 167, 168];
for (i, &amt) in amounts.iter().enumerate() {
let inv = upload(&env, &client, &business, amt, &alloc::format!("inv{}", i));
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv, InvoiceStatus::Verified);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv, InvoiceStatus::Funded);
store_investment(&env, &contract_id, &inv, &investor, amt, InvestmentStatus::Active);
}
let metrics = env.as_contract(&contract_id, || {
AnalyticsCalculator::calculate_platform_metrics(&env).unwrap()
});
let expected = compute_independent_metrics(&env, &contract_id);
// integer division truncates toward zero
// 1000 / 6 = 166
assert_eq!(expected.average_investment_amount, 166);
assert_eq!(metrics.average_investment_amount, expected.average_investment_amount);
}
#[test]
fn test_empty_platform_metrics_are_zero() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000u64);
let (_, contract_id, _, _) = setup(&env);
let metrics = env.as_contract(&contract_id, || {
AnalyticsCalculator::calculate_platform_metrics(&env).unwrap()
});
assert_eq!(metrics.total_invoices, 0);
assert_eq!(metrics.total_volume, 0);
assert_eq!(metrics.default_rate, 0);
assert_eq!(metrics.success_rate, 0);
assert_eq!(metrics.average_invoice_amount, 0);
assert_eq!(metrics.average_investment_amount, 0);
}
#[test]
fn test_all_invoices_defaulted() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000u64);
let (client, contract_id, admin, business) = setup(&env);
let investor = Address::generate(&env);
let inv1 = upload(&env, &client, &business, 1_000, "inv1");
let inv2 = upload(&env, &client, &business, 2_000, "inv2");
for inv in [&inv1, &inv2] {
call_update_invoice_status(&env, &client, &contract_id, &admin, inv, InvoiceStatus::Verified);
call_update_invoice_status(&env, &client, &contract_id, &admin, inv, InvoiceStatus::Funded);
store_investment(&env, &contract_id, inv, &investor, 1_000, InvestmentStatus::Active);
}
env.ledger().set_timestamp(env.ledger().timestamp() + 10_000_000u64);
for inv in [&inv1, &inv2] {
call_update_invoice_status(&env, &client, &contract_id, &admin, inv, InvoiceStatus::Defaulted);
}
let metrics = env.as_contract(&contract_id, || {
AnalyticsCalculator::calculate_platform_metrics(&env).unwrap()
});
let expected = compute_independent_metrics(&env, &contract_id);
// default_rate == 10000 bps (100%)
assert_eq!(expected.default_rate, 10000);
assert_eq!(expected.success_rate, 0);
assert_eq!(metrics.default_rate, expected.default_rate);
assert_eq!(metrics.success_rate, expected.success_rate);
}
#[test]
fn test_single_invoice() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000u64);
let (client, contract_id, admin, business) = setup(&env);
let investor = Address::generate(&env);
let inv_amount = 5_432;
let inv = upload(&env, &client, &business, inv_amount, "inv1");
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv, InvoiceStatus::Verified);
call_update_invoice_status(&env, &client, &contract_id, &admin, &inv, InvoiceStatus::Funded);
store_investment(&env, &contract_id, &inv, &investor, inv_amount, InvestmentStatus::Active);
let metrics = env.as_contract(&contract_id, || {
AnalyticsCalculator::calculate_platform_metrics(&env).unwrap()
});
let expected = compute_independent_metrics(&env, &contract_id);
assert_eq!(expected.average_invoice_amount, inv_amount);
assert_eq!(expected.average_investment_amount, inv_amount);
assert_eq!(metrics.average_invoice_amount, expected.average_invoice_amount);
assert_eq!(metrics.average_investment_amount, expected.average_investment_amount);
}
INNER_EOF
chmod +x fix_test_file35.sh
./fix_test_file35.sh