Skip to content

Commit

Permalink
fix unbalanced add liquidity boosted with fewer than total num of poo…
Browse files Browse the repository at this point in the history
…l tokens
  • Loading branch information
MattPereira committed Jan 29, 2025
1 parent 347aded commit 512547c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
59 changes: 38 additions & 21 deletions src/entities/addLiquidityBoosted/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export class AddLiquidityBoostedV3 {
});

const bptToken = new Token(input.chainId, poolState.address, 18);
const wrapUnderlying: boolean[] = [];
const wrapUnderlying: boolean[] = new Array(
poolState.tokens.length,
).fill(false);

let bptOut: TokenAmount;
let amountsIn: TokenAmount[];
Expand Down Expand Up @@ -86,9 +88,8 @@ export class AddLiquidityBoostedV3 {

switch (input.kind) {
case AddLiquidityKind.Unbalanced: {
// Infer wrapUnderlying from token addreses provided by amountsIn
const tokensIn: ExtendedMinimalToken[] = input.amountsIn
.map((amountIn) => {
const tokensIn: ExtendedMinimalToken[] = input.amountsIn.map(
(amountIn) => {
const amountInAddress = amountIn.address.toLowerCase();
const token = poolStateTokenMap[amountInAddress];
if (!token) {
Expand All @@ -97,11 +98,29 @@ export class AddLiquidityBoostedV3 {
);
}
return token;
})
.sort((a, b) => a.index - b.index); // sort by index so wrapUnderlying is in correct order
},
);

// if user provides fewer than the number of pool tokens, fill remaining indexes
// because smart contract requires length of pool tokens to match maxAmountsIn and wrapUnderlying
if (tokensIn.length < poolState.tokens.length) {
const existingIndices = new Set(
tokensIn.map((t) => t.index),
);
poolState.tokens.forEach((poolToken) => {
if (!existingIndices.has(poolToken.index)) {
tokensIn.push({
index: poolToken.index,
decimals: poolToken.decimals,
address: poolToken.address,
isUnderlyingToken: false,
});
}
});
}

tokensIn.forEach((t) => {
wrapUnderlying.push(t.isUnderlyingToken);
wrapUnderlying[t.index] = t.isUnderlyingToken;
});

// It is allowed not not provide the same amount of TokenAmounts as inputs
Expand All @@ -125,24 +144,22 @@ export class AddLiquidityBoostedV3 {
amountsIn = sortedTokens.map((t, i) =>
TokenAmount.fromRawAmount(t, maxAmountsIn[i]),
);

break;
}
case AddLiquidityKind.Proportional: {
// User provides tokensIn addresses so we can infer if they need to be wrapped

const sortedTokensInWithDetails = input.tokensIn
.map((t) => {
const tokenWithDetails =
poolStateTokenMap[t.toLowerCase() as Address];
if (!tokenWithDetails) {
throw new Error(`Invalid token address: ${t}`);
}
return tokenWithDetails;
})
.sort((a, b) => a.index - b.index);
// User provides addresses via input.tokensIn so we can infer if they need to be wrapped
const tokensWithDetails = input.tokensIn.map((t) => {
const tokenWithDetails =
poolStateTokenMap[t.toLowerCase() as Address];
if (!tokenWithDetails) {
throw new Error(`Invalid token address: ${t}`);
}
return tokenWithDetails;
});

sortedTokensInWithDetails.forEach((t) => {
wrapUnderlying.push(t.isUnderlyingToken);
tokensWithDetails.forEach((t) => {
wrapUnderlying[t.index] = t.isUnderlyingToken;
});

const bptAmount = await getBptAmountFromReferenceAmountBoosted(
Expand Down
4 changes: 2 additions & 2 deletions test/v3/priceImpact/priceImpact.V3.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('PriceImpact V3', () => {
addLiquidityInput,
boostedPool_USDC_USDT,
);
const priceImpactSpot = PriceImpactAmount.fromDecimal('0.000211');
const priceImpactSpot = PriceImpactAmount.fromDecimal('0.000208');
expect(priceImpactABA.decimal).eq(priceImpactSpot.decimal);
});
});
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('PriceImpact V3', () => {
nestedWithBoostedPool,
);
const priceImpactSpot = PriceImpactAmount.fromDecimal(
'0.10540652060304274',
'0.004206886163133692',
);
expect(priceImpactABA.decimal).eq(priceImpactSpot.decimal);
});
Expand Down

0 comments on commit 512547c

Please sign in to comment.