← Back to home

Balanced by pyth

Balanced by Pyth: Oracle-powered vault that auto-rebalances your crypto portfolio

Problem Statement

Balanced by Pyth is a non-custodial staking vault that keeps a portfolio at target allocations using Pyth price oracles and Uniswap v3. Users deposit supported tokens (e.g., WETH, USDC, WBTC) and receive vault shares. The vault continuously monitors real-time Pyth prices; when weights drift beyond a threshold, it executes on-chain swaps to restore balance—funds never leave the vault except on withdraw.How it works • Oracle freshness: A lightweight price-pusher bot fetches signed updates from Pyth Hermes and calls updatePriceFeeds on-chain (pull-oracle pattern = gas efficient, no stale data). • Monitoring: A small keeper bot checks portfolio drift vs targets. If drift > threshold (e.g., 3%), it calls the vault’s rebalance() (or rebalanceWithUpdates if prices are stale). • Execution: The vault reads Pyth USD prices, computes how much of the overweight token to sell into the underweight token, sets a minOut from oracle prices (slippage guard), and swaps via Uniswap v3 Router. Swap proceeds return to the vault address (recipient = address(this)), so custody is preserved. • Safety: Optional TWAP check vs the v3 pool, minimal/cleared allowances to the router, reentrancy guard, and detailed Rebalanced events for transparency.Why Pyth (vs other oracles) • Trading-grade latency & frequency: Sub-second delivery and confidence intervals → tighter, safer rebalances. • Pull model: We update only when needed, reducing gas burn and avoiding stale feeds common with fixed push models. • Multi-chain coverage (Base, Unichain, etc.) → easy portability. • Clear confidence bounds let us set robust minOut and rejection rules.

Solution

🧱 Smart Contracts (Solidity on Base Sepolia) • Vault.sol — Core contract that accepts user deposits (ETH, USDC, WBTC), issues shares, and manages balances. • Implements drift detection: compares current weights (via Pyth USD prices) to target allocations. • Provides rebalance() and rebalanceWithUpdates() methods: • Reads fresh prices from Pyth Oracle contract. • Calculates overweight vs underweight tokens. • Calls Uniswap v3 Router with slippage protection (minOut derived from Pyth). • All swap proceeds go back to the vault itself (recipient = address(this)). • Safety features: reentrancy guard, minimal/cleared approvals, optional TWAP deviation check.🔮 Oracle Integration (Pyth) • We used the Pyth EVM contract deployed on Base Sepolia. • Off-chain we run a price-pusher bot: • Pulls signed price updates from Pyth Hermes every 5 minutes. • Submits them on-chain with updatePriceFeeds(). • Ensures the Vault always has fresh oracle data without relying on fixed push models. • Chose Pyth over Chainlink because of: • Higher frequency updates (sub-second). • Confidence intervals for robust slippage guards. • Pull model → lower gas cost, no stale feeds.🔄 Automated Rebalancing (Keeper Bot) • A separate keeper service monitors the Vault: • Reads portfolio weights and drift vs targets. • If drift > threshold → fetches Hermes bundle if needed, then calls rebalanceWithUpdates(). • Logs every rebalance (tokenIn, tokenOut, amounts, tx hash). • Written in TypeScript using ethers.js.💧 Liquidity Layer (Uniswap v3) • All swaps routed through Uniswap v3 Router on Base Sepolia. • Single-hop (e.g. WETH→USDC) and multi-hop (e.g. WBTC→WETH→USDC) supported. • Fee tiers configurable via Vault owner settings.📡 Frontend / Demo UI • Minimal React UI: • Shows Vault TVL, target vs current weights, and oracle freshness. • Displays last Rebalanced events with token amounts and tx links. • Uses WalletConnect for deposits/withdrawals.⚡ Hacky / Notable Engineering Tricks • Oracle freshness guard: Vault reverts rebalance() if oracle data older than 90s, forcing keepers to call rebalanceWithUpdates(). • Pull Oracle pattern: Gas-efficient compared to always-on chainlink feeds. • One-transaction bundle: In rebalanceWithUpdates(), keeper both updates Pyth feeds and rebalances in the same call → atomic, cheaper. • Minimal approvals: Router allowance is set for the swap and reset back to zero, reducing ERC-20 allowance risks.🛠️ Stack Recap • Solidity (Vault + Oracle integration) • Pyth Network (Hermes API + EVM Oracle contract) • Uniswap v3 (swap execution) • TypeScript/Node.js (price-pusher + keeper bots) • React + WalletConnect (demo UI) • Base Sepolia (deployment network)

Hackathon

ETHGlobal New Delhi

2025

Contributors