Skip to content

Commit

Permalink
Merge pull request #21 from balancer/hook-support-afterRemove
Browse files Browse the repository at this point in the history
Hook support after remove
  • Loading branch information
johngrantuk authored Jul 24, 2024
2 parents 45ea98a + 7c53932 commit df4c2c2
Showing 13 changed files with 590 additions and 55 deletions.
Binary file modified testData/bun.lockb
Binary file not shown.
33 changes: 33 additions & 0 deletions typescript/src/hooks/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { HookBase } from './types';

export const defaultHook: HookBase = {
shouldCallComputeDynamicSwapFee: false,
shouldCallBeforeSwap: false,
shouldCallAfterSwap: false,
shouldCallBeforeAddLiquidity: false,
shouldCallAfterAddLiquidity: false,
shouldCallBeforeRemoveLiquidity: false,
shouldCallAfterRemoveLiquidity: false,
enableHookAdjustedAmounts: false,
onBeforeAddLiquidity: () => {
return false;
},
onAfterAddLiquidity: () => {
return { success: false, hookAdjustedAmountsInRaw: [] };
},
onBeforeRemoveLiquidity: () => {
return false;
},
onAfterRemoveLiquidity: () => {
return { success: false, hookAdjustedAmountsOutRaw: [] };
},
onBeforeSwap: () => {
return false;
},
onAfterSwap: () => {
return { success: false, hookAdjustedAmountCalculatedRaw: 0n };
},
onComputeDynamicSwapFee: () => {
return { success: false, dynamicSwapFee: 0n };
},
};
3 changes: 3 additions & 0 deletions typescript/src/hooks/exitFeeHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type HookStateExitFee = {
removeLiquidityHookFeePercentage: bigint;
};
76 changes: 76 additions & 0 deletions typescript/src/hooks/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { AddKind, RemoveKind, SwapInput, SwapKind } from '@/vault/types';
import { HookStateExitFee } from './exitFeeHook';

export type HookState = HookStateExitFee;

export type AfterSwapParams = {
kind: SwapKind;
tokenIn: string;
tokenOut: string;
amountInScaled18: bigint;
amountOutScaled18: bigint;
tokenInBalanceScaled18: bigint;
tokenOutBalanceScaled18: bigint;
amountCalculatedScaled18: bigint;
amountCalculatedRaw: bigint;
router: string;
pool: string;
};

export interface HookBase {
shouldCallComputeDynamicSwapFee: boolean;
shouldCallBeforeSwap: boolean;
shouldCallAfterSwap: boolean;
shouldCallBeforeAddLiquidity: boolean;
shouldCallAfterAddLiquidity: boolean;
shouldCallBeforeRemoveLiquidity: boolean;
shouldCallAfterRemoveLiquidity: boolean;
enableHookAdjustedAmounts: boolean;

onBeforeAddLiquidity(
router: string,
pool: string,
kind: AddKind,
maxAmountsInScaled18: bigint[],
minBptAmountOut: bigint,
balancesScaled18: bigint[],
): boolean;
onAfterAddLiquidity(
router: string,
pool: string,
kind: AddKind,
amountsInScaled18: bigint[],
amountsInRaw: bigint[],
bptAmountOut: bigint,
balancesScaled18: bigint[],
): { success: boolean; hookAdjustedAmountsInRaw: bigint[] };
onBeforeRemoveLiquidity(
router: string,
pool: string,
kind: RemoveKind,
maxBptAmountIn: bigint,
minAmountsOutScaled18: bigint[],
balancesScaled18: bigint[],
): boolean;
onAfterRemoveLiquidity(
kind: RemoveKind,
bptAmountIn: bigint,
amountsOutScaled18: bigint[],
amountsOutRaw: bigint[],
balancesScaled18: bigint[],
hookState: HookState | unknown,
): { success: boolean; hookAdjustedAmountsOutRaw: bigint[] };
onBeforeSwap(params: SwapInput, poolAddress: string): boolean;
onAfterSwap(params: AfterSwapParams): {
success: boolean;
hookAdjustedAmountCalculatedRaw: bigint;
};
onComputeDynamicSwapFee(
params: SwapInput,
poolAddress: string,
staticSwapFeePercentage: bigint,
): { success: boolean; dynamicSwapFee: bigint };
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type HookClassConstructor = new (..._args: any[]) => HookBase;
14 changes: 3 additions & 11 deletions typescript/src/stable/data.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { PoolState } from '@/vault/types';

type PoolType = 'Stable';

export type StableMutable = {
swapFee: bigint;
balancesLiveScaled18: bigint[];
tokenRates: bigint[];
totalSupply: bigint;
amp: bigint;
};

export type StableImmutable = {
tokens: string[];
scalingFactors: bigint[];
};

export type StableState = { poolType: PoolType } & StableImmutable &
StableMutable;
export type StableState = PoolState & { poolType: PoolType } & StableMutable;
16 changes: 11 additions & 5 deletions typescript/src/vault/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { BufferState } from '../buffer';
import { StableState } from '../stable';
import { WeightedState } from '../weighted';

export type PoolState = WeightedState | StableState | BufferState;
export type PoolState = {
poolType: string;
tokens: string[];
scalingFactors: bigint[];
tokenRates: bigint[];
balancesLiveScaled18: bigint[];
swapFee: bigint;
aggregateSwapFee: bigint;
totalSupply: bigint;
hookType?: string;
};

export enum SwapKind {
GivenIn = 0,
Loading

0 comments on commit df4c2c2

Please sign in to comment.