Skip to content

Commit

Permalink
Merge pull request #378 from balancer/377-remove-liquidity-nested-que…
Browse files Browse the repository at this point in the history
…ry-bug

Fix remove liquidity nested query (peek) logic
  • Loading branch information
brunoguerios authored Aug 7, 2024
2 parents 6f9b00e + 2fffd77 commit 74d0a90
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-points-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@balancer/sdk": patch
---

Fix remove liquidity nested query (peek) logic
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const doRemoveLiquidityNestedQuery = async (
chainId: ChainId,
rpcUrl: string,
encodedMulticall: Hex,
tokensOutLength: number,
tokensOutIndexes: number[],
): Promise<bigint[]> => {
const client = createPublicClient({
transport: http(rpcUrl),
Expand All @@ -30,11 +30,12 @@ export const doRemoveLiquidityNestedQuery = async (
data: data as Hex,
});

const resultsToPeek = result.slice(result.length - tokensOutLength);
const peekedValues: bigint[] = [];

const peekedValues = resultsToPeek.map(
(r) => decodeAbiParameters([{ type: 'uint256' }], r)[0],
);
result.forEach((r, i) => {
if (tokensOutIndexes.includes(i))
peekedValues.push(decodeAbiParameters([{ type: 'uint256' }], r)[0]);
});

return peekedValues;
};
14 changes: 10 additions & 4 deletions src/entities/removeLiquidityNested/getPeekCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ export const getPeekCalls = (

if (!isTokenBeingUsedAsInput) {
tokensOut.push(tokenOut);
const readOnlyChainedReference = Relayer.toChainedReference(
Relayer.fromChainedReference(outputReference.key),
false,
);
peekCalls.push(
Relayer.encodePeekChainedReferenceValue(
outputReference.key,
readOnlyChainedReference,
),
);
}
Expand All @@ -40,10 +44,12 @@ export const getPeekCalls = (
const tokenOut =
lastCall.sortedTokens[lastCall.tokenOutIndex as number];
tokensOut.push(tokenOut);
const readOnlyChainedReference = Relayer.toChainedReference(
Relayer.fromChainedReference(lastCall.outputReferences[0].key),
false,
);
peekCalls.push(
Relayer.encodePeekChainedReferenceValue(
lastCall.outputReferences[0].key,
),
Relayer.encodePeekChainedReferenceValue(readOnlyChainedReference),
);
}

Expand Down
20 changes: 17 additions & 3 deletions src/entities/removeLiquidityNested/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,22 @@ export class RemoveLiquidityNested {
isProportional,
);

// append peek calls to get amountsOut
encodedCalls.push(...peekCalls);
// insert peek calls to get amountsOut
let tokensOutCount = 0;
const tokensOutIndexes: number[] = [];
callsAttributes.forEach((call, i) => {
tokensOut.forEach((tokenOut, j) => {
if (
call.sortedTokens.some((t) =>
t.isSameAddress(tokenOut.address),
)
) {
tokensOutCount++;
encodedCalls.splice(i + tokensOutCount, 0, peekCalls[j]);
tokensOutIndexes.push(i + tokensOutCount);
}
});
});

const encodedMulticall = encodeFunctionData({
abi: balancerRelayerAbi,
Expand All @@ -54,7 +68,7 @@ export class RemoveLiquidityNested {
input.chainId,
input.rpcUrl,
encodedMulticall,
tokensOut.length,
tokensOutIndexes,
);

const amountsOut = tokensOut.map((tokenOut, i) =>
Expand Down

0 comments on commit 74d0a90

Please sign in to comment.