Cross-chain payment infrastructure that enables AI agents to autonomously pay using PYUSD
🔄 Complete Payment Flow
Phase 1: Service Discovery & Registration
// Service provider registers their API
mcpGateway.registerMCP(
"gpt-vision-api.com", // Service endpoint
1000000, // Price: 1 USDC per call
["USDC", "USDT"], // Accepted tokens
[30184, 30110] // Base & Arbitrum chains
);
Phase 2: Agent Payment Request
// Agent initiates cross-chain payment
const paymentRequest = await x402Router.requestPayment({
serviceId: "vision-api-123",
amount: "1000000", // 1 PYUSD
requestedToken: "USDC", // Service wants USDC
destinationChain: 30110, // Arbitrum
serviceData: encodedAPICall
});
Phase 3: Cross-Chain Routing
// Router creates cross-chain message
bytes memory lzMessage = abi.encode(
serviceProvider,
paymentAmount,
targetToken, // USDC
minAmountOut
);
// Send via LayerZero with composition
endpoint.send(
destinationChainId,
lzMessage,
options,
composeMsg // Instructions for swap
);
Phase 4: Token Swap Execution
// Composer receives and processes
function lzCompose(
address from,
bytes32 guid,
bytes calldata message
) external {
(address recipient, uint256 amount, address targetToken) =
abi.decode(message, (address, uint256, address));
// Swap PYUSD to USDC
uint256 usdcAmount = _swapPYUSDToToken(amount, targetToken);
// Transfer to service provider
IERC20(targetToken).transfer(recipient, usdcAmount);
}
Phase 5: Service Execution
Key Features:
Deployed: 0x72924Fa9C3dB52fbFC6581979226340B996F3487
Key Features:
Deployed: 0xAEAb897238015ce8d9C8a248B897D8aea3806795
Key Features:
Contract: X402PaymentRouter.sol
Key Features:
Contract: MCPPaymentGateway.sol
Use Case 1: Multi-Chain AI Workflow
1. GPT Agent (Ethereum) needs image analysis
2. Vision API (Base) charges 1 USDC per image
3. Agent pays in PYUSD from Ethereum
4. System routes payment cross-chain
5. Swaps PYUSD → USDC on Base
6. API receives USDC and processes image
7. Results returned to agent on Ethereum
Use Case 2: Decentralized AI Marketplace
1. Developer deploys new LLM on Arbitrum
2. Registers service with MCP Gateway
3. Sets pricing: 0.001 ETH per 1000 tokens
4. Agents worldwide can access service
5. Payments routed automatically
6. Developer receives ETH regardless of agent's token
Use Case 3: Autonomous Agent Economy
1. Trading bot (Base) needs market data
2. Data provider (Optimism) charges USDT
3. Bot pays PYUSD, system handles conversion
4. Multiple API calls batched for efficiency
5. Payment channels reduce per-call costs
6. Bot operates 24/7 without human intervention
🔄 Complete Payment Flow
Phase 1: Service Discovery & Registration
// Service provider registers their API
mcpGateway.registerMCP(
"gpt-vision-api.com", // Service endpoint
1000000, // Price: 1 USDC per call
["USDC", "USDT"], // Accepted tokens
[30184, 30110] // Base & Arbitrum chains
);
Phase 2: Agent Payment Request
// Agent initiates cross-chain payment
const paymentRequest = await x402Router.requestPayment({
serviceId: "vision-api-123",
amount: "1000000", // 1 PYUSD
requestedToken: "USDC", // Service wants USDC
destinationChain: 30110, // Arbitrum
serviceData: encodedAPICall
});
Phase 3: Cross-Chain Routing
// Router creates cross-chain message
bytes memory lzMessage = abi.encode(
serviceProvider,
paymentAmount,
targetToken, // USDC
minAmountOut
);
// Send via LayerZero with composition
endpoint.send(
destinationChainId,
lzMessage,
options,
composeMsg // Instructions for swap
);
Phase 4: Token Swap Execution
// Composer receives and processes
function lzCompose(
address from,
bytes32 guid,
bytes calldata message
) external {
(address recipient, uint256 amount, address targetToken) =
abi.decode(message, (address, uint256, address));
// Swap PYUSD to USDC
uint256 usdcAmount = _swapPYUSDToToken(amount, targetToken);
// Transfer to service provider
IERC20(targetToken).transfer(recipient, usdcAmount);
}
Phase 5: Service Execution
Key Features:
Deployed: 0x72924Fa9C3dB52fbFC6581979226340B996F3487
Key Features:
Deployed: 0xAEAb897238015ce8d9C8a248B897D8aea3806795
Key Features:
Contract: X402PaymentRouter.sol
Key Features:
Contract: MCPPaymentGateway.sol
Use Case 1: Multi-Chain AI Workflow
1. GPT Agent (Ethereum) needs image analysis
2. Vision API (Base) charges 1 USDC per image
3. Agent pays in PYUSD from Ethereum
4. System routes payment cross-chain
5. Swaps PYUSD → USDC on Base
6. API receives USDC and processes image
7. Results returned to agent on Ethereum
Use Case 2: Decentralized AI Marketplace
1. Developer deploys new LLM on Arbitrum
2. Registers service with MCP Gateway
3. Sets pricing: 0.001 ETH per 1000 tokens
4. Agents worldwide can access service
5. Payments routed automatically
6. Developer receives ETH regardless of agent's token
Use Case 3: Autonomous Agent Economy
1. Trading bot (Base) needs market data
2. Data provider (Optimism) charges USDT
3. Bot pays PYUSD, system handles conversion
4. Multiple API calls batched for efficiency
5. Payment channels reduce per-call costs
6. Bot operates 24/7 without human intervention