A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

0 downloads

dbbasic-browser/packages/engine/test/live-settlement.test.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 site icons (favicons) in tabs, address bar suggestions and the bookmarks panel 7249579 16 hours ago History
live-settlement.test.ts · 3.7 KB · 78 lines
1import { readFileSync } from "node:fs";
2import { fileURLToPath } from "node:url";
3import { describe, expect, it } from "vitest";
4import { createPublicClient, http } from "viem";
5import { baseSepolia } from "viem/chains";
6import { privateKeyToAccount } from "viem/accounts";
7import { signExactEvm } from "../src/schemes/exact-evm.js";
8import type { PaymentRequirements, Quote } from "../src/types.js";
9
10/**
11 * LIVE proof that our engine's signature settles through the real x402 facilitator on
12 * Base Sepolia — signs a tiny buyer→merchant payment and confirms real USDC moves.
13 *
14 * Gated behind RUN_LIVE_SETTLE=1 (and a funded .env), so it NEVER runs in normal test
15 * runs / CI. It moves 0.001 real testnet USDC per run. To run:
16 * RUN_LIVE_SETTLE=1 npx vitest run live-settlement (from packages/engine)
17 */
18const LIVE = process.env.RUN_LIVE_SETTLE === "1";
19const FACILITATOR = process.env.X402_FACILITATOR || "https://x402.org/facilitator";
20const USDC = "0x036CbD53842c5426634e7929541eC2318f3dCF7e";
21const BAL_ABI = [{ name: "balanceOf", type: "function", stateMutability: "view", inputs: [{ name: "a", type: "address" }], outputs: [{ type: "uint256" }] }] as const;
22
23function env(): Record<string, string> {
24 const path = fileURLToPath(new URL("../../../.env", import.meta.url)); // repo-root .env
25 const out: Record<string, string> = {};
26 for (const line of readFileSync(path, "utf8").split("\n")) {
27 const m = /^([A-Z0-9_]+)=(.*)$/.exec(line.trim());
28 if (m) out[m[1]!] = m[2]!;
29 }
30 return out;
31}
32
33describe.runIf(LIVE)("live facilitator settlement (Base Sepolia)", () => {
34 it("signs buyer→merchant and settles on-chain, moving real USDC", async () => {
35 const e = env();
36 const buyer = privateKeyToAccount(e.X402_PRIVATE_KEY as `0x${string}`);
37 const merchant = e.X402_MERCHANT_ADDRESS as `0x${string}`;
38 const client = createPublicClient({ chain: baseSepolia, transport: http("https://sepolia.base.org") });
39 const bal = (a: string) => client.readContract({ address: USDC, abi: BAL_ABI, functionName: "balanceOf", args: [a as `0x${string}`] });
40
41 const requirements: PaymentRequirements = {
42 scheme: "exact",
43 network: "eip155:84532",
44 amount: "1000", // $0.001
45 asset: USDC,
46 payTo: merchant,
47 maxTimeoutSeconds: 120,
48 extra: { assetTransferMethod: "eip3009", name: "USDC", version: "2" },
49 };
50 const quote: Quote = { requirements, priceUsdMicro: 1000n, assetSymbol: "USDC" };
51
52 const payload = await signExactEvm(buyer, quote, { maxAuthorizationLifetimeSeconds: 120 });
53 const body = JSON.stringify({ x402Version: 2, paymentPayload: payload, paymentRequirements: requirements });
54
55 // 1. verify (no money moves)
56 const verify = await (await fetch(`${FACILITATOR}/verify`, { method: "POST", headers: { "content-type": "application/json" }, body })).json();
57 expect(verify.isValid).toBe(true);
58
59 const buyerBefore = await bal(buyer.address);
60 const merchBefore = await bal(merchant);
61
62 // 2. settle (real transfer; the facilitator pays gas)
63 const settle = await (await fetch(`${FACILITATOR}/settle`, { method: "POST", headers: { "content-type": "application/json" }, body })).json();
64 expect(settle.success).toBe(true);
65 expect(settle.transaction).toMatch(/^0x[0-9a-fA-F]{64}$/);
66 console.log("settled: https://sepolia.basescan.org/tx/" + settle.transaction);
67
68 // 3. balances moved by exactly 0.001 USDC (1000 atomic)
69 let buyerAfter = buyerBefore, merchAfter = merchBefore;
70 for (let i = 0; i < 15 && merchAfter <= merchBefore; i++) {
71 await new Promise((r) => setTimeout(r, 2000));
72 buyerAfter = await bal(buyer.address);
73 merchAfter = await bal(merchant);
74 }
75 expect(buyerBefore - buyerAfter).toBe(1000n);
76 expect(merchAfter - merchBefore).toBe(1000n);
77 }, 60000);
78});