Skip to content
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/late-rings-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@balancer/sdk": patch
---

Fix Permit2 spender for add liquidity unbalanced via swap to use UnbalancedAddViaSwapRouter, and sign the slippage-adjusted max adjustable amount.
2 changes: 1 addition & 1 deletion src/entities/addLiquidityUnbalancedViaSwap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type AddLiquidityUnbalancedViaSwapInput = {
*
* Use flow-specific fields for `buildCall` and Permit2:
* - `pool` — router address argument (distinct from `poolId`)
* - `expectedAdjustableAmountIn` — user budget / permit ceiling
* - `expectedAdjustableAmountIn` — user budget; Permit2 signs budget + slippage
*
* Generic `signAddLiquidityApproval` does not apply; use
* `signAddLiquidityUnbalancedViaSwapApproval` instead.
Expand Down
9 changes: 7 additions & 2 deletions src/entities/permit2Helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,20 @@ export class Permit2Helper {
expiration?: number;
},
): Promise<Permit2> {
const spender = AddressProvider.Router(input.chainId);
const spender = AddressProvider.UnbalancedAddViaSwapRouter(
input.chainId,
);
const maxAdjustableAmountIn = input.slippage.applyTo(
input.expectedAdjustableAmountIn.amount,
);
const details: PermitDetails[] = [];
details.push(
await getDetails(
input.client,
input.expectedAdjustableAmountIn.token.address,
input.owner,
spender,
input.expectedAdjustableAmountIn.amount,
maxAdjustableAmountIn,
input.expiration,
input.nonce,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ import { validateAddLiquidityUnbalancedViaSwapInput } from '@/entities/addLiquid
import { AddressProvider } from '@/entities/inputValidator/utils/addressProvider';
import { Token } from '@/entities/token';
import { TokenAmount } from '@/entities/tokenAmount';
import { Permit2Batch } from '@/entities/permit2Helper';
import { Permit2Batch, Permit2Helper } from '@/entities/permit2Helper';
import { PublicWalletClient } from '@/utils';
import { TOKENS } from 'test/lib/utils/addresses';

const chainId = ChainId.MAINNET;

const testOwner = '0x1111111111111111111111111111111111111111' as Address;

const createMockClient = (clientChainId: ChainId): PublicWalletClient =>
({
getChainId: async () => clientChainId,
readContract: async () => [0n, 0n, 0] as const,
signTypedData: async () => `0x${'00'.repeat(65)}` as `0x${string}`,
}) as unknown as PublicWalletClient;

const AAVE = TOKENS[chainId].AAVE;
const WETH = TOKENS[chainId].WETH;
const DAI = TOKENS[chainId].DAI;
Expand Down Expand Up @@ -433,4 +443,110 @@ describe('AddLiquidityUnbalancedViaSwap', () => {
);
});
});

describe('signAddLiquidityUnbalancedViaSwapApproval', () => {
test('uses UnbalancedAddViaSwapRouter as permit2 spender', async () => {
const bptToken = new Token(chainId, mockPoolState.address, 18);
const exactToken = new Token(chainId, WETH.address, WETH.decimals);
const adjustableToken = new Token(
chainId,
AAVE.address,
AAVE.decimals,
);
const exactAmountIn = TokenAmount.fromRawAmount(exactToken, 0n);
const expectedAdjustableAmountIn = TokenAmount.fromRawAmount(
adjustableToken,
parseUnits('50', AAVE.decimals),
);

const buildCallInput = {
poolType: mockPoolState.type,
poolId: mockPoolState.id,
addLiquidityKind: AddLiquidityKind.UnbalancedViaSwap,
pool: mockPoolState.address,
bptOut: TokenAmount.fromRawAmount(
bptToken,
parseUnits('100', 18),
),
exactAmountIn,
expectedAdjustableAmountIn,
amountsIn: [expectedAdjustableAmountIn, exactAmountIn],
tokenInIndex: 0,
chainId,
protocolVersion: 3 as const,
to: AddressProvider.UnbalancedAddViaSwapRouter(chainId),
addLiquidityUserData: '0x' as const,
swapUserData: '0x' as const,
slippage: Slippage.fromPercentage('1'),
deadline: maxUint256,
};

const permit2 =
await Permit2Helper.signAddLiquidityUnbalancedViaSwapApproval({
...buildCallInput,
client: createMockClient(chainId),
owner: testOwner,
});

expect(permit2.batch.spender).toEqual(
AddressProvider.UnbalancedAddViaSwapRouter(chainId),
);
expect(permit2.batch.spender).not.toEqual(
AddressProvider.Router(chainId),
);
});

test('applies slippage to permit amount', async () => {
const slippage = Slippage.fromPercentage('5');
const adjustableToken = new Token(
chainId,
AAVE.address,
AAVE.decimals,
);
const expectedAdjustableAmountIn = TokenAmount.fromRawAmount(
adjustableToken,
parseUnits('50', AAVE.decimals),
);
const bptToken = new Token(chainId, mockPoolState.address, 18);
const exactToken = new Token(chainId, WETH.address, WETH.decimals);
const exactAmountIn = TokenAmount.fromRawAmount(exactToken, 0n);

const buildCallInput = {
poolType: mockPoolState.type,
poolId: mockPoolState.id,
addLiquidityKind: AddLiquidityKind.UnbalancedViaSwap,
pool: mockPoolState.address,
bptOut: TokenAmount.fromRawAmount(
bptToken,
parseUnits('100', 18),
),
exactAmountIn,
expectedAdjustableAmountIn,
amountsIn: [expectedAdjustableAmountIn, exactAmountIn],
tokenInIndex: 0,
chainId,
protocolVersion: 3 as const,
to: AddressProvider.UnbalancedAddViaSwapRouter(chainId),
addLiquidityUserData: '0x' as const,
swapUserData: '0x' as const,
slippage,
deadline: maxUint256,
};

const permit2 =
await Permit2Helper.signAddLiquidityUnbalancedViaSwapApproval({
...buildCallInput,
client: createMockClient(chainId),
owner: testOwner,
});

const maxAdjustableAmount = slippage.applyTo(
expectedAdjustableAmountIn.amount,
);
expect(permit2.batch.details[0].amount).toBe(maxAdjustableAmount);
expect(permit2.batch.details[0].amount).not.toBe(
expectedAdjustableAmountIn.amount,
);
});
});
});
Loading