An open API service indexing awesome lists of open source software.

https://github.com/pablomg-dev/xlayer-agentic-vault

Autonomous agent for asset management on X Layer
https://github.com/pablomg-dev/xlayer-agentic-vault

Last synced: about 1 month ago
JSON representation

Autonomous agent for asset management on X Layer

Awesome Lists containing this project

README

          

# xlayer-agentic-vault

> 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.

## ๐Ÿค– AI-Assisted Development

This project was built with the assistance of:
- **[Claude](https://claude.ai)** (Anthropic) โ€” architecture design, prompt engineering, code review
- **[MiniMax M2.5](https://www.minimaxi.com)** (free tier via OpenCode) โ€” code generation and implementation

## Overview

- **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 โ€” `any` is forbidden
- SOLID principles throughout
- 20/20 unit tests passing

## Tech Stack

- **Runtime**: Node.js 20 + TypeScript 6
- **Blockchain**: viem for X Layer RPC
- **Smart Contracts**: Solidity 0.8.0 + Hardhat
- **Testing**: Vitest
- **Environment**: dotenv

## Project Structure

```
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
```

## Smart Contract โ€” X Layer Mainnet

**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)`

## Live Transactions on X Layer Mainnet

| Operation | Tx Hash | Gas Used |
|-----------|---------|----------|
| Deposit 0.001 OKB | `0x0c0d5569...` | 45,165 |
| Withdraw 0.001 OKB | `0x5486973f...` | 33,146 |

## Installation

```bash
npm install
```

## Configuration

```bash
cp .env.example .env
```

```dotenv
RPC_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
```

## Scripts

| 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 |

## Running the Agent

```bash
npm run run:agent:ts-node
```

Example 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
==================================================
```

## Deploy New Vault Contract

```bash
npx hardhat compile
npx hardhat run scripts/deploy.cjs --network xlayer
```

## Core Interfaces

### IWalletAgent
- `connect()` โ€” initialize wallet connection
- `getBalance(tokenAddress)` โ€” balance in wei
- `simulateTransaction(tx)` โ€” dry-run before execution
- `executeTransaction(tx)` โ€” submit to X Layer

### IVault
- `deposit(tokenAddress, amount)` โ€” deposit into vault contract
- `withdraw(tokenAddress, amount, recipient)` โ€” withdraw from vault
- `getVaultBalance(tokenAddress)` โ€” current contract balance
- `getDepositHistory(tokenAddress)` โ€” operation history

### IPaymentHandler
- `handlePayment(response)` โ€” handle HTTP 402 payment request
- `verifyPayment(txHash)` โ€” confirm payment on-chain
- `getPaymentHistory()` โ€” payment history

### IOrchestrator
- `start()` โ€” initialize all modules
- `stop()` โ€” graceful shutdown
- `getStatus()` โ€” `idle` | `running` | `stopped` | `error`
- `runCycle()` โ€” one autonomous decision cycle

## Agent Decision Logic

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 |

## X Layer Network

| Property | Value |
|----------|-------|
| Chain ID | 196 |
| RPC | https://rpc.xlayer.tech |
| Explorer | https://www.oklink.com/xlayer |
| Currency | OKB (18 decimals) |

## License

MIT