AI Service API

AI-powered transaction analysis, risk assessment, and wallet suggestions with TEE-secured inference. Provides chat completions, embeddings, and specialized blockchain analytics.

Service Architecture

ProtocolPortDescription
gRPC9099Internal API (Tonic/Prost)
HTTP8089REST API and health checks

Authentication

All endpoints require multi-tenant authentication:

X-Tenant-Id: your-tenant-id
X-API-Key: your-api-key

REST Endpoints

Health & Metrics

MethodPathDescription
GET/healthHealth check
GET/healthzLiveness probe
GET/readyzReadiness probe (checks model availability)
GET/metricsPrometheus metrics with GPU stats

Transaction Analysis

MethodPathDescription
POST/api/ai/analyze/transactionAnalyze transaction for risk
POST/api/ai/analyze/bridgeAnalyze cross-chain bridge operations
POST/api/ai/check/addressCheck if address is potentially malicious

Chat & Suggestions

MethodPathDescription
POST/api/ai/chatChat with AI about wallet operations
POST/api/ai/suggestionsGet AI-powered wallet suggestions
GET/api/ai/gas/:chainGet gas optimization recommendations

Model Inference

MethodPathDescription
POST/api/ai/inferRun text completion inference
POST/api/ai/embedGenerate embeddings
GET/api/ai/modelsList available models
GET/api/ai/models/:idGet specific model information

Analyze Transaction

POST /api/ai/analyze/transaction
X-Tenant-Id: your-tenant-id
Content-Type: application/json

{
  "txHash": "0xa1b2c3d4...",
  "chain": "ethereum",
  "from": "0x742d35Cc6634C0532925a3b844Bc9e7595f3...",
  "to": "0x123456789abcdef...",
  "value": "1000000000000000000",
  "data": "0x..."
}

Response

{
  "data": {
    "riskScore": 0.15,
    "riskLevel": "low",
    "flags": [],
    "analysis": "Transaction appears to be a standard ERC-20 transfer...",
    "recommendations": []
  }
}

Check Address

POST /api/ai/check/address
X-Tenant-Id: your-tenant-id
Content-Type: application/json

{
  "address": "0x742d35Cc6634C0532925a3b844Bc9e7595f3...",
  "chain": "ethereum"
}

Response

{
  "data": {
    "address": "0x742d35Cc6634C0532925a3b844Bc9e7595f3...",
    "isMalicious": false,
    "confidence": 0.95,
    "labels": [],
    "lastChecked": "2024-01-15T10:30:00Z"
  }
}

Chat

POST /api/ai/chat
X-Tenant-Id: your-tenant-id
Content-Type: application/json

{
  "messages": [
    {
      "role": "user",
      "content": "What's the best way to bridge USDC from Ethereum to Base?"
    }
  ],
  "context": {
    "walletAddress": "0x...",
    "chain": "ethereum"
  }
}

Response

{
  "data": {
    "response": "To bridge USDC from Ethereum to Base, I recommend...",
    "suggestions": [
      {
        "action": "bridge",
        "params": { "fromChain": "ethereum", "toChain": "base", "asset": "USDC" }
      }
    ]
  }
}

Run Inference

POST /api/ai/infer
X-Tenant-Id: your-tenant-id
Content-Type: application/json

{
  "model": "gpt-4",
  "messages": [
    {
      "role": "system",
      "content": "You are a blockchain transaction analyst."
    },
    {
      "role": "user",
      "content": "Analyze this transaction for potential risks."
    }
  ],
  "temperature": 0.7,
  "maxTokens": 1000
}

Response

{
  "data": {
    "id": "inf_abc123",
    "model": "gpt-4",
    "content": "Based on my analysis...",
    "usage": {
      "promptTokens": 150,
      "completionTokens": 250,
      "totalTokens": 400
    },
    "finishReason": "stop"
  }
}

Generate Embeddings

POST /api/ai/embed
X-Tenant-Id: your-tenant-id
Content-Type: application/json

{
  "model": "text-embedding-3-small",
  "input": [
    "Transaction from wallet 0x742d to 0x123f",
    "Cross-chain bridge transfer USDC to Polygon"
  ],
  "dimensions": 512
}

Response

{
  "data": {
    "embeddings": [
      {
        "index": 0,
        "embedding": [0.0023, -0.0451, 0.0321, ...]
      },
      {
        "index": 1,
        "embedding": [0.0145, -0.0234, 0.0567, ...]
      }
    ],
    "model": "text-embedding-3-small",
    "usage": {
      "promptTokens": 25,
      "totalTokens": 25
    }
  }
}

Get Gas Recommendations

GET /api/ai/gas/ethereum
X-Tenant-Id: your-tenant-id

Response

{
  "data": {
    "chain": "ethereum",
    "recommendations": {
      "slow": { "maxFeePerGas": "20000000000", "estimatedTime": "5m" },
      "standard": { "maxFeePerGas": "30000000000", "estimatedTime": "2m" },
      "fast": { "maxFeePerGas": "50000000000", "estimatedTime": "30s" }
    },
    "insight": "Gas prices are elevated due to high network activity"
  }
}

List Models

GET /api/ai/models
X-Tenant-Id: your-tenant-id

Response

{
  "data": [
    {
      "id": "gpt-4",
      "name": "GPT-4",
      "type": "chat",
      "provider": "openai",
      "contextWindow": 8192,
      "available": true
    },
    {
      "id": "text-embedding-3-small",
      "name": "Text Embedding 3 Small",
      "type": "embedding",
      "provider": "openai",
      "dimensions": 1536,
      "available": true
    }
  ]
}

Risk Levels

LevelScore RangeDescription
low0.0 - 0.3Transaction appears safe
medium0.3 - 0.6Review recommended
high0.6 - 0.8Proceed with caution
critical0.8 - 1.0High risk, not recommended

Error Codes

CodeDescription
MODEL_NOT_FOUNDRequested model does not exist
CONTEXT_LENGTH_EXCEEDEDInput exceeds model context window
RATE_LIMIT_EXCEEDEDToo many requests
INVALID_CHAINChain not supported