MeowFi Yield Aggregator Agent
This script implements an automated agent for managing a DeFi yield aggregator protocol deployed on the Mantle Sepolia testnet.
视频
描述
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
BribeReceivedevents from a dedicatedBribeCollectorcontract, 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
viemto 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
.envfile.
Architecture
Configuration & Setup: Loads environment variables, defines the target blockchain (
TARGET_CHAIN- Mantle Sepolia), and sets upviempublic and wallet clients (publicClient,walletClient).Database: Initializes an SQLite database connection (
db) and creates tables forbribesandtoken_decimalsif they don't exist.Telegram Bot: Initializes a
telegrafbot instance (bot) and defines handlers for user and admin commands.Bribe Listener: Uses
publicClient.watchContractEventto listen forBribeReceivedevents on theBRIBE_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_boostbased on the amount, duration, andBRIBE_APY_SCALE_FACTOR.Stores the
project_id,apy_boost, andexpiry_timestampin thebribestable.
Rebalancing Cycle (
runRebalanceCycle): This core logic runs periodically (everyREBALANCE_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
YieldAggregatorcontract.If the optimal protocol (
bestProtocolId) differs from the current one (currentProtocolId), it simulates and sends arebalancetransaction using theagentAccountvia thewalletClient.
Helper Functions: Includes utilities like
getTokenDecimals(fetches/caches decimals) andisAdmin(checks Telegram user ID).Main Execution (
main): Initializes clients, starts the Telegram bot, starts the bribe listener, and initiates the rebalancing cycle interval.Graceful Shutdown: Handles
SIGINTandSIGTERMsignals to stop the bot, listener, and close the database connection cleanly.