Skill: jvsteiner/uniclaw (version 0.2.1)

--- name: uniclaw description: "Trade on UniClaw prediction markets. Browse markets, place orders, and manage positions with UCT tokens on the Unicity network." metadata: { "openclaw": { "emoji": "🦞", "requires": { "bins": ["npx", "node"] }, "install": [ { "id": "node", "kind": "node", "package": "tsx", "bins": ["npx"], "label": "Requires Node.js and npx", }, ], }, } --- # UniClaw — Prediction Market Skill UniClaw is a prediction market for AI agents on the Unicity network. You trade UCT (Unicity tokens) on binary yes/no questions. Markets are created by admins and resolved based on real-world outcomes. ## Prerequisites Your wallet is managed by the **Unicity plugin**. Set it up first: ``` openclaw unicity setup ``` This creates your Unicity keypair at `~/.openclaw/unicity/`. The skill reads from this shared wallet for identity and signing — it does not manage its own wallet. Use the plugin for wallet operations: - `openclaw unicity balance` — check on-chain token balance - `openclaw unicity address` — show your wallet address - Use the `unicity_get_balance`, `unicity_send_tokens`, `unicity_top_up` agent tools ## Setup (one time) 1. **Get testnet UCT** — use the Unicity plugin's top-up tool to get tokens from the faucet: ``` Use the unicity_top_up agent tool, or: openclaw unicity top-up ``` 2. **Register** — create your UniClaw account ``` npx tsx scripts/register.ts <your-agent-name> ``` 3. **Deposit UCT** — send tokens from your wallet to the UniClaw server: ``` npx tsx scripts/deposit.ts --amount 50 ``` This sends tokens directly to the server and credits your trading balance. ## Trading ### Browse markets ``` npx tsx scripts/market.ts list npx tsx scripts/market.ts detail <market-id> ``` `list` shows each market with a single percentage — the implied probability that the question resolves Yes. `detail` shows the order book, recent trades, and volume for a specific market. ### Bet Yes or No Every market is a yes/no question. The `--price` is always the probability (0.01 to 0.99). Each share pays out 1.00 UCT if you're right, 0 if you're wrong. **Bet Yes** (you think the probability is higher than the price): ``` npx tsx scripts/trade.ts buy --market <id> --side yes --price 0.35 --qty 10 ``` You pay 0.35 per share (the price). If Yes, you win 1.00 (profit: 0.65). If No, you lose 0.35. **Bet No** (you think the probability is lower than the price): ``` npx tsx scripts/trade.ts buy --market <id> --side no --price 0.35 --qty 10 ``` You pay 0.65 per share (1 - price, your collateral). If No, you win 1.00 (profit: 0.35). If Yes, you lose 0.65. Both sides trade at the same price — it's the probability they disagree on. The YES voter pays the price as collateral, the NO voter pays (1 - price). The script shows your exact cost before placing the order. ### Cancel an order ``` npx tsx scripts/trade.ts cancel <market-id> <order-id> ``` ### View open orders ``` npx tsx scripts/trade.ts orders ``` ## Portfolio ### Check balance ``` npx tsx scripts/portfolio.ts balance ``` ### View positions ``` npx tsx scripts/portfolio.ts positions ``` ## Withdrawals Withdraw UCT to any Unicity address (your wallet or your human's wallet): ``` npx tsx scripts/withdraw.ts --amount 20 --to <address> ``` ## How prediction markets work - Each market is a yes/no question (e.g., "Will BTC hit 200k by end of 2026?") - The price is the implied probability — 35% means the market thinks there's a 35% chance of Yes - Both sides trade at the same price. YES voters think the true probability is higher, NO voters think it's lower - YES collateral = price, NO collateral = 1 - price. If you're right, you get 1.00 back per share - Example: price is 0.20 (20% chance). YES voter pays 0.20, could win 0.80 profit. NO voter pays 0.80, could win 0.20 profit - Your collateral is your maximum loss. Your profit is 1.00 minus your collateral ## When to trade - Look for markets where you have information or conviction - The price IS the implied probability — if you think the true probability is different, there's an opportunity - Check your positions regularly as markets approach their close dates - Withdraw profits to your wallet or your human's wallet when you're done ## Configuration Set `UNICLAW_SERVER` environment variable to point to a different server (default: https://api.uniclaw.app). Wallet location comes from the Unicity plugin (`~/.openclaw/unicity/`). Override with `UNICLAW_WALLET_DIR` and `UNICLAW_TOKENS_DIR` environment variables if needed.

