Files
memecoin-botV2/tests/mocks/InMemoryRepositories.ts

152 lines
4.4 KiB
TypeScript

import {
IWalletRepository,
ICopySignalRepository,
ITradeRepository,
ISimulationExecutionRepository,
IEventJournalRepository,
IRawWalletTransactionRepository,
} from '../../src/domain/interfaces.js';
import {
WatchedWallet,
CopySignal,
Trade,
SimulatedExecution,
EventJournalEntry,
RawWalletTransaction,
} 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);
}
}
export class InMemoryRawWalletTransactionRepository implements IRawWalletTransactionRepository {
public transactions: RawWalletTransaction[] = [];
async saveTransaction(tx: RawWalletTransaction): Promise<void> {
const existing = this.transactions.find((t) => t.transactionSignature === tx.transactionSignature);
if (!existing) {
this.transactions.push(tx);
}
}
async getTransactionBySignature(signature: string): Promise<RawWalletTransaction | null> {
return this.transactions.find((t) => t.transactionSignature === signature) || null;
}
}