Architecture

Understanding how the X402 Facilitator enables gasless payments on ApeChain.

High-Level Overview

The X402 Facilitator acts as a trusted relayer between paying agents and the blockchain. It abstracts away gas management, transaction submission, and nonce tracking - letting developers focus on building great products.

System Components

🤖

Agent / Client

Your app or AI agent that needs to make payments. Uses the SDK to sign permits.

Facilitator API

Verifies permits and submits transactions. Pays gas fees on behalf of users.

📜

Settlement Contract

On-chain contract that executes permit + transfer atomically.

Payment Flow

1
Agent creates invoice
Specifies recipient, token, amount, and chain
2
Agent signs EIP-2612 permit
Off-chain signature - costs 0 gas
3
Facilitator verifies permit
Checks signature, balance, nonce, recipient
4
Facilitator settles on-chain
Submits tx to Settlement Contract, pays gas in $APE
5
Payment complete
Agent receives txHash confirmation

EIP-2612 Permits

The magic behind gasless payments is EIP-2612, an extension to ERC-20 tokens that allows approvals via signatures instead of transactions.

Traditional ERC-20 Flow (requires 2 transactions)

  1. User calls approve(spender, amount) - pays gas
  2. Spender calls transferFrom() - pays gas

EIP-2612 Permit Flow (1 transaction, user pays 0 gas)

  1. User signs permit message off-chain - FREE
  2. Relayer calls permit() + transferFrom() - relayer pays gas

Settlement Contract

The Settlement Contract is a simple, stateless contract that combines permit() andtransferFrom() into a single atomic transaction:

function settle(
    address token,
    address owner,
    address recipient,
    uint256 amount,
    uint256 deadline,
    uint8 v, bytes32 r, bytes32 s
) external {
    // 1. Execute the permit (approve)
    IERC20Permit(token).permit(
        owner, address(this), amount, deadline, v, r, s
    );
    
    // 2. Transfer tokens to recipient
    IERC20(token).transferFrom(owner, recipient, amount);
}

Key properties:

  • Atomic - If transfer fails, permit is not consumed
  • Stateless - No storage, minimal gas overhead
  • Non-custodial - Contract never holds funds

Security Model

✅ What the Facilitator CAN do

  • • Submit your signed permit to the blockchain
  • • Transfer the exact amount you authorized
  • • Send funds only to the recipient you specified

❌ What the Facilitator CANNOT do

  • • Change the recipient address
  • • Transfer more than the signed amount
  • • Reuse your permit (nonce protection)
  • • Access funds without your signature

Network Details

NetworkApeChain Mainnet
Chain ID33139
RPC URLhttps://apechain.calderachain.xyz/http
Block Explorerapescan.io
Settlement Contract0xe24a8dbf205ee116c991f60686f778a2337a844f
Native Token$APE (used for gas)