Skip to content

Commit

Permalink
Minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoguerios committed Oct 11, 2023
1 parent 584914b commit 4e64197
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
21 changes: 11 additions & 10 deletions src/entities/nestedJoin/doQueryNestedJoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import {
decodeFunctionResult,
http,
} from 'viem';
import { Hex } from '../../types';
import { Address, Hex } from '../../types';
import { BALANCER_RELAYER, CHAINS } from '../../utils';
import { balancerRelayerAbi } from '../../abi';
import { NestedJoinInput } from './types';

export async function doQueryNestedJoin(
input: NestedJoinInput,
export const doQueryNestedJoin = async (
chainId: number,
rpcUrl: string,
accountAddress: Address,
encodedMulticall: Hex,
) {
): Promise<bigint> => {
const client = createPublicClient({
transport: http(input.rpcUrl),
chain: CHAINS[input.chainId],
transport: http(rpcUrl),
chain: CHAINS[chainId],
});

const { data } = await client.call({
account: input.testAddress,
to: BALANCER_RELAYER[input.chainId],
account: accountAddress,
to: BALANCER_RELAYER[chainId],
data: encodedMulticall,
});

Expand All @@ -36,4 +37,4 @@ export async function doQueryNestedJoin(
)[0];

return peekedValue;
}
};
34 changes: 19 additions & 15 deletions src/entities/nestedJoin/getNestedJoinCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@ import { Token } from '../token';
import { getPoolAddress } from '../../utils';
import { NestedJoinInput, NestedPoolState, NestedJoinCall } from './types';

export function getNestedJoinCalls(
input: NestedJoinInput,
nestedPoolState: NestedPoolState,
) {
export const getNestedJoinCalls = (
{
amountsIn,
chainId,
accountAddress,
useNativeAssetAsWrappedAmountIn,
fromInternalBalance,
}: NestedJoinInput,
{ pools }: NestedPoolState,
): NestedJoinCall[] => {
/**
* Overall logic to build sequence of join calls:
* 1. Go from bottom pool to up filling out input amounts and output refs
* 2. Inputs will be amountsIn provided, output of the previous level or 0n
* 3. Output at max level is the bptOut
*/

const poolsSortedByLevel = nestedPoolState.pools.sort(
(a, b) => a.level - b.level,
);
const poolsSortedByLevel = pools.sort((a, b) => a.level - b.level);

const calls: NestedJoinCall[] = [];
for (const pool of poolsSortedByLevel) {
const sortedTokens = pool.tokens
.sort((a, b) => a.index - b.index)
.map((t) => new Token(input.chainId, t.address, t.decimals));
.map((t) => new Token(chainId, t.address, t.decimals));
calls.push({
chainId: input.chainId,
chainId: chainId,
useNativeAssetAsWrappedAmountIn:
input.useNativeAssetAsWrappedAmountIn ?? false,
useNativeAssetAsWrappedAmountIn ?? false,
sortedTokens,
poolId: pool.id,
poolType: pool.type,
kind: 0,
sender: input.testAddress,
recipient: input.testAddress,
sender: accountAddress,
recipient: accountAddress,
maxAmountsIn: sortedTokens.map((token) => {
const amountIn = input.amountsIn.find((a) =>
const amountIn = amountsIn.find((a) =>
token.isSameAddress(a.address),
);
const lowerLevelCall = calls.find(
Expand All @@ -57,9 +61,9 @@ export function getNestedJoinCalls(
}
}),
minBptOut: 0n,
fromInternalBalance: input.fromInternalBalance ?? false,
fromInternalBalance: fromInternalBalance ?? false,
outputReferenceKey: BigInt(poolsSortedByLevel.indexOf(pool)) + 100n,
});
}
return calls;
}
};
7 changes: 6 additions & 1 deletion src/entities/nestedJoin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ export class NestedJoin {
args: [encodedCalls],
});

const peekedValue = await doQueryNestedJoin(input, encodedMulticall);
const peekedValue = await doQueryNestedJoin(
input.chainId,
input.rpcUrl,
input.accountAddress,
encodedMulticall,
);

const tokenOut = new Token(
input.chainId,
Expand Down
6 changes: 3 additions & 3 deletions src/entities/nestedJoin/parseNestedJoinCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NestedJoinCall } from './types';
import { Relayer } from '../relayer';
import { replaceWrapped } from '../utils/replaceWrapped';

export function parseNestedJoinCall({
export const parseNestedJoinCall = ({
useNativeAssetAsWrappedAmountIn,
chainId,
sortedTokens,
Expand All @@ -19,7 +19,7 @@ export function parseNestedJoinCall({
minBptOut,
fromInternalBalance,
outputReferenceKey,
}: NestedJoinCall) {
}: NestedJoinCall) => {
// replace wrapped token with native asset if needed
let tokensIn = [...sortedTokens];
let value = 0n;
Expand Down Expand Up @@ -79,4 +79,4 @@ export function parseNestedJoinCall({
] as const,
value,
};
}
};
4 changes: 2 additions & 2 deletions src/entities/nestedJoin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type NestedJoinInput = {
}[];
chainId: number;
rpcUrl: string;
testAddress: Address;
accountAddress: Address;
useNativeAssetAsWrappedAmountIn?: boolean;
fromInternalBalance?: boolean;
};
Expand All @@ -31,7 +31,7 @@ export type NestedPoolState = {

export type NestedJoinCall = {
chainId: number;
useNativeAssetAsWrappedAmountIn?: boolean;
useNativeAssetAsWrappedAmountIn: boolean;
sortedTokens: Token[];
poolId: Address;
poolType: string;
Expand Down

0 comments on commit 4e64197

Please sign in to comment.