Skip to content

midasbal/PulseTrader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PulseTrader+ Logo

PulseTrader+

Autonomous AI Trading Agent on X Layer

Autonomous Β· Millisecond Β· Agentic

Talk to your wallet. Trade in 200ms. Watch the economy loop earn.

Live Demo

▢️ Try the Live App β†’ pulse-traderplus.vercel.app

Demo Video

πŸŽ₯ Watch the Demo Video β†’ youtu.be/BX2x0ddn5-Q


πŸ“‹ Table of Contents


What is PulseTrader+?

PulseTrader+ is a fully autonomous AI trading agent that lives on X Layer Testnet. Users chat with it in natural language, and it:

  1. 🧠 Parses intent β†’ Understands "swap 100 PUSDC to PWETH" using LLM function calling (18 tools)
  2. ⛓️ Executes onchain β†’ Signs & broadcasts real transactions from its agentic wallet
  3. ⚑ Confirms in 200ms β†’ Uses Flashblocks preconfirmations for sub-second feedback
  4. πŸ’° Earns revenue β†’ Takes a 0.1% micro-fee on every swap β†’ sweeps to treasury β†’ reinvests via DCA
  5. πŸ” Sells premium data β†’ 6 x402 micropayment-gated analytics endpoints (paid in PUSDC)
  6. πŸ”Œ Composes via MCP β†’ Exposes 14 tools + 3 resources + 3 prompts as a Model Context Protocol server
  7. πŸ€– Acts autonomously β†’ Rebalancer, fee sweep, and DCA reinvest run WITHOUT human prompting
  8. 🌐 Queries 12 OnchainOS skills β†’ Real-time prices, DEX quotes, Uniswap pools, portfolio data

Key Numbers

Metric Count
Agent Tool Definitions 18 (LLM function-calling)
OnchainOS Skills Integrated 12 (via MCP bridge)
MCP Server Tools 14
MCP Resources 3 (portfolio, economy-loop, config)
MCP Prompts 3 (trade_strategy, portfolio_review, economy_deep_dive)
x402 Gated Endpoints 6 (with real onchain payment receipts)
REST API Endpoints 24
Autonomous Loops 4 (rebalancer, fee sweep, DCA reinvest, Flashblocks listener)
Onchain Transactions 129+ (Builder Code–tagged on X Layer Testnet)
Unit Tests 17/17 passing
Lines of Code ~9,000 (hand-written TypeScript + Solidity)

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Frontend  (Next.js 16 Β· Turbopack)            β”‚
β”‚                                                                   β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚   β”‚  ChatPanel   β”‚  β”‚  Sidebar   β”‚  β”‚  Docs     β”‚  β”‚ PulseRing β”‚ β”‚
β”‚   β”‚ Rich Swap    β”‚  β”‚ Economy    β”‚  β”‚  /docs    β”‚  β”‚   Logo    β”‚ β”‚
β”‚   β”‚ Cards, Think β”‚  β”‚ Loop, Live β”‚  β”‚  route    β”‚  β”‚           β”‚ β”‚
β”‚   β”‚ Timeline     β”‚  β”‚ TX Feed    β”‚  β”‚           β”‚  β”‚           β”‚ β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚ HTTP + WebSocket
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Backend  (Express + ws Β· Port 3001)               β”‚
β”‚                                                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  AI Agent  β”‚  β”‚  Onchain   β”‚  β”‚   x402   β”‚  β”‚  Analytics   β”‚  β”‚
β”‚  β”‚  18 tools  β”‚  β”‚  Engine    β”‚  β”‚  6 gates  β”‚  β”‚  5 reports   β”‚  β”‚
β”‚  β”‚  Groq      β”‚  β”‚  burn+mint β”‚  β”‚  onchain  β”‚  β”‚  + AI signal β”‚  β”‚
β”‚  β”‚  Scout β†’   β”‚  β”‚  swap      β”‚  β”‚  receipts β”‚  β”‚              β”‚  β”‚
β”‚  β”‚  Cerebras β†’β”‚  β”‚            β”‚  β”‚           β”‚  β”‚              β”‚  β”‚
β”‚  β”‚  Groq 70B β†’β”‚  β”‚            β”‚  β”‚           β”‚  β”‚              β”‚  β”‚
β”‚  β”‚  Gemini    β”‚  β”‚            β”‚  β”‚           β”‚  β”‚              β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚        β”‚               β”‚              β”‚                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚              OnchainOS MCP Bridge (12 skills)                β”‚  β”‚
β”‚  β”‚  market_price Β· market_prices Β· swap_quote Β· swap_chains     β”‚  β”‚
β”‚  β”‚  swap_liquidity Β· token_search Β· portfolio_all_balances      β”‚  β”‚
β”‚  β”‚  portfolio_total_value Β· gateway_gas Β· defi_search           β”‚  β”‚
β”‚  β”‚  token_liquidity Β· defi_detail                               β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚              Autonomous Loops (no human prompting)            β”‚  β”‚
β”‚  β”‚  πŸ”„ Rebalancer (60s)  β”‚  πŸ’Έ Fee Sweep  β”‚  πŸ“ˆ DCA Reinvest   β”‚  β”‚
β”‚  β”‚  ⚑ Flashblocks WS    β”‚  🏷️ Builder Code on every TX         β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚           MCP Server (stdio Β· 14T / 3R / 3P)                 β”‚  β”‚
β”‚  β”‚  Composable by Claude Desktop, other AI agents, any MCP host β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚ viem 2.x + Flashblocks RPC
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   X Layer Testnet  (Chain ID 1952)                  β”‚
β”‚                                                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  PUSDC   β”‚  β”‚  PWETH   β”‚  β”‚  Agent Wallet                    β”‚  β”‚
β”‚  β”‚  6 dec   β”‚  β”‚  18 dec  β”‚  β”‚  0x5433B389d9C64f84aa01Dfc448…   β”‚  β”‚
β”‚  β”‚  0x9eb…  β”‚  β”‚  0x3717… β”‚  β”‚  129+ txs Β· Builder Code tagged  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                                     β”‚
β”‚  Flashblocks RPC: https://testrpc.xlayer.tech/flashblocks           β”‚
β”‚  WSS: wss://xlayertestws.okx.com                                   β”‚
β”‚  Explorer: https://www.oklink.com/xlayer-test                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Hackathon Track: X Layer Arena

