Skip to content

Commit

Permalink
test: Fix up hook tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Nov 21, 2024
1 parent 9530ef3 commit dfb63f3
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion typescript/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './types';
export * from './types';
6 changes: 3 additions & 3 deletions typescript/test/customPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions typescript/test/hooks/afterAddLiquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const pool = {
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
],
scalingFactors: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1n, 1n],
weights: [500000000000000000n, 500000000000000000n],
swapFee: 100000000000000000n,
aggregateSwapFee: 500000000000000000n,
Expand Down Expand Up @@ -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,
],
};
Expand All @@ -85,7 +85,7 @@ describe('hook - afterAddLiquidity', () => {
200000000000000001n,
100000000000000001n,
]);
expect(test.bptAmountOutRaw).to.deep.eq(146464294351915965n);
expect(test.bptAmountOutRaw).to.deep.eq(146464294351867896n);
});
});

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/hooks/afterRemoveLiquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const pool = {
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
],
scalingFactors: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1n, 1n],
weights: [500000000000000000n, 500000000000000000n],
swapFee: 100000000000000000n,
aggregateSwapFee: 500000000000000000n,
Expand Down
28 changes: 16 additions & 12 deletions typescript/test/hooks/afterSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pool = {
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
],
scalingFactors: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1n, 1n],
weights: [500000000000000000n, 500000000000000000n],
swapFee: 100000000000000000n,
aggregateSwapFee: 500000000000000000n,
Expand All @@ -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: {
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -92,7 +99,7 @@ class CustomPool implements PoolBase {
}

onSwap(): bigint {
return 100000000000n;
return expectedCalculated;
}
computeInvariant(): bigint {
return 1n;
Expand Down Expand Up @@ -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,
);
Expand Down
4 changes: 2 additions & 2 deletions typescript/test/hooks/beforeAddLiquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const pool = {
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
],
scalingFactors: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1n, 1n],
weights: [500000000000000000n, 500000000000000000n],
swapFee: 100000000000000000n,
aggregateSwapFee: 500000000000000000n,
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('hook - beforeAddLiquidity', () => {
200000000000000000n,
100000000000000000n,
]);
expect(test.bptAmountOutRaw).to.deep.eq(146464294351915965n);
expect(test.bptAmountOutRaw).to.deep.eq(146464294351867896n);
});
});

Expand Down
2 changes: 1 addition & 1 deletion typescript/test/hooks/beforeRemoveLiquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const pool = {
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
],
scalingFactors: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1n, 1n],
weights: [500000000000000000n, 500000000000000000n],
swapFee: 100000000000000000n,
aggregateSwapFee: 500000000000000000n,
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/hooks/beforeSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pool = {
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
],
scalingFactors: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1n, 1n],
weights: [500000000000000000n, 500000000000000000n],
swapFee: 100000000000000000n,
aggregateSwapFee: 500000000000000000n,
Expand Down
6 changes: 3 additions & 3 deletions typescript/test/hooks/exitFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const poolState = {
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
],
scalingFactors: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1n, 1n],
weights: [500000000000000000n, 500000000000000000n],
swapFee: 100000000000000000n,
aggregateSwapFee: 0n,
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('hook - exitFee', () => {
);
expect(outPutAmount.amountsOutRaw).to.deep.eq([
316227766016n,
316227766016840n,
316227766016844n,
]);
});

Expand All @@ -59,7 +59,7 @@ describe('hook - exitFee', () => {
);
expect(outPutAmount.amountsOutRaw).to.deep.eq([
300416377716n,
300416377715998n,
300416377716002n,
]);
});
});

0 comments on commit dfb63f3

Please sign in to comment.