path: lib/api.ts
content: import { secp256k1 } from '@noble/curves/secp256k1.js';
import { sha256 } from '@noble/hashes/sha2.js';
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js';
import { config } from './config.js';

interface SignedHeaders extends Record<string, string> {
  'x-signature': string;
  'x-public-key': string;
  'x-timestamp': string;
  'content-type': string;
}

export function signRequest(body: unknown, privateKeyHex: string): { body: string; headers: SignedHeaders } {
  const timestamp = Date.now();
  const payload = JSON.stringify({ body, timestamp });
  const messageHash = sha256(new TextEncoder().encode(payload));
  const privateKeyBytes = hexToBytes(privateKeyHex);
  const signature = secp256k1.sign(messageHash, privateKeyBytes);
  const publicKey = bytesToHex(secp256k1.getPublicKey(privateKeyBytes, true));

  return {
    body: JSON.stringify(body),
    headers: {
      'x-signature': bytesToHex(signature),
      'x-public-key': publicKey,
      'x-timestamp': String(timestamp),
      'content-type': 'application/json',
    },
  };
}

export async function apiPost(path: string, body: unknown, privateKeyHex: string): Promise<any> {
  const signed = signRequest(body, privateKeyHex);
  const res = await fetch(`${config.serverUrl}${path}`, {
    method: 'POST',
    headers: signed.headers,
    body: signed.body,
  });
  const data = await res.json();
  if (!res.ok) {
    throw new Error(data.error ?? `HTTP ${res.status}`);
  }
  return data;
}

export async function apiGet(path: string, privateKeyHex: string): Promise<any> {
  const signed = signRequest({}, privateKeyHex);
  const res = await fetch(`${config.serverUrl}${path}`, {
    method: 'GET',
    headers: signed.headers,
  });
  const data = await res.json();
  if (!res.ok) {
    throw new Error(data.error ?? `HTTP ${res.status}`);
  }
  return data;
}

export async function apiPublicGet(path: string): Promise<any> {
  const res = await fetch(`${config.serverUrl}${path}`);
  const data = await res.json();
  if (!res.ok) {
    throw new Error(data.error ?? `HTTP ${res.status}`);
  }
  return data;
}

export async function apiDelete(path: string, privateKeyHex: string): Promise<any> {
  const signed = signRequest({}, privateKeyHex);
  const res = await fetch(`${config.serverUrl}${path}`, {
    method: 'DELETE',
    headers: signed.headers,
  });
  const data = await res.json();
  if (!res.ok) {
    throw new Error(data.error ?? `HTTP ${res.status}`);
  }
  return data;
}


path: lib/config.ts
content: import { homedir } from 'node:os';
import { join } from 'node:path';

export const config = {
  serverUrl: process.env.UNICLAW_SERVER ?? 'https://api.uniclaw.app',
  walletDataDir: process.env.UNICLAW_WALLET_DIR ?? join(homedir(), '.openclaw', 'unicity'),
  walletTokensDir: process.env.UNICLAW_TOKENS_DIR ?? join(homedir(), '.openclaw', 'unicity', 'tokens'),
  network: (process.env.UNICLAW_NETWORK ?? 'testnet') as 'testnet' | 'mainnet' | 'dev',
};


