Skip to content
Open
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
6 changes: 6 additions & 0 deletions .claudeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
!.agent
!.agent/**/*


!coverage
!coverage-reports
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This file provides guidance to AI coding agents when working with code in this repository.

## Code Philosophy

- NEVER add fallbacks to production code. Errors are information, fallbacks hide reality.
- Tests are meant to test real production cases. Never stub tests, fake tests, or create junk tests.
- Never decrease code coverage requirements. If you increase the current coverage, you should increase the coverage requirements in QA and CI scripts such that they never regress.

## Essential Commands

**Development:**
Expand Down
54 changes: 53 additions & 1 deletion apps/api/src/lib/__tests__/trade-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("trade-utils", () => {

it("should apply minimal slippage for small trades", () => {
const result = calculateSlippage(100); // $100 trade
expect(result.actualSlippage).toBeGreaterThanOrEqual(0.0005); // At least MIN_SLIPPAGE (5 bps)
// With randomness, slippage can be MIN_SLIPPAGE * 0.95, so allow for that
expect(result.actualSlippage).toBeGreaterThanOrEqual(0.0005 * 0.95); // Allow for random multiplier minimum
expect(result.actualSlippage).toBeLessThan(0.002); // Less than 0.2%
expect(result.effectiveFromValueUSD).toBeGreaterThan(99.8);
expect(result.effectiveFromValueUSD).toBeLessThan(100);
Expand Down Expand Up @@ -79,6 +80,57 @@ describe("trade-utils", () => {
expect(maxSlippage).toBeLessThanOrEqual(expectedMax * 1.01);
});

it("should handle market microstructure edge cases", () => {
// Test values that could trigger floating point precision issues
const precisionTestValues = [
0.01, // Minimum trade
0.001, // Sub-penny trade
999999.99, // Just under $1M
1000000.01, // Just over $1M
0.000001, // Extremely small trade
];

for (const value of precisionTestValues) {
const result = calculateSlippage(value);

// All results must be financially sensible
expect(result.actualSlippage).toBeGreaterThanOrEqual(0);
expect(result.actualSlippage).toBeLessThanOrEqual(0.15);
expect(result.effectiveFromValueUSD).toBeGreaterThan(0);
expect(result.effectiveFromValueUSD).toBeLessThanOrEqual(value);
expect(result.slippagePercentage).toBe(result.actualSlippage * 100);

// Effective value must be mathematically correct
expect(result.effectiveFromValueUSD).toBeCloseTo(
value * (1 - result.actualSlippage),
8,
);
}
});

it("should behave correctly under high frequency trading scenarios", () => {
const tradeValue = 10000;
const results = [];

// Simulate rapid successive trades
const startTime = Date.now();
for (let i = 0; i < 1000; i++) {
results.push(calculateSlippage(tradeValue));
}
const endTime = Date.now();

// Performance constraint: should handle 1000 calculations quickly
expect(endTime - startTime).toBeLessThan(1000); // Less than 1 second

// All results should be valid
results.forEach((result) => {
expect(result.actualSlippage).toBeGreaterThanOrEqual(0);
expect(result.actualSlippage).toBeLessThanOrEqual(0.15);
expect(result.effectiveFromValueUSD).toBeGreaterThan(0);
expect(result.effectiveFromValueUSD).toBeLessThanOrEqual(tradeValue);
});
});

it("should handle edge case of exactly $181,818 (old breaking point)", () => {
// This was the value where the old formula would start producing > 100% slippage
const result = calculateSlippage(181818.18);
Expand Down
Loading