diff --git a/typescript/test/stableMath.test.ts b/typescript/test/stableMath.test.ts new file mode 100644 index 0000000..1b49986 --- /dev/null +++ b/typescript/test/stableMath.test.ts @@ -0,0 +1,27 @@ +// pnpm test ./test/stableMath.test.ts + +import { _computeBalance, _computeInvariant, _computeInGivenExactOut, _computeOutGivenExactIn } from 'src/stable/stableMath.ts'; +import { describe, expect, test } from 'vitest'; + +describe('test stableMath', () => { + test('_computeBalance', () => { + // based on this sim + // https://dashboard.tenderly.co/mcquardt/project/simulator/f174cf82-3525-4376-b13d-9e61bad1649c?trace=0.4.0 + const finalBalances = _computeBalance( + 1000000n, + [20099500000000000000000n,20000000000000000000000n], + 40000000000000000000000n, + 1 + ) + expect(finalBalances).toEqual(19900500494527739566845n) + }) + test('_computeInvariant', () => { + // based on this sim + // https://dashboard.tenderly.co/mcquardt/project/simulator/f174cf82-3525-4376-b13d-9e61bad1649c?trace=0.4.0 + const invariant = _computeInvariant( + 1000000n, + [20000000000000000000000n,20000000000000000000000n] + ) + expect(invariant).toEqual(40000000000000000000000n) + }); +}) \ No newline at end of file diff --git a/typescript/test/stablePool.test.ts b/typescript/test/stablePool.test.ts index deba318..99229af 100644 --- a/typescript/test/stablePool.test.ts +++ b/typescript/test/stablePool.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'vitest'; -import { SwapKind } from '../src/index'; +import { SwapKind, SwapParams } from '../src/index'; import { Stable } from '../src/stable'; describe('stable pool', () => { @@ -88,4 +88,25 @@ describe('stable pool', () => { }); }); }); + describe('onSwap matches onchain results', () => { + // create new pool + // sim https://dashboard.tenderly.co/mcquardt/project/simulator/f174cf82-3525-4376-b13d-9e61bad1649c?trace=0 + const tempPool = new Stable({ + amp: 1000000n + }); + + const swapParams: SwapParams = { + swapKind: SwapKind.GivenIn, + amountGivenScaled18: 99500000000000000000n, + balancesLiveScaled18: [ + 20000000000000000000000n, + 20000000000000000000000n, + ], + indexIn: 0, + indexOut: 1, + }; + + const amountOut = tempPool.onSwap(swapParams); + expect(amountOut).toEqual(99499505472260433154n); + }) });