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

438 update to allow optional address for addremoveswap #469

Merged
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/unlucky-mangos-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@balancer/sdk": minor
---

Add optional sender and userData inputs for add/remove/swap queries. Sender can be used to query accurate result when pool may have hook thats behaviour is affected by sender, e.g. loyalty fee hook.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createPublicClient, http, zeroAddress } from 'viem';
import { AddLiquidityProportionalInput } from '../types';
import { BALANCER_ROUTER, CHAINS } from '@/utils';
import { createPublicClient, Hex, http } from 'viem';
import { BALANCER_ROUTER, ChainId, CHAINS } from '@/utils';
import {
balancerRouterAbi,
permit2Abi,
Expand All @@ -10,7 +9,10 @@ import {
import { Address } from '@/types';

export const doAddLiquidityProportionalQuery = async (
{ rpcUrl, chainId }: AddLiquidityProportionalInput,
rpcUrl: string,
chainId: ChainId,
sender: Address,
userData: Hex,
poolAddress: Address,
bptOut: bigint,
): Promise<bigint[]> => {
Expand All @@ -28,7 +30,7 @@ export const doAddLiquidityProportionalQuery = async (
...permit2Abi,
],
functionName: 'queryAddLiquidityProportional',
args: [poolAddress, bptOut, zeroAddress, '0x'],
args: [poolAddress, bptOut, sender, userData],
});

return [...amountsIn];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createPublicClient, http, zeroAddress } from 'viem';
import { AddLiquiditySingleTokenInput } from '../types';
import { BALANCER_ROUTER, CHAINS } from '@/utils';
import { createPublicClient, Hex, http } from 'viem';
import { BALANCER_ROUTER, ChainId, CHAINS } from '@/utils';
import {
balancerRouterAbi,
permit2Abi,
Expand All @@ -10,7 +9,11 @@ import {
import { Address } from '@/types';

export const doAddLiquiditySingleTokenQuery = async (
{ rpcUrl, chainId, tokenIn }: AddLiquiditySingleTokenInput,
rpcUrl: string,
chainId: ChainId,
sender: Address,
userData: Hex,
tokenIn: Address,
poolAddress: Address,
bptOut: bigint,
): Promise<bigint> => {
Expand All @@ -28,7 +31,7 @@ export const doAddLiquiditySingleTokenQuery = async (
...permit2Abi,
],
functionName: 'queryAddLiquiditySingleTokenExactOut',
args: [poolAddress, tokenIn, bptOut, zeroAddress, '0x'],
args: [poolAddress, tokenIn, bptOut, sender, userData],
});
return amountIn;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createPublicClient, http, zeroAddress } from 'viem';
import { AddLiquidityUnbalancedInput } from '../types';
import { BALANCER_ROUTER, CHAINS } from '@/utils';
import { createPublicClient, Hex, http } from 'viem';
import { BALANCER_ROUTER, ChainId, CHAINS } from '@/utils';
import {
balancerRouterAbi,
permit2Abi,
Expand All @@ -10,7 +9,10 @@ import {
import { Address } from '@/types';

export const doAddLiquidityUnbalancedQuery = async (
{ rpcUrl, chainId }: AddLiquidityUnbalancedInput,
rpcUrl: string,
chainId: ChainId,
sender: Address,
userData: Hex,
poolAddress: Address,
maxAmountsIn: bigint[],
) => {
Expand All @@ -28,7 +30,7 @@ export const doAddLiquidityUnbalancedQuery = async (
...permit2Abi,
],
functionName: 'queryAddLiquidityUnbalanced',
args: [poolAddress, maxAmountsIn, zeroAddress, '0x'],
args: [poolAddress, maxAmountsIn, sender, userData],
});
return bptAmountOut;
};
31 changes: 21 additions & 10 deletions src/entities/addLiquidity/addLiquidityV3/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeFunctionData } from 'viem';
import { encodeFunctionData, zeroAddress } from 'viem';
import { balancerRouterAbi } from '@/abi';
import { Token } from '@/entities/token';
import { TokenAmount } from '@/entities/tokenAmount';
Expand Down Expand Up @@ -50,7 +50,10 @@ export class AddLiquidityV3 implements AddLiquidityBase {

// proportional join query returns exactAmountsIn for exactBptOut
const amountsInNumbers = await doAddLiquidityProportionalQuery(
input,
input.rpcUrl,
input.chainId,
input.sender ?? zeroAddress,
input.userData ?? '0x',
poolState.address,
bptAmount.rawAmount,
);
Expand All @@ -70,7 +73,10 @@ export class AddLiquidityV3 implements AddLiquidityBase {
case AddLiquidityKind.Unbalanced: {
const maxAmountsIn = getAmounts(sortedTokens, input.amountsIn);
const bptAmountOut = await doAddLiquidityUnbalancedQuery(
input,
input.rpcUrl,
input.chainId,
input.sender ?? zeroAddress,
input.userData ?? '0x',
poolState.address,
maxAmountsIn,
);
Expand All @@ -87,7 +93,11 @@ export class AddLiquidityV3 implements AddLiquidityBase {
input.bptOut.rawAmount,
);
const amountIn = await doAddLiquiditySingleTokenQuery(
input,
input.rpcUrl,
input.chainId,
input.sender ?? zeroAddress,
input.userData ?? '0x',
input.tokenIn,
poolState.address,
input.bptOut.rawAmount,
);
Expand All @@ -104,7 +114,7 @@ export class AddLiquidityV3 implements AddLiquidityBase {
}
}

const output: AddLiquidityBaseQueryOutput = {
const output: AddLiquidityBaseQueryOutput & { userData: Hex } = {
to: BALANCER_ROUTER[input.chainId],
poolType: poolState.type,
poolId: poolState.id,
Expand All @@ -114,13 +124,14 @@ export class AddLiquidityV3 implements AddLiquidityBase {
tokenInIndex,
chainId: input.chainId,
protocolVersion: 3,
userData: input.userData ?? '0x',
};

return output;
}

buildCall(
input: AddLiquidityBaseBuildCallInput,
input: AddLiquidityBaseBuildCallInput & { userData: Hex },
): AddLiquidityBuildCallOutput {
const amounts = getAmountsCall(input);
let callData: Hex;
Expand All @@ -135,7 +146,7 @@ export class AddLiquidityV3 implements AddLiquidityBase {
amounts.maxAmountsIn,
amounts.minimumBpt,
!!input.wethIsEth,
'0x',
input.userData,
],
});
}
Expand All @@ -150,7 +161,7 @@ export class AddLiquidityV3 implements AddLiquidityBase {
input.amountsIn.map((a) => a.amount),
amounts.minimumBpt,
!!input.wethIsEth,
'0x',
input.userData,
],
});
}
Expand All @@ -170,7 +181,7 @@ export class AddLiquidityV3 implements AddLiquidityBase {
input.amountsIn[input.tokenInIndex].amount,
input.bptOut.amount,
!!input.wethIsEth,
'0x',
input.userData,
],
});
}
Expand All @@ -192,7 +203,7 @@ export class AddLiquidityV3 implements AddLiquidityBase {
}

