Autonomous agent for asset management and x402 payments on X Layer (OKX L2, chainId: 196)
Built for the X Layer Onchain OS AI Hackathon — an agentic vault system that autonomously monitors balances, manages deposits/withdrawals, and executes x402 micropayments on X Layer mainnet.
This project was built with the assistance of:
- Claude (Anthropic) — architecture design, prompt engineering, code review
- MiniMax M2.5 (free tier via OpenCode) — code generation and implementation
- Smart Contract Vault deployed on X Layer mainnet
- Autonomous decision cycles (healthy / low / critical balance)
- x402 payment protocol for autonomous micropayments
- Full TypeScript with strong typing —
anyis forbidden - SOLID principles throughout
- 20/20 unit tests passing
- Runtime: Node.js 20 + TypeScript 6
- Blockchain: viem for X Layer RPC
- Smart Contracts: Solidity 0.8.0 + Hardhat
- Testing: Vitest
- Environment: dotenv
src/
├── agents/
│ └── OKXAgenticWallet.ts # OKX Agentic Wallet with viem
├── config/
│ └── network.ts # X Layer network config (chainId 196)
├── contracts/
│ └── Vault.sol # Smart contract
├── core/
│ ├── errors/ # Typed custom errors
│ │ ├── PaymentErrors.ts
│ │ ├── VaultErrors.ts
│ │ └── WalletErrors.ts
│ └── interfaces/ # Core interfaces (SOLID ISP)
│ ├── IPaymentHandler.ts
│ ├── IOrchestrator.ts
│ ├── IVault.ts
│ └── IWalletAgent.ts
├── orchestrator/
│ └── AgentOrchestrator.ts # Autonomous decision cycles
├── payments/
│ └── X402PaymentHandler.ts # x402 payment protocol
├── scripts/
│ ├── runAgent.ts # Main demo entry point
│ ├── testConnection.ts # Verify X Layer RPC
│ ├── testPayment.ts # Test payment flow
│ ├── testVault.ts # Read-only vault test
│ ├── testDeposit.cjs # Deposit to vault contract
│ ├── testWithdraw.cjs # Withdraw from vault contract
│ └── deploy.cjs # Deploy vault contract
├── utils/
│ └── logger.ts # Typed singleton logger
└── vault/
└── VaultService.ts # Vault contract interaction
tests/
├── agent.test.ts
├── orchestrator.test.ts
├── payment.test.ts
└── vault.test.ts
Vault Contract: 0x7FE71a4817Fe49658BCFFBCcD7FBc00B5f74F150
| Function | Description |
|---|---|
deposit() |
Payable — receive native OKB |
withdraw(uint256 amount) |
Owner only |
getBalance() |
Returns contract balance |
Events: Deposited(address, amount) · Withdrawn(address, amount)
| Operation | Tx Hash | Gas Used |
|---|---|---|
| Deposit 0.001 OKB | 0x0c0d5569... |
45,165 |
| Withdraw 0.001 OKB | 0x5486973f... |
33,146 |
npm installcp .env.example .envRPC_URL=https://rpc.xlayer.tech
PRIVATE_KEY=0x...
VAULT_ADDRESS=0x7FE71a4817Fe49658BCFFBCcD7FBc00B5f74F150
MAX_DEPOSIT_AMOUNT=1000000000000000000 # 1 OKB
MIN_DEPOSIT_AMOUNT=1000000000000000 # 0.001 OKB
VAULT_THRESHOLD_LOW=50000000000000000 # 0.05 OKB
VAULT_THRESHOLD_CRITICAL=10000000000000000 # 0.01 OKB| Command | Description |
|---|---|
npm test |
Run all 20 unit tests |
npm run test:watch |
Watch mode |
npm run test:connection |
Verify X Layer RPC + chainId |
npm run test:vault |
Read-only vault test |
npm run test:payment |
x402 payment flow test |
npm run run:agent |
Run autonomous agent demo |
npm run run:agent:ts-node |
Run with ts-node/esm |
npm run run:agent:ts-nodeExample output:
==================================================
X Layer Agentic Vault - Demo
==================================================
[INFO] Connecting to wallet...
[INFO] Wallet connected successfully
[INFO] VaultService initialized
--- Cycle 1 ---
Action: vault_healthy
Success: true
Details: Balance above threshold
--- Cycle 2 ---
Action: vault_healthy
Success: true
--- Cycle 3 ---
Action: vault_healthy
Success: true
==================================================
Demo Complete
==================================================
npx hardhat compile
npx hardhat run scripts/deploy.cjs --network xlayerconnect()— initialize wallet connectiongetBalance(tokenAddress)— balance in weisimulateTransaction(tx)— dry-run before executionexecuteTransaction(tx)— submit to X Layer
deposit(tokenAddress, amount)— deposit into vault contractwithdraw(tokenAddress, amount, recipient)— withdraw from vaultgetVaultBalance(tokenAddress)— current contract balancegetDepositHistory(tokenAddress)— operation history
handlePayment(response)— handle HTTP 402 payment requestverifyPayment(txHash)— confirm payment on-chaingetPaymentHistory()— payment history
start()— initialize all modulesstop()— graceful shutdowngetStatus()—idle|running|stopped|errorrunCycle()— one autonomous decision cycle
Each cycle evaluates vault health and acts accordingly:
| Status | Condition | Action |
|---|---|---|
healthy |
balance > VAULT_THRESHOLD_LOW |
Log, no action |
low |
balance between thresholds | Simulate deposit |
critical |
balance < VAULT_THRESHOLD_CRITICAL |
Alert + simulate deposit |
| Property | Value |
|---|---|
| Chain ID | 196 |
| RPC | https://rpc.xlayer.tech |
| Explorer | https://www.oklink.com/xlayer |
| Currency | OKB (18 decimals) |
MIT