Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate queryRemoveLiquidityRecovery #533

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-dolls-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@balancer/sdk": major
---

Remove `queryRemoveLiquidityRecovery` from `RemoveLiquidity`. Please use `query` with `RemoveLiquidityRecoveryInput` instead.
2 changes: 1 addition & 1 deletion examples/removeLiquidity/removeLiquidityRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const removeLiquidity = async ({

// Simulate removing liquidity to get the tokens out
const removeLiquidity = new RemoveLiquidity();
const queryOutput = await removeLiquidity.queryRemoveLiquidityRecovery(
const queryOutput = await removeLiquidity.query(
removeLiquidityRecoveryInput,
poolState,
);
Expand Down
34 changes: 0 additions & 34 deletions src/entities/removeLiquidity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
RemoveLiquidityConfig,
RemoveLiquidityInput,
RemoveLiquidityQueryOutput,
RemoveLiquidityRecoveryInput,
RemoveLiquidityProportionalInput,
RemoveLiquidityBaseBuildCallInput,
} from './types';
Expand Down Expand Up @@ -46,39 +45,6 @@ export class RemoveLiquidity implements RemoveLiquidityBase {
}
}

/**
* It's not possible to query Remove Liquidity Recovery in the same way as
* other remove liquidity kinds, so a separate handler is required for it.
* Since it's not affected by fees or anything other than pool balances,
* it's possible to calculate amountsOut as proportional amounts.
*/
public async queryRemoveLiquidityRecovery(
input: RemoveLiquidityRecoveryInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
this.inputValidator.validateRemoveLiquidityRecovery(input, poolState);
switch (poolState.protocolVersion) {
case 1: {
const removeLiquidity = new RemoveLiquidityCowAmm();
return removeLiquidity.queryRemoveLiquidityRecovery();
}
case 2: {
const removeLiquidity = new RemoveLiquidityV2(this.config);
return removeLiquidity.queryRemoveLiquidityRecovery(
input,
poolState,
);
}
case 3: {
const removeLiquidity = new RemoveLiquidityV3();
return removeLiquidity.queryRemoveLiquidityRecovery(
input,
poolState,
);
}
}
}

public buildCall(
input:
| RemoveLiquidityBaseBuildCallInput
Expand Down
6 changes: 0 additions & 6 deletions src/entities/removeLiquidity/removeLiquidityCowAmm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ export class RemoveLiquidityCowAmm implements RemoveLiquidityBase {
return output;
}

public queryRemoveLiquidityRecovery(): never {
throw new Error(
'Remove Liquidity Recovery is not supported for Cow AMM pools',
);
}

public buildCall(
input: RemoveLiquidityBaseBuildCallInput,
): RemoveLiquidityBuildCallOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
RemoveLiquidityBase,
RemoveLiquidityBuildCallOutput,
RemoveLiquidityInput,
RemoveLiquidityKind,
RemoveLiquidityQueryOutput,
RemoveLiquidityRecoveryInput,
} from '../../types';
Expand All @@ -28,6 +29,10 @@ export class RemoveLiquidityComposableStable implements RemoveLiquidityBase {
input: RemoveLiquidityInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
if (input.kind === RemoveLiquidityKind.Recovery) {
return this.queryRemoveLiquidityRecovery(input, poolState);
}

const sortedTokens = getSortedTokens(poolState.tokens, input.chainId);
const bptIndex = poolState.tokens.findIndex(
(t) => t.address === poolState.address,
Expand Down Expand Up @@ -80,7 +85,9 @@ export class RemoveLiquidityComposableStable implements RemoveLiquidityBase {
};
}

public async queryRemoveLiquidityRecovery(
// RemoveLiquidityRecovery doesn't have a proper query method on v2, so
// this method replicates SC behavior off-chain
private async queryRemoveLiquidityRecovery(
input: RemoveLiquidityRecoveryInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
Expand Down
10 changes: 0 additions & 10 deletions src/entities/removeLiquidity/removeLiquidityV2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
RemoveLiquidityConfig,
RemoveLiquidityInput,
RemoveLiquidityQueryOutput,
RemoveLiquidityRecoveryInput,
} from '@/entities';
import { RemoveLiquidityWeighted } from './weighted/removeLiquidityWeighted';
import { RemoveLiquidityComposableStable } from './composableStable/removeLiquidityComposableStable';
Expand Down Expand Up @@ -48,15 +47,6 @@ export class RemoveLiquidityV2 implements RemoveLiquidityBase {
return this.getRemoveLiquidity(poolState.type).query(input, poolState);
}

public async queryRemoveLiquidityRecovery(
input: RemoveLiquidityRecoveryInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
return this.getRemoveLiquidity(
poolState.type,
).queryRemoveLiquidityRecovery(input, poolState);
}

public buildCall(
input: RemoveLiquidityBuildCallInput,
): RemoveLiquidityBuildCallOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RemoveLiquidityBase,
RemoveLiquidityBuildCallOutput,
RemoveLiquidityInput,
RemoveLiquidityKind,
RemoveLiquidityQueryOutput,
RemoveLiquidityRecoveryInput,
} from '../../types';
Expand All @@ -27,6 +28,10 @@ export class RemoveLiquidityStable implements RemoveLiquidityBase {
input: RemoveLiquidityInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
if (input.kind === RemoveLiquidityKind.Recovery) {
return this.queryRemoveLiquidityRecovery(input, poolState);
}

const sortedTokens = getSortedTokens(poolState.tokens, input.chainId);
const amounts = getAmountsQuery(sortedTokens, input);

Expand Down Expand Up @@ -71,7 +76,9 @@ export class RemoveLiquidityStable implements RemoveLiquidityBase {
};
}

public async queryRemoveLiquidityRecovery(
// RemoveLiquidityRecovery doesn't have a proper query method on v2, so
// this method replicates SC behavior off-chain
private async queryRemoveLiquidityRecovery(
input: RemoveLiquidityRecoveryInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RemoveLiquidityBase,
RemoveLiquidityBuildCallOutput,
RemoveLiquidityInput,
RemoveLiquidityKind,
RemoveLiquidityQueryOutput,
RemoveLiquidityRecoveryInput,
} from '../../types';
Expand All @@ -27,6 +28,10 @@ export class RemoveLiquidityWeighted implements RemoveLiquidityBase {
input: RemoveLiquidityInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
if (input.kind === RemoveLiquidityKind.Recovery) {
return this.queryRemoveLiquidityRecovery(input, poolState);
}

const sortedTokens = getSortedTokens(poolState.tokens, input.chainId);
const amounts = getAmountsQuery(sortedTokens, input);

Expand Down Expand Up @@ -70,8 +75,9 @@ export class RemoveLiquidityWeighted implements RemoveLiquidityBase {
chainId: input.chainId,
};
}

public async queryRemoveLiquidityRecovery(
// RemoveLiquidityRecovery doesn't have a proper query method on v2, so
// this method replicates SC behavior off-chain
private async queryRemoveLiquidityRecovery(
input: RemoveLiquidityRecoveryInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
Expand Down
50 changes: 1 addition & 49 deletions src/entities/removeLiquidity/removeLiquidityV3/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Token } from '@/entities/token';
import { TokenAmount } from '@/entities/tokenAmount';
import { PoolState } from '@/entities/types';
import {
calculateProportionalAmounts,
getPoolStateWithBalancesV3,
getSortedTokens,
} from '@/entities/utils';
import { getSortedTokens } from '@/entities/utils';
import { Hex } from '@/types';
import {
BALANCER_ROUTER,
Expand All @@ -20,7 +16,6 @@ import {
RemoveLiquidityBuildCallOutput,
RemoveLiquidityInput,
RemoveLiquidityKind,
RemoveLiquidityRecoveryInput,
} from '../types';
import { doRemoveLiquiditySingleTokenExactOutQuery } from './doRemoveLiquiditySingleTokenExactOutQuery';
import { doRemoveLiquiditySingleTokenExactInQuery } from './doRemoveLiquiditySingleTokenExactInQuery';
Expand Down Expand Up @@ -127,49 +122,6 @@ export class RemoveLiquidityV3 implements RemoveLiquidityBase {
return output;
}

/**
* It's not possible to query Remove Liquidity Recovery in the same way as
* other remove liquidity kinds, but since it's not affected by fees or anything
* other than pool balances, we can calculate amountsOut as proportional amounts.
*/
public async queryRemoveLiquidityRecovery(
input: RemoveLiquidityRecoveryInput,
poolState: PoolState,
): Promise<RemoveLiquidityBaseQueryOutput> {
const poolStateWithBalances = await getPoolStateWithBalancesV3(
poolState,
input.chainId,
input.rpcUrl,
);

const { tokenAmounts } = calculateProportionalAmounts(
poolStateWithBalances,
input.bptIn,
);

const bptIn = TokenAmount.fromRawAmount(
new Token(input.chainId, input.bptIn.address, input.bptIn.decimals),
input.bptIn.rawAmount,
);
const amountsOut = tokenAmounts.map((amountIn) =>
TokenAmount.fromRawAmount(
new Token(input.chainId, amountIn.address, amountIn.decimals),
amountIn.rawAmount,
),
);
return {
to: BALANCER_ROUTER[input.chainId],
poolType: poolState.type,
removeLiquidityKind: input.kind,
poolId: poolState.id,
bptIn,
amountsOut,
tokenOutIndex: undefined,
protocolVersion: poolState.protocolVersion,
chainId: input.chainId,
};
}

public buildCall(
input: RemoveLiquidityBaseBuildCallInput & { userData: Hex },
): RemoveLiquidityBuildCallOutput {
Expand Down
4 changes: 0 additions & 4 deletions src/entities/removeLiquidity/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ export interface RemoveLiquidityBase {
input: RemoveLiquidityInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput>;
queryRemoveLiquidityRecovery(
input: RemoveLiquidityRecoveryInput,
poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput>;
buildCall(
input: RemoveLiquidityBuildCallInput,
): RemoveLiquidityBuildCallOutput;
Expand Down
11 changes: 1 addition & 10 deletions src/entities/removeLiquidityBoosted/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import {
RemoveLiquidityBase,
RemoveLiquidityKind,
RemoveLiquidityBuildCallOutput,
RemoveLiquidityQueryOutput,
RemoveLiquidityRecoveryInput,
} from '../removeLiquidity/types';

import { Permit } from '@/entities/permitHelper';

import { balancerCompositeLiquidityRouterAbi } from '@/abi';

import { PoolState, PoolStateWithUnderlyings } from '@/entities/types';
import { PoolStateWithUnderlyings } from '@/entities/types';

import { TokenAmount } from '@/entities/tokenAmount';
import { Token } from '@/entities/token';
Expand All @@ -32,13 +30,6 @@ import { getSortedTokens } from '../utils';
export class RemoveLiquidityBoostedV3 implements RemoveLiquidityBase {
private readonly inputValidator: InputValidator = new InputValidator();

public async queryRemoveLiquidityRecovery(
_input: RemoveLiquidityRecoveryInput,
_poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput> {
throw new Error('Not implemented');
}

public async query(
input: RemoveLiquidityBoostedProportionalInput,
poolState: PoolStateWithUnderlyings,
Expand Down
9 changes: 4 additions & 5 deletions test/lib/utils/removeLiquidityRecoveryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ export const sdkRemoveLiquidityRecovery = async ({
removeLiquidityBuildCallOutput: RemoveLiquidityBuildCallOutput;
removeLiquidityQueryOutput: RemoveLiquidityQueryOutput;
}> => {
const removeLiquidityQueryOutput =
await removeLiquidity.queryRemoveLiquidityRecovery(
removeLiquidityRecoveryInput,
poolState,
);
const removeLiquidityQueryOutput = await removeLiquidity.query(
removeLiquidityRecoveryInput,
poolState,
);

let removeLiquidityBuildInput: RemoveLiquidityBuildCallInput = {
...removeLiquidityQueryOutput,
Expand Down
Loading