Requirement Implementation Evidence
Deploy on X Layer PUSDC + PWETH contracts deployed to X Layer Testnet PUSDC Β· PWETH
Flashblocks 200ms preconfirmation polling on every swap via /flashblocks RPC flashblocks.ts β€” WebSocket to wss://xlayertestws.okx.com
Builder Code (ERC-8021) PULSETRDRV1XLYR0 appended to every tx via ox/erc8021 dataSuffix chain.ts line 60+ β€” check any tx's input data
OnchainOS Skills 12 skills via MCP bridge to onchainos mcp child process onchainos-mcp.ts β€” JSON-RPC over stdio
x402 Payment Protocol 6 premium endpoints gated by PUSDC micropayments + onchain receipts x402.ts β€” real token transfers on every payment
MCP Server 14 tools + 3 resources + 3 prompts via @modelcontextprotocol/sdk mcp-server.ts β€” stdio transport, .mcp.json config
Onchain Activity 129+ swap transactions with Builder Code attribution Agent Wallet on OKLink
Autonomous Agent 4 background loops: rebalancer, fee sweep, DCA reinvest, Flashblocks rebalancer.ts, analytics.ts, flashblocks.ts

Tech Stack

Layer Technology
Chain X Layer Testnet (Chain ID 1952, OP Stack L2)
Smart Contracts Solidity 0.8.20, Hardhat 2, OpenZeppelin
Backend TypeScript, Express, ws, viem 2.47
AI / LLM 4-provider failover: Groq-Scout β†’ Cerebras β†’ Groq-70B β†’ Gemini (all free, all via OpenAI SDK)
Models Llama 4 Scout 17B (Groq), Qwen 3 235B (Cerebras), Llama 3.3 70B (Groq), Gemini 2.0 Flash (Google)
Onchain Skills OnchainOS CLI v2.2.9 β€” MCP bridge (JSON-RPC over stdio, 12 skills)
MCP @modelcontextprotocol/sdk 1.29.0 β€” stdio transport
Frontend Next.js 16.2, React 19, Tailwind v4, Framer Motion
Protocols ERC-8021 (Builder Code via ox/erc8021), x402 (HTTP 402 Payment Required)
Confirmations Flashblocks RPC (200ms preconfirmations) + WSS (newHeads)
Testing Vitest β€” 17 unit tests
Deployment Vercel (frontend) Β· Railway (backend) Β· Docker Compose (self-hosted)