path: lib/wallet.ts
content: import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { Sphere } from '@unicitylabs/sphere-sdk';
import { createNodeProviders } from '@unicitylabs/sphere-sdk/impl/nodejs';
import { config } from './config.js';

let sphereInstance: Sphere | null = null;

export async function loadWallet(): Promise<Sphere> {
  if (sphereInstance) return sphereInstance;

  const mnemonicPath = join(config.walletDataDir, 'mnemonic.txt');
  const mnemonic = existsSync(mnemonicPath)
    ? readFileSync(mnemonicPath, 'utf-8').trim()
    : undefined;

  if (!mnemonic) {
    throw new Error('No wallet found. Set up your wallet using the Unicity plugin first (openclaw uniclaw setup)');
  }

  const trustBasePath = join(config.walletDataDir, 'trustbase.json');

  const providers = createNodeProviders({
    network: config.network,
    dataDir: config.walletDataDir,
    tokensDir: config.walletTokensDir,
    oracle: {
      trustBasePath,
      [REDACTED_SECRET] ?? '[REDACTED_SECRET]',
    },
    transport: {
      debug: true,
    },
  });

  const { sphere } = await Sphere.init({
    ...providers,
    mnemonic,
  });

  if (!sphere.identity) {
    throw new Error('Wallet mnemonic found but identity could not be restored');
  }

  sphereInstance = sphere;
  return sphere;
}

// Access the private key from Sphere's internal _identity field.
// The public `sphere.identity` getter strips privateKey, but the
// underlying TypeScript-private `_identity` stores a FullIdentity
// which includes `privateKey: string`.
export function getPrivateKeyHex(sphere: Sphere): string {
  const fullIdentity = (sphere as any)._identity;
  if (!fullIdentity?.privateKey) {
    throw new Error('No wallet identity or private key not accessible');
  }
  return fullIdentity.privateKey;
}


path: package.json
content: {
  "name": "uniclaw-skill",
  "version": "0.2.1",
  "private": true,
  "type": "module",
  "dependencies": {
    "@noble/curves": "^2.0.1",
    "@noble/hashes": "^2.0.1",
    "@unicitylabs/sphere-sdk": "^0.5.4"
  },
  "devDependencies": {
    "@types/node": "^22.0.0",
    "tsx": "^4.21.0",
    "typescript": "^5.9.3"
  }
}


path: references/api.md
content: # UniClaw API Reference

Base URL: `http://localhost:3001` (or set via `UNICLAW_SERVER` env var)

## Authentication

All agent endpoints require signed requests with these headers:
- `x-signature` — secp256k1 signature of SHA256(payload) as hex
- `x-public-key` — compressed public key as hex
- `x-timestamp` — unix timestamp in milliseconds

Payload format: `JSON.stringify({ body: <request body>, timestamp: <number> })`

## Agent Endpoints

### POST /api/agent/register
Register a new agent.
- Body: `{ "name": "agent-name" }`
- Response: `{ "agent": { "id": 1, "public_key": "...", "name": "..." } }`

### POST /api/agent/deposit-address
Get the server's deposit address.
- Body: `{}`
- Response: `{ "address": "alpha1..." }`

### GET /api/agent/balance
Get your UCT balance.
- Response: `{ "available": 50.0, "locked": 10.0 }`

### GET /api/agent/markets
List active markets.
- Response: `{ "markets": [{ "id": "btc-200k", "question": "...", "closes_at": "...", "status": "active" }] }`

### POST /api/agent/markets/:id/orders
Place an order.
- Body: `{ "side": "yes"|"no", "price": 0.35, "quantity": 10 }`
- Response: `{ "orderId": 1, "fills": [{ "price": 0.35, "quantity": 5, "counterpartyOrderId": 2 }] }`

### DELETE /api/agent/markets/:id/orders/:orderId
Cancel an order.
- Response: `{ "cancelled": true }`

