Skip to content

rofergon/Hedron

Β 
Β 

Repository files navigation

Hedera DeFi AI Agent πŸš€

πŸ”— Related Repositories

πŸ“œ Deployed Contracts

πŸ“‹ Project Description

An artificial intelligence agent specialized in the Hedera Hashgraph DeFi ecosystem that helps both novice and experienced users navigate and optimize their investments through integrated SaucerSwap DEX operations, Bonzo Finance lending protocols, and advanced AutoSwapLimit trading strategies. The agent provides real-time analysis and automated execution capabilities.

πŸš€ Quick Access

πŸ”— Quick Navigation πŸ“‚ Path πŸ“„ Description
🏦 DeFi Tools typescript/src/shared/tools/defi/ Complete DeFi integration suite
πŸ€– WebSocket Agent typescript/examples/langchain/websocket-agent.ts Main AI agent server
πŸ”§ Message Handlers typescript/examples/langchain/handlers/ WebSocket message processing

🎯 Problem Solved

For Novice Users:

  • Complexity of the DeFi ecosystem: New users feel overwhelmed by the number of protocols and available options
  • Lack of technical knowledge: Difficulty understanding concepts like liquidity mining, yield farming, lending, staking
  • Risk of losses: Making investment decisions without enough information can result in significant losses
  • Fragmented information: Data is scattered across multiple platforms without a unified view

For Experienced Users:

  • Inefficient manual monitoring: Manually reviewing multiple protocols consumes valuable time
  • Missed opportunities: The best arbitrage and yield opportunities can go unnoticed
  • Complex comparative analysis: Comparing yields, risks, and features between protocols takes a lot of time
  • Lack of automation: Need for tools that facilitate data-driven decision making

πŸ’‘ How It Solves the Problem

πŸ€– Intelligent AI Agent

  • Automated analysis: Continuous monitoring of SaucerSwap, Bonzo Finance, and AutoSwapLimit
  • Personalized recommendations: Suggestions based on the user's risk profile and objectives
  • Conversational interface: Natural interaction via WebSocket for real-time queries
  • Persistent context: Conversation memory that maintains session context

πŸ“Š Real-Time Multi-Protocol Analysis

Integration with APIs and Smart Contracts:

  • REST APIs: Direct connection to platform data endpoints
  • Smart contracts: Native interaction with on-chain protocols
  • Intelligent rate limiting: Optimized request management to avoid limitations
  • Smart cache: 30-second cache system to optimize performance

🏦 Integrated DeFi Platforms

πŸ₯‡ SaucerSwap

  • Type: DEX with AMM (Automated Market Maker)
  • Features:
    • Token swap quotes and execution
    • Real-time price discovery
    • Single-sided staking (Infinity Pools)
    • Liquidity analysis
    • Advanced trading with AutoSwapLimit orders
  • Dominance: +44% of total Hedera DeFi TVL and +60% of unique active wallets

πŸ’° Bonzo Finance

  • Type: Lending & borrowing protocol (Aave V2 fork)
  • Features:
    • Supply assets to earn interest
    • Real-time lending rates monitoring
    • Portfolio analysis and optimization
    • Risk assessment tools
  • TVL: Steady growth post-launch (~$25M in Q4 2024, later ~$38M)

🎯 AutoSwapLimit (SaucerSwap Integration)

  • Type: Advanced limit order system
  • Features:
    • Automated limit orders execution
    • Price monitoring and alerts
    • Order management and cancellation
    • Strategic trading automation

πŸ› οΈ Technical Architecture

πŸ”„ Agent Flow Diagram

graph TD
    A[Client connects WebSocket] --> B{Message type}
    
    B -->|CONNECTION_AUTH| C[Authenticate User]
    C --> D[Create User Connection]
    D --> E[Initialize LLM Agent]
    E --> F[Load Hedera Tools]
    F --> G[Load DeFi Tools]
    G --> H[User Authenticated βœ…]
    
    B -->|USER_MESSAGE| I[Process User Message]
    I --> J[Verify Authentication]
    J --> K{User Authenticated?}
    K -->|No| L[Send Authentication Error]
    K -->|Yes| M[Load Conversation Memory]
    M --> N[Execute LLM Agent]
    N --> O[Agent analyzes message with tools]
    
    O --> P{Response type?}
    P -->|Text response| Q[AGENT_RESPONSE]
    P -->|Transaction required| R[TRANSACTION_TO_SIGN]
    P -->|Swap quote| S[SWAP_QUOTE]
    P -->|System message| T[SYSTEM_MESSAGE]
    
    Q --> U[Send response to client]
    R --> V[Client signs transaction]
    S --> W[Show structured quote]
    T --> X[Show system message]
    
    V --> Y[TRANSACTION_RESULT]
    B -->|TRANSACTION_RESULT| Z[Process transaction result]
    Z --> AA[Update agent state]
    AA --> U
    
    G --> BB[DeFi Tools Loading]
    BB --> CC[🏦 Bonzo Finance<br/>- Lending/Borrowing<br/>- Portfolio Analysis<br/>- Interest Rates]
    BB --> DD[πŸ₯‡ SaucerSwap<br/>- Token Swaps<br/>- Liquidity Pools<br/>- Price Quotes]
    BB --> EE[🎯 AutoSwapLimit<br/>- Limit Orders<br/>- Order Management<br/>- Trading Automation]
    BB --> FF[πŸ“Š Analytics<br/>- Real-time Data<br/>- Account Balances<br/>- Market Monitoring]
    
    CC --> GG[Hedera Native Tools]
    DD --> GG
    EE --> GG
    FF --> GG
    
    GG --> HH[πŸ”— HTS - Token Operations<br/>πŸ—£οΈ HCS - Consensus Messages<br/>πŸ’° HBAR - Transfers & Balances<br/>πŸ“‹ Account Queries]
