Skip to content

Commit

Permalink
Merge pull request #318 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Release 4.0.1-beta.12
  • Loading branch information
John Grant committed Nov 17, 2022
2 parents 06f40a4 + 7eeebb2 commit 969e411
Show file tree
Hide file tree
Showing 35 changed files with 2,723 additions and 422 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -57,3 +57,9 @@ Example:
PRICE_ERROR_TOLERANCE=0.00001
INFINITESIMAL=0.000001
```

## Note on Licensing

Except where indicated otherwise, the code in this repository is licensed GPLv3.

Superluminal Labs Ltd. is the owner of the directories `balancer-sor/src/pools/gyro2Pool/`, `balancer-sor/src/pools/gyro3Pool/` and `balancer-sor/src/pools/gyroEPool/` and any accompanying files contained herein (collectively, these “Software”). Use of these Software is exclusively subject to the [Gyroscope Pool License](./src/pools/gyroEPool/LICENSE), which is available at the provided link (the “Gyroscope Pool License”). These Software are not covered by the General Public License and do not confer any rights to the user other than the limited rights specified in the Gyroscope Pool License. A special hybrid license between Superluminal Labs Ltd and Balancer Labs OÜ governs Superluminal Labs Ltd's use of the Balancer Labs OÜ code [Special License](./src/pools/gyroEPool/GyroscopeBalancerLicense.pdf), which is available at the provided link. By using these Software, you agree to be bound by the terms and conditions of the Gyroscope Pool License. If you do not agree to all terms and conditions of the Gyroscope Pool License, do not use any of these Software.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@balancer-labs/sor",
"version": "4.0.1-beta.11",
"version": "4.0.1-beta.12",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
5 changes: 5 additions & 0 deletions src/pools/gyro2Pool/LICENSE
@@ -0,0 +1,5 @@
(c) 2022 Superluminal Labs Ltd. All rights reserved.

All moral, intellectual property, and other rights (including rights to all inventions, codes, designs, and protocols) associated with the software code published by Superluminal Labs Ltd. and residing in this repository (this “Software”) are reserved by its right holder(s) except as otherwise provided in this Gyroscope Pool User Contract. This Software has been published for informational purposes and may be run only with the Balancer automated portfolio manager and trading platform solely for testing and internal evaluation purposes only; no license under patents, copyright, trademark, or any other intellectual property right (other than the limited license to run this Software for testing and internal evaluation) is granted or implied. A special hybrid license between Superluminal Labs Ltd and Balancer Labs OÜ governs Superluminal Labs Ltd.'s use of the Balancer Labs OÜ code [Special License](../gyroEPool/GyroscopeBalancerLicense.pdf), which is available at the provided link.

THE SOFTWARE AND INTELLECTUAL PROPERTY INCLUDED IN THIS SOFTWARE IS PROVIDED BY THE RIGHT HOLDER(S) "AS IS," AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE RIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE OR INTELLECTUAL PROPERTY (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION), HOWEVER CAUSED OR CLAIMED (WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)), EVEN IF SUCH DAMAGES WERE REASONABLY FORESEEABLE OR THE RIGHT HOLDER(S) WERE ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10 changes: 8 additions & 2 deletions src/pools/gyro2Pool/gyro2Math.ts
@@ -1,6 +1,12 @@
import { BigNumber } from '@ethersproject/bignumber';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { _sqrt, mulUp, divUp, mulDown, divDown } from './helpers';
import {
sqrt,
mulUp,
divUp,
mulDown,
divDown,
} from '../gyroHelpers/gyroSignedFixedPoint';

/////////
/// Virtual Parameter calculations
Expand Down Expand Up @@ -91,7 +97,7 @@ export function _calculateQuadratic(
const addTerm = mulDown(mulDown(mc, ONE.mul(4)), a);
// The minus sign in the radicand cancels out in this special case, so we add
const radicand = bSquare.add(addTerm);
const sqrResult = _sqrt(radicand, BigNumber.from(5));
const sqrResult = sqrt(radicand, BigNumber.from(5));
// The minus sign in the numerator cancels out in this special case
const numerator = mb.add(sqrResult);
const invariant = divDown(numerator, denominator);
Expand Down
95 changes: 41 additions & 54 deletions src/pools/gyro2Pool/gyro2Pool.ts
@@ -1,6 +1,6 @@
import { getAddress } from '@ethersproject/address';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { parseFixed, formatFixed, BigNumber } from '@ethersproject/bignumber';
import { formatFixed, BigNumber } from '@ethersproject/bignumber';
import { BigNumber as OldBigNumber, bnum } from '../../utils/bignumber';

import {
Expand All @@ -11,7 +11,7 @@ import {
SwapTypes,
SubgraphPoolBase,
} from '../../types';
import { isSameAddress } from '../../utils';
import { isSameAddress, safeParseFixed } from '../../utils';
import {
_calculateInvariant,
_calcOutGivenIn,
Expand All @@ -26,10 +26,9 @@ import {
_normalizeBalances,
_reduceFee,
_addFee,
mulDown,
divDown,
} from './helpers';
import { SWAP_LIMIT_FACTOR } from './constants';
} from '../gyroHelpers/helpers';
import { mulDown, divDown } from '../gyroHelpers/gyroSignedFixedPoint';
import { SWAP_LIMIT_FACTOR } from '../gyroHelpers/constants';

export type Gyro2PoolPairData = PoolPairBase & {
sqrtAlpha: BigNumber;
Expand All @@ -52,10 +51,6 @@ export class Gyro2Pool implements PoolBase {
sqrtAlpha: BigNumber;
sqrtBeta: BigNumber;

// Max In/Out Ratios
MAX_IN_RATIO = parseFixed('0.3', 18);
MAX_OUT_RATIO = parseFixed('0.3', 18);

static fromPool(pool: SubgraphPoolBase): Gyro2Pool {
if (!pool.sqrtAlpha || !pool.sqrtBeta)
throw new Error(
Expand Down Expand Up @@ -86,12 +81,12 @@ export class Gyro2Pool implements PoolBase {
) {
this.id = id;
this.address = address;
this.swapFee = parseFixed(swapFee, 18);
this.totalShares = parseFixed(totalShares, 18);
this.swapFee = safeParseFixed(swapFee, 18);
this.totalShares = safeParseFixed(totalShares, 18);
this.tokens = tokens;
this.tokensList = tokensList;
this.sqrtAlpha = parseFixed(sqrtAlpha, 18);
this.sqrtBeta = parseFixed(sqrtBeta, 18);
this.sqrtAlpha = safeParseFixed(sqrtAlpha, 18);
this.sqrtBeta = safeParseFixed(sqrtBeta, 18);
}

parsePoolPairData(tokenIn: string, tokenOut: string): Gyro2PoolPairData {
Expand Down Expand Up @@ -121,8 +116,8 @@ export class Gyro2Pool implements PoolBase {
tokenOut: tokenOut,
decimalsIn: Number(decimalsIn),
decimalsOut: Number(decimalsOut),
balanceIn: parseFixed(balanceIn, decimalsIn),
balanceOut: parseFixed(balanceOut, decimalsOut),
balanceIn: safeParseFixed(balanceIn, decimalsIn),
balanceOut: safeParseFixed(balanceOut, decimalsOut),
swapFee: this.swapFee,
sqrtAlpha: tokenInIsToken0
? this.sqrtAlpha
Expand All @@ -137,11 +132,10 @@ export class Gyro2Pool implements PoolBase {

getNormalizedLiquidity(poolPairData: Gyro2PoolPairData): OldBigNumber {
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
const normalizedBalances = _normalizeBalances(balances, [
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
poolPairData.decimalsOut,
]);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
Expand All @@ -166,11 +160,10 @@ export class Gyro2Pool implements PoolBase {
): OldBigNumber {
if (swapType === SwapTypes.SwapExactIn) {
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
const normalizedBalances = _normalizeBalances(balances, [
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
poolPairData.decimalsOut,
]);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
Expand Down Expand Up @@ -224,11 +217,10 @@ export class Gyro2Pool implements PoolBase {
): OldBigNumber {
try {
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
const normalizedBalances = _normalizeBalances(balances, [
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
poolPairData.decimalsOut,
]);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
Expand All @@ -239,7 +231,7 @@ export class Gyro2Pool implements PoolBase {
poolPairData.sqrtAlpha,
poolPairData.sqrtBeta
);
const inAmount = parseFixed(amount.toString(), 18);
const inAmount = safeParseFixed(amount.toString(), 18);
const inAmountLessFee = _reduceFee(inAmount, poolPairData.swapFee);

const outAmount = _calcOutGivenIn(
Expand All @@ -261,13 +253,12 @@ export class Gyro2Pool implements PoolBase {
amount: OldBigNumber
): OldBigNumber {
try {
const outAmount = parseFixed(amount.toString(), 18);
const outAmount = safeParseFixed(amount.toString(), 18);
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
const normalizedBalances = _normalizeBalances(balances, [
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
poolPairData.decimalsOut,
]);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
Expand Down Expand Up @@ -299,11 +290,10 @@ export class Gyro2Pool implements PoolBase {
): OldBigNumber {
try {
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
const normalizedBalances = _normalizeBalances(balances, [
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
poolPairData.decimalsOut,
]);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
Expand All @@ -314,7 +304,7 @@ export class Gyro2Pool implements PoolBase {
poolPairData.sqrtAlpha,
poolPairData.sqrtBeta
);
const inAmount = parseFixed(amount.toString(), 18);
const inAmount = safeParseFixed(amount.toString(), 18);
const inAmountLessFee = _reduceFee(inAmount, poolPairData.swapFee);
const outAmount = _calcOutGivenIn(
normalizedBalances[0],
Expand Down Expand Up @@ -342,13 +332,12 @@ export class Gyro2Pool implements PoolBase {
amount: OldBigNumber
): OldBigNumber {
try {
const outAmount = parseFixed(amount.toString(), 18);
const outAmount = safeParseFixed(amount.toString(), 18);
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
const normalizedBalances = _normalizeBalances(balances, [
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
poolPairData.decimalsOut,
]);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
Expand Down Expand Up @@ -388,11 +377,10 @@ export class Gyro2Pool implements PoolBase {
): OldBigNumber {
try {
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
const normalizedBalances = _normalizeBalances(balances, [
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
poolPairData.decimalsOut,
]);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
Expand All @@ -403,7 +391,7 @@ export class Gyro2Pool implements PoolBase {
poolPairData.sqrtAlpha,
poolPairData.sqrtBeta
);
const inAmount = parseFixed(amount.toString(), 18);
const inAmount = safeParseFixed(amount.toString(), 18);
const inAmountLessFee = _reduceFee(inAmount, poolPairData.swapFee);
const outAmount = _calcOutGivenIn(
normalizedBalances[0],
Expand All @@ -430,13 +418,12 @@ export class Gyro2Pool implements PoolBase {
amount: OldBigNumber
): OldBigNumber {
try {
const outAmount = parseFixed(amount.toString(), 18);
const outAmount = safeParseFixed(amount.toString(), 18);
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
const normalizedBalances = _normalizeBalances(balances, [
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
poolPairData.decimalsOut,
]);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
Expand Down
5 changes: 5 additions & 0 deletions src/pools/gyro3Pool/LICENSE
@@ -0,0 +1,5 @@
(c) 2022 Superluminal Labs Ltd. All rights reserved.

All moral, intellectual property, and other rights (including rights to all inventions, codes, designs, and protocols) associated with the software code published by Superluminal Labs Ltd. and residing in this repository (this “Software”) are reserved by its right holder(s) except as otherwise provided in this Gyroscope Pool User Contract. This Software has been published for informational purposes and may be run only with the Balancer automated portfolio manager and trading platform solely for testing and internal evaluation purposes only; no license under patents, copyright, trademark, or any other intellectual property right (other than the limited license to run this Software for testing and internal evaluation) is granted or implied. A special hybrid license between Superluminal Labs Ltd and Balancer Labs OÜ governs Superluminal Labs Ltd.'s use of the Balancer Labs OÜ code [Special License](../gyroEPool/GyroscopeBalancerLicense.pdf), which is available at the provided link.

THE SOFTWARE AND INTELLECTUAL PROPERTY INCLUDED IN THIS SOFTWARE IS PROVIDED BY THE RIGHT HOLDER(S) "AS IS," AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE RIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE OR INTELLECTUAL PROPERTY (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION), HOWEVER CAUSED OR CLAIMED (WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)), EVEN IF SUCH DAMAGES WERE REASONABLY FORESEEABLE OR THE RIGHT HOLDER(S) WERE ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 changes: 0 additions & 15 deletions src/pools/gyro3Pool/constants.ts
@@ -1,17 +1,5 @@
import { BigNumber } from '@ethersproject/bignumber';

// SQRT constants

export const SQRT_1E_NEG_1 = BigNumber.from('316227766016837933');
export const SQRT_1E_NEG_3 = BigNumber.from('31622776601683793');
export const SQRT_1E_NEG_5 = BigNumber.from('3162277660168379');
export const SQRT_1E_NEG_7 = BigNumber.from('316227766016837');
export const SQRT_1E_NEG_9 = BigNumber.from('31622776601683');
export const SQRT_1E_NEG_11 = BigNumber.from('3162277660168');
export const SQRT_1E_NEG_13 = BigNumber.from('316227766016');
export const SQRT_1E_NEG_15 = BigNumber.from('31622776601');
export const SQRT_1E_NEG_17 = BigNumber.from('3162277660');

// POW3 constant
// Threshold of x where the normal method of computing x^3 would overflow and we need a workaround.
// Equal to 4.87e13 scaled; 4.87e13 is the point x where x**3 * 10**36 = (x**2 native) * (x native) ~ 2**256
Expand All @@ -24,6 +12,3 @@ export const MIDDECIMAL = BigNumber.from(10).pow(9); // splits the fixed point d
// less-than-ideal starting point, which is important when alpha is small.
export const _INVARIANT_SHRINKING_FACTOR_PER_STEP = 8;
export const _INVARIANT_MIN_ITERATIONS = 5;

// Swap Limit factor
export const SWAP_LIMIT_FACTOR = BigNumber.from('999999000000000000');
8 changes: 4 additions & 4 deletions src/pools/gyro3Pool/gyro3Math.ts
Expand Up @@ -5,14 +5,14 @@ import {
_INVARIANT_MIN_ITERATIONS,
_INVARIANT_SHRINKING_FACTOR_PER_STEP,
} from './constants';
import { _safeLargePow3ADown } from './helpers';
import {
mulUp,
divUp,
mulDown,
divDown,
newtonSqrt,
_safeLargePow3ADown,
} from './helpers';
sqrt,
} from '../gyroHelpers/gyroSignedFixedPoint';

/////////
/// Invariant Calculation
Expand Down Expand Up @@ -85,7 +85,7 @@ export function _calculateCubicStartingPoint(
): BigNumber {
const radic = mulUp(mb, mb).add(mulUp(mulUp(a, mc), ONE.mul(3)));
const lmin = divUp(mb, a.mul(3)).add(
divUp(newtonSqrt(radic, BigNumber.from(5)), a.mul(3))
divUp(sqrt(radic, BigNumber.from(5)), a.mul(3))
);
// This formula has been found experimentally. It is exact for alpha -> 1, where the factor is 1.5. All
// factors > 1 are safe. For small alpha values, it is more efficient to fallback to a larger factor.
Expand Down

0 comments on commit 969e411

Please sign in to comment.