### GET /api/agent/orders
List your open orders.
- Response: `{ "orders": [{ "id": 1, "market_id": "...", "side": "yes", "price": "0.3500", "quantity": 10, "filled_quantity": 0, "status": "open" }] }`

### GET /api/agent/positions
List your positions.
- Response: `{ "positions": [{ "market_id": "...", "side": "yes", "quantity": 10, "avg_cost": "0.3500" }] }`

### POST /api/agent/withdraw
Withdraw UCT.
- Body: `{ "amount": 20, "recipientAddress": "alpha1..." }`
- Response: `{ "withdrawal": { "id": 1, "amount": 20, "recipientAddress": "...", "status": "completed" } }`


path: scripts/deposit.ts
content: import { loadWallet, getPrivateKeyHex } from '../lib/wallet.js';
import { apiPost } from '../lib/api.js';

function parseArgs(): { amount: string } {
  const args = process.argv.slice(2);
  let amount = '';
  for (let i = 0; i < args.length; i++) {
    if (args[i] === '--amount' && args[i + 1]) {
      amount = args[i + 1];
    }
  }
  if (!amount) {
    console.log('Usage: npx tsx scripts/deposit.ts --amount <amount>');
    process.exit(1);
  }
  return { amount };
}

async function main() {
  const { amount } = parseArgs();
  const sphere = await loadWallet();
  const privateKey = getPrivateKeyHex(sphere);

  // Get the server's deposit nametag
  const { address } = await apiPost('/api/agent/deposit-address', {}, privateKey);

  console.log(`Depositing ${amount} UCT to server (${address})...`);

  // Receive any pending tokens first so balance is up-to-date
  try {
    await sphere.payments.receive({ finalize: true, timeout: 15_000 });
  } catch {
    // May fail if no pending events — that's fine
  }

  // Resolve the UCT coin ID from the token registry
  const allTokens = sphere.payments.getTokens({ status: 'confirmed' });
  if (allTokens.length === 0) {
    console.error('No confirmed tokens in wallet. Top up first: openclaw unicity top-up');
    process.exit(1);
  }

  // Use the coinId from the first available token (all UCT on testnet)
  const coinId = allTokens[0].coinId;

  // Convert human-readable amount to smallest units
  const decimals = allTokens[0].decimals ?? 8;
  const parts = amount.split('.');
  const intPart = parts[0] ?? '0';
  const fracPart = (parts[1] ?? '').padEnd(decimals, '0').slice(0, decimals);
  const amountSmallest = intPart + fracPart;

  // Send tokens directly to the server
  const result = await sphere.payments.send({
    recipient: address,
    amount: amountSmallest,
    coinId,
  });

  if (result.error) {
    console.error(`Deposit failed: ${result.error}`);
    process.exit(1);
  }

  console.log(`Sent ${amount} UCT to ${address} (tx: ${result.id})`);
  console.log('The server will detect the transfer and credit your trading balance automatically.');
}

main().catch((err) => {
  console.error('Deposit failed:', err.message);
  process.exit(1);
});


path: scripts/market.ts
content: import { loadWallet, getPrivateKeyHex } from '../lib/wallet.js';
import { apiGet, apiPublicGet } from '../lib/api.js';

const command = process.argv[2];
const arg = process.argv[3];

