Skip to content

Commit

Permalink
Merge pull request #98 from balancer/fix-circular-deps
Browse files Browse the repository at this point in the history
fix: Circular deps.
  • Loading branch information
johngrantuk authored Sep 28, 2023
2 parents e1efe6b + 1d54c4c commit 3862fd5
Show file tree
Hide file tree
Showing 40 changed files with 214 additions and 185 deletions.
3 changes: 2 additions & 1 deletion src/data/enrichers/onChainPoolDataEnricher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RawPool,
RawPoolTokenWithRate,
RawWeightedPoolToken,
HumanAmount,
} from '../types';

import {
Expand All @@ -16,7 +17,7 @@ import {
poolHasVirtualSupply,
poolIsLinearPool,
} from '../../utils';
import { HumanAmount, SwapOptions } from '../../types';
import { SwapOptions } from '../../types';

interface OnChainPoolData {
id: string;
Expand Down
3 changes: 2 additions & 1 deletion src/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Address, Hex } from 'viem';
import { HumanAmount } from '../types';

export type HumanAmount = `${number}`;

// These are only the known pool types, additional pool types can be added via
// extension through custom PoolFactories and PoolDataProviders
Expand Down
2 changes: 1 addition & 1 deletion src/entities/exit/weighted/validateInputs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExitInput, ExitKind } from '..';
import { ExitInput, ExitKind } from '../types';
import { PoolStateInput } from '../../types';
import { areTokensInArray } from '../../utils/areTokensInArray';

Expand Down
2 changes: 1 addition & 1 deletion src/entities/join/weighted/validateInputs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JoinInput, JoinKind } from '..';
import { JoinInput, JoinKind } from '../types';
import { PoolStateInput } from '../../types';
import { areTokensInArray } from '../../utils/areTokensInArray';

Expand Down
6 changes: 4 additions & 2 deletions src/entities/join/weighted/weightedJoin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { encodeFunctionData } from 'viem';
import { Token, TokenAmount, WeightedEncoder } from '../../..';
import { Token } from '../../../entities/token';
import { TokenAmount } from '../../../entities/tokenAmount';
import { WeightedEncoder } from '../../../entities/encoders/weighted';
import { Address } from '../../../types';
import { BALANCER_VAULT, MAX_UINT256, ZERO_ADDRESS } from '../../../utils';
import { vaultAbi } from '../../../abi';
Expand All @@ -10,7 +12,7 @@ import {
JoinInput,
JoinKind,
JoinQueryResult,
} from '..';
} from '../types';
import { AmountsJoin, PoolState } from '../../types';
import { doQueryJoin, getAmounts, parseJoinArgs } from '../../utils';

Expand Down
3 changes: 2 additions & 1 deletion src/entities/path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BasePool } from './pools/';
import { Token, TokenAmount } from './';
import { Token } from './token';
import { TokenAmount } from './tokenAmount';
import { SwapKind } from '../types';

export class Path {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/pools/fx/fxFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasePool, BasePoolFactory } from '../';
import { FxPool } from './';
import { FxPool } from './fxPool';
import { RawFxPool, RawPool } from '../../../data/types';

export class FxPoolFactory implements BasePoolFactory {
Expand Down
4 changes: 2 additions & 2 deletions src/entities/pools/fx/fxMath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseUnits } from 'viem';
import { RAY } from '../../../utils';
import { FxPoolPairData } from './fxPool';
import { RAY } from '../../../utils/math';
import { FxPoolPairData } from './types';
import { SwapKind } from '../../../types';

export const CURVEMATH_MAX_DIFF = parseUnits('-0.000001000000000000024', 36);
Expand Down
107 changes: 6 additions & 101 deletions src/entities/pools/fx/fxPool.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Hex, parseEther, parseUnits } from 'viem';
import { HumanAmount, PoolType, SwapKind } from '../../../types';
import { BigintIsh, Token, TokenAmount } from '../../';
import { PoolType, SwapKind } from '../../../types';
import { Token } from '../../token';
import { TokenAmount } from '../../tokenAmount';
import { BasePool } from '../../pools';
import { RAY, WAD, getPoolAddress } from '../../../utils';
import { RAY, getPoolAddress } from '../../../utils';
import { _calcInGivenOut, _calcOutGivenIn } from './fxMath';
import { RawFxPool } from '../../../data/types';
import { MathFx, parseFixedCurveParam } from './helpers';
import { FxPoolPairData } from './types';
import { FxPoolToken } from './fxPoolToken';