Quick Start

Prerequisites

1. Clone & Install

git clone https://github.com/midasbal/PulseTrader.git
cd PulseTrader
npm install

2. Configure Environment

cp .env.example backend/.env

Edit backend/.env with your keys:

GROQ_API_KEY=gsk_...              # Primary LLM (free 100K tokens/day)
CEREBRAS_API_KEY=csk-...          # Fallback LLM (optional)
GEMINI_API_KEY=AIza...            # Second fallback (free, https://aistudio.google.com/apikey)
AGENT_PRIVATE_KEY=0x...           # Throwaway testnet wallet key

3. Deploy Contracts (optional β€” addresses are pre-configured)

cd contracts
npx hardhat run scripts/deploy.ts --network xlayer-testnet

4. Run

npm run dev     # Starts backend (3001) + frontend (3000) concurrently
Service URL
Frontend http://localhost:3000
Docs http://localhost:3000/docs
Backend API http://localhost:3001
WebSocket ws://localhost:3001/ws
Health Check http://localhost:3001/api/health

5. Boost Onchain Activity (optional)

npx tsx scripts/boost-activity.ts

Generates 60+ real swap transactions on X Layer Testnet, all tagged with Builder Code PULSETRDRV1XLYR0.

6. Use as MCP Server (Claude Desktop)

The .mcp.json file is pre-configured:

{
  "mcpServers": {
    "pulsetrader-plus": {
      "command": "npx",
      "args": ["tsx", "backend/src/mcp-server.ts"],
      "cwd": "/path/to/PulseTrader"
    }
  }
}

7. Docker

docker compose up --build

Project Structure