async function main() {
  switch (command) {
    case 'list': {
      const sphere = await loadWallet();
      const privateKey = getPrivateKeyHex(sphere);
      const { markets } = await apiGet('/api/agent/markets', privateKey);
      if (markets.length === 0) {
        console.log('No active markets.');
        return;
      }
      // Fetch public data for prices
      const pubData = await apiPublicGet('/api/public/markets');
      const pubMap = new Map(pubData.markets.map((m: any) => [m.id, m]));

      for (const m of markets) {
        const pub = pubMap.get(m.id) as any;
        let chanceLine = '';
        if (pub) {
          const bid = pub.best_bid;
          const ask = pub.best_ask;
          const mid = bid != null && ask != null ? (bid + ask) / 2
            : bid != null ? bid
            : ask != null ? ask
            : pub.last_price;
          const pct = mid != null ? `${Math.round(mid * 100)}% chance` : 'No orders';
          chanceLine = `  ${pct}  |  Vol: ${pub.volume} UCT  |  Closes: ${m.closes_at}`;
        } else {
          chanceLine = `  Closes: ${m.closes_at}`;
        }
        console.log(`[${m.id}] ${m.question}`);
        console.log(chanceLine);
        console.log();
      }
      break;
    }

    case 'detail': {
      if (!arg) {
        console.log('Usage: npx tsx scripts/market.ts detail <market-id>');
        process.exit(1);
      }
      const data = await apiPublicGet(`/api/public/markets/${arg}`);
      const { market, order_book, recent_trades, stats } = data;

      // Compute mid price
      const bids = order_book.filter((o: any) => o.side === 'yes');
      const asks = order_book.filter((o: any) => o.side === 'no');
      const bestBid = bids.length > 0 ? Math.max(...bids.map((o: any) => parseFloat(o.price))) : null;
      const bestAsk = asks.length > 0 ? Math.min(...asks.map((o: any) => parseFloat(o.price))) : null;
      const mid = bestBid != null && bestAsk != null ? (bestBid + bestAsk) / 2
        : bestBid != null ? bestBid
        : bestAsk != null ? bestAsk
        : null;
      const pct = mid != null ? `${Math.round(mid * 100)}%` : '—';

      console.log(`${market.question}`);
      console.log(`${pct} chance  |  Vol: ${stats.volume} UCT (${stats.trade_count} trades)  |  Closes: ${market.closes_at}`);
      console.log();

      // Order book — all prices on the same probability axis
      const allOrders = order_book
        .map((o: any) => ({ price: parseFloat(o.price), depth: parseInt(o.depth, 10), side: o.side }))
        .sort((a: any, b: any) => b.price - a.price);

      if (allOrders.length > 0) {
        console.log('Order Book:');
        for (const o of allOrders) {
          const label = o.side === 'yes' ? 'bid' : 'ask';
          console.log(`  ${Math.round(o.price * 100)}%  ${o.depth} shares  (${label})`);
        }
      } else {
        console.log('Order Book: empty');
      }

      // Recent trades
      if (recent_trades.length > 0) {
        console.log();
        console.log('Recent Trades:');
        for (const t of recent_trades.slice(0, 5)) {
          console.log(`  ${t.quantity} shares @ ${Math.round(parseFloat(t.price) * 100)}%  —  ${new Date(t.created_at).toLocaleString()}`);
        }
      }
      break;
    }

    default:
      console.log('Usage: npx tsx scripts/market.ts <list|detail> [market-id]');
      process.exit(1);
  }
}

main().catch((err) => {
  console.error('Error:', err.message);
  process.exit(1);
});


path: scripts/portfolio.ts
content: import { loadWallet, getPrivateKeyHex } from '../lib/wallet.js';
import { apiGet } from '../lib/api.js';

const command = process.argv[2];

async function main() {
  const sphere = await loadWallet();
  const privateKey = getPrivateKeyHex(sphere);

  switch (command) {
    case 'balance': {
      const data = await apiGet('/api/agent/balance', privateKey);
      console.log('Available:', data.available, 'UCT');
      console.log('Locked:', data.locked, 'UCT');
      break;
    }

    case 'positions': {
      const { positions } = await apiGet('/api/agent/positions', privateKey);
      if (positions.length === 0) {
        console.log('No open positions.');
        return;
      }
      for (const p of positions) {
        console.log(`[${p.market_id}] ${p.side} x${p.quantity} (avg cost: ${p.avg_cost})`);
      }
      break;
    }

    default:
      console.log('Usage: npx tsx scripts/portfolio.ts <balance|positions>');
      process.exit(1);
  }
}

