import { describe, it, expect } from 'vitest'; import { SwapDetails } from './swap-details'; describe('SwapDetails.getAmountOut invariant rounding', () => { it('computes amountOut for token1-in -> token0-out (adjusted-balance model)', () => { // Given reserves: token0 = 300_000_000_000, token1 = 300_000 // Input: 888 of token1, feeTier = 10_000 (hundredths of a basis point base 1_000_000) // Expected amountOut under adjusted-balance model: 876_551_353 const amountIn = 888n; const reserveIn = 300_000n; // token1 reserve const reserveOut = 300_000_000_000n; // token0 reserve const feeTier = 10_000n; const swapDetails = new SwapDetails({} as any); const out = (swapDetails as any).getAmountOut( amountIn, reserveIn, reserveOut, feeTier, ) as bigint; expect(out).toBe(876_551_353n); }); it('satisfies adjusted-balance invariant at boundary case from logs', () => { // From program log (updated model): validate adjusted-balance inequality holds. const amountIn = 888n; const reserveIn = 293_741n; // token1 reserve const reserveOut = 306_456_968_222n; // token0 reserve const feeTier = 10_000n; const swapDetails = new SwapDetails({} as any); const out = (swapDetails as any).getAmountOut( amountIn, reserveIn, reserveOut, feeTier, ) as bigint; // Adjusted-balance invariant check: const D = 1_000_000n; const fee = BigInt(feeTier); const b0Adj = (reserveOut - out) * D; // token0 in amount is 0 const b1Adj = reserveIn * D + amountIn * (D - fee); const rhs = reserveOut * reserveIn * D * D; expect(b0Adj * b1Adj >= rhs).toBe(true); }); });