Autonomous AI agents need to pay each other and pay for services — compute, data, APIs — with no human in the loop. AiFinPay is the payment protocol (x402); Casper is the on-chain settlement layer. This repository is the live Casper settlement contract, the x402 bridge, SDK examples, and an MCP server that lets AI agents settle payments on Casper.
Quick Start · Architecture · Payment Flow · Sample Transactions · Demo · Roadmap
- Introduction
- Problem
- Solution
- Features
- Architecture
- Why Casper
- Payment Flow
- Repository Structure
- Quick Start
- Installation & Configuration
- Local Development
- Deploying the Contract
- Casper Mainnet Deployment
- Casper Testnet Deployment
- Contract Package Hash
- Sample Transactions
- SDK & API Examples
- Environment Variables
- Testing Instructions
- Screenshots
- Demo Video
- Roadmap
- Security
- Contributing
- License
- Acknowledgements
- Casper Agentic Buildathon
- Useful Links
AiFinPay is payment infrastructure for the machine economy. As autonomous AI agents begin to buy compute, data, and API access on their own, they need a way to pay and be paid with a verifiable, non-custodial settlement record. AiFinPay provides that as a protocol layer over HTTP 402 (x402), and this repository implements the Casper settlement backend: a Rust → Wasm smart contract that gives every agent an on-chain identity and records every payment permanently.
Agent-to-agent and agent-to-service commerce needs three things that today's payment rails don't provide together:
- Identity — a stable, verifiable on-chain identity for each agent.
- A payment protocol — a machine-native way to request and authorize payment (no checkout page, no human).
- Settlement proof — an immutable, independently verifiable record that a payment happened.
Card rails and custodial wallets assume a human and a browser. Agents need programmatic settlement with cryptographic proof — at micropayment scale and micropayment cost.
AiFinPay closes the loop:
- x402 protocol — a service returns
HTTP 402 Payment Required; the agent settles on-chain and retries with proof. - Casper settlement contract — agents
register_agentfor an on-chain identity, thenpay_agentto settle. Every settlement emits aPaymentSettledevent. - Idempotent settlement — payments are keyed by
request_id, so retries are safe and double-settlement is impossible. - MCP server — AI agents (e.g. Claude via Claude Code / Claude Desktop) settle on Casper as a native tool call.
- 🧾 On-chain agent registry —
register_agent(agent_id, wallet)with anAgentRegisteredevent. - 💸 Verifiable settlement —
pay_agent(from, to, amount, request_id)emitsPaymentSettled, permanently recorded on Casper. - 🔁 Idempotent by design — duplicate
request_idis rejected (no double spend). - 🌐 x402 bridge — a reference compute gate that enforces
HTTP 402and verifies settlement on-chain before releasing a resource. - 🤖 MCP integration — drive settlements directly from an AI agent runtime.
- 📊 Live dashboard — reads Casper RPC directly to show settlement counts and records.
- 🦀 Minimal, auditable Rust contract — deterministic Wasm execution.
flowchart LR
subgraph Client["AI Agent Runtime"]
A[AI Agent]
SDK[AiFinPay SDK / MCP server]
end
subgraph Protocol["AiFinPay x402 Protocol"]
API[x402 Bridge / API]
end
subgraph CasperChain["Casper Blockchain"]
SC[AiFinPay Settlement Contract]
LEDGER[(Immutable ledger<br/>agents · payments · events)]
end
M[Merchant / Service]
A --> SDK
SDK -->|register_agent / pay_agent| API
API -->|HTTP 402 challenge| A
API --> SC
SC --> LEDGER
SC -->|PaymentSettled event| API
API -->|verified · unlock resource| M
M -->|deliver result| A
Full storage layout, named keys, and error codes are in docs/ARCHITECTURE.md. A component + lifecycle overview is in ARCHITECTURE.md.
- Predictable, low fees for high-frequency machine-to-machine micropayments.
- Deterministic Wasm execution (Rust) — settlement logic is auditable and small.
- Native on-chain events (
PaymentSettled) give merchants cryptographic proof of payment without a trusted intermediary. - Clean account / identity model that fits agent registries well.
- Idempotent settlement keyed by
request_id— safe retries, no double spend.
sequenceDiagram
autonumber
participant Agent as AI Agent
participant Bridge as x402 Bridge / API
participant Casper as Casper Contract
participant Merchant as Merchant / Service
Agent->>Casper: register_agent(agent_id, wallet)
Merchant->>Casper: register_agent(provider_id, wallet)
Agent->>Bridge: request resource (compute)
Bridge-->>Agent: HTTP 402 Payment Required (pay_casper challenge)
Agent->>Casper: pay_agent(from, to, amount, request_id)
Casper-->>Casper: validate agents · record payment · emit PaymentSettled
Casper-->>Agent: tx confirmed (deploy hash)
Agent->>Bridge: retry request (+ request_id)
Bridge->>Casper: verify PaymentSettled(request_id)
Casper-->>Bridge: settlement confirmed
Bridge->>Merchant: unlock resource
Merchant-->>Agent: deliver result
casper-contract/
├── src/ # Rust → Wasm settlement contract
│ └── main.rs # register_agent · pay_agent · get_payment_count
├── demo/ # Node.js: agent, x402 bridge, MCP server, dashboard
│ ├── agent-compute-demo.js # ⭐ AI agent buys compute, settles on Casper
│ ├── compute-bridge.js # x402 gate — verifies settlement on-chain
│ ├── casper-mcp.mjs # MCP server for AI-agent-driven settlement
│ ├── deploy.js · keygen.js # deploy + keypair generation
│ ├── dashboard.html # live settlement dashboard
│ └── .env.example
├── examples/ # Copy-paste integration examples
├── scripts/ # build / deploy / demo wrappers
├── docs/ # ARCHITECTURE · DEPLOYMENT · DEMO_VIDEO
├── .github/ # CI, CodeQL, Dependabot, issue/PR templates
├── Cargo.toml # aifinpay-casper (Rust contract)
├── Makefile # make setup / build / deploy / agent-demo
└── SUBMISSION.md # Buildathon submission summary
# 1. Clone
git clone https://github.com/AiFinPay/casper-contract.git
cd casper-contract
# 2. One-time setup (Rust wasm target + Node deps)
make setup
# 3. Build the contract to Wasm
make build
# 4. Generate a keypair, then fund it at the faucet
make keygen
# → paste the account hash at https://testnet.cspr.live/tools/faucet, wait ~2 min
# 5. Run the headline demo: AI agent buys compute, settled on Casper
make agent-demoThe contract is already deployed on Casper testnet, so you can run the demo against the live contract hash without deploying your own. To deploy your own copy, use
make deploy.
Prerequisites: Rust (stable) with the wasm32-unknown-unknown target, and Node.js ≥ 18.
# Rust toolchain (contract)
rustup target add wasm32-unknown-unknown
# Node toolchain (demo / SDK / MCP)
cd demo && npm install && cp .env.example .envSet CONTRACT_HASH in demo/.env after deploying (or use the live hash below).
make fmt # format Rust
make clippy # lint (warnings as errors)
make test # contract tests
make demo # basic register + settle flow
make agent-demo # AI agent buys compute (x402 → Casper)
make mcp # start the Casper MCP server
make dashboard # serve the live dashboardmake build # target/wasm32-unknown-unknown/release/aifinpay_casper.wasm
make deploy # deploys via demo/deploy.js, prints the new CONTRACT_HASHSave the printed CONTRACT_HASH into demo/.env. Full walkthrough: docs/DEPLOYMENT.md.
The settlement contract is live on Casper Mainnet — not only testnet. Every entry point below has been exercised on mainnet with real CSPR.
| Field | Value |
|---|---|
| Network | casper (Casper 2.0 mainnet) |
| Contract hash | contract-9903a5e3948e799196df54b17270bc6769338ac1cc36c9eb47e113f88d23f019 |
| Package hash | hash-7ad34a204952eef63d5dcf5159fb7d009e85dea4f49cbdf73dde190652dfa375 |
| Explorer | cspr.live mainnet |
| Install deploy | 0d560c62… |
A full agent-to-agent settlement executed on mainnet — two agents registered, a payment settled on-chain, and real CSPR delivered to the provider's wallet:
| Action | Deploy | Explorer |
|---|---|---|
| Register agent (buyer) | aee06f58… |
view |
| Register agent (provider) | 4f06da16… |
view |
PaymentSettled (pay_agent) |
80df5895… |
view |
| Value transfer (2.5 CSPR → provider) | 564f19be… |
view |
Reproduce on mainnet with a funded key at demo/keys-mainnet/secret_key.pem: node demo/deploy-mainnet.js then node demo/demo-mainnet.js.
| Field | Value |
|---|---|
| Network | casper-test (Casper 2.0) |
| Public RPC | https://node.testnet.casper.network/rpc |
| Explorer | cspr.live testnet |
| Language | Rust → WebAssembly (casper-contract 5.1.1, casper-types 6.1.0) |
| Entry Point | Args | Description |
|---|---|---|
register_agent |
agent_id: String, wallet: String |
Register an AI agent on-chain → AgentRegistered |
pay_agent |
from_agent: String, to_agent: String, amount: U512, request_id: String |
Settle a payment → PaymentSettled |
get_payment_count |
— | Total settled payments |
AgentRegistered—agent_id,walletPaymentSettled—from,to,amount(motes),request_id
hash-47df409829ddf0612617460293ba591a19b26fa0c06918878204088d3eb9b78a
Real transactions from the agent-compute demo on casper-test:
| Action | Deploy | Explorer |
|---|---|---|
| Register agent (buyer) | d4b7d0ad…27d23e0 |
view |
| Register agent (provider) | 6c41c885…faac4e8 |
view |
PaymentSettled (pay_agent) |
0b55b516…ffb0137 |
view |
Reproduce them yourself: make agent-demo (with a funded key at demo/keys/secret_key.pem).
Small, runnable examples live in examples/:
register-agent.js— register an agent (register_agent)pay-agent.js— settle a payment (pay_agent)ai-agent-buys-compute.md— the headline x402 → Casper flowmerchant-integration.md— gate an endpoint behind on-chain settlementmcp-server.md— drive settlements from Claude via MCP
The reference x402 gate is demo/compute-bridge.js; the AI agent client is demo/agent-compute-demo.js.
Configure in demo/.env (see demo/.env.example):
| Variable | Default | Description |
|---|---|---|
NODE_URL |
https://node.testnet.casper.network/rpc |
Casper RPC endpoint |
NETWORK_NAME |
casper-test |
Casper network name |
KEYS_DIR |
./keys |
Directory holding the signing key |
CONTRACT_HASH |
— | Deployed contract hash (hash-…) |
COMPUTE_UPSTREAM_URL |
(optional) | Real OpenAI-compatible compute endpoint |
COMPUTE_API_KEY |
(optional) | API key for the upstream provider |
Never commit
.env, keys, or keypairs..gitignoreblocksdemo/.envanddemo/keys/, but always double-check before pushing.
make test # Rust contract tests
cargo clippy --all-targets -- -D warnings # lint
cargo fmt --all -- --check # format check
cd demo && node test-mcp.mjs # MCP server smoke testEnd-to-end: run make agent-demo against the live contract and confirm the printed PaymentSettled deploy resolves to Success on cspr.live.
Agent Settlement Dashboard — a live view of the deployed testnet contract: registered agents, on-chain settlements, per-payment receipts, and the raw event log, all read straight from the Casper RPC. Run it locally with cd demo && node serve-dashboard.js.
The narrated walkthrough script is in docs/DEMO_VIDEO.md — an autonomous AI agent buys compute and settles it on Casper testnet in ~90 seconds, ending on the on-chain settlement proof.
📹 Video link: to be added.
See ROADMAP.md for the full plan. Highlights:
- ✅ Live Casper settlement contract + end-to-end agentic payment flow + MCP server.
- 🔜 USDC settlement, a typed
@aifinpay/casperSDK on npm, hosted x402 gateway. - 🧭 Casper Mainnet, programmable spend limits, on-chain agent reputation.
- CodeQL, Dependabot, and secret scanning with push protection run on this repository.
- Report vulnerabilities privately — see
SECURITY.md. Do not open a public issue for security problems.
Contributions are welcome — see CONTRIBUTING.md and our CODE_OF_CONDUCT.md. Use the issue and PR templates; CI (build, lint, CodeQL) must pass.
MIT © 2026 AiFinPay.
- Casper Network and the Casper Agentic Buildathon.
- The Model Context Protocol and Claude for agent-driven settlement.
casper-js-sdkand the Casper Rust contract toolchain.
This repository is AiFinPay's submission to the Casper Agentic Buildathon — a fully functional MVP on Casper testnet with a live contract, verifiable on-chain settlements, demo + testing instructions, and CI/security automation. See SUBMISSION.md.
- 🌐 Website — https://aifinpay.io
- 💻 Organization — https://github.com/AiFinPay
- 🔎 Contract on cspr.live — testnet explorer
- 🚰 Casper testnet faucet — https://testnet.cspr.live/tools/faucet
- 📚 Casper docs — https://docs.casper.network/