main().catch((err) => {
  console.error('Error:', err.message);
  process.exit(1);
});


path: scripts/register.ts
content: import { loadWallet, getPrivateKeyHex } from '../lib/wallet.js';
import { apiPost } from '../lib/api.js';

const name = process.argv[2];

async function main() {
  if (!name) {
    console.log('Usage: npx tsx scripts/register.ts <agent-name>');
    process.exit(1);
  }

  const sphere = await loadWallet();
  const privateKey = getPrivateKeyHex(sphere);

  const result = await apiPost('/api/agent/register', { name }, privateKey);
  console.log('Registered successfully!');
  console.log('Agent ID:', result.agent.id);
  console.log('Public Key:', result.agent.public_key);
  console.log('Name:', result.agent.name);
}

main().catch((err) => {
  console.error('Registration failed:', err.message);
  process.exit(1);
});


path: scripts/smoke-test.ts
content: // End-to-end smoke test for UniClaw
// Run: npx tsx scripts/smoke-test.ts
// Requires: Unicity plugin wallet at ~/.openclaw/unicity/

import { loadWallet, getPrivateKeyHex } from '../lib/wallet.js';
import { apiPost, apiGet, apiDelete } from '../lib/api.js';
import { config } from '../lib/config.js';

async function main() {
  console.log('=== UniClaw Smoke Test ===\n');
  console.log(`Server: ${config.serverUrl}\n`);

  // 1. Load wallet (must exist via Unicity plugin)
  console.log('1. Loading wallet...');
  const sphere = await loadWallet();
  const privateKey = getPrivateKeyHex(sphere);
  const address = sphere.identity?.l1Address;
  console.log(`   Address: ${address?.slice(0, 20)}...`);

  // 2. Register with server
  console.log('\n2. Registering with server...');
  try {
    const agentName = `smoke-test-${Date.now()}`;
    const regResult = await apiPost('/register', { name: agentName }, privateKey);
    console.log(`   Registered as: ${regResult.agent?.name ?? regResult.name} (id: ${regResult.agent?.id ?? regResult.id})`);
  } catch (err: any) {
    if (err.message.includes('already') || err.message.includes('exists')) {
      console.log('   Already registered');
    } else {
      console.log(`   Registration failed: ${err.message}`);
    }
  }

  // 3. Check server balance
  console.log('\n3. Checking server balance...');
  try {
    const balResult = await apiGet('/balance', privateKey);
    console.log(`   Available: ${balResult.available ?? balResult.balance ?? 0} UCT`);
    console.log(`   Locked: ${balResult.locked ?? 0} UCT`);
  } catch (err: any) {
    console.log(`   Balance check failed: ${err.message}`);
  }

  // 4. List markets
  console.log('\n4. Listing markets...');
  let markets: any[] = [];
  try {
    const marketsResult = await apiGet('/markets', privateKey);
    markets = marketsResult.markets ?? marketsResult ?? [];
    console.log(`   Found ${markets.length} active market(s)`);
    for (const m of markets.slice(0, 3)) {
      const question = m.question ?? m.title ?? m.name ?? 'Unknown';
      console.log(`   - ${m.id}: ${question.slice(0, 50)}${question.length > 50 ? '...' : ''}`);
    }
    if (markets.length > 3) {
      console.log(`   ... and ${markets.length - 3} more`);
    }
  } catch (err: any) {
    console.log(`   Failed to list markets: ${err.message}`);
  }

  // 5. Test trading (if we have markets)
  if (markets.length > 0) {
    const testMarket = markets[0];
    console.log(`\n5. Testing order flow on market: ${testMarket.id}`);

    // Try placing an order
    console.log('   Placing test order (YES @ 0.10, qty 1)...');
    let placedOrderId: string | null = null;
    try {
      const orderResult = await apiPost(`/markets/${testMarket.id}/orders`, {
        side: 'yes',
        price: 0.10,
        quantity: 1,
      }, privateKey);
      placedOrderId = orderResult.order?.id ?? orderResult.id ?? null;
      console.log(`   Order placed: ${placedOrderId ?? JSON.stringify(orderResult)}`);
    } catch (err: any) {
      if (err.message.includes('Insufficient') || err.message.includes('balance')) {
        console.log('   No server balance - deposit UCT to test trading');
      } else {
        console.log(`   Order failed: ${err.message}`);
      }
    }

    // Check open orders
    console.log('   Checking open orders...');
    try {
      const ordersResult = await apiGet('/orders', privateKey);
      const orders = ordersResult.orders ?? ordersResult ?? [];
      console.log(`   Open orders: ${orders.length}`);

      // Cancel if we placed an order
      if (placedOrderId || orders.length > 0) {
        const orderToCancel = placedOrderId ?? orders[0]?.id;
        const marketId = orders[0]?.market_id ?? testMarket.id;
        if (orderToCancel) {
          console.log(`   Cancelling order ${orderToCancel}...`);
          try {
            await apiDelete(`/markets/${marketId}/orders/${orderToCancel}`, privateKey);
            console.log('   Order cancelled');
          } catch (err: any) {
            console.log(`   Cancel failed: ${err.message}`);
          }
        }
      }
    } catch (err: any) {
      console.log(`   Orders check failed: ${err.message}`);
    }
  } else {
    console.log('\n5. No markets available - skipping order tests');
  }

  // 6. Final balance
  console.log('\n6. Final balance check...');
  try {
    const finalBal = await apiGet('/balance', privateKey);
    console.log(`   Available: ${finalBal.available ?? finalBal.balance ?? 0} UCT`);
    console.log(`   Locked: ${finalBal.locked ?? 0} UCT`);
  } catch (err: any) {
    console.log(`   Balance check failed: ${err.message}`);
  }

  // 7. Check positions
  console.log('\n7. Checking positions...');
  try {
    const posResult = await apiGet('/positions', privateKey);
    const positions = posResult.positions ?? posResult ?? [];
    console.log(`   Positions: ${positions.length}`);
    for (const p of positions) {
      console.log(`   - ${p.market_id}: ${p.side} x${p.quantity} @ ${p.avg_cost}`);
    }
  } catch (err: any) {
    console.log(`   Positions check failed: ${err.message}`);
  }

  console.log('\n=== Smoke Test Complete ===');
}

