-
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 #35 from balancer/26-add-example-hook
26 add example hook
- Loading branch information
Showing
9 changed files
with
196 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,97 @@ | ||
import { RemoveKind } from '../vault/types'; | ||
import { HookBase } from './types'; | ||
import { MathSol } from '../utils/math'; | ||
|
||
export type HookStateExitFee = { | ||
tokens: string[]; | ||
removeLiquidityHookFeePercentage: bigint; | ||
}; | ||
|
||
/** | ||
* This hook implements the ExitFeeHookExample found in mono-repo: https://github.com/balancer/balancer-v3-monorepo/blob/c848c849cb44dc35f05d15858e4fba9f17e92d5e/pkg/pool-hooks/contracts/ExitFeeHookExample.sol | ||
*/ | ||
export class ExitFeeHook implements HookBase { | ||
public shouldCallComputeDynamicSwapFee = false; | ||
public shouldCallBeforeSwap = false; | ||
public shouldCallAfterSwap = false; | ||
public shouldCallBeforeAddLiquidity = false; | ||
public shouldCallAfterAddLiquidity = false; | ||
public shouldCallBeforeRemoveLiquidity = false; | ||
public shouldCallAfterRemoveLiquidity = true; | ||
public enableHookAdjustedAmounts = true; | ||
|
||
onBeforeAddLiquidity() { | ||
return { success: false, hookAdjustedBalancesScaled18: [] }; | ||
} | ||
onAfterAddLiquidity() { | ||
return { success: false, hookAdjustedAmountsInRaw: [] }; | ||
} | ||
onBeforeRemoveLiquidity() { | ||
return { success: false, hookAdjustedBalancesScaled18: [] }; | ||
} | ||
onAfterRemoveLiquidity( | ||
kind: RemoveKind, | ||
bptAmountIn: bigint, | ||
amountsOutScaled18: bigint[], | ||
amountsOutRaw: bigint[], | ||
balancesScaled18: bigint[], | ||
hookState: HookStateExitFee, | ||
) { | ||
if ( | ||
!( | ||
typeof hookState === 'object' && | ||
hookState !== null && | ||
'removeLiquidityHookFeePercentage' in hookState && | ||
'tokens' in hookState | ||
) | ||
) | ||
throw new Error('Unexpected hookState'); | ||
|
||
// Our current architecture only supports fees on tokens. Since we must always respect exact `amountsOut`, and | ||
// non-proportional remove liquidity operations would require taking fees in BPT, we only support proportional | ||
// removeLiquidity. | ||
if (kind !== RemoveKind.PROPORTIONAL) { | ||
throw new Error(`ExitFeeHook: Unsupported RemoveKind: ${kind}`); | ||
} | ||
const accruedFees = new Array(hookState.tokens.length).fill(0n); | ||
const hookAdjustedAmountsOutRaw = [...amountsOutRaw]; | ||
if (hookState.removeLiquidityHookFeePercentage > 0) { | ||
// Charge fees proportional to amounts out of each token | ||
for (let i = 0; i < amountsOutRaw.length; i++) { | ||
const hookFee = MathSol.mulDownFixed( | ||
amountsOutRaw[i], | ||
hookState.removeLiquidityHookFeePercentage, | ||
); | ||
accruedFees[i] = hookFee; | ||
hookAdjustedAmountsOutRaw[i] -= hookFee; | ||
// Fees don't need to be transferred to the hook, because donation will reinsert them in the vault | ||
} | ||
|
||
// In SC Hook Donates accrued fees back to LPs | ||
// _vault.addLiquidity( | ||
// AddLiquidityParams({ | ||
// pool: pool, | ||
// to: msg.sender, // It would mint BPTs to router, but it's a donation so no BPT is minted | ||
// maxAmountsIn: accruedFees, // Donate all accrued fees back to the pool (i.e. to the LPs) | ||
// minBptAmountOut: 0, // Donation does not return BPTs, any number above 0 will revert | ||
// kind: AddLiquidityKind.DONATION, | ||
// userData: bytes(''), // User data is not used by donation, so we can set to an empty string | ||
// }), | ||
// ); | ||
} | ||
|
||
return { | ||
success: true, | ||
hookAdjustedAmountsOutRaw, | ||
}; | ||
} | ||
onBeforeSwap() { | ||
return { success: false, hookAdjustedBalancesScaled18: [] }; | ||
} | ||
onAfterSwap() { | ||
return { success: false, hookAdjustedAmountCalculatedRaw: 0n }; | ||
} | ||
onComputeDynamicSwapFee() { | ||
return { success: false, dynamicSwapFee: 0n }; | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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,65 @@ | ||
// pnpm test -- exitFee.test.ts | ||
import { describe, expect, test } from 'vitest'; | ||
import { RemoveKind, Vault } from '../../src'; | ||
|
||
const poolState = { | ||
poolType: 'Weighted', | ||
hookType: 'ExitFee', | ||
chainId: '11155111', | ||
blockNumber: '5955145', | ||
poolAddress: '0x03722034317d8fb16845213bd3ce15439f9ce136', | ||
tokens: [ | ||
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9', | ||
'0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75', | ||
], | ||
scalingFactors: [1000000000000000000n, 1000000000000000000n], | ||
weights: [500000000000000000n, 500000000000000000n], | ||
swapFee: 100000000000000000n, | ||
aggregateSwapFee: 0n, | ||
balancesLiveScaled18: [5000000000000000n, 5000000000000000000n], | ||
tokenRates: [1000000000000000000n, 1000000000000000000n], | ||
totalSupply: 158113883008415798n, | ||
}; | ||
|
||
const removeLiquidityInput = { | ||
pool: '0xb2456a6f51530053bc41b0ee700fe6a2c37282e8', | ||
minAmountsOut: [1n, 1n], | ||
maxBptAmountIn: 10000000000000n, | ||
kind: RemoveKind.PROPORTIONAL, | ||
}; | ||
|
||
describe('hook - exitFee', () => { | ||
const vault = new Vault(); | ||
|
||
test('exitFee of 0', () => { | ||
const inputHookState = { | ||
removeLiquidityHookFeePercentage: 0n, | ||
tokens: poolState.tokens, | ||
}; | ||
const outPutAmount = vault.removeLiquidity( | ||
removeLiquidityInput, | ||
poolState, | ||
inputHookState, | ||
); | ||
expect(outPutAmount.amountsOut).to.deep.eq([ | ||
316227766016n, | ||
316227766016840n, | ||
]); | ||
}); | ||
|
||
test('exitFee of 5%', () => { | ||
const inputHookState = { | ||
removeLiquidityHookFeePercentage: 50000000000000000n, | ||
tokens: poolState.tokens, | ||
}; | ||
const outPutAmount = vault.removeLiquidity( | ||
removeLiquidityInput, | ||
poolState, | ||
inputHookState, | ||
); | ||
expect(outPutAmount.amountsOut).to.deep.eq([ | ||
300416377716n, | ||
300416377715998n, | ||
]); | ||
}); | ||
}); |
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