Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
imports
coverage.lcov
coverage.json

# fuzzy testing archives
archives/
128 changes: 128 additions & 0 deletions FUZZY_TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
### Fuzzy Testing Framework

This document explains how to generate scenario CSVs, build Cadence tests, run them, and produce a precision drift report. It also summarizes the current state and precision expectations.

### What it does

- Generates deterministic scenario CSVs for unified fuzzy testing (Scenarios 1–9, compact numbering) via `tidal_simulator.py`.
- Converts CSVs into Cadence tests (`cadence/tests/rebalance_*.cdc`) via `generate_cadence_tests.py` with strict comparisons at ±0.0000001.
- Emits machine-parsable DRIFT logs from tests and aggregates them into `precision_reports/UNIFIED_FUZZY_DRIFT_REPORT.md`.

### Prerequisites

- Python 3.11+
- Flow CLI installed and available as `flow`

Optional: venv for Python packages (pandas).

```bash
python3 -m venv .venv && source .venv/bin/activate
pip install pandas
```

### Generate scenario CSVs

```bash
python3 tidal_simulator.py
```

Outputs the following CSV files in the repo root (compact numbering, with splits):
- Scenario1_FLOW.csv
- Scenario2_Instant.csv
- Scenario3_Path_{A,B,C,D}_precise.csv
- Scenario4_VolatileMarkets.csv
- Scenario5_GradualTrends.csv
- Scenario6_EdgeCases.csv
- Scenario7_MultiStepPaths_{Bear,Bull,Sideways,Crisis}.csv
- Scenario8_RandomWalks.csv
- Scenario9_ExtremeShocks_{FlashCrash,Rebound,YieldHyperInflate,MixedShock}.csv

### Generate Cadence tests

```bash
python3 generate_cadence_tests.py
```

Outputs Cadence tests directly into `cadence/tests/` with names like:
- rebalance_scenario1_flow_test.cdc
- rebalance_scenario2_instant_test.cdc
- rebalance_scenario3_path_{a,b,c,d}_test.cdc
- rebalance_scenario4_volatilemarkets_test.cdc
- rebalance_scenario5_gradualtrends_test.cdc
- rebalance_scenario6_edgecases_test.cdc
- rebalance_scenario7_multisteppaths_{bear,bull,sideways,crisis}_test.cdc
- rebalance_scenario8_randomwalks_test.cdc
- rebalance_scenario9_extremeshocks_{flashcrash,rebound,yieldhyperinflate,mixedshock}_test.cdc

Notes:
- Scaling table is removed from CSV outputs; compact numbering starts at VolatileMarkets for Scenario 4.
- All tests use exact comparisons with tolerance 0.0000001 and 8-decimal formatting.
- Scenario 1 generated test asserts post-rebalance values for each price point to match CSV expectations.
- Legacy tests remain intact (rebalance_scenario{1,2}_test.cdc and 3a–3d).

### Run tests

Run an individual scenario:
```bash
flow test cadence/tests/rebalance_scenario1_flow_test.cdc
```

Run multiple scenarios (example 4–9):
```bash
for f in \
cadence/tests/rebalance_scenario4_volatilemarkets_test.cdc \
cadence/tests/rebalance_scenario5_gradualtrends_test.cdc \
cadence/tests/rebalance_scenario6_edgecases_test.cdc \
cadence/tests/rebalance_scenario7_multisteppaths_*_test.cdc \
cadence/tests/rebalance_scenario8_randomwalks_test.cdc \
cadence/tests/rebalance_scenario9_extremeshocks_*_test.cdc; do
echo "\n=== RUN $f ==="; flow test "$f" | cat;
done
```

### Generate precision drift report

Tests emit DRIFT logs in the form:
```
DRIFT|<Label>|<step>|<actualDebt>|<expectedDebt>|<actualY>|<expectedY>|<actualColl>|<expectedColl>
```

Aggregate these into a markdown report:
```bash
python3 precision_reports/generate_drift_report.py
```

Report path: `precision_reports/UNIFIED_FUZZY_DRIFT_REPORT.md`

### One-command shortcut

Use the provided script to archive old artifacts, regenerate CSVs and tests, and rebuild the drift report in one go:

```bash
bash scripts/run_fuzzy.sh
```

What it does:
- Archives previous Scenario*.csv, generated test files, and the last drift report under `archives/fuzzy_run_<timestamp>/`.
- Ensures a venv with pandas is ready.
- Runs the simulator, test generator, and drift report builder end to end.

### Current status and interpretation