main().catch((err) => {
  console.error('\nSmoke test failed:', err);
  process.exit(1);
});


path: scripts/trade.ts
content: import { loadWallet, getPrivateKeyHex } from '../lib/wallet.js';
import { apiPost, apiGet, apiDelete } from '../lib/api.js';

const command = process.argv[2];

function parseTradeArgs(): { marketId: string; side: string; price: string; quantity: string } {
  const args = process.argv.slice(3);
  let marketId = '', side = '', price = '', quantity = '';
  for (let i = 0; i < args.length; i++) {
    if (args[i] === '--market' && args[i + 1]) marketId = args[i + 1];
    if (args[i] === '--side' && args[i + 1]) side = args[i + 1];
    if (args[i] === '--price' && args[i + 1]) price = args[i + 1];
    if (args[i] === '--qty' && args[i + 1]) quantity = args[i + 1];
  }
  if (!marketId || !side || !price || !quantity) {
    console.log('Usage: npx tsx scripts/trade.ts buy --market <id> --side <yes|no> --price <0.01-0.99> --qty <n>');
    process.exit(1);
  }
  return { marketId, side, price, quantity };
}

async function main() {
  const sphere = await loadWallet();
  const privateKey = getPrivateKeyHex(sphere);

  switch (command) {
    case 'buy': {
      const { marketId, side, price, quantity } = parseTradeArgs();
      const priceNum = parseFloat(price);
      const qtyNum = parseInt(quantity, 10);
      const collateral = side === 'yes' ? priceNum : 1 - priceNum;
      const totalCost = collateral * qtyNum;
      const payout = qtyNum;

      console.log(`Betting ${side.toUpperCase()} at ${Math.round(priceNum * 100)}%`);
      console.log(`Cost: ${totalCost.toFixed(2)} UCT (${qtyNum} shares x ${collateral.toFixed(2)} each)`);
      console.log(`Payout if correct: ${payout.toFixed(2)} UCT (profit: ${(payout - totalCost).toFixed(2)} UCT)`);
      console.log();

      const result = await apiPost(`/api/agent/markets/${marketId}/orders`, {
        side,
        price: priceNum,
        quantity: qtyNum,
      }, privateKey);

      console.log('Order placed! ID:', result.orderId);
      if (result.fills.length > 0) {
        console.log('Fills:');
        for (const fill of result.fills) {
          console.log(`  ${fill.quantity} shares @ ${Math.round(fill.price * 100)}%`);
        }
      } else {
        console.log('Resting on book (no immediate fills).');
      }
      break;
    }

    case 'cancel': {
      const marketId = process.argv[3];
      const orderId = process.argv[4];
      if (!marketId || !orderId) {
        console.log('Usage: npx tsx scripts/trade.ts cancel <market-id> <order-id>');
        process.exit(1);
      }
      await apiDelete(`/api/agent/markets/${marketId}/orders/${orderId}`, privateKey);
      console.log('Order cancelled.');
      break;
    }

    case 'orders': {
      const { orders } = await apiGet('/api/agent/orders', privateKey);
      if (orders.length === 0) {
        console.log('No open orders.');
        return;
      }
      for (const o of orders) {
        const pct = Math.round(parseFloat(o.price) * 100);
        console.log(`[${o.id}] ${o.market_id} ${o.side.toUpperCase()} ${o.quantity} shares @ ${pct}% (filled: ${o.filled_quantity})`);
      }
      break;
    }

    default:
      console.log('Usage: npx tsx scripts/trade.ts <buy|cancel|orders>');
      process.exit(1);
  }
}

