Full lesson
402lab
The spending account for AI agents: agents pay any API per call in USDC on Solana via HTTP 402 (x402), inside limits a human sets.
What it does
From the project README:
The spending account for AI agents. Agents pay for any API, one call at a time, in USDC on Solana. You set the limits, watch every payment live and stop them with one tap.
- Original
- emersonjds/402lab
- SuperStack fork
- Blockchains/402lab @
59b0917 - Stars
- 0
- Last push (original)
- 2026-10-13
- Languages
- TypeScript, JavaScript, CSS, Dockerfile
- Detected stack
- @solana/kitNext.js
- Categories
- AI & AgentsPayments
- Licence
- MIT
- Homepage
- https://web-production-1b5d1.up.railway.app
Architecture
223 source files. By extension: .ts ×132, .tsx ×89, .js ×2.
| Top-level directory | Source files |
|---|---|
apps | 196 |
packages | 22 |
scripts | 4 |
(root) | 1 |
Dapp: devnet deploy pending
⏳ Devnet deploy pending — goes live when the devnet wallet is topped up. This fork's own dapp is not deployed yet, so there is no program address or transaction to show. It is queued in dapp-queue.txt; once devnet SOL is available it is deployed, exercised with real devnet transactions, and this section switches to the live dapp at /dapps/402lab/. Until then, the lesson and step-by-step guide below are complete, and you can run it locally with the commands below.
Key code, explained
Excerpts chosen automatically where the code uses the most Solana concepts, pinned to the fork's commit.
apps/web/src/shared/wallet/fund-allowance.ts L37–60 @ 59b0917 · MIT · original sessionAddress: string; amount: AtomicAmount;}; /** * Builds and sends a devnet USDC TransferChecked from the human's wallet to the * agent's session wallet, creating the session's associated token account first * if it does not exist yet. Returns the base58 transaction signature for a * Solscan link. */export const fundSessionWallet = async ({ humanSigner, sessionAddress, amount,}: FundSessionInput): Promise<string> => { const owner = address(sessionAddress); const [{ value: latestBlockhash }, createSessionAta, source, destination] = await Promise.all([ solanaRpc.getLatestBlockhash().send(), getCreateAssociatedTokenIdempotentInstructionAsync({ payer: humanSigner, owner, mint }), associatedTokenAccount(humanSigner.address), associatedTokenAccount(owner), ]); const transferChecked = getTransferCheckedInstruction({- Line 42
* Builds and sends a devnet USDC TransferChecked from the human's wallet to the: A checked token transfer, which also verifies the mint and decimals. - Line 54
solanaRpc.getLatestBlockhash().send(),: Fetches a recent blockhash. Transactions expire if it gets too old.
apps/api/src/shared/solana/rpc.ts L37–60 @ 59b0917 · MIT · original const COMMITMENT = 'confirmed';const RPC_TIMEOUT_MS = 15_000; /** The `SolanaChain`/`DevnetChain` used by the split worker, reconciliation job and demo * on-ramp. A call past the timeout rejects, failing the split round instead of hanging it. */export const createSolanaChain = (rpcUrl: string, timeoutMs = RPC_TIMEOUT_MS): DevnetChain => { const rpc = createSolanaRpc(rpcUrl); const deadline = () => ({ abortSignal: AbortSignal.timeout(timeoutMs) }); return { latestBlockhash: async () => (await rpc.getLatestBlockhash({ commitment: COMMITMENT }).send(deadline())).value, send: async (transaction) => { await rpc .sendTransaction(getBase64EncodedWireTransaction(transaction), { encoding: 'base64', preflightCommitment: COMMITMENT, skipPreflight: false, }) .send(deadline()); }, // Without the history search a status older than the RPC's recent cache reads as null, // which would let reconciliation release a batch that did pay out.- Line 49
(await rpc.getLatestBlockhash({ commitment: COMMITMENT }).send(deadline())).value,: Fetches a recent blockhash. Transactions expire if it gets too old. - Line 52
.sendTransaction(getBase64EncodedWireTransaction(transaction), {: Submits the signed transaction to the cluster.
How to run, build and test
These commands come from the repo's own manifests (package.json scripts, Anchor.toml, Cargo, go.mod, Makefile). Check the README for prerequisites and environment variables.
| Directory | Command | Runs |
|---|---|---|
. | npm run dev | concurrently -n api,web -c green,cyan "npm run dev -w @402lab/api" "npm run dev -w @402lab/web" |
. | npm run lint | eslint . |
packages/shared | npm run test | vitest run |
packages/mcp | npm run build | tsc --noEmit && esbuild src/cli.ts --bundle --platform=node --format=esm --outfile=dist/cli.js --external:@modelcontextp |
apps/web | npm run dev | next dev --webpack |
apps/web | npm run start | next start |
apps/web | npm run build | next build --webpack |
apps/web | npm run test | vitest run |
apps/web | npm run lint | eslint . |
apps/mobile | npm run start | expo start |
apps/api | npm run dev | tsx watch --env-file-if-exists=.env src/server.ts |
apps/api | npm run start | tsx --env-file-if-exists=.env src/server.ts |
apps/api | npm run test | vitest run |
What you could build from it
- Fork the SuperStack copy and extend it within the AI & Agents gaps listed on the idea page.
- Learn the pattern first: AI agents on Solana
- Learn the pattern first: Building and sending transactions from a client