Full lesson
x402-sdk-for-solana
What it does
From the project README:
中文 Readme A TypeScript SDK for Solana payment gateway based on the X402 protocol (implemented based on x402). Quickly integrate pay-per-use functionality into your application, supporting any Solana network and any SPL Token.
- Original
- xilibi2003/x402-sdk-for-solana
- SuperStack fork
- Blockchains/x402-sdk-for-solana @
956b972 - Stars
- 0
- Last push (original)
- 2025-10-27
- Languages
- TypeScript, CSS
- Detected stack
- @solana/kit
- Categories
- AI & AgentsInfrastructure & Tooling
- Licence
- MIT
Architecture
92 source files. By extension: .ts ×88, .tsx ×4.
| Top-level directory | Source files |
|---|---|
lib | 84 |
examples | 6 |
(root) | 1 |
scripts | 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/x402-sdk-for-solana/. 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.
lib/x402/types/verify/x402Specs.ts L13–36 @ 956b972 · MIT · originalexport const schemes = ["exact"] as const;export const x402Versions = [1] as const;export const ErrorReasons = [ "insufficient_funds", "invalid_exact_evm_payload_authorization_valid_after", "invalid_exact_evm_payload_authorization_valid_before", "invalid_exact_evm_payload_authorization_value", "invalid_exact_evm_payload_signature", "invalid_exact_evm_payload_recipient_mismatch", "invalid_exact_svm_payload_transaction", "invalid_exact_svm_payload_transaction_amount_mismatch", "invalid_exact_svm_payload_transaction_create_ata_instruction", "invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_payee", "invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_asset", "invalid_exact_svm_payload_transaction_instructions", "invalid_exact_svm_payload_transaction_instructions_length", "invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction", "invalid_exact_svm_payload_transaction_instructions_compute_price_instruction", "invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high", "invalid_exact_svm_payload_transaction_instruction_not_spl_token_transfer_checked", "invalid_exact_svm_payload_transaction_instruction_not_token_2022_transfer_checked", "invalid_exact_svm_payload_transaction_not_a_transfer_instruction", "invalid_exact_svm_payload_transaction_receiver_ata_not_found", "invalid_exact_svm_payload_transaction_sender_ata_not_found",- Line 14
export const x402Versions = [1] as const;: Uses x402, an HTTP 402 pattern where an agent pays per request with an on-chain payment. - Line 32
"invalid_exact_svm_payload_transaction_instruction_not_spl_token_transfer_checked",: A checked token transfer, which also verifies the mint and decimals. - Line 33
"invalid_exact_svm_payload_transaction_instruction_not_token_2022_transfer_checked",: Targets the Token-2022 program instead of the original Token Program.
lib/x402-fetch/index.ts L109–126 @ 956b972 · MIT · original ...init, headers: { ...(init.headers || {}), "X-PAYMENT": paymentHeader, "Access-Control-Expose-Headers": "X-PAYMENT-RESPONSE", }, __is402Retry: true, }; const secondResponse = await fetch(input, newInit); return secondResponse; };} export { decodeXPaymentResponse } from "../x402/shared/index.js";export { createSigner, type Signer, type MultiNetworkSigner, type X402Config } from "../x402/types/index.js";export { type PaymentRequirementsSelector } from "../x402/client/index.js";export type { Hex } from "viem";- Line 109
...init,: Creates and pays for a new account. Space and payer must be specified. - Line 123
export { decodeXPaymentResponse } from "../x402/shared/index.js";: Uses x402, an HTTP 402 pattern where an agent pays per request with an on-chain payment.
lib/x402/schemes/exact/svm/facilitator/verify.ts L25–48 @ 956b972 · MIT · original AccountMeta, InstructionWithData,} from "@solana/kit";import { parseSetComputeUnitLimitInstruction, parseSetComputeUnitPriceInstruction, COMPUTE_BUDGET_PROGRAM_ADDRESS,} from "@solana-program/compute-budget";import { findAssociatedTokenPda, identifyToken2022Instruction, parseCreateAssociatedTokenInstruction, parseTransferCheckedInstruction as parseTransferCheckedInstruction2022, Token2022Instruction, TOKEN_2022_PROGRAM_ADDRESS,} from "@solana-program/token-2022";import { identifyTokenInstruction, parseTransferCheckedInstruction as parseTransferCheckedInstructionToken, TOKEN_PROGRAM_ADDRESS, TokenInstruction,} from "@solana-program/token";import { decodeTransactionFromPayload,- Line 35
identifyToken2022Instruction,: Targets the Token-2022 program instead of the original Token Program. - Line 37
parseTransferCheckedInstruction as parseTransferCheckedInstruction2022,: A checked token transfer, which also verifies the mint and decimals.
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 build | tsc |
examples | npm run test | echo "Error: no test specified" && exit 1 |
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
- Learn the pattern first: Blinks and Solana Actions