Skip to content

Commit

Permalink
fix(TS): Check for supportsUnbalancedLiquidity in pool state for unba…
Browse files Browse the repository at this point in the history
…lanced operations.
  • Loading branch information
johngrantuk committed Dec 19, 2024
1 parent 398d95c commit a7e8f64
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions typescript/src/vault/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type BasePoolState = {
swapFee: bigint;
aggregateSwapFee: bigint;
totalSupply: bigint;
supportsUnbalancedLiquidity: boolean;
hookType?: string;
};

Expand Down
10 changes: 10 additions & 0 deletions typescript/src/vault/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ export class Vault {
let swapFeeAmountsScaled18: bigint[];

if (addLiquidityInput.kind === AddKind.UNBALANCED) {
this._requireUnbalancedLiquidityEnabled(poolState);
amountsInScaled18 = maxAmountsInScaled18;
const computed = computeAddLiquidityUnbalanced(
updatedBalancesLiveScaled18,
Expand All @@ -399,6 +400,7 @@ export class Vault {
bptAmountOut = computed.bptAmountOut;
swapFeeAmountsScaled18 = computed.swapFeeAmounts;
} else if (addLiquidityInput.kind === AddKind.SINGLE_TOKEN_EXACT_OUT) {
this._requireUnbalancedLiquidityEnabled(poolState);
const tokenIndex = this._getSingleInputIndex(maxAmountsInScaled18);
amountsInScaled18 = maxAmountsInScaled18;
bptAmountOut = addLiquidityInput.minBptAmountOutRaw;
Expand Down Expand Up @@ -557,6 +559,7 @@ export class Vault {
} else if (
removeLiquidityInput.kind === RemoveKind.SINGLE_TOKEN_EXACT_IN
) {
this._requireUnbalancedLiquidityEnabled(poolState);
bptAmountIn = removeLiquidityInput.maxBptAmountInRaw;
amountsOutScaled18 = minAmountsOutScaled18;
tokenOutIndex = this._getSingleInputIndex(
Expand All @@ -581,6 +584,7 @@ export class Vault {
} else if (
removeLiquidityInput.kind === RemoveKind.SINGLE_TOKEN_EXACT_OUT
) {
this._requireUnbalancedLiquidityEnabled(poolState);
amountsOutScaled18 = minAmountsOutScaled18;
tokenOutIndex = this._getSingleInputIndex(
removeLiquidityInput.minAmountsOutRaw,
Expand Down Expand Up @@ -780,4 +784,10 @@ export class Vault {
}
return true;
}

private _requireUnbalancedLiquidityEnabled(poolState: PoolState): void {
if (!poolState.supportsUnbalancedLiquidity) {
throw new Error('DoesNotSupportUnbalancedLiquidity');
}
}
}
9 changes: 9 additions & 0 deletions typescript/test/customPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('custom pool tests', () => {
totalSupply: 1736721048412749353n,
randoms: [7000000000000000000n, 8000000000000000000n],
aggregateSwapFee: 0n,
supportsUnbalancedLiquidity: true,
};

const calculatedAmount = vault.swap(
Expand All @@ -49,6 +50,14 @@ class CustomPool implements PoolBase {
this.randoms = poolState.randoms;
}

getMaximumInvariantRatio(): bigint {
return 1n;
}

getMinimumInvariantRatio(): bigint {
return 1n;
}

getMaxSwapAmount(): bigint {
return 1n;
}
Expand Down
1 change: 1 addition & 0 deletions typescript/test/hooks/afterAddLiquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const pool = {
balancesLiveScaled18: [1000000000000000000n, 1000000000000000000n],
tokenRates: [1000000000000000000n, 1000000000000000000n],
totalSupply: 1000000000000000000n,
supportsUnbalancedLiquidity: true,
};

describe('hook - afterAddLiquidity', () => {
Expand Down
1 change: 1 addition & 0 deletions typescript/test/hooks/afterRemoveLiquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const pool = {
balancesLiveScaled18: [1000000000000000000n, 1000000000000000000n],
tokenRates: [1000000000000000000n, 1000000000000000000n],
totalSupply: 1000000000000000000n,
supportsUnbalancedLiquidity: true,
};

describe('hook - afterRemoveLiquidity', () => {
Expand Down
9 changes: 9 additions & 0 deletions typescript/test/hooks/afterSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const pool = {
balancesLiveScaled18: [1000000000000000000n, 1000000000000000000n],
tokenRates: [1000000000000000000n, 1000000000000000000n],
totalSupply: 1000000000000000000n,
supportsUnbalancedLiquidity: true,
};

const swapInput = {
Expand Down Expand Up @@ -87,6 +88,14 @@ describe('hook - afterSwap', () => {
class CustomPool implements PoolBase {
constructor() {}

getMaximumInvariantRatio(): bigint {
return 1n;
}

getMinimumInvariantRatio(): bigint {
return 1n;
}

getMaxSwapAmount(): bigint {
return 1n;
}
Expand Down
1 change: 1 addition & 0 deletions typescript/test/hooks/beforeAddLiquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const pool = {
balancesLiveScaled18: [2000000000000000000n, 2000000000000000000n],
tokenRates: [1000000000000000000n, 1000000000000000000n],
totalSupply: 1000000000000000000n,
supportsUnbalancedLiquidity: true,
};

describe('hook - beforeAddLiquidity', () => {
Expand Down
1 change: 1 addition & 0 deletions typescript/test/hooks/beforeRemoveLiquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const pool = {
balancesLiveScaled18: [2000000000000000000n, 2000000000000000000n],
tokenRates: [1000000000000000000n, 1000000000000000000n],
totalSupply: 1000000000000000000n,
supportsUnbalancedLiquidity: true,
};

describe('hook - afterRemoveLiquidity', () => {
Expand Down
1 change: 1 addition & 0 deletions typescript/test/hooks/beforeSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const pool = {
balancesLiveScaled18: [2000000000000000000n, 2000000000000000000n],
tokenRates: [1000000000000000000n, 1000000000000000000n],
totalSupply: 1000000000000000000n,
supportsUnbalancedLiquidity: true,
};

const swapInput = {
Expand Down
2 changes: 2 additions & 0 deletions typescript/test/hooks/directionalFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const stablePoolStateWithHook: StableState = {
aggregateSwapFee: 0n,
totalSupply: totalSupply,
amp: 1000000n,
supportsUnbalancedLiquidity: true,
};

const stablePoolStateWithoutHook: StableState = {
Expand All @@ -51,6 +52,7 @@ const stablePoolStateWithoutHook: StableState = {
aggregateSwapFee: 0n,
totalSupply: totalSupply,
amp: 1000000n,
supportsUnbalancedLiquidity: true,
};

const swapInput: SwapInput = {
Expand Down
1 change: 1 addition & 0 deletions typescript/test/hooks/exitFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const poolState = {
balancesLiveScaled18: [5000000000000000n, 5000000000000000000n],
tokenRates: [1000000000000000000n, 1000000000000000000n],
totalSupply: 158113883008415798n,
supportsUnbalancedLiquidity: true,
};

const removeLiquidityInput = {
Expand Down
9 changes: 9 additions & 0 deletions typescript/test/hooks/hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('hook tests', () => {
totalSupply: 1736721048412749353n,
randoms: [77n, 88n],
aggregateSwapFee: 0n,
supportsUnbalancedLiquidity: true,
};
test('should throw when no hook state passed', () => {
expect(() => {
Expand Down Expand Up @@ -67,6 +68,14 @@ class CustomPool implements PoolBase {
this.randoms = poolState.randoms;
}

getMaximumInvariantRatio(): bigint {
return 1n;
}

getMinimumInvariantRatio(): bigint {
return 1n;
}

getMaxSwapAmount(): bigint {
return 1n;
}
Expand Down
1 change: 1 addition & 0 deletions typescript/test/hooks/stableSurge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const poolState: StableState = {
tokenRates: [1000000000000000000n, 1000000000000000000n],
totalSupply: 100000000000000000000000n,
amp: 1000000n,
supportsUnbalancedLiquidity: true,
};

const hookState: HookStateStableSurge = {
Expand Down
8 changes: 8 additions & 0 deletions typescript/test/utils/readTestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ function mapPool(
pool as TransformBigintToString<WeightedPool>
).weights.map((w) => BigInt(w)),
aggregateSwapFee: BigInt(pool.aggregateSwapFee ?? '0'),
supportsUnbalancedLiquidity:
pool.supportsUnbalancedLiquidity === undefined
? true
: pool.supportsUnbalancedLiquidity,
};
}
if (pool.poolType === 'STABLE') {
Expand All @@ -166,6 +170,10 @@ function mapPool(
totalSupply: BigInt(pool.totalSupply),
amp: BigInt((pool as TransformBigintToString<StablePool>).amp),
aggregateSwapFee: BigInt(pool.aggregateSwapFee ?? '0'),
supportsUnbalancedLiquidity:
pool.supportsUnbalancedLiquidity === undefined
? true
: pool.supportsUnbalancedLiquidity,
};
}
if (pool.poolType === 'Buffer') {
Expand Down

0 comments on commit a7e8f64

Please sign in to comment.