Sol.College

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 directorySource files
apps196
packages22
scripts4
(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.

emersonjds/402lab · 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({
  1. 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.
  2. Line 54 solanaRpc.getLatestBlockhash().send(),: Fetches a recent blockhash. Transactions expire if it gets too old.
emersonjds/402lab · 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.
  1. Line 49 (await rpc.getLatestBlockhash({ commitment: COMMITMENT }).send(deadline())).value,: Fetches a recent blockhash. Transactions expire if it gets too old.
  2. 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.

DirectoryCommandRuns
.npm run devconcurrently -n api,web -c green,cyan "npm run dev -w @402lab/api" "npm run dev -w @402lab/web"
.npm run linteslint .
packages/sharednpm run testvitest run
packages/mcpnpm run buildtsc --noEmit && esbuild src/cli.ts --bundle --platform=node --format=esm --outfile=dist/cli.js --external:@modelcontextp
apps/webnpm run devnext dev --webpack
apps/webnpm run startnext start
apps/webnpm run buildnext build --webpack
apps/webnpm run testvitest run
apps/webnpm run linteslint .
apps/mobilenpm run startexpo start
apps/apinpm run devtsx watch --env-file-if-exists=.env src/server.ts
apps/apinpm run starttsx --env-file-if-exists=.env src/server.ts
apps/apinpm run testvitest run

What you could build from it

Used in lessons