WIP: bootstrap and partial real Solana watcher implementation
This commit is contained in:
134
tests/mocks/InMemoryRepositories.ts
Normal file
134
tests/mocks/InMemoryRepositories.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import {
|
||||
IWalletRepository,
|
||||
ICopySignalRepository,
|
||||
ITradeRepository,
|
||||
ISimulationExecutionRepository,
|
||||
IEventJournalRepository,
|
||||
} from '../../src/domain/interfaces.js';
|
||||
import {
|
||||
WatchedWallet,
|
||||
CopySignal,
|
||||
Trade,
|
||||
SimulatedExecution,
|
||||
EventJournalEntry,
|
||||
} from '../../src/domain/models.js';
|
||||
|
||||
export class InMemoryWalletRepository implements IWalletRepository {
|
||||
public wallets: WatchedWallet[] = [];
|
||||
|
||||
async getWatchedWallets(): Promise<WatchedWallet[]> {
|
||||
return this.wallets.filter((w) => w.isActive);
|
||||
}
|
||||
|
||||
async isWalletWatched(address: string): Promise<boolean> {
|
||||
return this.wallets.some((w) => w.address === address && w.isActive);
|
||||
}
|
||||
|
||||
async addWatchedWallet(
|
||||
wallet: Omit<WatchedWallet, 'id' | 'createdAt' | 'updatedAt'>,
|
||||
): Promise<WatchedWallet> {
|
||||
const newWallet: WatchedWallet = {
|
||||
id: Math.random().toString(),
|
||||
...wallet,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
this.wallets.push(newWallet);
|
||||
return newWallet;
|
||||
}
|
||||
}
|
||||
|
||||
export class InMemoryCopySignalRepository implements ICopySignalRepository {
|
||||
public signals: CopySignal[] = [];
|
||||
|
||||
async saveSignal(signal: CopySignal): Promise<void> {
|
||||
this.signals.push(signal);
|
||||
}
|
||||
|
||||
async getSignalByCorrelationId(correlationId: string): Promise<CopySignal | null> {
|
||||
return this.signals.find((s) => s.correlationId === correlationId) || null;
|
||||
}
|
||||
}
|
||||
|
||||
export class InMemoryTradeRepository implements ITradeRepository {
|
||||
public trades: Trade[] = [];
|
||||
|
||||
async saveTrade(trade: Trade): Promise<void> {
|
||||
const idx = this.trades.findIndex((t) => t.id === trade.id);
|
||||
if (idx !== -1) {
|
||||
this.trades[idx] = trade;
|
||||
} else {
|
||||
this.trades.push(trade);
|
||||
}
|
||||
}
|
||||
|
||||
async updateTrade(trade: Trade): Promise<void> {
|
||||
await this.saveTrade(trade);
|
||||
}
|
||||
|
||||
async getTradeByCorrelationIdAndScenario(
|
||||
correlationId: string,
|
||||
latencyScenario: number,
|
||||
): Promise<Trade | null> {
|
||||
return (
|
||||
this.trades.find(
|
||||
(t) => t.correlationId === correlationId && t.latencyScenario === latencyScenario,
|
||||
) || null
|
||||
);
|
||||
}
|
||||
|
||||
async getActiveTradesForScenario(latencyScenario: number): Promise<Trade[]> {
|
||||
return this.trades.filter((t) => t.latencyScenario === latencyScenario && t.status === 'open');
|
||||
}
|
||||
|
||||
async getTradeById(id: string): Promise<Trade | null> {
|
||||
return this.trades.find((t) => t.id === id) || null;
|
||||
}
|
||||
|
||||
async getTradesForScenario(latencyScenario: number): Promise<Trade[]> {
|
||||
return this.trades.filter((t) => t.latencyScenario === latencyScenario);
|
||||
}
|
||||
}
|
||||
|
||||
export class InMemorySimulationExecutionRepository implements ISimulationExecutionRepository {
|
||||
public executions: SimulatedExecution[] = [];
|
||||
|
||||
async saveExecution(execution: SimulatedExecution): Promise<void> {
|
||||
this.executions.push(execution);
|
||||
}
|
||||
|
||||
async getExecutionsByTradeId(tradeId: string): Promise<SimulatedExecution[]> {
|
||||
return this.executions.filter((e) => e.tradeId === tradeId);
|
||||
}
|
||||
|
||||
async getPendingExecutionsBefore(timestamp: Date): Promise<SimulatedExecution[]> {
|
||||
return this.executions.filter(
|
||||
(e) => e.status === 'pending' && e.executionTimestamp <= timestamp,
|
||||
);
|
||||
}
|
||||
|
||||
async updateExecution(execution: SimulatedExecution): Promise<void> {
|
||||
const idx = this.executions.findIndex((e) => e.id === execution.id);
|
||||
if (idx !== -1) {
|
||||
this.executions[idx] = execution;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class InMemoryEventJournalRepository implements IEventJournalRepository {
|
||||
public events: EventJournalEntry[] = [];
|
||||
|
||||
async logEvent(entry: EventJournalEntry): Promise<void> {
|
||||
this.events.push(entry);
|
||||
}
|
||||
|
||||
async getEventsByCorrelationId(correlationId: string): Promise<EventJournalEntry[]> {
|
||||
return this.events.filter((e) => e.correlationId === correlationId);
|
||||
}
|
||||
}
|
||||
export class InMemoryStrategyRunsRepository {
|
||||
public runs: any[] = [];
|
||||
async saveRun(run: any): Promise<void> {
|
||||
this.runs.push(run);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user