Skip to content

Commit

Permalink
chore: Merge changes from Join.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Sep 14, 2023
2 parents 294c082 + 4d49b3f commit ced8f97
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 67 deletions.
15 changes: 9 additions & 6 deletions src/entities/encoders/weighted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export class WeightedEncoder {
* @param amountsIn - the amounts each of token to deposit in the pool as liquidity
* @param minimumBPT - the minimum acceptable BPT to receive in return for deposited tokens
*/
static joinExactIn = (amountsIn: bigint[], minimumBPT: bigint): Address =>
static joinUnbalanced = (
amountsIn: bigint[],
minimumBPT: bigint,
): Address =>
encodeAbiParameters(
[{ type: 'uint256' }, { type: 'uint256[]' }, { type: 'uint256' }],
[
Expand All @@ -54,7 +57,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
*/
static joinExactOutSingleAsset = (
static joinSingleAsset = (
bptAmountOut: bigint,
enterTokenIndex: number,
): Address => {
Expand All @@ -73,7 +76,7 @@ export class WeightedEncoder {
* Encodes the userData parameter for joining a WeightedPool proportionally to receive an exact amount of BPT
* @param bptAmountOut - the amount of BPT to be minted
*/
static joinExactOutProportional = (bptAmountOut: bigint): Address => {
static joinProportional = (bptAmountOut: bigint): Address => {
return encodeAbiParameters(
[{ type: 'uint256' }, { type: 'uint256' }],
[
Expand All @@ -88,7 +91,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
*/
static exitExactInSingleAsset = (
static exitSingleAsset = (
bptAmountIn: bigint,
exitTokenIndex: number,
): Address => {
Expand All @@ -106,7 +109,7 @@ export class WeightedEncoder {
* Encodes the userData parameter for exiting a WeightedPool by removing tokens in return for an exact amount of BPT
* @param bptAmountIn - the amount of BPT to be burned
*/
static exitExactInProportional = (bptAmountIn: bigint): Address => {
static exitProportional = (bptAmountIn: bigint): Address => {
return encodeAbiParameters(
[{ type: 'uint256' }, { type: 'uint256' }],
[
Expand All @@ -121,7 +124,7 @@ export class WeightedEncoder {
* @param amountsOut - the amounts of each token to be withdrawn from the pool
* @param maxBPTAmountIn - the minimum acceptable BPT to burn in return for withdrawn tokens
*/
static exitExactOut = (
static exitUnbalanced = (
amountsOut: bigint[],
maxBPTAmountIn: bigint,
): Address =>
Expand Down
2 changes: 1 addition & 1 deletion src/entities/exit/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TokenAmount } from '../tokenAmount';
import { Slippage } from '../slippage';
import { Address } from '../../types';
import { PoolState } from '../common';
import { PoolState } from '../utils';

export enum ExitKind {
UNBALANCED = 'UNBALANCED', // exitExactOut
Expand Down
20 changes: 7 additions & 13 deletions src/entities/exit/weighted/weightedExit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ import {
ExitKind,
ExitQueryResult,
} from '../types';
import { PoolState, replaceWrapped } from '../../common';
import { PoolState, replaceWrapped } from '../../utils';

export class WeightedExit implements BaseExit {
public async query(
input: ExitInput,
poolState: PoolState,
): Promise<ExitQueryResult> {
// TODO - This would need extended to work with relayer

const poolTokens = poolState.tokens.map(
(t) => new Token(input.chainId, t.address, t.decimals),
);
Expand All @@ -40,23 +38,21 @@ export class WeightedExit implements BaseExit {
input.amountsOut.find((a) => a.token.isEqual(t))
?.amount ?? 0n,
);
userData = WeightedEncoder.exitExactOut(
userData = WeightedEncoder.exitUnbalanced(
minAmountsOut,
MAX_UINT256,
);
break;
case ExitKind.SINGLE_ASSET:
userData = WeightedEncoder.exitExactInSingleAsset(
userData = WeightedEncoder.exitSingleAsset(
input.bptIn.amount,
poolTokens.findIndex(
(t) => t.address === input.tokenOut.toLowerCase(),
),
);
break;
case ExitKind.PROPORTIONAL:
userData = WeightedEncoder.exitExactInProportional(
input.bptIn.amount,
);
userData = WeightedEncoder.exitProportional(input.bptIn.amount);
break;
}

Expand Down Expand Up @@ -121,7 +117,7 @@ export class WeightedExit implements BaseExit {
case ExitKind.UNBALANCED:
minAmountsOut = input.amountsOut.map((a) => a.amount);
maxBptIn = input.slippage.applyTo(input.bptIn.amount);
userData = WeightedEncoder.exitExactOut(
userData = WeightedEncoder.exitUnbalanced(
minAmountsOut,
maxBptIn,
);
Expand All @@ -136,7 +132,7 @@ export class WeightedExit implements BaseExit {
input.slippage.removeFrom(a.amount),
);
maxBptIn = input.bptIn.amount;
userData = WeightedEncoder.exitExactInSingleAsset(
userData = WeightedEncoder.exitSingleAsset(
maxBptIn,
input.tokenOutIndex,
);
Expand All @@ -146,9 +142,7 @@ export class WeightedExit implements BaseExit {
input.slippage.removeFrom(a.amount),
);
maxBptIn = input.bptIn.amount;
userData = WeightedEncoder.exitExactInProportional(
input.bptIn.amount,
);
userData = WeightedEncoder.exitProportional(input.bptIn.amount);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export * from './slippage';
export * from './token';
export * from './tokenAmount';
export * from './pools/';
export * from './common';
export * from './utils';
26 changes: 13 additions & 13 deletions src/entities/join/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TokenAmount } from '../tokenAmount';
import { Slippage } from '../slippage';
import { Address } from '../../types';
import { PoolState } from '../common';
import { PoolState } from '../utils';

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

// This will be extended for each pools specific input requirements
Expand All @@ -22,27 +22,27 @@ export type InitJoinInput = BaseJoinInput & {
kind: JoinKind.Init;
};

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

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

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

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

// Returned from a join query
export type JoinQueryResult = {
Expand Down
38 changes: 38 additions & 0 deletions src/entities/join/weighted/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JoinInput, JoinKind, PoolState } from '..';
import { Address } from '../../../types';

export function getJoinParameters({
Expand All @@ -24,3 +25,40 @@ export function getJoinParameters({

return [poolId, sender, recipient, joinPoolRequest] as const;
}

export function checkInputs(input: JoinInput, poolState: PoolState) {
switch (input.kind) {
case JoinKind.Init:
checkTokenMismatch(
input.initAmountsIn.map((a) => a.token.address),
poolState.tokens.map((t) => t.address),
);
break;
case JoinKind.Unbalanced:
checkTokenMismatch(
input.amountsIn.map((a) => a.token.address),
poolState.tokens.map((t) => t.address),
);
break;
case JoinKind.SingleAsset:
checkTokenMismatch(
[input.tokenIn],
poolState.tokens.map((t) => t.address),
);
case JoinKind.Proportional:
checkTokenMismatch(
[input.bptOut.token.address.toLowerCase() as Address],
[poolState.address],
);
default:
break;
}
}

function checkTokenMismatch(tokensIn: Address[], poolTokens: Address[]) {
for (const tokenIn of tokensIn) {
if (!poolTokens.includes(tokenIn.toLowerCase() as Address)) {
throw new Error(`Token ${tokenIn} not found in pool`);
}
}
}
45 changes: 24 additions & 21 deletions src/entities/join/weighted/weightedJoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
ZERO_ADDRESS,
} from '../../../utils';
import { balancerHelpersAbi, vaultAbi } from '../../../abi';
import { getJoinParameters } from './helpers';
import { checkInputs, getJoinParameters } from './helpers';
import {
BaseJoin,
JoinCallInput,
JoinInput,
JoinKind,
JoinQueryResult,
} from '..';
import { PoolState, replaceWrapped } from '../../common';
import { PoolState, replaceWrapped } from '../../utils';

export class WeightedJoin implements BaseJoin {
public async query(
Expand All @@ -26,12 +26,12 @@ export class WeightedJoin implements BaseJoin {
): Promise<JoinQueryResult> {
// TODO - This would need extended to work with relayer

// TODO: check inputs
// joinExactOutProportional only works for Weighted v2+ -> should we handle at the SDK level or should we just let the query fail?
// TODO: Extend input validation for cases we'd like to check
checkInputs(input, poolState);

const poolTokens = poolState.tokens.map(
(t) => new Token(input.chainId, t.address, t.decimals),
);
const poolTokens = poolState.tokens
.sort((a, b) => a.index - b.index)
.map((t) => new Token(input.chainId, t.address, t.decimals));
let maxAmountsIn = Array(poolTokens.length).fill(MAX_UINT256);
let userData: Address;

Expand All @@ -44,24 +44,24 @@ export class WeightedJoin implements BaseJoin {
);
userData = WeightedEncoder.joinInit(maxAmountsIn);
break;
case JoinKind.ExactIn:
case JoinKind.Unbalanced:
maxAmountsIn = poolTokens.map(
(t) =>
input.amountsIn.find((a) => a.token.isEqual(t))
?.amount ?? 0n,
);
userData = WeightedEncoder.joinExactIn(maxAmountsIn, 0n);
userData = WeightedEncoder.joinUnbalanced(maxAmountsIn, 0n);
break;
case JoinKind.ExactOutSingleAsset:
userData = WeightedEncoder.joinExactOutSingleAsset(
case JoinKind.SingleAsset:
userData = WeightedEncoder.joinSingleAsset(
input.bptOut.amount,
poolTokens.findIndex(
(t) => t.address === input.tokenIn.toLowerCase(),
),
);
break;
case JoinKind.ExactOutProportional:
userData = WeightedEncoder.joinExactOutProportional(
case JoinKind.Proportional:
userData = WeightedEncoder.joinProportional(
input.bptOut.amount,
);
break;
Expand Down Expand Up @@ -103,7 +103,7 @@ export class WeightedJoin implements BaseJoin {
);

const tokenInIndex =
input.kind === JoinKind.ExactOutSingleAsset
input.kind === JoinKind.SingleAsset
? poolTokens.findIndex(
(t) => t.address === input.tokenIn.toLowerCase(),
)
Expand Down Expand Up @@ -135,31 +135,34 @@ export class WeightedJoin implements BaseJoin {
userData = WeightedEncoder.joinInit(maxAmountsIn);
break;
}
case JoinKind.ExactIn: {
case JoinKind.Unbalanced: {
maxAmountsIn = input.amountsIn.map((a) => a.amount);
minBptOut = input.slippage.removeFrom(input.bptOut.amount);
userData = WeightedEncoder.joinExactIn(maxAmountsIn, minBptOut);
userData = WeightedEncoder.joinUnbalanced(
maxAmountsIn,
minBptOut,
);
break;
}
case JoinKind.ExactOutSingleAsset:
case JoinKind.SingleAsset:
if (input.tokenInIndex === undefined) {
throw new Error(
'tokenInIndex must be defined for ExactOutSingleAsset joins',
'tokenInIndex must be defined for SingleAsset joins',
);
}
maxAmountsIn = input.amountsIn.map((a) =>
input.slippage.applyTo(a.amount),
);
userData = WeightedEncoder.joinExactOutSingleAsset(
userData = WeightedEncoder.joinSingleAsset(
input.bptOut.amount,
input.tokenInIndex,
);
break;
case JoinKind.ExactOutProportional: {
case JoinKind.Proportional: {
maxAmountsIn = input.amountsIn.map((a) =>
input.slippage.applyTo(a.amount),
);
userData = WeightedEncoder.joinExactOutProportional(
userData = WeightedEncoder.joinProportional(
input.bptOut.amount,
);
break;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ced8f97

Please sign in to comment.