import { TokenDetector } from "x402x-detector";import { createPublicClient, http } from "viem";import { bsc } from "viem/chains";const client = createPublicClient({ chain: bsc, transport: http() });const detector = new TokenDetector(client);// Warm up cache (can be done at service startup)await detector.initialize([ "0x8d0D000Ee44948FC98c9B98A4FA4921476f08B0d", // USD1]);// Millisecond response on cache hitconst result = await detector.detect("0x8d0D000Ee44948FC98c9B98A4FA4921476f08B0d");const method = await detector.getRecommendedMethod("0x8d0D000Ee44948FC98c9B98A4FA4921476f08B0d");
Full example (class with cache)
import { TokenDetector } from "x402x-detector";import { createPublicClient, http } from "viem";import { bsc } from "viem/chains";async function main() { const client = createPublicClient({ chain: bsc, transport: http() }); const detector = new TokenDetector(client); // Warm up common tokens at startup const tokens = [ "0x8d0D000Ee44948FC98c9B98A4FA4921476f08B0d", // USD1 ]; await detector.initialize(tokens); // Detection (super fast on cache hits) const result = await detector.detect(tokens[0]); console.log(result); // Recommended payment method const method = await detector.getRecommendedMethod(tokens[0]); console.log("recommended:", method); // Cache stats console.log(detector.getCacheStats());}main();
import express from "express";import { createPublicClient, http } from "viem";import { bsc } from "viem/chains";import { TokenDetector } from "x402x-detector";const app = express();const client = createPublicClient({ chain: bsc, transport: http() });const detector = new TokenDetector(client);// Warm up (recommended)detector.initialize([ "0x8d0D000Ee44948FC98c9B98A4FA4921476f08B0d", // USD1]);app.get("/api/token/:address/capabilities", async (req, res) => { const data = await detector.detect(req.params.address); res.json(data);});app.get("/api/token/:address/recommended", async (req, res) => { const method = await detector.getRecommendedMethod(req.params.address); res.json({ recommendedMethod: method });});
Detect whether a seller recipient address (recommended: EIP-7702 deployed wallet/contract) implements x402x settlement extension methods. Can be used for pre-launch checks, health checks, and canary rollouts.
import { detectSettleMethods } from "x402x-detector";import { createPublicClient, http } from "viem";import { bsc } from "viem/chains";const client = createPublicClient({ chain: bsc, transport: http() });const recipient = "0xSellerEIP7702RecipientAddress";const settle = await detectSettleMethods(client, recipient);// {// supportsSettleWithPermit: boolean; // 0x02ccc23e// supportsSettleWithERC3009: boolean; // 0x1fe200d9// supportsSettleWithPermit2: boolean; // 0xa7fcafbb// }if (!settle.supportsSettleWithPermit && !settle.supportsSettleWithERC3009 && !settle.supportsSettleWithPermit2) { throw new Error("Recipient does not implement any x402x settlement method. Please confirm EIP-7702 integration.");}
How to understand settle detection (implementation highlights)
Settle methods are recognized by function selectors (via ERC-165 capability exposure or static bytecode analysis):
settleWithPermit → 0x02ccc23e
settleWithERC3009 → 0x1fe200d9
settleWithPermit2 → 0xa7fcafbb
We recommend using the EIP-7702 account model to deploy an extension contract for the recipient address, so settlement can be performed across authorization methods.