const isUSDC = (address: string): boolean => {
return (
Expand All @@ -15,104 +18,6 @@ const isUSDC = (address: string): boolean => {
);
};

export type FxPoolPairData = {
tIn: FxPoolToken;
tOut: FxPoolToken;
alpha: bigint;
beta: bigint;
delta: bigint;
lambda: bigint;
_oGLiq: bigint;
_nGLiq: bigint;
_oBals: bigint[];
_nBals: bigint[];
givenToken: FxPoolToken;
swapKind: SwapKind;
};

export class FxPoolToken extends TokenAmount {
public readonly index: number;
public readonly latestFXPrice: HumanAmount;
public readonly fxOracleDecimals: number;
public numeraire: bigint; // in 36 decimals
private readonly scalar36 = this.scalar * WAD;

public constructor(
token: Token,
amount: BigintIsh,
latestFXPrice: HumanAmount,
fxOracleDecimals: number,
index: number,
) {
super(token, amount);
this.latestFXPrice = latestFXPrice;
this.fxOracleDecimals = fxOracleDecimals;
const truncatedNumeraire = MathFx.mulDownFixed(
this.amount,
parseUnits(this.latestFXPrice, this.fxOracleDecimals),
this.fxOracleDecimals,
);
this.numeraire = truncatedNumeraire * this.scalar36;
this.index = index;
}

public increase(amount: bigint): TokenAmount {
this.amount = this.amount + amount;
this.scale18 = this.amount * this.scalar;
const truncatedNumeraire = MathFx.mulDownFixed(
this.amount,
parseUnits(this.latestFXPrice, this.fxOracleDecimals),
this.fxOracleDecimals,
);
this.numeraire = truncatedNumeraire * this.scalar36;
return this;
}

public decrease(amount: bigint): TokenAmount {
this.amount = this.amount - amount;
this.scale18 = this.amount * this.scalar;
const truncatedNumeraire = MathFx.mulDownFixed(
this.amount,
parseUnits(this.latestFXPrice, this.fxOracleDecimals),
this.fxOracleDecimals,
);
this.numeraire = truncatedNumeraire * this.scalar36;
return this;
}

public static fromNumeraire(
poolToken: FxPoolToken,
numeraire: BigintIsh,
divUp?: boolean,
): FxPoolToken {
const truncatedNumeraire = BigInt(numeraire) / poolToken.scalar36; // loss of precision required to match SC implementation
const amount = divUp
? MathFx.divUpFixed(
BigInt(truncatedNumeraire),
parseUnits(
poolToken.latestFXPrice,
poolToken.fxOracleDecimals,
),
poolToken.fxOracleDecimals,
)
: MathFx.divDownFixed(
BigInt(truncatedNumeraire),
parseUnits(
poolToken.latestFXPrice,
poolToken.fxOracleDecimals,
),
poolToken.fxOracleDecimals,
);
return new FxPoolToken(
poolToken.token,
amount,
poolToken.latestFXPrice,
poolToken.fxOracleDecimals,
poolToken.index,
);
}
}

export class FxPool implements BasePool {
public readonly chainId: number;
public readonly id: Hex;
Expand Down
90 changes: 90 additions & 0 deletions src/entities/pools/fx/fxPoolToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { parseUnits } from 'viem';
import { Token } from '../../token';
import { TokenAmount, BigintIsh } from '../../tokenAmount';
import { WAD } from '../../../utils/math';
import { _calcInGivenOut, _calcOutGivenIn } from './fxMath';
import { HumanAmount } from '../../../data/types';
import { MathFx } from './helpers';

export class FxPoolToken extends TokenAmount {
public readonly index: number;
public readonly latestFXPrice: HumanAmount;
public readonly fxOracleDecimals: number;
public numeraire: bigint; // in 36 decimals
private readonly scalar36 = this.scalar * WAD;

public constructor(
token: Token,
amount: BigintIsh,
latestFXPrice: HumanAmount,
fxOracleDecimals: number,
index: number,
) {
super(token, amount);
this.latestFXPrice = latestFXPrice;
this.fxOracleDecimals = fxOracleDecimals;
const truncatedNumeraire = MathFx.mulDownFixed(
this.amount,
parseUnits(this.latestFXPrice, this.fxOracleDecimals),
this.fxOracleDecimals,
);
this.numeraire = truncatedNumeraire * this.scalar36;
this.index = index;
}

public increase(amount: bigint): TokenAmount {
this.amount = this.amount + amount;
this.scale18 = this.amount * this.scalar;
const truncatedNumeraire = MathFx.mulDownFixed(
this.amount,
parseUnits(this.latestFXPrice, this.fxOracleDecimals),
this.fxOracleDecimals,
);
this.numeraire = truncatedNumeraire * this.scalar36;
return this;
}

public decrease(amount: bigint): TokenAmount {
this.amount = this.amount - amount;
this.scale18 = this.amount * this.scalar;
const truncatedNumeraire = MathFx.mulDownFixed(
this.amount,
parseUnits(this.latestFXPrice, this.fxOracleDecimals),
this.fxOracleDecimals,
);
this.numeraire = truncatedNumeraire * this.scalar36;
return this;
}

public static fromNumeraire(
poolToken: FxPoolToken,
numeraire: BigintIsh,
divUp?: boolean,
): FxPoolToken {
const truncatedNumeraire = BigInt(numeraire) / poolToken.scalar36; // loss of precision required to match SC implementation
const amount = divUp
? MathFx.divUpFixed(
BigInt(truncatedNumeraire),
parseUnits(
poolToken.latestFXPrice,
poolToken.fxOracleDecimals,
),
poolToken.fxOracleDecimals,
)
: MathFx.divDownFixed(
BigInt(truncatedNumeraire),
parseUnits(
poolToken.latestFXPrice,
poolToken.fxOracleDecimals,
),
poolToken.fxOracleDecimals,
);
return new FxPoolToken(
poolToken.token,
amount,
poolToken.latestFXPrice,
poolToken.fxOracleDecimals,
poolToken.index,
);
}
}
2 changes: 1 addition & 1 deletion src/entities/pools/fx/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseUnits } from 'viem';
import { HumanAmount } from '../../../types';
import { HumanAmount } from '../../../data/types';

export class MathFx {
static mulDownFixed(a: bigint, b: bigint, decimals = 36): bigint {
Expand Down
2 changes: 2 additions & 0 deletions src/entities/pools/fx/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './fxFactory';
export * from './fxPool';
export * from './fxMath';
export * from './types';
export * from './fxPoolToken';
17 changes: 17 additions & 0 deletions src/entities/pools/fx/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SwapKind } from '../../../types';
import { FxPoolToken } from './fxPoolToken';

export type FxPoolPairData = {
tIn: FxPoolToken;
tOut: FxPoolToken;
alpha: bigint;
beta: bigint;
delta: bigint;
lambda: bigint;
_oGLiq: bigint;
_nGLiq: bigint;
_oBals: bigint[];
_nBals: bigint[];
givenToken: FxPoolToken;
swapKind: SwapKind;
};
4 changes: 2 additions & 2 deletions src/entities/pools/gyro2/gyro2Factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Gyro2Pool } from '.';
import { BasePool, BasePoolFactory } from '..';
import { Gyro2Pool } from './gyro2Pool';
import { BasePool, BasePoolFactory } from '../index';
import { RawGyro2Pool, RawPool } from '../../../data/types';

export class Gyro2PoolFactory implements BasePoolFactory {
Expand Down
3 changes: 2 additions & 1 deletion src/entities/pools/gyro2/gyro2Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
_findVirtualParams,
} from './gyro2Math';
import { BasePool } from '..';
import { BigintIsh, Token, TokenAmount } from '../..';
import { Token } from '../../token';
import { TokenAmount, BigintIsh } from '../../tokenAmount';
import { RawGyro2Pool } from '../../../data/types';
import { PoolType, SwapKind } from '../../../types';
import { getPoolAddress, MathSol, WAD } from '../../../utils';
Expand Down
4 changes: 2 additions & 2 deletions src/entities/pools/gyro3/gyro3Factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Gyro3Pool } from '.';
import { BasePool, BasePoolFactory } from '..';
import { Gyro3Pool } from './gyro3Pool';
import { BasePool, BasePoolFactory } from '../index';
import { RawGyro3Pool, RawPool } from '../../../data/types';

