DEV Community

Cover image for Building a Gasless Marketplace on Polygon with x402 Protocol
uratmangun Subscriber for kirodotdev

Posted on

Building a Gasless Marketplace on Polygon with x402 Protocol

Have you ever wanted to build a marketplace where users can buy and sell products using stablecoins without worrying about gas fees? In this article, I'll walk you through how I built Polygon x402 Marketplace, a gasless peer-to-peer marketplace using the x402 protocol on Polygon Amoy.

The Problem

Traditional blockchain marketplaces have a major UX problem: users need to hold native tokens (like MATIC) just to pay for gas fees. This creates friction because:

  1. New users need to acquire native tokens from exchanges first
  2. Users must manage multiple token balances
  3. Gas prices fluctuate, making transaction costs unpredictable

The Solution: x402 Protocol

The x402 protocol enables gasless USDC transfers by using EIP-712 signatures and a facilitator network. Users only need USDC in their wallet to participate in the marketplace - no native tokens required!

Tech Stack

  • Frontend: Next.js 16, React 19, TailwindCSS 4
  • Web3: RainbowKit, wagmi, viem, x402-axios
  • Database: PostgreSQL with Drizzle ORM
  • Blockchain: Polygon Amoy (Testnet), USDC Token
  • Protocol: x402 Protocol with PayAI Facilitator

Key Features

Gasless USDC Payments

The marketplace uses the x402 protocol to handle payments. When a user makes a purchase, the payment flow works like this:

  1. User clicks "Buy Now"
  2. The API returns a 402 Payment Required response with payment requirements
  3. x402-axios intercepts this and prompts the user to sign an EIP-712 message
  4. The signed payment is sent to the PayAI facilitator for verification and settlement
  5. The transaction is executed on-chain without the user paying gas

Product Listings

Sellers can list items with title, description, price, stock quantity, and images. The marketplace supports:

  • Creating new listings
  • Editing existing products
  • Automatic stock management after purchases
  • Comments on product listings

Wallet Integration

RainbowKit provides seamless wallet connection supporting MetaMask, WalletConnect, and more. The app automatically handles chain switching to Polygon Amoy.

Implementation Highlights

The Facilitator Proxy

One of the challenges was building a local facilitator proxy that correctly forwards requests to PayAI while maintaining proper CORS headers:

const paymentRequirements = {
  scheme: "exact",
  network: "polygon-amoy",
  maxAmountRequired: atomicAmount,
  payTo: recipientAddress,
  asset: USDC_ADDRESS,
  extra: {
    name: "USDC",
    version: "2",
  },
};
Enter fullscreen mode Exit fullscreen mode

Using x402-axios

The x402-axios library makes it easy to handle the payment flow:

const axiosInstance = withPaymentInterceptor(
  axios.create(),
  walletClient
);

const response = await axiosInstance.post("/api/usdc-pay", {
  to: recipient,
  amount: amount,
});
Enter fullscreen mode Exit fullscreen mode

Live Demo

Check out the live marketplace at: https://polygon-x402.vercel.app/

GitHub Repository: https://github.com/uratmangun/polygon-x402

What's Next

  • Deploy to Polygon Mainnet
  • Add order history and transaction tracking
  • Implement seller profiles with ratings
  • Support multiple stablecoins (USDT, DAI)
  • Mobile optimization with PWA features

How We Used Kiro in This Project

This project leverages Kiro's steering rules and MCP (Model Context Protocol) configuration to maintain consistent development practices. The .kiro folder contains:

Steering Rules

Kiro steering rules are markdown files that guide AI assistants during development:

  1. Shell Preferences (shell-preferences.md): Enforces fish shell syntax for all terminal commands, ensuring consistent command execution across the project.

  2. No Auto Start Dev Server (no auto start dev server.md): Prevents AI assistants from automatically starting development servers, giving developers control over resource usage.

  3. UI Color Preferences (ui color prefer non purple primary.md): Establishes cyan/blue as primary colors instead of purple, maintaining consistent visual identity.

  4. Package Manager Standards (use pnpm with yarn fallback for nodejs dependency manager.md): Mandates pnpm as the primary package manager with yarn as fallback, ensuring efficient disk usage and consistent dependency resolution.

MCP Configuration

The .kiro/settings/mcp.json configures Model Context Protocol servers:

  • chrome-devtools: Enables browser automation for testing and debugging
  • context7: Provides enhanced context awareness
  • next-devtools: Integrates Next.js-specific development tools

These Kiro configurations ensure that any AI assistant working on this project follows the same conventions, making collaboration smoother and code more consistent.


If you're interested in building gasless dApps, I highly recommend exploring the x402 protocol. It's a game-changer for blockchain UX!

Happy coding! 🚀

Demo

Top comments (0)