hackquest logo

MeowFi Yield Aggregator Agent

This script implements an automated agent for managing a DeFi yield aggregator protocol deployed on the Mantle Sepolia testnet.

Videos

Description

MeowFi Yield Aggregator Agent (Mantle Sepolia)

Overview

This script implements an automated agent for managing a DeFi yield aggregator protocol deployed on the Mantle Sepolia testnet. The agent monitors market conditions (simulated APYs and received bribes), calculates optimal yield strategies, and automatically rebalances funds between different underlying DeFi protocols to maximize returns. It also provides a Telegram bot interface for users to check status and for administrators to manage the protocol. Pitch Doc

Features

  • Automated Rebalancing: Periodically evaluates yield opportunities across configured protocols and rebalances funds to the highest-yielding option.

  • Bribe Integration: Listens for BribeReceived events from a dedicated BribeCollector contract, calculates the corresponding APY boost, and factors it into rebalancing decisions.

  • Telegram Bot Interface:

    • /status: Displays current vault allocations and total balances.

    • /deposithelp, /withdrawhelp, /bribehelp: Provides instructions for interacting with the contracts.

    • Admin commands (/manage_yield_token, /manage_protocol, /manage_bribe_token) for protocol configuration.

  • On-Chain Interaction: Uses viem to interact with the Mantle Sepolia network, read contract states, and send transactions.

  • Persistent Bribe Storage: Uses an SQLite database to store active bribe details and cache token decimals.

  • Configuration via Environment: Sensitive keys, contract addresses, and RPC URLs are managed through a .env file.

Architecture

  1. Configuration & Setup: Loads environment variables, defines the target blockchain (TARGET_CHAIN - Mantle Sepolia), and sets up viem public and wallet clients (publicClient, walletClient).

  2. Database: Initializes an SQLite database connection (db) and creates tables for bribes and token_decimals if they don't exist.

  3. Telegram Bot: Initializes a telegraf bot instance (bot) and defines handlers for user and admin commands.

  4. Bribe Listener: Uses publicClient.watchContractEvent to listen for BribeReceived events on the BRIBE_COLLECTOR_CONTRACT_ADDRESS. When an event is detected (startBribeListener):

    • Fetches block timestamp.

    • Calculates the bribe's expiry timestamp.

    • Fetches the bribe token's decimals (using getTokenDecimals, which includes DB caching).

    • Calculates the apy_boost based on the amount, duration, and BRIBE_APY_SCALE_FACTOR.

    • Stores the project_id, apy_boost, and expiry_timestamp in the bribes table.

  5. Rebalancing Cycle (runRebalanceCycle): This core logic runs periodically (every REBALANCE_INTERVAL_MS):

    • Fetches simulated base APYs (getSimulatedAPYs).

    • Fetches active bribe boosts from the database (getActiveBribes).

    • Calculates the effective APY for each token in each protocol by adding the base APY and any active bribe boost.

    • For each managed token in tokenRegistry:

      • Determines the protocol with the highest effective APY (bestProtocolId).

      • Reads the current total balance and allocation of the token from the YieldAggregator contract.

      • If the optimal protocol (bestProtocolId) differs from the current one (currentProtocolId), it simulates and sends a rebalance transaction using the agentAccount via the walletClient.

  6. Helper Functions: Includes utilities like getTokenDecimals (fetches/caches decimals) and isAdmin (checks Telegram user ID).

  7. Main Execution (main): Initializes clients, starts the Telegram bot, starts the bribe listener, and initiates the rebalancing cycle interval.

  8. Graceful Shutdown: Handles SIGINT and SIGTERM signals to stop the bot, listener, and close the database connection cleanly.

Tech Stack

Web3
Ethers
Solidity
Python
Team Leader
AAcchyutam Agrawal
Sector
DeFi