export class Gyro3PoolFactory implements BasePoolFactory {
Expand Down
3 changes: 2 additions & 1 deletion src/entities/pools/gyro3/gyro3Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
_calculateInvariant,
} from './gyro3Math';
import { BasePool } from '..';
import { BigintIsh, Token, TokenAmount } from '../..';
import { Token } from '../../token';
import { TokenAmount, BigintIsh } from '../../tokenAmount';
import { RawGyro3Pool } from '../../../data/types';
import { PoolType, SwapKind } from '../../../types';
import { getPoolAddress, MathSol, WAD } from '../../../utils';
Expand Down
4 changes: 2 additions & 2 deletions src/entities/pools/gyroE/gyroEFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GyroEPool } from '.';
import { BasePool, BasePoolFactory } from '..';
import { GyroEPool } from './gyroEPool';
import { BasePool, BasePoolFactory } from '../index';
import { RawGyroEPool, RawPool } from '../../../data/types';

export class GyroEPoolFactory implements BasePoolFactory {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/pools/gyroE/gyroEMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
normalizedLiquidityXIn,
normalizedLiquidityYIn,
} from './gyroEMathFunctions';
import { DerivedGyroEParams, GyroEParams, Vector2 } from './gyroEPool';
import { DerivedGyroEParams, GyroEParams, Vector2 } from './types';
import { MathGyro, ONE_XP, SMALL } from '../../../utils/gyroHelpers/math';

export function calculateNormalizedLiquidity(
Expand Down
2 changes: 1 addition & 1 deletion src/entities/pools/gyroE/gyroEMathFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WAD } from '../../../utils';
import { MathGyro } from '../../../utils/gyroHelpers/math';
import { virtualOffset0, virtualOffset1 } from './gyroEMathHelpers';
import { DerivedGyroEParams, GyroEParams, Vector2 } from './gyroEPool';
import { DerivedGyroEParams, GyroEParams, Vector2 } from './types';

/////////
/// SPOT PRICE DERIVATIVE CALCULATIONS
Expand Down
2 changes: 1 addition & 1 deletion src/entities/pools/gyroE/gyroEMathHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MAX_BALANCES } from './constants';
import { DerivedGyroEParams, GyroEParams, Vector2 } from './gyroEPool';
import { DerivedGyroEParams, GyroEParams, Vector2 } from './types';
import { MathGyro, ONE_XP } from '../../../utils/gyroHelpers/math';

/////////
Expand Down
Loading

0 comments on commit 3862fd5

Please sign in to comment.