public buildCallWithPermit2(
input: AddLiquidityBaseBuildCallInput,
input: AddLiquidityBaseBuildCallInput & { userData: Hex },
permit2: Permit2,
): AddLiquidityBuildCallOutput {
const buildCallOutput = this.buildCall(input);
Expand Down
17 changes: 15 additions & 2 deletions src/entities/addLiquidity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AddLiquidityV3 } from './addLiquidityV3';
import { InputValidator } from '../inputValidator/inputValidator';
import { AddLiquidityCowAmm } from './addLiquidityCowAmm';
import { Permit2 } from '../permit2Helper';
import { Hex } from 'viem';

export class AddLiquidity implements AddLiquidityBase {
constructor(public config?: AddLiquidityConfig) {}
Expand Down Expand Up @@ -42,7 +43,11 @@ export class AddLiquidity implements AddLiquidityBase {
}
}

buildCall(input: AddLiquidityBuildCallInput): AddLiquidityBuildCallOutput {
buildCall(
input:
| AddLiquidityBuildCallInput
| (AddLiquidityBuildCallInput & { userData: Hex }),
): AddLiquidityBuildCallOutput {
switch (input.protocolVersion) {
case 1: {
const addLiquidity = new AddLiquidityCowAmm();
Expand All @@ -57,6 +62,10 @@ export class AddLiquidity implements AddLiquidityBase {
}
case 3: {
if (!('sender' in input)) {
if (!('userData' in input))
throw new Error(
'UserData must be provided in buildCall input',
);
const addLiquidity = new AddLiquidityV3();
return addLiquidity.buildCall(input);
}
Expand All @@ -68,10 +77,14 @@ export class AddLiquidity implements AddLiquidityBase {
}

buildCallWithPermit2(
input: AddLiquidityBuildCallInput,
input:
| AddLiquidityBuildCallInput
| (AddLiquidityBuildCallInput & { userData: Hex }),
permit2: Permit2,
): AddLiquidityBuildCallOutput {
InputValidator.validateBuildCallWithPermit2(input);
if (!('userData' in input))
throw new Error('UserData must be provided in buildCall input');

const addLiquidity = new AddLiquidityV3();
return addLiquidity.buildCallWithPermit2(input, permit2);
Expand Down
2 changes: 2 additions & 0 deletions src/entities/addLiquidity/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export enum AddLiquidityKind {
export type AddLiquidityBaseInput = {
chainId: number;
rpcUrl: string;
sender?: Address;
userData?: Hex;
};

export type AddLiquidityUnbalancedInput = AddLiquidityBaseInput & {
Expand Down
4 changes: 2 additions & 2 deletions src/entities/addLiquidityBoosted/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class AddLiquidityBoostedV3 {
const bptAmountOut = await doAddLiquidityUnbalancedQuery(
input.rpcUrl,
input.chainId,
input.userAddress ?? zeroAddress,
input.sender ?? zeroAddress,
input.userData ?? '0x',
poolState.address,
maxAmountsIn,
Expand All @@ -102,7 +102,7 @@ export class AddLiquidityBoostedV3 {
await doAddLiquidityProportionalQuery(
input.rpcUrl,
input.chainId,
input.userAddress ?? zeroAddress,
input.sender ?? zeroAddress,
input.userData ?? '0x',
poolState.address,
input.referenceAmount.rawAmount,
Expand Down
4 changes: 2 additions & 2 deletions src/entities/addLiquidityBoosted/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type AddLiquidityBoostedProportionalInput = {
rpcUrl: string;
referenceAmount: InputAmount;
kind: AddLiquidityKind.Proportional;
userAddress?: Address;
sender?: Address;
userData?: Hex;
};

Expand All @@ -18,7 +18,7 @@ export type AddLiquidityBoostedUnbalancedInput = {
rpcUrl: string;
amountsIn: InputAmount[];
kind: AddLiquidityKind.Unbalanced;
userAddress?: Address;
sender?: Address;
userData?: Hex;
};

Expand Down
13 changes: 11 additions & 2 deletions src/entities/removeLiquidity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { RemoveLiquidityV3 } from './removeLiquidityV3';
import { RemoveLiquidityCowAmm } from './removeLiquidityCowAmm';
import { Permit } from '../permitHelper';
import { RemoveLiquidityV2BuildCallInput } from './removeLiquidityV2/types';
import { Hex } from 'viem';

export class RemoveLiquidity implements RemoveLiquidityBase {
private readonly inputValidator: InputValidator = new InputValidator();
Expand Down Expand Up @@ -81,7 +82,8 @@ export class RemoveLiquidity implements RemoveLiquidityBase {
public buildCall(
input:
| RemoveLiquidityBaseBuildCallInput
| RemoveLiquidityV2BuildCallInput,
| RemoveLiquidityV2BuildCallInput
| (RemoveLiquidityBaseBuildCallInput & { userData: Hex }),
): RemoveLiquidityBuildCallOutput {
const isV2Input = 'sender' in input;
switch (input.protocolVersion) {
Expand All @@ -98,6 +100,10 @@ export class RemoveLiquidity implements RemoveLiquidityBase {
}
case 3: {
if (!isV2Input) {
if (!('userData' in input))
throw new Error(
'UserData must be provided in buildCall input',
);
const removeLiquidity = new RemoveLiquidityV3();
return removeLiquidity.buildCall(input);
}
Expand All @@ -111,10 +117,13 @@ export class RemoveLiquidity implements RemoveLiquidityBase {
public buildCallWithPermit(
input:
| RemoveLiquidityBaseBuildCallInput
| RemoveLiquidityV2BuildCallInput,
| RemoveLiquidityV2BuildCallInput
| (RemoveLiquidityBaseBuildCallInput & { userData: Hex }),
permit: Permit,
): RemoveLiquidityBuildCallOutput {
if (input.protocolVersion === 3) {
if (!('userData' in input))
throw new Error('UserData must be provided in buildCall input');
const removeLiquidity = new RemoveLiquidityV3();
return removeLiquidity.buildCallWithPermit(input, permit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import {
vaultExtensionAbi_V3,
vaultV3Abi,
} from '@/abi';
import { BALANCER_ROUTER, CHAINS } from '@/utils';
import { createPublicClient, http, zeroAddress } from 'viem';
import { RemoveLiquidityProportionalInput } from '../types';
import { BALANCER_ROUTER, ChainId, CHAINS } from '@/utils';
import { createPublicClient, Hex, http } from 'viem';
import { Address } from '@/types';

export const doRemoveLiquidityProportionalQuery = async (
{ chainId, rpcUrl, bptIn }: RemoveLiquidityProportionalInput,
rpcUrl: string,
chainId: ChainId,
sender: Address,
userData: Hex,
poolAddress: Address,
exactBptAmountIn: bigint,
): Promise<readonly bigint[]> => {
const client = createPublicClient({
transport: http(rpcUrl),
Expand All @@ -26,7 +29,7 @@ export const doRemoveLiquidityProportionalQuery = async (
...permit2Abi,
],
functionName: 'queryRemoveLiquidityProportional',
args: [poolAddress, bptIn.rawAmount, zeroAddress, '0x'],
args: [poolAddress, exactBptAmountIn, sender, userData],
});
return amountsOut;
};
Loading
Loading