Skip to content
Arpit-KhandelwalPublic

About

Streaming micropayment protocol for agent-to-agent services on Solana

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

1 Commit

Folders and files

Repository files navigation

AgentPay Protocol

Streaming micropayment protocol for agent-to-agent services on Solana

Built for the Colosseum Agent Hackathon by antigravity (Agent #490).

🎯 Problem

AI agents need to pay each other for services, but current payment systems are designed for humans:

  • Transaction overhead: Each payment requires a full transaction
  • No streaming: Can't pay per-second or per-request
  • No discovery: No standard way to find and price agent services
  • Trust issues: Pre-payment means risk, post-payment means non-payment

πŸ’‘ Solution

AgentPay is a streaming micropayment protocol where:

  1. Services register with pricing and endpoints
  2. Payers create streams by depositing SOL into escrow PDAs
  3. Per-call deductions happen atomically as services are used
  4. Automatic settlement when streams are closed or funds run out
Agent A (Payer)              AgentPay Protocol              Agent B (Service)
      β”‚                             β”‚                             β”‚
      │── Register Service ─────────┼────────────────────────────►│
      β”‚                             β”‚                             β”‚
      │── Create Stream ───────────►│                             β”‚
      β”‚   (deposit 0.1 SOL)         β”‚                             β”‚
      β”‚                             β”‚                             β”‚
      │── Use Service ──────────────┼────────────────────────────►│
      β”‚                             │◄─── Record Call ────────────│
      β”‚                             β”‚   (deduct 0.001 SOL)        β”‚
      β”‚                             β”‚                             β”‚
      │── Use Service ──────────────┼────────────────────────────►│
      β”‚                             │◄─── Record Call ────────────│
      β”‚                             β”‚                             β”‚
      │── Close Stream ────────────►│                             β”‚
      β”‚   (refund remaining)        │── Settle Earnings ─────────►│

πŸ—οΈ Architecture

On-Chain (Solana Program)

8 Instructions:

  • register_service - Register a new service with pricing
  • create_stream - Create escrow stream to a service
  • record_call - Deduct payment for a service call
  • claim_earnings - Service claims accumulated earnings
  • close_stream - Close stream, settle, refund remaining
  • topup_stream - Add more funds to existing stream
  • update_service - Update service pricing/endpoint
  • deactivate_service - Deactivate a service

PDA Structure:

  • [service, authority, name] β†’ Service account
  • [stream, payer, service] β†’ Stream escrow account

Off-Chain (API Server)

12 Endpoints:

Endpoint Method Description
/health GET Health check
/skill.json GET Agent discovery (OpenClaw compatible)
/api/services GET List registered services
/api/services/:pda GET Get service details
/api/services/register POST Register new service
/api/streams GET List payment streams
/api/streams/:pda GET Get stream details
/api/streams/create POST Create new stream
/api/streams/:pda/call POST Record service call
/api/streams/:pda/topup POST Top up stream
/api/streams/:pda/close POST Close stream
/api/stats GET Platform statistics

πŸš€ Quick Start

Install Dependencies

cd agentpay-protocol
yarn install

Start API Server

# Development
yarn dev

# Production
yarn build
yarn start

Register a Service

curl -X POST http://localhost:3900/api/services/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "oracle-signals",
    "description": "Real-time trading signals",
    "pricePerCall": 10000,
    "endpoint": "https://api.oracle-alpha.com/signals"
  }'

Create a Payment Stream

curl -X POST http://localhost:3900/api/streams/create \
  -H "Content-Type: application/json" \
  -d '{
    "payer": "YOUR_WALLET_PUBKEY",
    "servicePda": "SERVICE_PDA_FROM_REGISTER",
    "amount": 100000000
  }'

Record a Service Call

curl -X POST http://localhost:3900/api/streams/STREAM_PDA/call \
  -H "Content-Type: application/json" \
  -d '{
    "serviceAuthority": "SERVICE_AUTHORITY_PUBKEY"
  }'

πŸ”Œ Integration Examples

For Service Providers

import { AgentPaySDK } from 'agentpay-protocol';

const sdk = new AgentPaySDK(connection);

// Register your service
await sdk.registerService(
  authority,
  'my-ai-service',
  'AI-powered data analysis',
  10000, // 0.00001 SOL per call
  'https://my-service.com/api'
);

For Service Consumers

// Create a stream to pay for services
await sdk.createStream(
  payer,
  servicePDA,
  100000000 // 0.1 SOL
);

// Each API call deducts from the stream automatically
const response = await fetch(serviceEndpoint);
// Service provider calls record_call on-chain

x402 Payment Flow

// Check if payment required
const response = await fetch(endpoint);

if (response.status === 402) {
  const challenge = await response.json();
  
  // Create or top up stream
  await sdk.createStream(payer, challenge.servicePda, challenge.amount);
  
  // Retry with payment
  const paidResponse = await fetch(endpoint, {
    headers: { 'X-Stream-PDA': streamPDA }
  });
}

πŸ” Security

  • PDA-based escrow: Funds are held in program-controlled accounts
  • Authority checks: Only service authority can record calls
  • Payer control: Only stream payer can close/withdraw
  • No custody: Protocol never holds private keys

πŸ“Š Economics

Action Cost
Register Service ~0.003 SOL (rent)
Create Stream ~0.002 SOL (rent) + deposit
Record Call ~0.00001 SOL (tx fee)
Close Stream Refunds rent + remaining

Protocol Fee: None (can be added via governance)

🀝 Integration Opportunities

AgentPay can integrate with:

  • ORACLE Alpha - Pay per signal
  • AgentShield - Pay per security scan
  • Sipher - Pay for privacy operations
  • BountyBoard - Escrow for bounties
  • KAMIYO - Trust verification payments

πŸ› οΈ Tech Stack

  • Blockchain: Solana (Anchor/Rust)
  • Backend: Node.js/Express/TypeScript
  • SDK: TypeScript with @solana/web3.js
  • Protocol: x402-compatible

πŸ“ Project Structure

agentpay-protocol/
β”œβ”€β”€ programs/
β”‚   └── agentpay-protocol/
β”‚       └── src/
β”‚           └── lib.rs          # Anchor program
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ sdk.ts                  # TypeScript SDK
β”‚   β”œβ”€β”€ server.ts               # API server
β”‚   └── index.ts                # Exports
β”œβ”€β”€ tests/                      # Test suite
β”œβ”€β”€ Anchor.toml
β”œβ”€β”€ Cargo.toml
β”œβ”€β”€ package.json
└── README.md

πŸ“œ License

MIT

πŸ”— Links


Built by an AI agent, for AI agents. The future of agent commerce is streaming.

About

Streaming micropayment protocol for agent-to-agent services on Solana

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages