From dfb63f31a5170c90f2f62b82785162cf7ecfa54e Mon Sep 17 00:00:00 2001 From: johngrantuk Date: Thu, 21 Nov 2024 11:49:36 +0000 Subject: [PATCH] test: Fix up hook tests. --- typescript/src/hooks/index.ts | 2 +- typescript/test/customPool.test.ts | 6 ++-- .../test/hooks/afterAddLiquidity.test.ts | 12 ++++---- .../test/hooks/afterRemoveLiquidity.test.ts | 2 +- typescript/test/hooks/afterSwap.test.ts | 28 +++++++++++-------- .../test/hooks/beforeAddLiquidity.test.ts | 4 +-- .../test/hooks/beforeRemoveLiquidity.test.ts | 2 +- typescript/test/hooks/beforeSwap.test.ts | 2 +- typescript/test/hooks/exitFee.test.ts | 6 ++-- 9 files changed, 34 insertions(+), 30 deletions(-) diff --git a/typescript/src/hooks/index.ts b/typescript/src/hooks/index.ts index fcdac2d..fcb073f 100644 --- a/typescript/src/hooks/index.ts +++ b/typescript/src/hooks/index.ts @@ -1 +1 @@ -export * from './types'; \ No newline at end of file +export * from './types'; diff --git a/typescript/test/customPool.test.ts b/typescript/test/customPool.test.ts index 25fd56d..9ecdeba 100644 --- a/typescript/test/customPool.test.ts +++ b/typescript/test/customPool.test.ts @@ -19,19 +19,19 @@ describe('custom pool tests', () => { '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', ], - scalingFactors: [1000000000000000000n, 1000000000000000000n], + scalingFactors: [1n, 1n], weights: [500000000000000000n, 500000000000000000n], swapFee: 0n, balancesLiveScaled18: [64604926441576011n, 46686842105263157924n], tokenRates: [1000000000000000000n, 1000000000000000000n], totalSupply: 1736721048412749353n, - randoms: [77n, 88n], + randoms: [7000000000000000000n, 8000000000000000000n], aggregateSwapFee: 0n, }; const calculatedAmount = vault.swap( { - amountRaw: 1n, + amountRaw: 1000000000000000000n, tokenIn: '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', tokenOut: '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', swapKind: 0, diff --git a/typescript/test/hooks/afterAddLiquidity.test.ts b/typescript/test/hooks/afterAddLiquidity.test.ts index 0d92b98..52b7ff0 100644 --- a/typescript/test/hooks/afterAddLiquidity.test.ts +++ b/typescript/test/hooks/afterAddLiquidity.test.ts @@ -22,7 +22,7 @@ const pool = { '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', ], - scalingFactors: [1000000000000000000n, 1000000000000000000n], + scalingFactors: [1n, 1n], weights: [500000000000000000n, 500000000000000000n], swapFee: 100000000000000000n, aggregateSwapFee: 500000000000000000n, @@ -62,17 +62,17 @@ describe('hook - afterAddLiquidity', () => { 200000000000000001n, 100000000000000001n, ]); - expect(test.bptAmountOutRaw).to.deep.eq(146464294351915965n); + expect(test.bptAmountOutRaw).to.deep.eq(146464294351867896n); }); test('aggregateSwapFee of 50% should take half of remaining', () => { /* hook state is used to pass expected value to tests - aggregate fee amount is 2554373534619714n which is deducted from amount in + aggregate fee amount is 2554373534622012n which is deducted from amount in */ const inputHookState = { expectedBalancesLiveScaled18: [ - 1197445626465380286n, + 1200000000000000000n - 2554373534622012n, 1100000000000000000n, ], }; @@ -85,7 +85,7 @@ describe('hook - afterAddLiquidity', () => { 200000000000000001n, 100000000000000001n, ]); - expect(test.bptAmountOutRaw).to.deep.eq(146464294351915965n); + expect(test.bptAmountOutRaw).to.deep.eq(146464294351867896n); }); }); @@ -119,7 +119,7 @@ class CustomHook implements HookBase { ) throw new Error('Unexpected hookState'); expect(kind).to.eq(addLiquidityInput.kind); - expect(bptAmountOut).to.eq(146464294351915965n); + expect(bptAmountOut).to.eq(146464294351867896n); expect(amountsInScaled18).to.deep.eq(addLiquidityInput.maxAmountsInRaw); expect(amountsInRaw).to.deep.eq(addLiquidityInput.maxAmountsInRaw); expect(balancesScaled18).to.deep.eq( diff --git a/typescript/test/hooks/afterRemoveLiquidity.test.ts b/typescript/test/hooks/afterRemoveLiquidity.test.ts index 8e45589..d5b3078 100644 --- a/typescript/test/hooks/afterRemoveLiquidity.test.ts +++ b/typescript/test/hooks/afterRemoveLiquidity.test.ts @@ -28,7 +28,7 @@ const pool = { '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', ], - scalingFactors: [1000000000000000000n, 1000000000000000000n], + scalingFactors: [1n, 1n], weights: [500000000000000000n, 500000000000000000n], swapFee: 100000000000000000n, aggregateSwapFee: 500000000000000000n, diff --git a/typescript/test/hooks/afterSwap.test.ts b/typescript/test/hooks/afterSwap.test.ts index d3331d7..7b5e5c4 100644 --- a/typescript/test/hooks/afterSwap.test.ts +++ b/typescript/test/hooks/afterSwap.test.ts @@ -13,7 +13,7 @@ const pool = { '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', ], - scalingFactors: [1000000000000000000n, 1000000000000000000n], + scalingFactors: [1n, 1n], weights: [500000000000000000n, 500000000000000000n], swapFee: 100000000000000000n, aggregateSwapFee: 500000000000000000n, @@ -24,11 +24,13 @@ const pool = { const swapInput = { swapKind: SwapKind.GivenIn, - amountRaw: 1n, + amountRaw: 1000000000000000000n, tokenIn: pool.tokens[0], tokenOut: pool.tokens[1], }; +const expectedCalculated = 100000000000n; + describe('hook - afterSwap', () => { const vault = new Vault({ customPoolClasses: { @@ -47,8 +49,9 @@ describe('hook - afterSwap', () => { const inputHookState = { expectedBalancesLiveScaled18: [ pool.balancesLiveScaled18[0] + swapInput.amountRaw, - 999999910000000000n, + pool.balancesLiveScaled18[1] - expectedCalculated, ], + expectedCalculated, }; const test = vault.swap( swapInput, @@ -66,11 +69,15 @@ describe('hook - afterSwap', () => { hook state is used to pass expected value to tests Aggregate fee amount is 50% of swap fee */ + const expectedAggregateSwapFeeAmount = 50000000000000000n; const inputHookState = { expectedBalancesLiveScaled18: [ - pool.balancesLiveScaled18[0] + swapInput.amountRaw, - 999999905000000000n, + pool.balancesLiveScaled18[0] + + swapInput.amountRaw - + expectedAggregateSwapFeeAmount, + pool.balancesLiveScaled18[1] - expectedCalculated, ], + expectedCalculated, }; const test = vault.swap(swapInput, pool, inputHookState); expect(test).to.eq(1n); @@ -92,7 +99,7 @@ class CustomPool implements PoolBase { } onSwap(): bigint { - return 100000000000n; + return expectedCalculated; } computeInvariant(): bigint { return 1n; @@ -155,12 +162,9 @@ class CustomHook implements HookBase { expect(tokenIn).to.eq(swapInput.tokenIn); expect(tokenOut).to.eq(swapInput.tokenOut); expect(amountInScaled18).to.eq(swapInput.amountRaw); - expect(amountCalculatedRaw).to.eq(90000000000n); - expect(amountCalculatedScaled18).to.eq(90000000000n); - expect(amountOutScaled18).to.eq(90000000000n); - expect(tokenInBalanceScaled18).to.eq( - pool.balancesLiveScaled18[0] + swapInput.amountRaw, - ); + expect(amountCalculatedRaw).to.eq(expectedCalculated); + expect(amountCalculatedScaled18).to.eq(expectedCalculated); + expect(amountOutScaled18).to.eq(expectedCalculated); expect([tokenInBalanceScaled18, tokenOutBalanceScaled18]).to.deep.eq( hookState.expectedBalancesLiveScaled18, ); diff --git a/typescript/test/hooks/beforeAddLiquidity.test.ts b/typescript/test/hooks/beforeAddLiquidity.test.ts index 23fbb2d..d0290d8 100644 --- a/typescript/test/hooks/beforeAddLiquidity.test.ts +++ b/typescript/test/hooks/beforeAddLiquidity.test.ts @@ -22,7 +22,7 @@ const pool = { '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', ], - scalingFactors: [1000000000000000000n, 1000000000000000000n], + scalingFactors: [1n, 1n], weights: [500000000000000000n, 500000000000000000n], swapFee: 100000000000000000n, aggregateSwapFee: 500000000000000000n, @@ -57,7 +57,7 @@ describe('hook - beforeAddLiquidity', () => { 200000000000000000n, 100000000000000000n, ]); - expect(test.bptAmountOutRaw).to.deep.eq(146464294351915965n); + expect(test.bptAmountOutRaw).to.deep.eq(146464294351867896n); }); }); diff --git a/typescript/test/hooks/beforeRemoveLiquidity.test.ts b/typescript/test/hooks/beforeRemoveLiquidity.test.ts index 4387716..75eca22 100644 --- a/typescript/test/hooks/beforeRemoveLiquidity.test.ts +++ b/typescript/test/hooks/beforeRemoveLiquidity.test.ts @@ -28,7 +28,7 @@ const pool = { '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', ], - scalingFactors: [1000000000000000000n, 1000000000000000000n], + scalingFactors: [1n, 1n], weights: [500000000000000000n, 500000000000000000n], swapFee: 100000000000000000n, aggregateSwapFee: 500000000000000000n, diff --git a/typescript/test/hooks/beforeSwap.test.ts b/typescript/test/hooks/beforeSwap.test.ts index a1bb9cd..e0fabf0 100644 --- a/typescript/test/hooks/beforeSwap.test.ts +++ b/typescript/test/hooks/beforeSwap.test.ts @@ -13,7 +13,7 @@ const pool = { '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', ], - scalingFactors: [1000000000000000000n, 1000000000000000000n], + scalingFactors: [1n, 1n], weights: [500000000000000000n, 500000000000000000n], swapFee: 100000000000000000n, aggregateSwapFee: 500000000000000000n, diff --git a/typescript/test/hooks/exitFee.test.ts b/typescript/test/hooks/exitFee.test.ts index 86a02fc..484b7b1 100644 --- a/typescript/test/hooks/exitFee.test.ts +++ b/typescript/test/hooks/exitFee.test.ts @@ -12,7 +12,7 @@ const poolState = { '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', ], - scalingFactors: [1000000000000000000n, 1000000000000000000n], + scalingFactors: [1n, 1n], weights: [500000000000000000n, 500000000000000000n], swapFee: 100000000000000000n, aggregateSwapFee: 0n, @@ -43,7 +43,7 @@ describe('hook - exitFee', () => { ); expect(outPutAmount.amountsOutRaw).to.deep.eq([ 316227766016n, - 316227766016840n, + 316227766016844n, ]); }); @@ -59,7 +59,7 @@ describe('hook - exitFee', () => { ); expect(outPutAmount.amountsOutRaw).to.deep.eq([ 300416377716n, - 300416377715998n, + 300416377716002n, ]); }); });