PulseTrader/
β”œβ”€β”€ README.md                    # This file
β”œβ”€β”€ .env.example                 # Environment template (all vars documented)
β”œβ”€β”€ .mcp.json                    # MCP server config for Claude Desktop
β”œβ”€β”€ .gitignore                   # Proper exclusions (.env, node_modules, .next, artifacts)
β”œβ”€β”€ docker-compose.yml           # Production deployment (backend + frontend)
β”œβ”€β”€ package.json                 # Monorepo root (concurrently dev script)
β”‚
β”œβ”€β”€ backend/                     # Express + WS server
β”‚   β”œβ”€β”€ .env                     # Secrets (git-ignored)
β”‚   β”œβ”€β”€ .env.example             # Full env template
β”‚   β”œβ”€β”€ Dockerfile               # Standalone backend container
β”‚   β”œβ”€β”€ package.json             # Dependencies: viem, openai, express, ws, zod, mcp sdk
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   β”œβ”€β”€ vitest.config.ts
β”‚   └── src/
β”‚       β”œβ”€β”€ server.ts            # HTTP + WS server (24 endpoints)
β”‚       β”œβ”€β”€ agent.ts             # AI agent core β€” multi-provider LLM + 18 tool definitions
β”‚       β”œβ”€β”€ chain.ts             # X Layer chain config, viem clients, ERC-8021 dataSuffix
β”‚       β”œβ”€β”€ onchain.ts           # Swap execution engine (burn + mint), live prices
β”‚       β”œβ”€β”€ onchainos-mcp.ts     # OnchainOS MCP bridge (12 skills via JSON-RPC stdio)
β”‚       β”œβ”€β”€ flashblocks.ts       # Flashblocks WebSocket listener (200ms newHeads)
β”‚       β”œβ”€β”€ analytics.ts         # Trade history, fee revenue, economy loop, AI signal
β”‚       β”œβ”€β”€ rebalancer.ts        # Autonomous portfolio rebalancer (55%/60% thresholds)
β”‚       β”œβ”€β”€ x402.ts              # x402 payment gate (6 tiers, onchain receipts)
β”‚       β”œβ”€β”€ mcp-server.ts        # MCP server (14 tools, 3 resources, 3 prompts)
β”‚       β”œβ”€β”€ abi/
β”‚       β”‚   └── erc20.ts         # ERC-20 ABI (mint, burn, transfer, approve, balanceOf)
β”‚       └── __tests__/
β”‚           └── onchain.test.ts  # 17 unit tests (vitest)
β”‚
β”œβ”€β”€ contracts/                   # Hardhat project
β”‚   β”œβ”€β”€ hardhat.config.ts        # X Layer Testnet network config
β”‚   β”œβ”€β”€ contracts/
β”‚   β”‚   β”œβ”€β”€ MockUSDC.sol         # PulseUSDC (PUSDC) β€” 6 decimals, mint() + faucet()
β”‚   β”‚   └── MockWETH.sol         # PulseWETH (PWETH) β€” 18 decimals, mint() + faucet()
β”‚   └── scripts/
β”‚       β”œβ”€β”€ deploy.ts            # Deployment script
β”‚       └── mint-to-agent.ts     # Mint tokens to agent wallet
β”‚
β”œβ”€β”€ frontend/                    # Next.js 16 (App Router, Turbopack)
β”‚   β”œβ”€β”€ Dockerfile               # Standalone frontend container
β”‚   β”œβ”€β”€ package.json
β”‚   └── src/
β”‚       β”œβ”€β”€ app/
β”‚       β”‚   β”œβ”€β”€ globals.css      # Electric Cyan design system + glass morphism
β”‚       β”‚   β”œβ”€β”€ layout.tsx       # Dark theme, OG meta, Geist fonts
β”‚       β”‚   β”œβ”€β”€ page.tsx         # Main app shell (sidebar + chat)
β”‚       β”‚   └── docs/
β”‚       β”‚       └── page.tsx     # Full architecture documentation page
β”‚       β”œβ”€β”€ components/
β”‚       β”‚   β”œβ”€β”€ ChatPanel.tsx    # Chat UI: rich swap cards, thinking timeline, tool traces
β”‚       β”‚   β”œβ”€β”€ Sidebar.tsx      # 3-zone sidebar: identity, economy loop, live TX feed
β”‚       β”‚   └── PulseRingLogo.tsx # Animated SVG logo
β”‚       └── lib/
β”‚           └── api.ts           # Typed API client + WebSocket (auto-reconnect)
β”‚
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ boost-activity.ts        # Generate 60+ Builder Code–tagged swap transactions
β”‚   └── normalize-balances.ts    # Normalize portfolio balances to target allocation
β”‚
└── mcp-server/                  # MCP server standalone (symlink)

Features Deep Dive

🧠 AI Trading Agent (18 Tools)

The agent uses multi-provider LLM failover with 18 tool definitions (OpenAI-compatible function calling):

Tool Description Data Source
get_balances Wallet token balances Onchain (viem)
get_swap_quote Price quote for a swap Internal pricing
execute_swap Execute a token swap Onchain (burn+mint)
create_dca Create DCA schedule Internal scheduler
manage_dca Pause/resume/cancel DCA Internal scheduler
get_agent_info Agent wallet + capabilities Local state
get_market_price Real-time token price OnchainOS market_price
get_batch_prices Batch token prices OnchainOS market_prices
get_dex_quote DEX aggregator quote (500+ sources) OnchainOS swap_quote
search_token Search tokens across 20+ chains OnchainOS token_search
get_portfolio Portfolio balances on any chain OnchainOS portfolio_all_balances
get_gas_price Gas prices for X Layer OnchainOS gateway_gas
get_premium_analytics x402-gated market/pool analytics + AI signal x402 + OnchainOS
get_x402_pricing x402 pricing tiers Local state
get_uniswap_pools Uniswap V3 pool search OnchainOS defi_search
get_token_liquidity Top liquidity pools for a token OnchainOS token_liquidity
get_defi_detail Pool details (APY, TVL, fee rate) OnchainOS defi_detail
get_rebalancer_status Autonomous rebalancer status Local state

