-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from balancer/hook-support-afterAdd
feat: Add support for after add hook.
- Loading branch information
Showing
4 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// pnpm test -- afterAddLiquidity.test.ts | ||
import { describe, expect, test } from 'vitest'; | ||
import { AddKind, Vault, Weighted } from '../../src'; | ||
import { HookBase, HookState } from '@/hooks/types'; | ||
|
||
const addLiquidityInput = { | ||
pool: '0xb2456a6f51530053bc41b0ee700fe6a2c37282e8', | ||
maxAmountsIn: [200000000000000000n, 100000000000000000n], | ||
minBptAmountOut: 0n, | ||
kind: AddKind.UNBALANCED, | ||
}; | ||
|
||
class CustomPool extends Weighted {} | ||
|
||
const pool = { | ||
poolType: 'CustomPool', | ||
hookType: 'CustomHook', | ||
chainId: '11155111', | ||
blockNumber: '5955145', | ||
poolAddress: '0xb2456a6f51530053bc41b0ee700fe6a2c37282e8', | ||
tokens: [ | ||
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', | ||
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', | ||
], | ||
scalingFactors: [1000000000000000000n, 1000000000000000000n], | ||
weights: [500000000000000000n, 500000000000000000n], | ||
swapFee: 100000000000000000n, | ||
aggregateSwapFee: 500000000000000000n, | ||
balancesLiveScaled18: [1000000000000000000n, 1000000000000000000n], | ||
tokenRates: [1000000000000000000n, 1000000000000000000n], | ||
totalSupply: 1000000000000000000n, | ||
}; | ||
|
||
describe('hook - afterAddLiquidity', () => { | ||
const vault = new Vault({ | ||
customPoolClasses: { | ||
CustomPool: CustomPool, | ||
}, | ||
customHookClasses: { | ||
CustomHook: CustomHook, | ||
}, | ||
}); | ||
|
||
test('aggregateSwapFee of 0 should not take any protocol fees from updated balances', () => { | ||
// hook state is used to pass expected value to tests | ||
const inputHookState = { | ||
expectedBalancesLiveScaled18: [ | ||
1200000000000000000n, | ||
1100000000000000000n, | ||
], | ||
}; | ||
const test = vault.addLiquidity( | ||
addLiquidityInput, | ||
{ | ||
...pool, | ||
aggregateSwapFee: 0n, | ||
}, | ||
inputHookState, | ||
); | ||
// Hook adds 1n to amountsIn | ||
expect(test.amountsIn).to.deep.eq([ | ||
200000000000000001n, | ||
100000000000000001n, | ||
]); | ||
expect(test.bptAmountOut).to.deep.eq(146464294351915965n); | ||
}); | ||
|
||
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 | ||
*/ | ||
const inputHookState = { | ||
expectedBalancesLiveScaled18: [ | ||
1197445626465380286n, | ||
1100000000000000000n, | ||
], | ||
}; | ||
const test = vault.addLiquidity( | ||
addLiquidityInput, | ||
pool, | ||
inputHookState, | ||
); | ||
expect(test.amountsIn).to.deep.eq([ | ||
200000000000000001n, | ||
100000000000000001n, | ||
]); | ||
expect(test.bptAmountOut).to.deep.eq(146464294351915965n); | ||
}); | ||
}); | ||
|
||
class CustomHook implements HookBase { | ||
public shouldCallComputeDynamicSwapFee = false; | ||
public shouldCallBeforeSwap = false; | ||
public shouldCallAfterSwap = false; | ||
public shouldCallBeforeAddLiquidity = false; | ||
public shouldCallAfterAddLiquidity = true; | ||
public shouldCallBeforeRemoveLiquidity = false; | ||
public shouldCallAfterRemoveLiquidity = false; | ||
public enableHookAdjustedAmounts = true; | ||
|
||
onBeforeAddLiquidity() { | ||
return false; | ||
} | ||
onAfterAddLiquidity( | ||
kind: AddKind, | ||
amountsInScaled18: bigint[], | ||
amountsInRaw: bigint[], | ||
bptAmountOut: bigint, | ||
balancesScaled18: bigint[], | ||
hookState: HookState | unknown, | ||
) { | ||
if ( | ||
!( | ||
typeof hookState === 'object' && | ||
hookState !== null && | ||
'expectedBalancesLiveScaled18' in hookState | ||
) | ||
) | ||
throw new Error('Unexpected hookState'); | ||
expect(kind).to.eq(addLiquidityInput.kind); | ||
expect(bptAmountOut).to.eq(146464294351915965n); | ||
expect(amountsInScaled18).to.deep.eq(addLiquidityInput.maxAmountsIn); | ||
expect(amountsInRaw).to.deep.eq(addLiquidityInput.maxAmountsIn); | ||
expect(balancesScaled18).to.deep.eq( | ||
hookState.expectedBalancesLiveScaled18, | ||
); | ||
return { | ||
success: true, | ||
hookAdjustedAmountsInRaw: [ | ||
amountsInRaw[0] + 1n, | ||
amountsInRaw[1] + 1n, | ||
], | ||
}; | ||
} | ||
onBeforeRemoveLiquidity() { | ||
return false; | ||
} | ||
onAfterRemoveLiquidity() { | ||
return { success: false, hookAdjustedAmountsOutRaw: [] }; | ||
} | ||
onBeforeSwap() { | ||
return false; | ||
} | ||
onAfterSwap() { | ||
return { success: false, hookAdjustedAmountCalculatedRaw: 0n }; | ||
} | ||
onComputeDynamicSwapFee() { | ||
return { success: false, dynamicSwapFee: 0n }; | ||
} | ||
} |