TreasuryRebalancer (Drift Detection)
Monitors a 2-token treasury for allocation drift and signals when a rebalance swap is needed — no VRF required.
Overview
TreasuryRebalancer checks a treasury's token0/token1 allocation against target weights on schedule. When drift exceeds the configured threshold, AutoLoop emits a RebalanceRequired signal with the swap direction and amount. An off-chain executor reads the signal and performs the swap.
| Property | Value |
|---|---|
| Base contract | AutoLoopCompatible (no VRF) |
| VRF usage | None |
| Gas per tick | ~65k |
| Source | autoloop/src/agents/TreasuryRebalancer.sol |
| Tests | 16 passing (unit + fuzz) |
Why AutoLoop Is Structurally Required
The trigger holder can front-run the rebalance swap. A treasury rebalance always moves tokens in a predictable direction — if token0 is overweight, the swap sells token0 for token1. Whoever triggers this knows the swap's price impact before submission. AutoLoop calls at block cadence with no advance warning to any participant.
Mechanics
currentDrift()returns the current token0 allocation in basis points and the absolute drift from targetshouldProgressLoop()returns true whendrift > driftThresholdBpsandcheckIntervalhas elapsedprogressLoop()records the rebalance in the history array and emitsRebalanceRequired(loopID, tokenIn, tokenOut, currentBps, targetBps, driftBps)— it does not execute the swap itself- Signal-not-execute architecture: the contract emits the signal and an off-chain executor performs the swap. This separates the neutral timing (AutoLoop) from the MEV-sensitive execution.
Revenue Model
- AutoLoop gas fee per check tick
- No protocol rake — this is a monitoring/signaling contract
Deploy
forge create src/agents/TreasuryRebalancer.sol:TreasuryRebalancer \
--constructor-args 0x0000000000000000000000000000000000000000 6000 0xTOKEN1 500 86400 \
--rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcastArgs: token0 (address(0) = ETH), token0TargetBps (e.g. 6000 = 60%), token1, driftThresholdBps (e.g. 500 = 5%), checkInterval (seconds).
Dashboard
View TreasuryRebalancer on the AutoLoop Dashboard.