Example conversations:

  • "Swap 100 PUSDC to PWETH" β†’ Executes burn+mint swap, confirms in 200ms
  • "Set up a DCA: buy 10 PUSDC worth of PWETH every 60s, 5 times" β†’ Creates automated schedule
  • "Show me Uniswap pools for ETH on Ethereum" β†’ Queries OnchainOS defi_search
  • "What's the AI trading signal?" β†’ Calls x402-gated AI analytics endpoint

πŸ”€ Multi-Provider LLM Failover

The agent automatically fails over between 4 LLM providers when rate limits are hit:

Groq-Scout (primary β€” Llama 4 Scout 17B, 326ms avg response)
  ↓ 429/rate-limit
Cerebras (fallback 1 β€” Qwen 3 235B, free, ultra-fast inference)
  ↓ 429/rate-limit
Groq-70B (fallback 2 β€” Llama 3.3 70B Versatile, highest quality)
  ↓ 429/rate-limit
Gemini (final fallback β€” Gemini 2.0 Flash, free 15 RPM)

All four providers are 100% free β€” no credit card needed. Successful providers are auto-promoted to primary position for subsequent calls. All providers use the OpenAI-compatible SDK for consistent tool calling behavior.

⚑ Flashblocks (200ms Confirmations)

Every swap is confirmed via Flashblocks RPC polling β€” transactions appear in ~200ms instead of the standard 2s block time.

  • RPC: https://testrpc.xlayer.tech/flashblocks
  • WSS: wss://xlayertestws.okx.com (newHeads subscription)
  • Confirmation time is displayed in the chat UI swap cards

🏷️ Builder Code (ERC-8021)

Every transaction includes PULSETRDRV1XLYR0 as a data suffix via ox/erc8021:

// chain.ts
import { dataSuffix } from "ox/erc8021";
const builderCode = dataSuffix({ builderCode: "PULSETRDRV1XLYR0" });
// Appended to every transaction automatically via viem's dataSuffix option

Verify: Check any agent transaction's input data on OKLink Explorer β€” the last bytes contain the encoded builder code.

πŸ”Œ OnchainOS Integration (12 Skills)

PulseTrader+ bridges to OnchainOS via a child process MCP client (JSON-RPC over stdio):

Agent Tool β†’ onchainos-mcp.ts β†’ spawn("onchainos mcp") β†’ JSON-RPC stdio β†’ OnchainOS API
# OnchainOS Skill Agent Tool Category
1 market_price get_market_price DEX Market
2 market_prices get_batch_prices DEX Market
3 swap_quote get_dex_quote DEX Swap
4 swap_chains (internal) DEX Swap
5 swap_liquidity (internal) DEX Swap
6 token_search search_token DEX Token
7 portfolio_all_balances get_portfolio Wallet
8 portfolio_total_value (internal) Wallet
9 gateway_gas get_gas_price Gateway
10 defi_search get_uniswap_pools DeFi / Uniswap
11 token_liquidity get_token_liquidity DeFi / Uniswap
12 defi_detail get_defi_detail DeFi / Uniswap

Health check: GET /api/onchainos/status β€” makes a live market_price call to verify the bridge is operational.

πŸ’° x402 Payment Protocol (6 Gated Endpoints)

Premium analytics are gated by the x402 micropayment standard (HTTP 402 Payment Required):

Endpoint Price Description
/api/analytics/market-overview 0.50 PUSDC Token prices + gas data from OnchainOS
/api/analytics/pool-stats 1.00 PUSDC Uniswap pool TVL, volume, APY
/api/analytics/trade-history 0.25 PUSDC Recent agent trades with P&L
/api/analytics/economy-loop 0.50 PUSDC Revenue dashboard: fees, sweeps, reinvestments
/api/analytics/ai-signal 2.00 PUSDC AI-generated trading signal (LLM reasoning)
/api/analytics/full-analytics 2.50 PUSDC All 5 analytics in one response

Payment modes:

  • demo: Creates a real onchain PUSDC self-transfer as a payment receipt β†’ generates a verifiable X Layer transaction
  • onchain: Verifies a user-provided txHash for PUSDC transfer to the agent wallet

