Skip to content

Commit

Permalink
Merge pull request #327 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Release 4.0.1-beta.14
  • Loading branch information
John Grant authored Nov 30, 2022
2 parents 9df290b + f561d8a commit ea4585e
Show file tree
Hide file tree
Showing 46 changed files with 507 additions and 413 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@balancer-labs/sor",
"version": "4.0.1-beta.13",
"version": "4.0.1-beta.14",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
2 changes: 1 addition & 1 deletion src/frontendHelpers/phantomStableHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function BPTForTokensZeroPriceImpact(
fee: BigNumberish,
rates: BigNumberish[]
): BigNumber {
const amountsAfterFee = amounts.map((amountIn, i) => {
const amountsAfterFee = amounts.map((amountIn) => {
const amount = BigNumber.from(amountIn);
const feeAmount = amount.mul(fee).div(ONE);
return amount.sub(feeAmount);
Expand Down
12 changes: 11 additions & 1 deletion src/pools/elementPool/elementPool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber, formatFixed, parseFixed } from '@ethersproject/bignumber';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { WeiPerEther as ONE, Zero } from '@ethersproject/constants';
import { isSameAddress } from '../../utils';
import { BigNumber as OldBigNumber, bnum } from '../../utils/bignumber';
import {
Expand Down Expand Up @@ -219,6 +219,16 @@ export class ElementPool implements PoolBase {
return _tokenInForExactTokenOut(amount, poolPairData);
}

_calcTokensOutGivenExactBptIn(bptAmountIn: BigNumber): BigNumber[] {
// Missing maths for this
return new Array(this.tokens.length).fill(Zero);
}

_calcBptOutGivenExactTokensIn(amountsIn: BigNumber[]): BigNumber {
// Missing maths for this
return Zero;
}

_spotPriceAfterSwapExactTokenInForTokenOut(
poolPairData: ElementPoolPairData,
amount: OldBigNumber
Expand Down
12 changes: 11 additions & 1 deletion src/pools/gyro2Pool/gyro2Pool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAddress } from '@ethersproject/address';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { WeiPerEther as ONE, Zero } from '@ethersproject/constants';
import { formatFixed, BigNumber } from '@ethersproject/bignumber';
import { BigNumber as OldBigNumber, bnum } from '../../utils/bignumber';

Expand Down Expand Up @@ -284,6 +284,16 @@ export class Gyro2Pool implements PoolBase {
}
}

_calcTokensOutGivenExactBptIn(bptAmountIn: BigNumber): BigNumber[] {
// Missing maths for this
return new Array(this.tokens.length).fill(Zero);
}

_calcBptOutGivenExactTokensIn(amountsIn: BigNumber[]): BigNumber {
// Missing maths for this
return Zero;
}

_spotPriceAfterSwapExactTokenInForTokenOut(
poolPairData: Gyro2PoolPairData,
amount: OldBigNumber
Expand Down
1 change: 0 additions & 1 deletion src/pools/gyro3Pool/gyro3Math.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BigNumber } from '@ethersproject/bignumber';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import {
_SAFE_LARGE_POW3_THRESHOLD,
_INVARIANT_MIN_ITERATIONS,
_INVARIANT_SHRINKING_FACTOR_PER_STEP,
} from './constants';
Expand Down
12 changes: 11 additions & 1 deletion src/pools/gyro3Pool/gyro3Pool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAddress } from '@ethersproject/address';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { WeiPerEther as ONE, Zero } from '@ethersproject/constants';
import { formatFixed, BigNumber } from '@ethersproject/bignumber';
import { BigNumber as OldBigNumber, bnum } from '../../utils/bignumber';

Expand Down Expand Up @@ -315,6 +315,16 @@ export class Gyro3Pool implements PoolBase {
}
}

_calcTokensOutGivenExactBptIn(bptAmountIn: BigNumber): BigNumber[] {
// Missing maths for this
return new Array(this.tokens.length).fill(Zero);
}

_calcBptOutGivenExactTokensIn(amountsIn: BigNumber[]): BigNumber {
// Missing maths for this
return Zero;
}

_spotPriceAfterSwapExactTokenInForTokenOut(
poolPairData: Gyro3PoolPairData,
amount: OldBigNumber
Expand Down
2 changes: 1 addition & 1 deletion src/pools/gyro3Pool/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function _safeLargePow3ADown(
l: BigNumber,
root3Alpha: BigNumber,
d: BigNumber
) {
): BigNumber {
let ret = BigNumber.from(0);
if (l.lte(_SAFE_LARGE_POW3_THRESHOLD)) {
// Simple case where there is no overflow
Expand Down
6 changes: 3 additions & 3 deletions src/pools/gyroEPool/gyroEMath/gyroEMath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, formatFixed } from '@ethersproject/bignumber';
import { BigNumber } from '@ethersproject/bignumber';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { MAX_BALANCES, MAX_INVARIANT } from './constants';
import { ONE_XP, SMALL } from '../../gyroHelpers/constants';
Expand Down Expand Up @@ -41,7 +41,7 @@ export function calculateNormalizedLiquidity(
r: Vector2,
fee: BigNumber,
tokenInIsToken0: boolean
) {
): BigNumber {
if (tokenInIsToken0) {
return normalizedLiquidityXIn(balances, params, derived, fee, r);
} else {
Expand All @@ -53,7 +53,7 @@ export function calculateInvariantWithError(
balances: BigNumber[],
params: GyroEParams,
derived: DerivedGyroEParams
) {
): [BigNumber, BigNumber] {
const [x, y] = balances;

if (x.add(y).gt(MAX_BALANCES)) throw new Error('MAX ASSETS EXCEEDED');
Expand Down
16 changes: 8 additions & 8 deletions src/pools/gyroEPool/gyroEMath/gyroEMathFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function calcSpotPriceYGivenX(
params: GyroEParams,
d: DerivedGyroEParams,
r: Vector2
) {
): BigNumber {
const ab: Vector2 = {
x: virtualOffset0(params, d, r),
y: virtualOffset1(params, d, r),
Expand All @@ -55,7 +55,7 @@ export function calcSpotPriceXGivenY(
params: GyroEParams,
d: DerivedGyroEParams,
r: Vector2
) {
): BigNumber {
const ba: Vector2 = {
x: virtualOffset1(params, d, r),
y: virtualOffset0(params, d, r),
Expand Down Expand Up @@ -168,7 +168,7 @@ export function normalizedLiquidityYIn(
derived: DerivedGyroEParams,
fee: BigNumber,
rVec: Vector2
) {
): BigNumber {
const { y0, c, s, lambda, b, ls, R } = setup(
balances,
params,
Expand Down Expand Up @@ -215,7 +215,7 @@ export function normalizedLiquidityXIn(
derived: DerivedGyroEParams,
fee: BigNumber,
rVec: Vector2
) {
): BigNumber {
const { x0, c, s, lambda, a, ls, R } = setup(
balances,
params,
Expand Down Expand Up @@ -262,7 +262,7 @@ export function dPyDXIn(
derived: DerivedGyroEParams,
fee: BigNumber,
rVec: Vector2
) {
): BigNumber {
const { x0, c, s, lambda, a, ls, R } = setup(
balances,
params,
Expand Down Expand Up @@ -307,7 +307,7 @@ export function dPxDYIn(
derived: DerivedGyroEParams,
fee: BigNumber,
rVec: Vector2
) {
): BigNumber {
const { y0, c, s, lambda, b, ls, R } = setup(
balances,
params,
Expand Down Expand Up @@ -352,7 +352,7 @@ export function dPxDXOut(
derived: DerivedGyroEParams,
fee: BigNumber,
rVec: Vector2
) {
): BigNumber {
const { x0, s, lambda, a, ls, R, f } = setup(
balances,
params,
Expand Down Expand Up @@ -384,7 +384,7 @@ export function dPyDYOut(
derived: DerivedGyroEParams,
fee: BigNumber,
rVec: Vector2
) {
): BigNumber {
const { y0, c, lambda, b, ls, R, f } = setup(
balances,
params,
Expand Down
23 changes: 13 additions & 10 deletions src/pools/gyroEPool/gyroEMath/gyroEMathHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, formatFixed, parseFixed } from '@ethersproject/bignumber';
import { BigNumber, parseFixed } from '@ethersproject/bignumber';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { ONE_XP } from '../../gyroHelpers/constants';
import {
Expand Down Expand Up @@ -81,7 +81,7 @@ export function balancesFromTokenInOut(
balanceTokenIn: BigNumber,
balanceTokenOut: BigNumber,
tokenInIsToken0: boolean
) {
): [BigNumber, BigNumber] {
return tokenInIsToken0
? [balanceTokenIn, balanceTokenOut]
: [balanceTokenOut, balanceTokenIn];
Expand All @@ -96,7 +96,7 @@ export function calcAtAChi(
y: BigNumber,
p: GyroEParams,
d: DerivedGyroEParams
) {
): BigNumber {
const dSq2 = mulXpU(d.dSq, d.dSq);

// (cx - sy) * (w/lambda + z) / lambda
Expand Down Expand Up @@ -129,7 +129,7 @@ export function calcInvariantSqrt(
y: BigNumber,
p: GyroEParams,
d: DerivedGyroEParams
) {
): [BigNumber, BigNumber] {
let val = calcMinAtxAChiySqPlusAtxSq(x, y, p, d).add(
calc2AtxAtyAChixAChiy(x, y, p, d)
);
Expand All @@ -151,7 +151,7 @@ function calcMinAtxAChiySqPlusAtxSq(
termNp = termNp.sub(
mulDownMagU(mulDownMagU(mulDownMagU(x, y), p.c.mul(2)), p.s)
);
let termXp = mulXpU(d.u, d.u)
const termXp = mulXpU(d.u, d.u)
.add(divDownMagU(mulXpU(d.u.mul(2), d.v), p.lambda))
.add(divDownMagU(divDownMagU(mulXpU(d.v, d.v), p.lambda), p.lambda));

Expand Down Expand Up @@ -211,7 +211,10 @@ function calcMinAtyAChixSqPlusAtySq(
return val;
}

export function calcAChiAChiInXp(p: GyroEParams, d: DerivedGyroEParams) {
export function calcAChiAChiInXp(
p: GyroEParams,
d: DerivedGyroEParams
): BigNumber {
const dSq3 = mulXpU(mulXpU(d.dSq, d.dSq), d.dSq);
let val = mulUpMagU(p.lambda, divXpU(mulXpU(d.u.mul(2), d.v), dSq3));
val = val.add(
Expand All @@ -236,7 +239,7 @@ export function checkAssetBounds(
invariant: Vector2,
newBal: BigNumber,
assetIndex: number
) {
): void {
if (assetIndex === 0) {
const xPlus = maxBalances0(params, derived, invariant);
if (newBal.gt(MAX_BALANCES) || newBal.gt(xPlus))
Expand Down Expand Up @@ -283,7 +286,7 @@ export function calcYGivenX(
params: GyroEParams,
d: DerivedGyroEParams,
r: Vector2
) {
): BigNumber {
const ab: Vector2 = {
x: virtualOffset0(params, d, r),
y: virtualOffset1(params, d, r),
Expand All @@ -307,7 +310,7 @@ export function calcXGivenY(
params: GyroEParams,
d: DerivedGyroEParams,
r: Vector2
) {
): BigNumber {
const ba: Vector2 = {
x: virtualOffset1(params, d, r),
y: virtualOffset0(params, d, r),
Expand Down Expand Up @@ -492,7 +495,7 @@ export function mulA(params: GyroEParams, tp: Vector2): Vector2 {
};
}

export function scalarProd(t1: Vector2, t2: Vector2) {
export function scalarProd(t1: Vector2, t2: Vector2): BigNumber {
const ret = mulDownMagU(t1.x, t2.x).add(mulDownMagU(t1.y, t2.y));
return ret;
}
14 changes: 12 additions & 2 deletions src/pools/gyroEPool/gyroEPool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAddress } from '@ethersproject/address';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { WeiPerEther as ONE, Zero } from '@ethersproject/constants';
import { formatFixed, BigNumber } from '@ethersproject/bignumber';
import { BigNumber as OldBigNumber, bnum } from '../../utils/bignumber';

Expand Down Expand Up @@ -211,7 +211,7 @@ export class GyroEPool implements PoolBase {
return poolPairData;
}

getNormalizedLiquidity(poolPairData: GyroEPoolPairData) {
getNormalizedLiquidity(poolPairData: GyroEPoolPairData): OldBigNumber {
const normalizedBalances = normalizeBalances(
[poolPairData.balanceIn, poolPairData.balanceOut],
[poolPairData.decimalsIn, poolPairData.decimalsOut]
Expand Down Expand Up @@ -392,6 +392,16 @@ export class GyroEPool implements PoolBase {
return bnum(formatFixed(inAmount, 18));
}

_calcTokensOutGivenExactBptIn(bptAmountIn: BigNumber): BigNumber[] {
// Missing maths for this
return new Array(this.tokens.length).fill(Zero);
}

_calcBptOutGivenExactTokensIn(amountsIn: BigNumber[]): BigNumber {
// Missing maths for this
return Zero;
}

_spotPriceAfterSwapExactTokenInForTokenOut(
poolPairData: GyroEPoolPairData,
amount: OldBigNumber
Expand Down
20 changes: 12 additions & 8 deletions src/pools/gyroEPool/testingHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ export function calculateDerivedValues(p: GyroEParams): DerivedGyroEParams {
)
);

let tauAlpha: BigNumber[] = [];
const tauAlpha: BigNumber[] = [];
tauAlpha.push(mul(mul(alpha, c).sub(s), dAlpha));
tauAlpha.push(mul(c.add(mul(s, alpha)), div(dAlpha, lambda)));

let tauBeta: BigNumber[] = [];
const tauBeta: BigNumber[] = [];

tauBeta.push(mul(mul(beta, c).sub(s), dBeta));
tauBeta.push(mul(c.add(mul(s, beta)), div(dBeta, lambda)));

let w = mul(mul(s, c), tauBeta[1].sub(tauAlpha[1]));
let z = mul(mul(c, c), tauBeta[0]).add(mul(mul(s, s), tauAlpha[0]));
let u = mul(mul(s, c), tauBeta[0].sub(tauAlpha[0]));
let v = mul(mul(s, s), tauBeta[1]).add(mul(mul(c, c), tauAlpha[1]));
const w = mul(mul(s, c), tauBeta[1].sub(tauAlpha[1]));
const z = mul(mul(c, c), tauBeta[0]).add(mul(mul(s, s), tauAlpha[0]));
const u = mul(mul(s, c), tauBeta[0].sub(tauAlpha[0]));
const v = mul(mul(s, s), tauBeta[1]).add(mul(mul(c, c), tauAlpha[1]));

const derived = {
tauAlpha: {
Expand All @@ -80,14 +80,18 @@ function scale(bn: BigNumber, decimalsIn: number, decimalsOut: number) {
.div(parseFixed('1', decimalsIn));
}

export function sqrtArbitraryDecimal(input: BigNumber, decimals: number) {
export function sqrtArbitraryDecimal(
input: BigNumber,
decimals: number
): BigNumber {
if (input.isZero()) {
return BigNumber.from(0);
}
let guess = input.gt(parseFixed('1', decimals)) ? input.div(2) : input;

// 100 iterations
for (let i of new Array(100).fill(0)) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const i of new Array(100).fill(0)) {
guess = guess
.add(input.mul(parseFixed('1', decimals)).div(guess))
.div(2);
Expand Down
Loading

0 comments on commit ea4585e

Please sign in to comment.