Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/core/src/runtime/PaperTradingState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import type { TradeResult, OrderBook, Position, OrderOpts, BinanceTradeResult, B

const STORAGE_KEY = '__paper_state';

/** Starting balance of each paper pot (Polymarket USDC and Binance USDT) when the bot config has no paperBalance */
export const DEFAULT_PAPER_BALANCE = 10_000;

interface PaperPosition {
tokenId: string;
size: number;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/runtime/ScriptBotRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { BotRecord } from '../bots/types';
import type { WalletManager } from '../bots/WalletManager';
import { ScriptRunner } from './ScriptRunner';
import type { LedgerReader, RpcHandlerDeps } from './RpcHandler';
import { PaperTradingState } from './PaperTradingState';
import { PaperTradingState, DEFAULT_PAPER_BALANCE } from './PaperTradingState';
import type { BotStorageRepository } from '../db/repositories/BotStorageRepository';
import type { BotTradeRepository } from '../db/repositories/BotTradeRepository';
import type { OrderService } from '../orders/OrderService';
Expand Down Expand Up @@ -74,7 +74,7 @@ export interface ScriptBotRunnerDeps {
copy?: RpcHandlerDeps['copy'];
}

export const DEFAULT_PAPER_BALANCE = 10_000;
export { DEFAULT_PAPER_BALANCE };

export class ScriptBotRunner extends BaseBotRunner {
private scriptRunner: ScriptRunner | null = null;
Expand Down
106 changes: 58 additions & 48 deletions packages/core/src/services/BotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { ServiceError, conflict, notFound } from './errors';
import type { BotCreateInput, BotUpdateInput } from './schemas';
import type { LimitsConfig, MandateConfig } from '../config/schema';
import { copyLeadersOf, MAX_COPY_LEADERS, type CopyLeaderService } from './CopyLeaderService';
import { getBinancePrice } from '../exchange/binance/client';
import { DEFAULT_PAPER_BALANCE } from '../runtime/PaperTradingState';

// Module-level cache for token → market info resolution (static data)
const tokenInfoCache = new Map<string, { market: string; outcome: string }>();
Expand Down Expand Up @@ -368,6 +370,8 @@ export interface BotServiceDeps {
cachedTokenMarketInfo?: (tokenId: string) => StrategyMarketInfo | null;
/** Every market of an event: `fetch` may call Gamma (creates), `cached` does not (updates) */
eventMarketIds?: { fetch: (eventId: string) => Promise<string[] | null>; cached: (eventId: string) => string[] | null };
/** Current Binance spot price of a pair such as ETHUSDT (paper valuation); default: the public ticker */
binancePrice?: (symbol: string) => Promise<number>;
/** A ledger forecast by id (resolution-rule `forecast_id`) */
getForecast?: (id: string) => { tokenId: string; status: string } | null;
/** Mandate in force (`strategies.allow` gates agent strategy bots); null = config unavailable */
Expand Down Expand Up @@ -1045,6 +1049,12 @@ export class BotService {
} catch (err) {
logger.error(`Failed to fetch price for ${pos.symbol}: ${err}`);
}
} else if (pos.exchange === 'binance' && pos.symbol) {
try {
currentPrice = await this.binancePrice(pos.symbol);
} catch (err) {
logger.error(`Failed to fetch Binance price for ${pos.symbol}: ${err}`);
}
}

const currentValue = currentPrice != null ? pos.netQuantity * currentPrice : null;
Expand All @@ -1054,9 +1064,53 @@ export class BotService {
return positions;
}

private binancePrice(symbol: string): Promise<number> {
return this.deps.binancePrice ? this.deps.binancePrice(symbol) : getBinancePrice(symbol).then((p) => p.price);
}

/**
* A paper bot's cash and positions value. The paper state has two pots seeded with the same starting
* balance: Polymarket USDC (`balance`, positions priced on Gamma) and Binance (`binanceBalances`, assets
* priced against USDT). Cash is the Polymarket cash plus the Binance USDT change, so a bot trading on one
* exchange reads as one balance and a bot trading on both adds both pots' changes. Unpriced assets are left out.
*/
private async paperValue(
botId: string,
config: { paperBalance?: unknown },
rawPositions: ReturnType<BotTradeRepository['getPositions']>
): Promise<{ cashBalance: number; positionsValue: number }> {
const paperState = this.deps.storageRepo.get(botId, '__paper_state') as {
balance?: number;
binanceBalances?: Record<string, number>;
} | null;
let cashBalance = paperState?.balance ?? (config.paperBalance ? Number(config.paperBalance) : 0);

const polymarket = rawPositions.filter((p) => p.exchange === 'polymarket' && p.symbol);
const binanceBalances = paperState?.binanceBalances;
const binanceAssets = Object.entries(binanceBalances ?? {}).filter(([asset, qty]) => asset !== 'USDT' && qty > 0);
const [polymarketPrices, binancePrices] = await Promise.all([
Promise.allSettled(polymarket.map((p) => fetchCurrentPrice(p.symbol))),
Promise.allSettled(binanceAssets.map(([asset]) => this.binancePrice(`${asset}USDT`))),
]);

let positionsValue = 0;
polymarketPrices.forEach((r, i) => {
if (r.status === 'fulfilled' && r.value != null) positionsValue += polymarket[i].netQuantity * r.value;
});
binancePrices.forEach((r, i) => {
if (r.status === 'fulfilled') positionsValue += binanceAssets[i][1] * r.value;
});

if (binanceBalances) {
const seed = Number(config.paperBalance) > 0 ? Number(config.paperBalance) : DEFAULT_PAPER_BALANCE;
cashBalance += (binanceBalances.USDT ?? 0) - seed;
}
return { cashBalance, positionsValue };
}

/** Cash balance, positions value and P&L of one bot */
async stats(botId: string): Promise<BotStatsView> {
const { botRepo, walletRepo, tradeRepo, storageRepo, settingsRepo } = this.deps;
const { botRepo, walletRepo, tradeRepo, settingsRepo } = this.deps;
const botRow = botRepo.findById(botId);
if (!botRow) throw notFound('Bot not found');

Expand All @@ -1068,30 +1122,8 @@ export class BotService {
let pnlAllTime: number | null = null;

if (mode === 'paper') {
// Paper mode: read state from bot_storage
const paperState = storageRepo.get(botId, '__paper_state') as {
balance?: number;
positions?: Array<{ tokenId: string; size: number; avgPrice: number }>;
binanceBalances?: Record<string, number>;
} | null;

cashBalance = paperState?.balance ?? (config.paperBalance ? Number(config.paperBalance) : 0);
initialBalance = config.paperBalance ? Number(config.paperBalance) : null;

// Paper positions value from trade history + Gamma API prices
const rawPositions = tradeRepo.getPositions(botId);
for (const pos of rawPositions) {
if (pos.exchange === 'polymarket' && pos.symbol) {
try {
const price = await fetchCurrentPrice(pos.symbol);
if (price != null) {
positionsValue += pos.netQuantity * price;
}
} catch {
/* ignore */
}
}
}
({ cashBalance, positionsValue } = await this.paperValue(botId, config, tradeRepo.getPositions(botId)));

const totalValue = cashBalance + positionsValue;
pnlAllTime = initialBalance != null ? totalValue - initialBalance : null;
Expand Down Expand Up @@ -1139,7 +1171,7 @@ export class BotService {

/** Dashboard stats for all bots: totalValue, positionCount and P&L per bot, fetched concurrently */
async dashboardStats(): Promise<BotDashboardStat[]> {
const { botRepo, walletRepo, tradeRepo, storageRepo, settingsRepo } = this.deps;
const { botRepo, walletRepo, tradeRepo, settingsRepo } = this.deps;
const rows = botRepo.findAll({});
const settings = settingsRepo.get();

Expand All @@ -1153,33 +1185,11 @@ export class BotService {
try {
if (mode === 'paper') {
const config = JSON.parse(botRow.config || '{}');
const paperState = storageRepo.get(botRow.id, '__paper_state') as {
balance?: number;
positions?: Array<{ tokenId: string; size: number; avgPrice: number }>;
} | null;

const cashBalance = paperState?.balance ?? (config.paperBalance ? Number(config.paperBalance) : 0);
const initialBalance = config.paperBalance ? Number(config.paperBalance) : null;
let positionsValue = 0;

const rawPositions = tradeRepo.getPositions(botRow.id);
positionCount = rawPositions.length;

// Fetch prices concurrently for paper positions
const priceResults = await Promise.allSettled(
rawPositions
.filter((p) => p.exchange === 'polymarket' && p.symbol)
.map(async (pos) => {
const price = await fetchCurrentPrice(pos.symbol);
return { netQuantity: pos.netQuantity, price };
})
);
for (const pr of priceResults) {
if (pr.status === 'fulfilled' && pr.value.price != null) {
positionsValue += pr.value.netQuantity * pr.value.price;
}
}

const { cashBalance, positionsValue } = await this.paperValue(botRow.id, config, rawPositions);
totalValue = cashBalance + positionsValue;
pnlAllTime = initialBalance != null ? totalValue - initialBalance : null;
} else {
Expand Down
144 changes: 144 additions & 0 deletions tests/runtime/bot-service-paper-stats.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { BotService } from '../../packages/core/src/services/BotService';

/**
* Paper bot valuation (bot Overview, dashboard Bot Overview, Open Positions). A paper bot has two simulated
* pots seeded with the same starting balance: Polymarket USDC (`balance`) and Binance (`binanceBalances`).
* Both must count, priced at the current market.
*/

const ETH_PRICE = 2300;
const ETH_QTY = 0.0088;

function makeBotRow(overrides: Record<string, unknown> = {}) {
return {
id: 'bot-1',
name: 'Paper Bot',
script_id: 'script-1',
wallet_id: 'wallet-1',
config: JSON.stringify({ paperBalance: 100 }),
status: 'running',
enabled: 1,
mode: 'paper',
...overrides,
};
}

function position(symbol: string, exchange: string, netQuantity: number, totalCost: number) {
return { symbol, exchange, market: null, outcome: null, netQuantity, totalCost, tradeCount: 1 };
}

function makeService(opts: {
row?: Record<string, unknown>;
paperState: unknown;
positions: ReturnType<typeof position>[];
binancePrice?: (symbol: string) => Promise<number>;
}) {
const row = makeBotRow(opts.row);
const binancePrice = opts.binancePrice ?? vi.fn(async (symbol: string) => (symbol === 'ETHUSDT' ? ETH_PRICE : Promise.reject(new Error(`no price for ${symbol}`))));
const service = new BotService({
botRepo: { findById: vi.fn().mockReturnValue(row), findAll: vi.fn().mockReturnValue([row]) } as any,
walletRepo: { findById: vi.fn().mockReturnValue(null) } as any,
scriptRepo: {} as any,
tradeRepo: {
getPositions: vi.fn().mockReturnValue(opts.positions),
findByBotId: vi.fn().mockReturnValue([]),
updateMarketInfo: vi.fn(),
} as any,
storageRepo: { get: vi.fn((_id: string, key: string) => (key === '__paper_state' ? opts.paperState : null)) } as any,
settingsRepo: { get: vi.fn().mockReturnValue({}) } as any,
botManager: () => null,
walletManager: () => null,
binancePrice,
});
return { service, binancePrice };
}

describe('BotService paper valuation', () => {
beforeEach(() => {
// Gamma: every Polymarket token is priced 0.6
vi.stubGlobal(
'fetch',
vi.fn(async (url: string) => {
const tokenId = new URL(url).searchParams.get('clob_token_ids');
return new Response(JSON.stringify([{ clobTokenIds: JSON.stringify([tokenId]), outcomePrices: JSON.stringify(['0.6']) }]));
})
);
});
afterEach(() => vi.unstubAllGlobals());

describe('a bot trading on Binance', () => {
const paperState = { balance: 100, positions: [], binanceBalances: { USDT: 80, ETH: ETH_QTY } };
const positions = [position('ETHUSDT', 'binance', ETH_QTY, 20)];

it('stats count the Binance USDT cash and the held asset at its price', async () => {
const { service } = makeService({ paperState, positions });
const stats = await service.stats('bot-1');
expect(stats.cashBalance).toBeCloseTo(80);
expect(stats.positionsValue).toBeCloseTo(ETH_QTY * ETH_PRICE);
expect(stats.totalValue).toBeCloseTo(80 + ETH_QTY * ETH_PRICE);
expect(stats.initialBalance).toBe(100);
expect(stats.pnlAllTime).toBeCloseTo(80 + ETH_QTY * ETH_PRICE - 100);
});

it('dashboard stats value the bot the same way', async () => {
const { service } = makeService({ paperState, positions });
const [stat] = await service.dashboardStats();
expect(stat.totalValue).toBeCloseTo(80 + ETH_QTY * ETH_PRICE);
expect(stat.pnlAllTime).toBeCloseTo(80 + ETH_QTY * ETH_PRICE - 100);
expect(stat.positionCount).toBe(1);
});

it('positions price the Binance row', async () => {
const { service } = makeService({ paperState, positions });
const [row] = await service.positions('bot-1');
expect(row.currentPrice).toBe(ETH_PRICE);
expect(row.currentValue).toBeCloseTo(ETH_QTY * ETH_PRICE);
});

it('a failed price leaves that asset out (cash still counts)', async () => {
const { service } = makeService({ paperState, positions, binancePrice: vi.fn().mockRejectedValue(new Error('down')) });
const stats = await service.stats('bot-1');
expect(stats.cashBalance).toBeCloseTo(80);
expect(stats.positionsValue).toBe(0);
const [row] = await service.positions('bot-1');
expect(row.currentPrice).toBeNull();
});

it('without paperBalance the Binance pot is measured against the default seed', async () => {
const { service } = makeService({
row: { config: '{}' },
paperState: { balance: 10_000, positions: [], binanceBalances: { USDT: 9_980, ETH: ETH_QTY } },
positions,
});
const stats = await service.stats('bot-1');
expect(stats.cashBalance).toBeCloseTo(9_980);
expect(stats.positionsValue).toBeCloseTo(ETH_QTY * ETH_PRICE);
expect(stats.initialBalance).toBeNull();
expect(stats.pnlAllTime).toBeNull();
});
});

describe('a bot trading on Polymarket only', () => {
const positions = [position('tok-1', 'polymarket', 50, 30)];

it('is unchanged: Polymarket cash plus positions at the Gamma price, no Binance lookups', async () => {
const { service, binancePrice } = makeService({
paperState: { balance: 70, positions: [], binanceBalances: { USDT: 100 } },
positions,
});
const stats = await service.stats('bot-1');
expect(stats.cashBalance).toBeCloseTo(70);
expect(stats.positionsValue).toBeCloseTo(30);
expect(stats.pnlAllTime).toBeCloseTo(0);
expect(binancePrice).not.toHaveBeenCalled();
});

it('a state saved before Binance balances existed is valued as before', async () => {
const { service } = makeService({ paperState: { balance: 70, positions: [] }, positions });
const stats = await service.stats('bot-1');
expect(stats.cashBalance).toBeCloseTo(70);
expect(stats.totalValue).toBeCloseTo(100);
});
});
});
1 change: 1 addition & 0 deletions tests/runtime/script-bot-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const mockPaperState = {

vi.mock('../../packages/core/src/runtime/PaperTradingState', () => ({
PaperTradingState: vi.fn().mockImplementation(() => mockPaperState),
DEFAULT_PAPER_BALANCE: 10_000,
}));

import { ScriptBotRunner } from '../../packages/core/src/runtime/ScriptBotRunner';
Expand Down
Loading