Every payment generates a session token valid for 1 hour.

πŸ”§ MCP Server (14 Tools + 3 Resources + 3 Prompts)

PulseTrader+ exposes itself as a fully-compliant MCP server for agent-to-agent composability:

Tools (14):

Tool Description
pulsetrader_swap Execute token swap on X Layer
pulsetrader_quote Get swap price quote
pulsetrader_balance Check wallet balances
pulsetrader_dca_create Create DCA schedule
pulsetrader_dca_status Manage DCA schedules
pulsetrader_market_price Real-time token price (OnchainOS)
pulsetrader_search_token Search tokens across chains
pulsetrader_agent_info Agent info + capabilities
pulsetrader_uniswap_pools Search Uniswap V3 pools (OnchainOS)
pulsetrader_token_liquidity Token liquidity pools (OnchainOS)
pulsetrader_defi_detail Pool details: APY, TVL, fee rate (OnchainOS)
pulsetrader_x402_analytics Premium analytics (auto-pay x402)
pulsetrader_x402_pricing x402 pricing tiers
pulsetrader_ai_signal AI trading signal (x402-gated, LLM reasoning)

Resources (3):

URI Description
pulsetrader://portfolio Live portfolio balances (JSON)
pulsetrader://economy-loop Economy loop stats + treasury (JSON)
pulsetrader://config Agent config, contracts, capabilities (JSON)

Prompts (3):

Prompt Description
trade_strategy Guided workflow: analyze portfolio β†’ research token β†’ suggest strategy
portfolio_review Comprehensive review: balances, prices, allocation vs targets
economy_deep_dive Full economy loop dashboard: fees, sweeps, reinvestments, x402 revenue

πŸ€– Autonomous Behaviors

PulseTrader+ runs 4 autonomous loops that operate WITHOUT any user prompting:

Loop Interval Behavior
Portfolio Rebalancer 60s check, 120s cooldown Sells PWETH if >55% of portfolio; buys PWETH if PUSDC >60%
Fee Treasury Sweep On trade events When fees reach 2 PUSDC, sweeps 50% to treasury (burn address)
DCA Reinvest On sweep events When treasury reaches 5 PUSDC, auto-creates DCA buy of PWETH
Flashblocks Listener Continuous (WSS) Subscribes to newHeads for real-time block events

All autonomous trades use the same swap engine β†’ pay the 0.1% fee β†’ tag with Builder Code β†’ feed the economy loop.

πŸ”„ Economy Loop

The agent's self-sustaining economy:

User chats β†’ Swap executes β†’ 0.1% fee collected
                                    ↓
                           Fee Treasury accumulates
                                    ↓
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    ↓                               ↓
            Treasury Sweep                   x402 Payment Revenue
            (burn 50% of fees)               (sell premium data)
                    ↓                               ↓
            Auto-DCA Reinvest                Agent balance grows
            (buy PWETH with surplus)
                    ↓
            Portfolio rebalances automatically
                    ↓
            Cycle repeats ∞

API Reference

Core Endpoints (8)

Method Path Description
GET / Root proof-of-life (service name, chain, uptime)
GET /api/health Health check (agent, flashblocks, onchainos, rebalancer)
POST /api/chat Chat with AI agent (18 tools, reasoning traces)
GET /api/agent Agent wallet info + capabilities
GET /api/balances Token balances (PUSDC, PWETH, OKB)
GET /api/quote Swap price quote
POST /api/swap Execute swap (burn+mint, 200ms confirm)
POST /api/reset Reset chat session

Market Data Endpoints (4)

Method Path Description
GET /api/prices Live token prices
GET /api/trades Recent trade history
GET /api/rebalancer/status Autonomous rebalancer state
GET /api/treasury/status Fee treasury status

OnchainOS Endpoints (3)

Method Path Description
GET /api/onchainos/status OnchainOS bridge health check
GET /api/onchainos/uniswap-pools?query=ETH Search Uniswap pools
GET /api/onchainos/token-liquidity?address=0x... Token liquidity data

x402 Endpoints (3 + 6 gated)

