Skip to content

Commit

Permalink
test: some examples on precision
Browse files Browse the repository at this point in the history
  • Loading branch information
mkflow27 committed Nov 22, 2024
1 parent f934887 commit 3848b4c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
27 changes: 27 additions & 0 deletions typescript/test/stableMath.test.ts
Original file line number Diff line number Diff line change
@@ -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)

Check failure on line 16 in typescript/test/stableMath.test.ts

View workflow job for this annotation

GitHub Actions / checks

test/stableMath.test.ts > test stableMath > _computeBalance

AssertionError: expected 19900500494527739566895n to deeply equal 19900500494527739566845n - Expected + Received - 19900500494527739566845n + 19900500494527739566895n ❯ test/stableMath.test.ts:16:31
})
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)
});
})
23 changes: 22 additions & 1 deletion typescript/test/stablePool.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);

Check failure on line 110 in typescript/test/stablePool.test.ts

View workflow job for this annotation

GitHub Actions / checks

test/stablePool.test.ts

AssertionError: expected 99499505472260433104n to deeply equal 99499505472260433154n - Expected + Received - 99499505472260433154n + 99499505472260433104n ❯ test/stablePool.test.ts:110:27
})
});

0 comments on commit 3848b4c

Please sign in to comment.