Docs/Examples/Treasury Rebalancer

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.

PropertyValue
Base contractAutoLoopCompatible (no VRF)
VRF usageNone
Gas per tick~65k
Sourceautoloop/src/agents/TreasuryRebalancer.sol
Tests16 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 target
  • shouldProgressLoop() returns true when drift > driftThresholdBps and checkInterval has elapsed
  • progressLoop() records the rebalance in the history array and emits RebalanceRequired(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 --broadcast

Args: token0 (address(0) = ETH), token0TargetBps (e.g. 6000 = 60%), token1, driftThresholdBps (e.g. 500 = 5%), checkInterval (seconds).

Dashboard

View TreasuryRebalancer on the AutoLoop Dashboard.