Method Path Description
GET /api/x402/pricing Pricing info for all tiers (free)
GET /api/x402/status Active payment sessions
POST /api/x402/pay Create payment session (demo or onchain)
GET /api/analytics/market-overview πŸ”’ 0.50 PUSDC β€” Token prices + gas
GET /api/analytics/pool-stats πŸ”’ 1.00 PUSDC β€” Pool TVL, volume, APY
GET /api/analytics/trade-history πŸ”’ 0.25 PUSDC β€” Agent trade history
GET /api/analytics/economy-loop πŸ”’ 0.50 PUSDC β€” Revenue dashboard
GET /api/analytics/ai-signal πŸ”’ 2.00 PUSDC β€” AI trading signal
GET /api/analytics/full-analytics πŸ”’ 2.50 PUSDC β€” All analytics combined

WebSocket

Connect to ws://localhost:3001/ws for real-time events:

  • ⚑ Flashblock newHeads (every 200ms)
  • πŸ“Š DCA execution events
  • πŸ”„ Rebalancer trade events
  • πŸ’“ Heartbeat (every 30s)

Smart Contracts

PulseUSDC (PUSDC)

Property Value
Address 0x9eb8679A851A383D1E2678c29ed92FbB85c72c0E
Decimals 6
Standard ERC-20 (OpenZeppelin)
Features Public mint(address, uint256), faucet() (1000 PUSDC per call)

PulseWETH (PWETH)

Property Value
Address 0x3717C06A65CEd56A99e8ffef1c65a9193e991411
Decimals 18
Standard ERC-20 (OpenZeppelin)
Features Public mint(address, uint256), faucet() (1 PWETH per call)

Swap Mechanism (Burn + Mint)

User: "Swap 100 PUSDC to PWETH"

1. BURN  β†’  Transfer 100 PUSDC to 0x000...dEaD (burn address)
             └── Flashblocks confirmation (~200ms)
             └── Builder Code: PULSETRDRV1XLYR0

2. PRICE β†’  Calculate output: 100 PUSDC Γ· $2,500/ETH = 0.04 PWETH
             └── 0.1% fee deducted: 0.03996 PWETH to user, 0.00004 PWETH as fee

3. MINT  β†’  Mint 0.03996 PWETH to agent wallet
             └── Flashblocks confirmation (~200ms)
             └── Builder Code: PULSETRDRV1XLYR0

Agent Wallet & Builder Code

Property Value
Agent Address 0x5433B389d9C64f84aa01Dfc4488594F3A72eA1e6
Builder Code PULSETRDRV1XLYR0 (16-char ERC-8021)
Wallet Type Throwaway testnet wallet (agentic key)
Transaction Count 129+ (all Builder Code–tagged)

Docker Deployment

docker compose up --build
Container Port Description
pulsetrader-backend 3001 Express API + WebSocket + MCP bridge
pulsetrader-frontend 3000 Next.js standalone (Turbopack build)

Both containers use multi-stage builds for minimal image size. The frontend uses output: "standalone" for serverless-ready deployment.

Live Deployment

Service Platform URL
Frontend Vercel pulse-traderplus.vercel.app
Backend Railway pulsetrader-production-3d28.up.railway.app
Explorer OKLink X Layer Testnet Explorer

Testing

cd backend && npx vitest run

17/17 tests passing β€” covering:

  • Token registry resolution (symbol β†’ address β†’ decimals)
  • Price calculation engine (PUSDC ↔ PWETH at live rates)
  • Fee computation (0.1% = 10 bps, with rounding verification)
  • Swap quote generation (input/output/fee breakdown)
  • Edge cases (zero amounts, unknown tokens, max precision)

Team

Role Builder
Solo Builder @midasbal

License

MIT


Built for the OKX X Layer Hackathon β€” X Layer Arena Track

~9,000 lines of TypeScript + Solidity Β· 18 agent tools Β· 12 OnchainOS skills Β· 14 MCP tools Β· 6 x402 endpoints Β· 4 LLM providers Β· 129+ onchain transactions

About

The ultimate autonomous trading terminal for X Layer. Powered by 200ms Flashblocks, multi-provider LLM failover (Groq/Cerebras/Gemini), and a self-balancing agentic economy loop.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages