Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 230 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ENV } from "./config/env.js";
import { handleMonitor } from "./handlers/monitor.js";
import { handleGetHistory } from "./handlers/history.js";
import { handleEcho } from "./handlers/echo.js";
import { handleUniversalMonitor, universalMonitorSchema } from "./handlers/universal-monitor.js";
import { blockchainProvider } from "./providers/blockchain.js";
import { z } from "zod";

Expand All @@ -23,10 +24,21 @@ export const { app, addEntrypoint } = createAgentApp({
// Initialize blockchain provider for both local and production
const initializeBlockchain = async () => {
try {
await blockchainProvider.initialize();
console.log(`✅ Blockchain provider initialized successfully`);
// Initialize all supported networks for Universal Monitor
const supportedNetworks = ["base", "ethereum", "polygon", "arbitrum", "optimism"];
console.log(`🌐 Initializing ${supportedNetworks.length} networks for Universal Monitor...`);

for (const network of supportedNetworks) {
try {
await blockchainProvider.initialize(network as any);
} catch (error) {
console.warn(`⚠️ Failed to initialize ${network}:`, error instanceof Error ? error.message : error);
}
}

console.log(`🚀 Universal DeFi Monitor ready across ${supportedNetworks.length} networks`);
} catch (error) {
console.error(`❌ Failed to initialize blockchain provider:`, error);
console.error(`❌ Failed to initialize blockchain providers:`, error);
}
};

Expand Down Expand Up @@ -83,6 +95,15 @@ addEntrypoint({
handler: handleEcho
});

// NEW: Universal DeFi Pool Monitor - Premium endpoint
addEntrypoint({
key: "universal_monitor",
description: "🚀 Universal DeFi Pool Monitor - Track APY, TVL, and yields across ALL major protocols (Aave V3, Compound V3) on multiple networks (Ethereum, Base, Polygon, Arbitrum, Optimism) with real-time alerts, historical data, and comprehensive analytics. Perfect for DeFi applications and yield farming strategies.",
price: "0.01", // Premium pricing for comprehensive multi-network data
input: universalMonitorSchema,
handler: handleUniversalMonitor
});

// Add x402scan-compatible endpoint for get_history with proper schema
app.post('/x402/get_history', async (c) => {
// Check for x402 payment header
Expand Down Expand Up @@ -291,6 +312,94 @@ app.post('/x402/echo', async (c) => {
return c.json(result.output);
} catch (error) {
return c.json({ error: "Internal server error" }, 500);
}
});

// Add x402scan-compatible endpoint for universal_monitor with proper schema
app.post('/x402/universal_monitor', async (c) => {
const paymentHeader = c.req.header('X-PAYMENT');

if (!paymentHeader) {
return c.json({
x402Version: 1,
error: "X-PAYMENT header is required",
accepts: [{
scheme: "exact",
network: "base",
maxAmountRequired: "10000", // Higher amount for premium endpoint
resource: "https://yield-pool-watcher.vercel.app/x402/universal_monitor",
description: "🚀 Universal DeFi Pool Monitor - Track APY, TVL, and yields across ALL major protocols (Aave V3, Compound V3) on multiple networks (Ethereum, Base, Polygon, Arbitrum, Optimism) with real-time alerts, historical data, and comprehensive analytics. Perfect for DeFi applications and yield farming strategies.",
mimeType: "application/json",
payTo: "0x7e296A887F7Bd9827D911f01D61ACe27DE542F87",
maxTimeoutSeconds: 300,
asset: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
outputSchema: {
input: {
type: "http",
method: "POST",
bodyType: "json",
bodyFields: {
protocols: {
type: "array",
required: false,
description: "DeFi protocols to monitor (aave_v3, compound_v3)"
},
networks: {
type: "array",
required: false,
description: "Blockchain networks to monitor (ethereum, base, polygon, arbitrum, optimism)"
},
assets: {
type: "array",
required: false,
description: "Specific asset addresses to monitor (leave empty for default assets)"
},
include_historical: {
type: "boolean",
required: false,
description: "Include historical data in response"
},
include_predictions: {
type: "boolean",
required: false,
description: "Include yield predictions (future feature)"
},
threshold_rules: {
type: "object",
required: false,
description: "Alert threshold configuration with apy_spike_percent, apy_drop_percent, tvl_drain_percent, tvl_surge_percent"
}
}
}
},
extra: {
name: "USD Coin",
version: "2"
}
}],
payer: "0x7e296A887F7Bd9827D911f01D61ACe27DE542F87"
}, 402);
}

try {
let body = {};
try {
body = await c.req.json() || {};
} catch (e) {
// Handle case where no JSON body is provided
body = {};
}

const { handleUniversalMonitor } = await import("./handlers/universal-monitor.js");

const result = await handleUniversalMonitor({
input: body
});

return c.json(result.output);
} catch (error) {
console.error("Universal Monitor x402 error:", error);
return c.json({ error: "Internal server error" }, 500);
}
});

Expand Down Expand Up @@ -376,7 +485,7 @@ app.get('/schema', (c) => {
description: "Health check endpoint that echoes input text and provides system status including RPC connectivity, current block number, monitored pools count, and server uptime.",
method: "POST",
endpoint: "/entrypoints/echo/invoke",
price: "0.005",
price: "0.001",
input_schema: {
type: "object",
properties: {
Expand All @@ -388,7 +497,64 @@ app.get('/schema', (c) => {
description: "Text to echo back"
}
},
required: ["text"]
required: []
}
},
required: ["input"]
}
},
universal_monitor: {
description: "🚀 Universal DeFi Pool Monitor - Track APY, TVL, and yields across ALL major protocols (Aave V3, Compound V3) on multiple networks (Ethereum, Base, Polygon, Arbitrum, Optimism) with real-time alerts, historical data, and comprehensive analytics. Perfect for DeFi applications and yield farming strategies.",
method: "POST",
endpoint: "/entrypoints/universal_monitor/invoke",
price: "0.01", // Premium pricing
input_schema: {
type: "object",
properties: {
input: {
type: "object",
properties: {
protocols: {
type: "array",
items: { enum: ["aave_v3", "compound_v3"] },
default: ["aave_v3", "compound_v3"],
description: "DeFi protocols to monitor"
},
networks: {
type: "array",
items: { type: "string" },
default: ["base", "ethereum"],
description: "Blockchain networks to monitor (ethereum, base, polygon, arbitrum, optimism)"
},
assets: {
type: "array",
items: { type: "string" },
default: [],
description: "Specific asset addresses to monitor (leave empty for default assets)"
},
include_historical: {
type: "boolean",
default: false,
description: "Include historical data in response"
},
include_predictions: {
type: "boolean",
default: false,
description: "Include yield predictions (future feature)"
},
threshold_rules: {
type: "object",
properties: {
apy_spike_percent: { type: "number", description: "Alert if APY increases by this percentage" },
apy_drop_percent: { type: "number", description: "Alert if APY decreases by this percentage" },
tvl_drain_percent: { type: "number", description: "Alert if TVL decreases by this percentage" },
tvl_surge_percent: { type: "number", description: "Alert if TVL increases by this percentage" }
},
default: {},
description: "Alert threshold configuration"
}
},
required: []
}
},
required: ["input"]
Expand Down Expand Up @@ -483,7 +649,65 @@ app.get('/openapi.json', (c) => {
properties: {
text: { type: "string", description: "Text to echo back" }
},
required: ["text"]
required: []
}
},
required: ["input"]
}
}
}
}
}
},
"/entrypoints/universal_monitor/invoke": {
post: {
summary: "Universal DeFi Pool Monitor",
description: "🚀 Universal DeFi Pool Monitor - Track APY, TVL, and yields across ALL major protocols (Aave V3, Compound V3) on multiple networks (Ethereum, Base, Polygon, Arbitrum, Optimism) with real-time alerts, historical data, and comprehensive analytics. Perfect for DeFi applications and yield farming strategies.",
requestBody: {
required: true,
content: {
"application/json": {
schema: {
type: "object",
properties: {
input: {
type: "object",
properties: {
protocols: {
type: "array",
items: { enum: ["aave_v3", "compound_v3"] },
default: ["aave_v3", "compound_v3"],
description: "DeFi protocols to monitor"
},
networks: {
type: "array",
items: { type: "string" },
default: ["base", "ethereum"],
description: "Blockchain networks to monitor"
},
assets: {
type: "array",
items: { type: "string" },
default: [],
description: "Specific asset addresses to monitor"
},
include_historical: {
type: "boolean",
default: false,
description: "Include historical data"
},
include_predictions: {
type: "boolean",
default: false,
description: "Include yield predictions"
},
threshold_rules: {
type: "object",
default: {},
description: "Alert threshold configuration"
}
},
required: []
}
},
required: ["input"]
Expand Down
10 changes: 5 additions & 5 deletions src/config/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const NETWORKS: Record<NetworkId, NetworkConfig> = {
"ethereum": {
name: "Ethereum Mainnet",
chainId: 1,
rpcUrl: process.env.RPC_URL || process.env.ETHEREUM_RPC_URL || `https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
rpcUrl: process.env.ETHEREUM_RPC_URL || process.env.RPC_URL || `https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
nativeCurrency: "ETH",
explorer: "https://etherscan.io"
},
Expand All @@ -37,7 +37,7 @@ export const NETWORKS: Record<NetworkId, NetworkConfig> = {
"base": {
name: "Base Mainnet",
chainId: 8453,
rpcUrl: process.env.RPC_URL || process.env.BASE_RPC_URL || `https://base-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
rpcUrl: process.env.BASE_RPC_URL || process.env.RPC_URL || `https://base-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
nativeCurrency: "ETH",
explorer: "https://basescan.org"
},
Expand All @@ -53,7 +53,7 @@ export const NETWORKS: Record<NetworkId, NetworkConfig> = {
"optimism": {
name: "OP Mainnet",
chainId: 10,
rpcUrl: process.env.RPC_URL || process.env.OPTIMISM_RPC_URL || `https://opt-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
rpcUrl: process.env.OPTIMISM_RPC_URL || process.env.RPC_URL || `https://opt-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
nativeCurrency: "ETH",
explorer: "https://optimistic.etherscan.io"
},
Expand All @@ -69,7 +69,7 @@ export const NETWORKS: Record<NetworkId, NetworkConfig> = {
"polygon": {
name: "Polygon Mainnet",
chainId: 137,
rpcUrl: process.env.RPC_URL || process.env.POLYGON_RPC_URL || `https://polygon-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
rpcUrl: process.env.POLYGON_RPC_URL || process.env.RPC_URL || `https://polygon-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
nativeCurrency: "MATIC",
explorer: "https://polygonscan.com"
},
Expand All @@ -85,7 +85,7 @@ export const NETWORKS: Record<NetworkId, NetworkConfig> = {
"arbitrum": {
name: "Arbitrum One",
chainId: 42161,
rpcUrl: process.env.RPC_URL || process.env.ARBITRUM_RPC_URL || `https://arb-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
rpcUrl: process.env.ARBITRUM_RPC_URL || process.env.RPC_URL || `https://arb-mainnet.g.alchemy.com/v2/YOUR_API_KEY`,
nativeCurrency: "ETH",
explorer: "https://arbiscan.io"
},
Expand Down
Loading