- Precision target: ±0.0000001 for all asserted values (debt, yield units, collateral).
- Scenarios 1–3: generated tests align with legacy expectations under strict precision.
- Some later steps in Scenarios 4–9 exhibit larger deltas in the drift report. These are due to step-order/semantics mismatches between the CSV action sequencing and on-chain rebalancing (e.g., when to rebalance tide vs protocol), not formula changes. Tolerances remain unchanged.

### Files of interest

- `tidal_simulator.py`: generates CSVs with shared formulas across scenarios (collateral × CF ÷ targetHealth; balancer sell-to-debt at 1.05× threshold).
- `generate_cadence_tests.py`: maps CSVs to tests, with Scenario 1 using immediate post-rebalance semantics; embeds DRIFT logs and strict comparisons.
- `cadence/tests/test_helpers.cdc`: helpers used by tests.
- `precision_reports/generate_drift_report.py`: runs selected tests (including split S7/S9) and produces the drift report.

### Troubleshooting

- If tests fail at step 0 with large deltas, verify CSV action ordering versus the generator’s step execution order for that scenario.
- Ensure Flow CLI is installed and contracts deploy in test setup (handled by generated test header’s `setup()`).


9 changes: 9 additions & 0 deletions Scenario1_FLOW.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FlowPrice,Collateral,BorrowEligible,DebtBefore,HealthBefore,Action,DebtAfter,YieldAfter,HealthAfter
0.500000000,500.000000000,400.000000000,615.384615385,0.650000000,Repay 307.692307693,307.692307692,307.692307692,1.300000000
0.800000000,800.000000000,640.000000000,615.384615385,1.040000000,Repay 123.076923077,492.307692308,492.307692308,1.300000000
1.000000000,1000.000000000,800.000000000,615.384615385,1.300000000,none,615.384615385,615.384615385,1.300000000
1.200000000,1200.000000000,960.000000000,615.384615385,1.560000000,Borrow 123.076923077,738.461538462,738.461538462,1.300000000
1.500000000,1500.000000000,1200.000000000,615.384615385,1.950000000,Borrow 307.692307692,923.076923077,923.076923077,1.300000000
2.000000000,2000.000000000,1600.000000000,615.384615385,2.600000000,Borrow 615.384615384,1230.769230769,1230.769230769,1.300000000
3.000000000,3000.000000000,2400.000000000,615.384615385,3.900000000,Borrow 1230.769230769,1846.153846154,1846.153846154,1.300000000
5.000000000,5000.000000000,4000.000000000,615.384615385,6.500000000,Borrow 2461.538461538,3076.923076923,3076.923076923,1.300000000
8 changes: 8 additions & 0 deletions Scenario2_Instant.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
YieldPrice,Debt,YieldUnits,Collateral,Health,Actions
1.000000000,615.384615385,615.384615385,1000.000000000,1.300000000,none
1.100000000,653.254437870,593.867670791,1061.538461539,1.300000000,Bal sell 55.944055944 | Borrow 37.869822485
1.200000000,689.800140688,574.833450573,1120.925228618,1.300000000,Bal sell 49.488972566 | Borrow 36.545702818
1.300000000,725.174506877,557.826543752,1178.408573675,1.300000000,Bal sell 44.217957736 | Borrow 35.374366189
1.500000000,793.830081493,529.220054328,1289.973882426,1.300000000,Bal sell 74.376872501 | Borrow 68.655574616
2.000000000,956.667021286,478.333510643,1554.583909589,1.300000000,Bal sell 132.305013582 | Borrow 162.836939793
3.000000000,1251.026104758,417.008701586,2032.917420232,1.300000000,Bal sell 159.444503548 | Borrow 294.359083472
4 changes: 4 additions & 0 deletions Scenario3_Path_A_precise.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Step,Label,FlowPrice,YieldPrice,Debt,YieldUnits,Collateral,Health,Action
0.000000000,start,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1.300000000,none
1.000000000,after FLOW,0.800000000,1.000000000,492.307692308,492.307692308,800.000000000,1.300000000,Repay 123.076923077
2.000000000,after YIELD,0.800000000,1.200000000,552.899408284,460.749506904,898.461538462,1.300000000,Bal sell 82.051282051 | Borrow 60.591715976
4 changes: 4 additions & 0 deletions Scenario3_Path_B_precise.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Step,Label,FlowPrice,YieldPrice,Debt,YieldUnits,Collateral,Health,Action
0.000000000,start,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1.300000000,none
1.000000000,after FLOW,1.500000000,1.000000000,923.076923077,923.076923077,1500.000000000,1.300000000,Borrow 307.692307692
2.000000000,after YIELD,1.500000000,1.300000000,1093.491124260,841.147018662,1776.923076923,1.300000000,Bal sell 213.017751479 | Borrow 170.414201183
4 changes: 4 additions & 0 deletions Scenario3_Path_C_precise.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Step,Label,FlowPrice,YieldPrice,Debt,YieldUnits,Collateral,Health,Action
0.000000000,start,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1.300000000,none
1.000000000,after FLOW,2.000000000,1.000000000,1230.769230769,1230.769230769,2000.000000000,1.300000000,Borrow 615.384615384
2.000000000,after YIELD,2.000000000,2.000000000,1988.165680474,994.082840237,3230.769230770,1.300000000,Bal sell 615.384615385 | Borrow 757.396449705
4 changes: 4 additions & 0 deletions Scenario3_Path_D_precise.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Step,Label,FlowPrice,YieldPrice,Debt,YieldUnits,Collateral,Health,Action
0.000000000,start,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1.300000000,none
1.000000000,after FLOW,0.500000000,1.000000000,307.692307692,307.692307692,500.000000000,1.300000000,Repay 307.692307693
2.000000000,after YIELD,0.500000000,1.500000000,402.366863905,268.244575937,653.846153846,1.300000000,Bal sell 102.564102564 | Borrow 94.674556213
11 changes: 11 additions & 0 deletions Scenario4_VolatileMarkets.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Step,FlowPrice,YieldPrice,Debt,YieldUnits,FlowUnits,Collateral,Health,Actions
0.000000000,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1000.000000000,1.300000000,none
1.000000000,1.800000000,1.200000000,1183.431952663,986.193293886,1068.376068376,1923.076923077,1.300000000,Bal sell 102.564102564 | Borrow 568.047337278
2.000000000,0.600000000,1.500000000,576.543771810,384.362514540,1561.472715319,936.883629191,1.300000000,Bal sell 197.238658777 | Repay 606.888180853
3.000000000,2.200000000,1.500000000,2113.993829970,1409.329219980,1561.472715319,3435.239973702,1.300000000,Borrow 1537.450058160
4.000000000,0.400000000,2.500000000,1251.642034528,500.656813811,5084.795765269,2033.918306108,1.300000000,Bal sell 563.731687992 | Repay 862.351795442
5.000000000,3.000000000,2.500000000,9387.315258958,3754.926103583,5084.795765269,15254.387295807,1.300000000,Borrow 8135.673224430
6.000000000,1.000000000,3.500000000,5439.828842370,1554.236812106,8839.721868852,8839.721868852,1.300000000,Bal sell 1072.836029595 | Repay 3947.486416588
7.000000000,0.200000000,3.500000000,1087.965768474,310.847362421,8839.721868852,1767.944373770,1.300000000,Repay 4351.863073896
8.000000000,4.000000000,4.000000000,21854.960711766,5463.740177941,8878.577789155,35514.311156620,1.300000000,Bal sell 38.855920303 | Borrow 20766.994943292
9.000000000,1.500000000,4.000000000,8195.610266913,2048.902566728,8878.577789155,13317.866683733,1.300000000,Repay 13659.350444853
21 changes: 21 additions & 0 deletions Scenario5_GradualTrends.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Step,FlowPrice,YieldPrice,Debt,YieldUnits,FlowUnits,Collateral,Health,Actions
0.000000000,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1000.000000000,1.300000000,none
1.000000000,1.154508497,1.020000000,710.466767385,708.602411463,1000.000000000,1154.508497000,1.300000000,Borrow 95.082152000
2.000000000,1.293892626,1.040000000,796.241616000,791.078227439,1000.000000000,1293.892626000,1.300000000,Borrow 85.774848615
3.000000000,1.404508497,1.060000000,890.344493590,839.947635462,1030.118226536,1446.809802084,1.300000000,Bal sell 39.906891590 | Borrow 94.102877590
4.000000000,1.475528258,1.080000000,935.365262975,881.633533041,1030.118226536,1519.968552335,1.300000000,Borrow 45.020769385
5.000000000,1.500000000,1.100000000,950.878362956,895.736351206,1030.118226536,1545.177339804,1.300000000,Borrow 15.513099981
6.000000000,1.475528258,1.120000000,967.578401680,863.909287215,1065.594572117,1572.314902730,1.300000000,Bal sell 46.737812852 | Borrow 16.700038724
7.000000000,1.404508497,1.140000000,921.007157474,823.057318613,1065.594572117,1496.636630895,1.300000000,Repay 46.571244206
8.000000000,1.293892626,1.160000000,848.470744103,760.525927776,1065.594572117,1378.764959168,1.300000000,Repay 72.536413371
9.000000000,1.154508497,1.180000000,787.192516025,667.112301716,1107.993437782,1279.187838540,1.300000000,Bal sell 41.482924299 | Repay 61.278228078
10.000000000,1.000000000,1.200000000,681.842115558,579.320301327,1107.993437782,1107.993437782,1.300000000,Repay 105.350400467
11.000000000,0.845491503,1.220000000,576.491715092,492.967514060,1107.993437782,936.799037024,1.300000000,Repay 105.350400466
12.000000000,0.706107374,1.240000000,502.861747141,405.533667049,1157.260735679,817.150339104,1.300000000,Bal sell 28.054840599 | Repay 73.629967951
13.000000000,0.595491503,1.260000000,424.085498370,343.012834691,1157.260735679,689.138934852,1.300000000,Repay 78.776248771
14.000000000,0.524471742,1.280000000,373.508033225,303.499190046,1157.260735679,606.950553990,1.300000000,Repay 50.577465145
15.000000000,0.500000000,1.300000000,369.028481031,283.868062332,1199.342563349,599.671281675,1.300000000,Bal sell 16.185318334 | Repay 4.479552194
16.000000000,0.524471742,1.320000000,387.090020587,297.551046844,1199.342563349,629.021283454,1.300000000,Borrow 18.061539556
17.000000000,0.595491503,1.340000000,439.506649638,336.667934195,1199.342563349,714.198305661,1.300000000,Borrow 52.416629051
18.000000000,0.706107374,1.360000000,521.147463343,396.697944272,1199.342563349,846.864627933,1.300000000,Borrow 81.640813705
19.000000000,0.845491503,1.380000000,640.202859231,463.915115385,1230.443644387,1040.329646250,1.300000000,Bal sell 19.054854893 | Borrow 119.055395888
7 changes: 7 additions & 0 deletions Scenario6_EdgeCases.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
TestCase,InitialFlow,FlowPrice,YieldPrice,Debt,YieldUnits,FlowUnits,Collateral,Health,Actions
VeryLowFlow,1000.000000000,0.010000000,1.000000000,6.153846154,6.153846154,1000.000000000,10.000000000,1.300000000,Repay 609.230769231
VeryHighFlow,1000.000000000,100.000000000,1.000000000,61538.461538462,61538.461538462,1000.000000000,100000.000000000,1.300000000,Borrow 60923.076923077
VeryHighYield,1000.000000000,1.000000000,50.000000000,19171.597633148,383.431952663,31153.846153865,31153.846153865,1.300000000,Bal sell 603.076923077 | Borrow 18556.213017763
BothVeryLow,1000.000000000,0.050000000,0.020000000,30.769230769,-28615.384615415,1000.000000000,50.000000000,1.300000000,Repay 584.615384616
MinimalPosition,1.000000000,1.000000000,1.000000000,0.615384615,0.615384615,1.000000000,1.000000000,1.300000001,none
LargePosition,1000000.000000000,1.000000000,1.000000000,615384.615384615,615384.615384615,1000000.000000000,1000000.000000000,1.300000000,none
9 changes: 9 additions & 0 deletions Scenario7_MultiStepPaths_Bear.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Step,FlowPrice,YieldPrice,Debt,YieldUnits,FlowUnits,Collateral,Health,Actions
0.000000000,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1000.000000000,1.300000000,none
1.000000000,0.900000000,1.100000000,591.715976332,537.923614847,1068.376068377,961.538461539,1.300000000,Bal sell 55.944055944 | Repay 23.668639053
2.000000000,0.800000000,1.200000000,559.072748422,465.893957018,1135.616520232,908.493216186,1.300000000,Bal sell 44.826967904 | Repay 32.643227910
3.000000000,0.700000000,1.300000000,517.859052224,398.353117096,1202.172799805,841.520959864,1.300000000,Bal sell 35.837996693 | Repay 41.213696198
4.000000000,0.600000000,1.400000000,468.393225596,334.566589711,1268.564985988,761.138991593,1.300000000,Bal sell 28.453794079 | Repay 49.465826628
5.000000000,0.500000000,1.500000000,410.916401209,273.944267472,1335.478303930,667.739151965,1.300000000,Bal sell 22.304439314 | Repay 57.476824387
6.000000000,0.400000000,1.600000000,345.591229734,215.994518584,1403.964370795,561.585748318,1.300000000,Bal sell 17.121516716 | Repay 65.325171475
7.000000000,0.300000000,1.700000000,272.485392675,160.285525103,1475.962543658,442.788763097,1.300000000,Bal sell 12.705559917 | Repay 73.105837059
9 changes: 9 additions & 0 deletions Scenario7_MultiStepPaths_Bull.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Step,FlowPrice,YieldPrice,Debt,YieldUnits,FlowUnits,Collateral,Health,Actions
0.000000000,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1000.000000000,1.300000000,none
1.000000000,1.200000000,1.000000000,738.461538462,738.461538462,1000.000000000,1200.000000000,1.300000000,Borrow 123.076923077
2.000000000,1.500000000,1.050000000,923.076923077,914.285714286,1000.000000000,1500.000000000,1.300000000,Borrow 184.615384615
3.000000000,2.000000000,1.050000000,1230.769230769,1207.326007326,1000.000000000,2000.000000000,1.300000000,Borrow 307.692307692
4.000000000,2.500000000,1.100000000,1598.331924486,1453.029022260,1038.915750916,2597.289377290,1.300000000,Bal sell 88.444888445 | Borrow 367.562693717
5.000000000,3.000000000,1.100000000,1917.998309383,1743.634826712,1038.915750916,3116.747252748,1.300000000,Borrow 319.666384897
6.000000000,3.500000000,1.150000000,2237.664694281,2021.605596189,1038.915750916,3636.205128206,1.300000000,Borrow 319.666384898
7.000000000,4.000000000,1.200000000,2673.184630654,2227.653858878,1085.981256203,4343.925024812,1.300000000,Bal sell 156.885017622 | Borrow 435.519936373
9 changes: 9 additions & 0 deletions Scenario7_MultiStepPaths_Crisis.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Step,FlowPrice,YieldPrice,Debt,YieldUnits,FlowUnits,Collateral,Health,Actions
0.000000000,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1000.000000000,1.300000000,none
1.000000000,0.500000000,2.000000000,686.390532545,343.195266272,2230.769230770,1115.384615385,1.300000000,Bal sell 307.692307693 | Borrow 71.005917160
2.000000000,0.200000000,5.000000000,908.147473827,181.629494765,7378.698224845,1475.739644969,1.300000000,Bal sell 205.917159763 | Borrow 221.756941282
3.000000000,0.100000000,10.000000000,1012.933720805,101.293372081,16460.172963075,1646.017296308,1.300000000,Bal sell 90.814747382 | Borrow 104.786246978
4.000000000,0.150000000,10.000000000,1519.400581207,151.940058121,16460.172963075,2469.025944461,1.300000000,Borrow 506.466860402
5.000000000,0.300000000,10.000000000,3038.801162414,303.880116242,16460.172963075,4938.051888923,1.300000000,Borrow 1519.400581207
6.000000000,0.700000000,10.000000000,7090.536045633,709.053604564,16460.172963075,11522.121074153,1.300000000,Borrow 4051.734883219
7.000000000,1.200000000,10.000000000,12155.204649655,1215.520464966,16460.172963075,19752.207555690,1.300000000,Borrow 5064.668604022
9 changes: 9 additions & 0 deletions Scenario7_MultiStepPaths_Sideways.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Step,FlowPrice,YieldPrice,Debt,YieldUnits,FlowUnits,Collateral,Health,Actions
0.000000000,1.000000000,1.000000000,615.384615385,615.384615385,1000.000000000,1000.000000000,1.300000000,none
1.000000000,1.100000000,1.050000000,676.923076923,673.992673993,1000.000000000,1100.000000000,1.300000000,Borrow 61.538461538
2.000000000,0.900000000,1.050000000,553.846153846,556.776556777,1000.000000000,900.000000000,1.300000000,Repay 123.076923077
3.000000000,1.050000000,1.100000000,682.220343759,620.200312508,1055.817198675,1108.608058609,1.300000000,Bal sell 53.280053281 | Borrow 128.374189913
4.000000000,0.950000000,1.100000000,617.246977687,561.133616079,1055.817198675,1003.026338741,1.300000000,Repay 64.973366072
5.000000000,1.020000000,1.150000000,662.728333938,600.682621515,1055.817198675,1076.933542649,1.300000000,Borrow 45.481356251
6.000000000,0.980000000,1.150000000,636.738987509,578.083189838,1055.817198675,1034.700854702,1.300000000,Repay 25.989346429
7.000000000,1.000000000,1.200000000,684.786485521,570.655404601,1112.778038972,1112.778038972,1.300000000,Bal sell 47.467366914 | Borrow 48.047498012
Loading
Loading