Skip to content

Commit

Permalink
Refactor join types to match contract interface
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoguerios committed Sep 8, 2023
1 parent 9f9935b commit c95d21e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 141 deletions.
2 changes: 2 additions & 0 deletions src/entities/encoders/weighted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class WeightedEncoder {
* @param bptAmountOut - the amount of BPT to be minted
* @param enterTokenIndex - the index of the token to be provided as liquidity
*/
// TODO: refactor this into 2 separate functions
static joinGivenOut = (
bptAmountOut: bigint,
enterTokenIndex?: number,
Expand Down Expand Up @@ -86,6 +87,7 @@ export class WeightedEncoder {
* @param bptAmountIn - the amount of BPT to be burned
* @param enterTokenIndex - the index of the token to removed from the pool
*/
// TODO: refactor this into 2 separate functions
static exitGivenIn = (
bptAmountIn: bigint,
exitTokenIndex?: number,
Expand Down
35 changes: 15 additions & 20 deletions src/entities/join/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { Token } from '../token';

export enum JoinKind {
Init = 'Init',
Proportional = 'Proportional',
Unbalanced = 'Unbalanced',
SingleAsset = 'SingleAsset',
ExactOut = 'ExactOut',
ExactIn = 'ExactIn',
ExactOutSingleAsset = 'ExactOutSingleAsset',
ExactOutProportional = 'ExactOutProportional',
}

// Returned from API and used as input
Expand All @@ -32,39 +31,35 @@ export type InitJoinInput = BaseJoinInput & {
kind: JoinKind.Init;
};

export type ProportionalJoinInput = BaseJoinInput & {
refAmountIn: TokenAmount;
kind: JoinKind.Proportional;
};

export type UnbalancedJoinInput = BaseJoinInput & {
export type ExactInJoinInput = BaseJoinInput & {
amountsIn: TokenAmount[];
kind: JoinKind.Unbalanced;
kind: JoinKind.ExactIn;
};

export type SingleAssetJoinInput = BaseJoinInput & {
amountIn: TokenAmount;
kind: JoinKind.SingleAsset;
export type ExactOutSingleAssetJoinInput = BaseJoinInput & {
bptOut: TokenAmount;
tokenIn: Address;
kind: JoinKind.ExactOutSingleAsset;
};

export type ExactOutJoinInput = BaseJoinInput & {
export type ExactOutProportionalJoinInput = BaseJoinInput & {
bptOut: TokenAmount;
kind: JoinKind.ExactOut;
kind: JoinKind.ExactOutProportional;
};

export type JoinInput =
| InitJoinInput
| ProportionalJoinInput
| UnbalancedJoinInput
| SingleAssetJoinInput
| ExactOutJoinInput;
| ExactInJoinInput
| ExactOutSingleAssetJoinInput
| ExactOutProportionalJoinInput;

// Returned from a join query
export type JoinQueryResult = {
id: Address;
joinKind: JoinKind;
bptOut: TokenAmount;
amountsIn: TokenAmount[];
tokenInIndex?: number;
};

export type JoinCallInput = JoinQueryResult & {
Expand Down
37 changes: 6 additions & 31 deletions src/entities/join/weighted/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Address } from '../../../types';
import { ZERO_ADDRESS } from '../../../utils';
import { WeightedEncoder } from '../../encoders';
import { TokenAmount } from '../../tokenAmount';
import { JoinInput, JoinKind, PoolState } from '..';
import { JoinInput, JoinKind } from '..';
import { Token } from '../../token';

export function getJoinParameters({
Expand Down Expand Up @@ -30,21 +29,6 @@ export function getJoinParameters({
return [poolId, sender, recipient, joinPoolRequest] as const;
}

export function checkInputs(input: JoinInput, poolState: PoolState) {
const poolAssets = poolState.tokens.map((t) => t.address);
switch (input.kind) {
case JoinKind.Proportional: {
if (!poolAssets.includes(input.refAmountIn.token.address)) {
throw new Error('Reference token not in pool');
}
}
break;
// TODO: think about a way to consolidate checks so this doesn't become uneccessarily hard to maintain
default:
break;
}
}

// TODO: amounts for native asset join relies on the fact that the user provided wrapped address for input tokens - is that a fair assumption?
export function getAmountsIn(input: JoinInput, poolTokens: Token[]): bigint[] {
return poolTokens.map((token) => {
Expand All @@ -55,18 +39,9 @@ export function getAmountsIn(input: JoinInput, poolTokens: Token[]): bigint[] {
t.token.isEqual(token),
);
break;
case JoinKind.Proportional:
if (input.refAmountIn.token.isEqual(token))
tokenIn = input.refAmountIn;
// TODO: calculate proportional amounts based on reference token
break;
case JoinKind.Unbalanced:
case JoinKind.ExactIn:
tokenIn = input.amountsIn.find((t) => t.token.isEqual(token));
break;
case JoinKind.SingleAsset:
if (input.amountIn.token.isEqual(token))
tokenIn = input.amountIn;
break;
}
return tokenIn?.amount ?? 0n;
});
Expand All @@ -79,13 +54,13 @@ export function getUserData(input: JoinInput, amountsIn: bigint[]): Address {
userData = WeightedEncoder.joinInit(amountsIn);
break;
}
case JoinKind.Proportional:
case JoinKind.Unbalanced:
case JoinKind.SingleAsset: {
case JoinKind.ExactIn: {
userData = WeightedEncoder.joinGivenIn(amountsIn, 0n);
break;
}
case JoinKind.ExactOut: {
case JoinKind.ExactOutSingleAsset:
// TODO: add tokenInIndex after refactoring into switch/case into separate functions
case JoinKind.ExactOutProportional: {
userData = WeightedEncoder.joinGivenOut(input.bptOut.amount);
break;
}
Expand Down
49 changes: 27 additions & 22 deletions src/entities/join/weighted/weightedJoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import {
ZERO_ADDRESS,
} from '../../../utils';
import { balancerHelpersAbi, vaultAbi } from '../../../abi';
import {
checkInputs,
getAmountsIn,
getJoinParameters,
getUserData,
} from './helpers';
import { getAmountsIn, getJoinParameters, getUserData } from './helpers';
import {
BaseJoin,
JoinCallInput,
Expand All @@ -36,22 +31,22 @@ export class WeightedJoin implements BaseJoin {
): Promise<JoinQueryResult> {
// TODO - This would need extended to work with relayer

checkInputs(input, poolState);
// TODO: check inputs after refactoring switch/case into separate functions

const maxAmountsIn = getAmountsIn(input, poolState.tokens);
const userData = getUserData(input, maxAmountsIn);

let tokensIn = poolState.tokens;
// replace wrapped token with native asset if needed
const tokensIn = poolState.tokens.map((token) => {
if (
input.joinWithNativeAsset &&
token.isUnderlyingEqual(NATIVE_ASSETS[input.chainId])
) {
return new Token(input.chainId, ZERO_ADDRESS, 18);
} else {
return token;
}
});
if (input.joinWithNativeAsset) {
tokensIn = poolState.tokens.map((token) => {
if (token.isUnderlyingEqual(NATIVE_ASSETS[input.chainId])) {
return new Token(input.chainId, ZERO_ADDRESS, 18);
} else {
return token;
}
});
}

const queryArgs = getJoinParameters({
poolId: poolState.id,
Expand Down Expand Up @@ -83,11 +78,17 @@ export class WeightedJoin implements BaseJoin {
TokenAmount.fromRawAmount(tokensIn[i], a),
);

const tokenInIndex =
input.kind === JoinKind.ExactOutSingleAsset
? poolState.tokens.findIndex((t) => t.address === input.tokenIn)
: undefined;

return {
joinKind: input.kind,
id: poolState.id,
bptOut,
amountsIn,
tokenInIndex,
};
}

Expand All @@ -96,6 +97,7 @@ export class WeightedJoin implements BaseJoin {
to: Address;
value: bigint | undefined;
minBptOut: bigint;
// TODO: add maxAmountsIn after creating test scenario for ExactOut joins
} {
let maxAmountsIn: bigint[];
let userData: Address;
Expand All @@ -107,19 +109,22 @@ export class WeightedJoin implements BaseJoin {
userData = WeightedEncoder.joinInit(maxAmountsIn);
break;
}
case JoinKind.Proportional:
case JoinKind.Unbalanced:
case JoinKind.SingleAsset: {
case JoinKind.ExactIn: {
maxAmountsIn = input.amountsIn.map((a) => a.amount);
minBptOut = input.slippage.removeFrom(input.bptOut.amount);
userData = WeightedEncoder.joinGivenIn(maxAmountsIn, minBptOut);
break;
}
case JoinKind.ExactOut: {
case JoinKind.ExactOutSingleAsset:
case JoinKind.ExactOutProportional: {
// TODO: refactor this after splitting encoder into 2 methods
maxAmountsIn = input.amountsIn.map((a) =>
input.slippage.applyTo(a.amount),
);
userData = WeightedEncoder.joinGivenOut(input.bptOut.amount);
userData = WeightedEncoder.joinGivenOut(
input.bptOut.amount,
input.tokenInIndex,
);
break;
}
default:
Expand Down
Loading

0 comments on commit c95d21e

Please sign in to comment.