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 .cursorignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

keshav-scheduled-testnet.pkey
demo2.pkey
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ solidity/out/
broadcast
cache
db

# logs
run_logs/*.log

# local key files
testnet-deployer.pkey
testnet-uniswapV3-connectors-deployer.pkey
mock-strategy-deployer.pkey
keshav-scheduled-testnet.pkey
demo2.pkey
111 changes: 111 additions & 0 deletions .pr_scheduled_rebalancing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Global Supervisor Rebalancing + Auto-Register + Strict Proofs + Two-Terminal Tests

## Summary
This PR delivers a production-ready, isolated and perpetual rebalancing system for FlowVaults “Tides”, centered on a Global Supervisor that seeds per‑Tide jobs and a per‑Tide handler that auto‑reschedules itself after every execution. It adds robust verification (status/event/on-chain proof) and end-to-end two‑terminal scripts, plus auto‑registration at Tide creation so the first rebalance is seeded without any manual step.

Key outcomes:
- Global Supervisor periodically ensures every registered Tide has a scheduled job (fan‑out), while preserving strict isolation between Tides.
- Per‑Tide RebalancingHandler auto‑reschedules next run after successful execution → perpetual operation per Tide.
- On-chain proof in `FlowVaultsSchedulerProofs` records that a specific scheduled transaction executed.
- Strict two‑terminal tests verify scheduled execution and actual asset movement.
- Tide creation now auto-registers the Tide for scheduling, so the first rebalance is seeded automatically.

## Architecture
- FlowTransactionScheduler integration with a new contract `FlowVaultsScheduler`:
- `Supervisor` TransactionHandler: runs on a scheduled callback and seeds per‑Tide child jobs for all registered Tide IDs (using pre-issued wrapper caps); skips those with a valid pending job.
- `RebalancingHandler` wrapper: executes the underlying AutoBalancer, emits `RebalancingExecuted`, marks on-chain execution proof, and now calls `scheduleNextIfRecurring(...)` to perpetuate per‑Tide schedules.
- `SchedulerManager`: stores per‑Tide scheduled resources and schedule metadata; `hasScheduled(...)` enhanced to ignore executed/removed entries.
- `scheduleRebalancing(...)`: cleans up executed entries before scheduling to prevent collisions, validates inputs explicitly (replaces fragile pre-conditions + cleanup).
- Proof of execution `FlowVaultsSchedulerProofs`:
- Simple on-chain map of `(tideID, scheduledTxID) → true` written during handler execution, independent of emulator events.
- Registry `FlowVaultsSchedulerRegistry`:
- Tracks registered Tide IDs and wrapper capabilities; used by Supervisor to seed child schedules. (Supervisor now primarily seeds; child jobs perpetuate themselves.)
- Auto-register on Tide creation:
- `FlowVaults.TideManager.createTide(...)` now returns new Tide ID.
- `create_tide.cdc` calls `FlowVaultsScheduler.registerTide(newID)` immediately after creation.

## Files (Core)
- cadence/contracts/FlowVaultsScheduler.cdc
- Adds `Supervisor` and `RebalancingHandler` logic; `scheduleNextIfRecurring`; `hasScheduled` improvements; cleanup in `scheduleRebalancing`.
- cadence/contracts/FlowVaultsSchedulerProofs.cdc
- On-chain execution marker; read via scripts.
- cadence/contracts/FlowVaultsSchedulerRegistry.cdc
- Stores registered Tide IDs and their wrapper caps for Supervisor seeding.
- cadence/contracts/FlowVaults.cdc
- `TideManager.createTide` now returns `UInt64` (new Tide ID).
- cadence/transactions/flow-vaults/create_tide.cdc
- Calls `FlowVaultsScheduler.registerTide(newID)` to auto-register immediately after creation.

## Transactions & Scripts (Ops)
- Supervisor lifecycle:
- cadence/transactions/flow-vaults/setup_supervisor.cdc
- cadence/transactions/flow-vaults/schedule_supervisor.cdc
- Tide registry admin:
- cadence/transactions/flow-vaults/register_tide.cdc
- cadence/transactions/flow-vaults/unregister_tide.cdc
- Introspection & verification:
- cadence/scripts/flow-vaults/estimate_rebalancing_cost.cdc
- cadence/scripts/flow-vaults/get_scheduled_rebalancing.cdc
- cadence/scripts/flow-vaults/get_all_scheduled_rebalancing.cdc
- cadence/scripts/flow-vaults/get_scheduled_tx_status.cdc
- cadence/scripts/flow-vaults/was_rebalancing_executed.cdc

## Two‑Terminal E2E Tests
- run_all_rebalancing_scheduled_tests.sh
- Single‑Tide strict verification; polls scheduler status; checks scheduler Executed events; uses on‑chain proof; asserts balance/value change or Rebalanced event.
- run_multi_tide_supervisor_test.sh
- Creates/ensures multiple Tides; registers; schedules Supervisor once; verifies each Tide got a job and rebalanced (execution proof + movement per Tide).
- NEW: run_auto_register_rebalance_test.sh
- Seeds Supervisor to run, creates a Tide (auto-register), induces price drift, polls the child scheduled tx and asserts execution (status/event/on‑chain proof) and asset movement.

## How it ensures isolation and continuity
- Isolation: Each Tide has its own scheduled job via a dedicated `RebalancingHandler` wrapper. Failures in one Tide’s job don’t affect others.
- Continuity: After the first seeded job, `RebalancingHandler.executeTransaction` calls `scheduleNextIfRecurring(...)`, ensuring there is always a “next run” per Tide. The Supervisor is primarily needed to seed jobs (e.g., for new or missing entries).

## Operational Flow
1) Setup once:
- Deploy/Update contracts (flow.json already wired for emulator); ensure `SchedulerManager` exists; `setup_supervisor` to store Supervisor.
2) For new Tides:
- `create_tide.cdc` auto-registers the Tide with the scheduler.
- Supervisor (scheduled) seeds first job for the new Tide.
- Per‑Tide handler auto‑reschedules subsequently.

## Verification Strategy (Strict)
For every scheduled job execution we validate at least one of:
- Status polling to Executed (rawValue 2) or resource removal (nil status), and/or
- FlowTransactionScheduler.Executed event in the block window, and/or
- On-chain proof via `FlowVaultsSchedulerProofs` using `was_rebalancing_executed.cdc`.
And we require movement:
- DeFiActions.Rebalanced events and/or deltas in AutoBalancer balance/current value/Tide balance. Otherwise the test fails.

## Breaking Changes
- `FlowVaults.TideManager.createTide(...)` now returns `UInt64` (new Tide ID). Callers already updated (e.g., `create_tide.cdc`).

## Cleanup
- Removed accidental binary/log artifacts: `.DS_Store`, `run_logs/*.log` and added ignores for future logs.

## How to Run Locally (Two-Terminal)
1) Terminal A (emulator with scheduled txs):
- `./local/start_emulator_scheduled.sh` (or your emulator start with `--scheduled-transactions`).
2) Terminal B (tests):
- Single tide: `./run_all_rebalancing_scheduled_tests.sh`
- Multi‑tide fan‑out: `./run_multi_tide_supervisor_test.sh`
- Auto-register flow: `./run_auto_register_rebalance_test.sh`

## Risks & Mitigations
- Scheduler events may be flaky on emulator → we added multi-pronged proof (status+event+on-chain marker).
- Fee estimation drift → small buffer used when scheduling.
- Supervisor is seed-only; perpetual operation is per‑Tide and isolated, reducing blast radius.

## Future Work
- Optional: richer metrics & monitoring endpoints.
- Optional: alternative cadence to Supervisor cycles per environment.
- Optional: additional hooks for ops alerting if a Tide misses N consecutive cycles.

## Checklist
- [x] Contracts compile and deploy locally
- [x] Supervisor seeds child entries
- [x] Per‑Tide handler auto‑reschedules
- [x] Auto-register on Tide creation
- [x] Two‑terminal tests pass: execution proof + movement
- [x] Docs updated; repo cleanliness improved
Loading
Loading