This repository contains a practical example of AI integration with the NEAR blockchain protocol, specifically an Automated Market Maker (AMM) agent built with the @iqai/adk
library. The agent listens for blockchain events and performs automated market making calculations.
The AMM Near Agent is a specialized AI agent that facilitates trading between two assets on the NEAR Protocol. It works with the AMM Near Contract to provide automated price calculations for token swaps using the constant product formula (x * y = k).
- Event Listening: The Near Agent continuously monitors the
amm-iqai.near
contract forrun_agent
events - Event Processing: When a swap event is detected, the agent processes the transaction details
- AMM Calculations: The AMM Agent calculates optimal output amounts using the constant product formula
- Response: The agent responds with calculated swap amounts back to the contract
- Transaction Completion: The contract finalizes the swap and transfers tokens to the user
The project consists of two main agents:
- Near Agent (
src/agents/near-agent.ts
): Handles blockchain event listening using MCP (Model Context Protocol) - AMM Agent (
src/agents/amm-agent.ts
): Performs automated market making calculations using custom tools
- Amount Out Calculator (
src/tools/amount-out-caluculator.ts
): Calculates swap output amounts using AMM constant product formula
- Node.js (v18 or higher)
- pnpm package manager
- NEAR account and access to NEAR testnet/mainnet
# Clone the repository
git clone https://github.com/IQAIcom/near-amm-agent
# Navigate to the project directory
cd amm-near-agent
# Install dependencies
pnpm install
Create a .env
file in the root directory with the following variables:
# NEAR Account Configuration
ACCOUNT_ID=your-account.near
ACCOUNT_KEY=your-private-key
# NEAR Network Configuration
NEAR_NETWORK_ID=mainnet # or testnet
NEAR_NODE_URL=https://rpc.mainnet.near.org # or https://rpc.testnet.near.org
NEAR_GAS_LIMIT=300000000000000
# Debug Mode
DEBUG=false
# Start the AMM Near Agent
pnpm dev
The agent will start listening for events on the amm-iqai.near
contract and process swap calculations automatically.
We've deployed the NEAR contract on mainnet, which you can view at https://nearblocks.io/address/amm-iqai.near. Here are some example transactions:
- Creating new pool for USDT and wNEAR
- Swap USDT with wNEAR
- Agent response to the swap transaction
- FT Transfer
- Create a new tool class extending
BaseTool
in thesrc/tools/
directory - Implement the required methods:
getDeclaration()
andrunAsync()
- Add the tool to your agent in the respective agent file
- New Agents: Create additional agents in
src/agents/
- Custom Tools: Add specialized tools for different calculations
- Event Handlers: Extend event listening capabilities
- ADK Library Documentation
- NEAR Protocol Documentation
- AMM Near Contract Repository
- MCP Near Agent Package
MIT License - see the LICENSE file for details.