Skip to main content
Scaffold this example locally and test it with the CLI:

circuit.toml

circuit.toml

Example

Sample Output

How It Works

  1. Mark the basket: Reads each token’s held amount and current USD value, plus idle USDC (valued from its raw balance, since it’s ~$1).
  2. Set the target: Splits the portfolio’s total value equally across the basket - that’s each token’s target allocation.
  3. Trim overweight: For any leg above target by more than the rebalance band, sells just the excess fraction back to USDC.
  4. Top up underweight: For any leg below target, buys the shortfall with USDC - funded by the trims plus any idle cash.
  5. Weekly cadence: the schedule trigger’s every is set to a weekly interval. The first run (all USDC) builds the basket; later runs correct whatever weights have drifted.
  6. Unwind: Sells every basket token held back to USDC.

Notes

  • The basket is yours to curate. INDEX_TOKENS seeds with WETH and cbBTC as a runnable example - replace them with the tokens you want exposure to (e.g. the top AI tokens). Each must be a valid, liquid ERC-20 on the configured network.
  • All legs trade on a single network (Base) so swaps are same-chain and settle quickly. Point startingAsset and NETWORK at another chain if your basket lives elsewhere.
  • Weights are computed from each balance’s marked marketValueUsd. If a held token is unpriced the agent skips the run rather than rebalance on a fabricated value - it never coerces a missing price to zero.
  • REBALANCE_BAND_USD is a deadband: drift smaller than it is left alone so the agent doesn’t churn swap fees on noise.
  • agent.swap.quote(...) previews a route. Pass the same limits to agent.swap.execute(...), which gets a fresh executable quote. Always check result.success before assuming the trade landed.