Swap.io Agent API + HTTP 402 Micropayments

DriftGuard: Zero-Subscription Solana Treasury Rebalancer

DAO & creator treasuries check portfolio drift 8,640 times a month—but only actually rebalance 3 to 6 times. With Swap.io Hermes + x402, DriftGuard monitors weights via free RPC reads and pays 0.003007 USDC (< $0.01) per quote & transaction build only when drift crosses threshold—zero API keys, zero monthly SaaS burn, and gasless Solana USDC fee sponsorship.

1. Treasury Economics Simulator

Compare traditional $99.00/mo aggregator API plans against Swap.io x402 pay-per-execution (0.003007 USDC quote + 0.003007 USDC build-tx, < $0.01 total).

$250,000.00
5.0%
4 trades / mo
Legacy API Plan
$99.00
Swap.io x402 Cost
$0.02
Monthly Savings
99.98%

2. Live Hermes HTTP 402 Packet Inspector

Probe https://hermes.swap.io/v1/swap/quote live or step through the 4-stage x402 rebalance lifecycle.

Ready — showing verified live HTTP 402 challenge from hermes.swap.io

      

3. Autonomous DriftGuard Loop (x402-solana + hermes.swap.io)

Zero API keys stored in environment variables. Every paid call returns an on-chain X-PAYMENT-RESPONSE settlement hash verifiable on Solscan.

import { createX402Client } from 'x402-solana/client';
import { Connection, Keypair, PublicKey, VersionedTransaction } from '@solana/web3.js';

const SOL_MINT  = 'So11111111111111111111111111111111111111112';
const USDC_MINT = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';

export async function rebalanceIfDrifted(rpc: Connection, treasury: Keypair, driftBps: number) {
  // 1. Free check: 99.95% of 5-minute ticks cost $0.00 and zero API quota
  if (Math.abs(driftBps) < 500) return { action: 'HOLD', apiSpentUsdc: 0 };

  // 2. Initialize keyless x402 client (Swap.io feePayer sponsors payment gas!)
  const x402 = createX402Client({
    network: 'solana',
    wallet: {
      address: treasury.publicKey.toBase58(),
      signTransaction: async (tx) => { tx.sign([treasury]); return tx; },
    },
  });

  // 3. Paid Call #1 (0.003007 USDC, < $0.01): 402 Challenge -> USDC Micropayment -> Ranked Quotes
  const quoteRes = await x402.fetch('https://hermes.swap.io/v1/swap/quote', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      input:  { token: SOL_MINT, amount: '10000000000', chainKey: 'sol' },
      output: { token: USDC_MINT, chainKey: 'sol' },
      userAddress: treasury.publicKey.toBase58(),
      slippageBps: 50,
    }),
  });
  const paymentReceipt1 = quoteRes.headers.get('X-PAYMENT-RESPONSE');
  const { quotes } = await quoteRes.json();
  const best = quotes[0];

  // 4. Paid Call #2 (0.003007 USDC, < $0.01): Build unsigned VersionedTransaction
  const buildRes = await x402.fetch('https://hermes.swap.io/v1/swap/build-tx', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      provider: best.provider.key,
      userAddress: treasury.publicKey.toBase58(),
      chainKey: 'sol',
      quoteResponse: best.providerQuote,
    }),
  });
  const { transaction } = await buildRes.json();
  const vtx = VersionedTransaction.deserialize(Buffer.from(transaction, 'base64'));
  vtx.sign([treasury]);
  const sig = await rpc.sendRawTransaction(vtx.serialize());
  return { action: 'REBALANCED', swapSig: sig, paymentReceipt1, apiSpentUsdc: 0.006014 };
}