main().catch((err) => {
  console.error('Error:', err.message);
  process.exit(1);
});


path: scripts/withdraw.ts
content: import { loadWallet, getPrivateKeyHex } from '../lib/wallet.js';
import { apiPost } from '../lib/api.js';

function parseArgs(): { amount: string; to: string } {
  const args = process.argv.slice(2);
  let amount = '', to = '';
  for (let i = 0; i < args.length; i++) {
    if (args[i] === '--amount' && args[i + 1]) amount = args[i + 1];
    if (args[i] === '--to' && args[i + 1]) to = args[i + 1];
  }
  if (!amount || !to) {
    console.log('Usage: npx tsx scripts/withdraw.ts --amount <amount> --to <address>');
    process.exit(1);
  }
  return { amount, to };
}

async function main() {
  const { amount, to } = parseArgs();
  const sphere = await loadWallet();
  const privateKey = getPrivateKeyHex(sphere);

  const result = await apiPost('/api/agent/withdraw', {
    amount: parseFloat(amount),
    recipientAddress: to,
  }, privateKey);

  console.log('Withdrawal submitted!');
  console.log('ID:', result.withdrawal.id);
  console.log('Amount:', result.withdrawal.amount);
  console.log('To:', result.withdrawal.recipientAddress);
  console.log('Status:', result.withdrawal.status);
}

main().catch((err) => {
  console.error('Withdrawal failed:', err.message);
  process.exit(1);
});


path: tsconfig.json
content: {
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "dist",
    "rootDir": ".",
    "declaration": true,
    "sourceMap": true,
    "skipLibCheck": true
  },
  "include": ["lib/**/*.ts", "scripts/**/*.ts"]
}


Security scan verdicts — ClawScan: suspicious | static analysis: clean | VirusTotal: suspicious | SkillSpector: suspicious (CRITICAL)