Loading

πŸ”Œ WebSocket Communication Flow

  1. Connection Establishment: Client connects to WebSocket server
  2. Authentication: User sends CONNECTION_AUTH with their Hedera account ID
  3. Agent Initialization: System creates personalized agent with tools and memory
  4. Message Processing: Agent processes user queries using integrated DeFi and Hedera tools
  5. Response Generation: AI generates responses, quotes, or transaction requests
  6. Transaction Handling: Client signs transactions externally, reports results back

🧰 Integrated Tools Architecture

🏦 DeFi Tools Suite β†’ πŸ“‚ Browse DeFi Tools

πŸ”— Hedera Native Tools

  • HTS (Hedera Token Service): Token operations and management
  • HCS (Hedera Consensus Service): Messaging and consensus
  • HBAR Operations: Transfers, balance queries, account management
  • Account Queries: Real-time account information and token balances

πŸ“‘ WebSocket Message Types

Message Type Direction Purpose
CONNECTION_AUTH Client β†’ Agent User authentication with account ID
USER_MESSAGE Client β†’ Agent User queries and instructions
AGENT_RESPONSE Agent β†’ Client AI-generated text responses
SWAP_QUOTE Agent β†’ Client Structured swap quotation data
TRANSACTION_TO_SIGN Agent β†’ Client Transaction bytes requiring signature
TRANSACTION_RESULT Client β†’ Agent Signed transaction confirmation
SYSTEM_MESSAGE Agent β†’ Client System notifications and errors

🧠 Memory Management

  • Persistent Context: Each user maintains conversation history
  • Token Management: Intelligent memory pruning to optimize performance
  • Session Isolation: Each WebSocket connection has independent context
  • Auto Cleanup: Memory cleared on disconnection

πŸ€– How the Agent Works

The Hedera DeFi AI Agent operates as an intelligent middleware between users and the Hedera DeFi ecosystem:

  1. πŸ” Authentication: Users authenticate with their Hedera account ID
  2. 🧠 Personalization: Each user gets a dedicated agent instance with memory
  3. πŸ”§ Tool Loading: Agent loads 25+ specialized tools for DeFi operations
  4. πŸ’¬ Natural Language: Users interact in plain language - no technical knowledge required
  5. 🎯 Intelligent Routing: AI determines which tools to use based on user intent
  6. πŸ“Š Real-time Data: Tools fetch live data from protocols and mirror nodes
  7. ⚑ Smart Execution: Multi-step operations handled automatically
  8. πŸ”’ Secure Transactions: Users sign transactions on their side - agent never holds keys

Example User Journey:

User: "Show me the best yield opportunities on SaucerSwap"
β”œβ”€β”€ Agent analyzes intent: yield opportunities + SaucerSwap
β”œβ”€β”€ Calls SaucerSwap API tools for pool data
β”œβ”€β”€ Calls Infinity Pool tools for staking rewards
β”œβ”€β”€ Processes and ranks opportunities
└── Returns formatted analysis with APYs and risks

User: "Set up a limit order to sell 1000 SAUCE at $0.15"
β”œβ”€β”€ Agent processes: limit order + SAUCE + price target
β”œβ”€β”€ Calls AutoSwapLimit tools to prepare order
β”œβ”€β”€ Generates transaction bytes
β”œβ”€β”€ Sends TRANSACTION_TO_SIGN message
β”œβ”€β”€ User signs transaction externally
└── Agent confirms order creation

πŸš€ Quick Start

Prerequisites

  • Node.js (v16 or higher)
  • npm or yarn package manager

Installation & Startup

  1. Navigate to the langchain directory:

    cd typescript/examples/langchain
  2. Install dependencies:

    npm install
  3. Start the WebSocket agent:

    npm run start:websocket

The agent will start and be ready to accept WebSocket connections for real-time DeFi analysis and recommendations.

πŸ“„ License

This project is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0).

What this means:

βœ… You can:

  • Share and redistribute the code
  • Adapt and modify the code
  • Use for personal, educational, and research purposes

❌ You cannot:

  • Use for commercial purposes without explicit permission
  • Distribute modified versions under a different license

For Commercial Use:

If you want to use this project for commercial purposes, please contact the project maintainers for licensing options.

Attribution:

When using this project, please provide appropriate credit and link to this repository.


Built to democratize access to DeFi on Hedera Hashgraph 🌐

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 86.0%
  • Solidity 7.0%
  • TeX 3.8%
  • JavaScript 3.1%
  • Other 0.1%