Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.0.1-beta.10 #309

Merged
merged 8 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.9",
"version": "4.0.1-beta.10",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
8 changes: 4 additions & 4 deletions src/pools/gyro2Pool/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { BigNumber, parseFixed } from '@ethersproject/bignumber';
import { BigNumber } from '@ethersproject/bignumber';

// Swap limits: amounts swapped may not be larger than this percentage of total balance.

export const _MAX_IN_RATIO: BigNumber = parseFixed('0.3', 18);
export const _MAX_OUT_RATIO: BigNumber = parseFixed('0.3', 18);

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');
Expand All @@ -14,3 +11,6 @@ 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');

// Swap Limit factor
export const SWAP_LIMIT_FACTOR = BigNumber.from('999999000000000000');
33 changes: 29 additions & 4 deletions src/pools/gyro2Pool/gyro2Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
mulDown,
divDown,
} from './helpers';
import { SWAP_LIMIT_FACTOR } from './constants';

export type Gyro2PoolPairData = PoolPairBase & {
sqrtAlpha: BigNumber;
Expand Down Expand Up @@ -160,20 +161,44 @@ export class Gyro2Pool implements PoolBase {
}

getLimitAmountSwap(
poolPairData: PoolPairBase,
poolPairData: Gyro2PoolPairData,
swapType: SwapTypes
): OldBigNumber {
if (swapType === SwapTypes.SwapExactIn) {
const balances = [poolPairData.balanceIn, poolPairData.balanceOut];
const normalizedBalances = _normalizeBalances(
balances,
poolPairData.decimalsIn,
poolPairData.decimalsOut
);
const invariant = _calculateInvariant(
normalizedBalances,
poolPairData.sqrtAlpha,
poolPairData.sqrtBeta
);
const maxAmountInAssetInPool = mulDown(
invariant,
divDown(ONE, poolPairData.sqrtAlpha).sub(
divDown(ONE, poolPairData.sqrtBeta)
)
); // x+ = L * (1/sqrtAlpha - 1/sqrtBeta)
const limitAmountIn = maxAmountInAssetInPool.sub(
normalizedBalances[0]
);
const limitAmountInPlusSwapFee = divDown(
limitAmountIn,
ONE.sub(poolPairData.swapFee)
);
return bnum(
formatFixed(
mulDown(poolPairData.balanceIn, this.MAX_IN_RATIO),
poolPairData.decimalsIn
mulDown(limitAmountInPlusSwapFee, SWAP_LIMIT_FACTOR),
18
)
);
} else {
return bnum(
formatFixed(
mulDown(poolPairData.balanceOut, this.MAX_OUT_RATIO),
mulDown(poolPairData.balanceOut, SWAP_LIMIT_FACTOR),
poolPairData.decimalsOut
)
);
Expand Down
10 changes: 4 additions & 6 deletions src/pools/gyro3Pool/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { BigNumber, parseFixed } from '@ethersproject/bignumber';

// Swap limits: amounts swapped may not be larger than this percentage of total balance.

export const _MAX_IN_RATIO: BigNumber = parseFixed('0.3', 18);
export const _MAX_OUT_RATIO: BigNumber = parseFixed('0.3', 18);
import { BigNumber } from '@ethersproject/bignumber';

// SQRT constants

Expand All @@ -29,3 +24,6 @@ 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');
39 changes: 35 additions & 4 deletions src/pools/gyro3Pool/gyro3Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
mulDown,
divDown,
} from './helpers';
import { SWAP_LIMIT_FACTOR } from './constants';

export type Gyro3PoolPairData = PoolPairBase & {
balanceTertiary: BigNumber; // Balance of the unchanged asset
Expand Down Expand Up @@ -179,20 +180,50 @@ export class Gyro3Pool implements PoolBase {
}

getLimitAmountSwap(
poolPairData: PoolPairBase,
poolPairData: Gyro3PoolPairData,
swapType: SwapTypes
): OldBigNumber {
if (swapType === SwapTypes.SwapExactIn) {
const balances = [
poolPairData.balanceIn,
poolPairData.balanceOut,
poolPairData.balanceTertiary,
];
const decimals = [
poolPairData.decimalsIn,
poolPairData.decimalsOut,
poolPairData.decimalsTertiary,
];
const normalizedBalances = _normalizeBalances(balances, decimals);
const invariant = _calculateInvariant(
normalizedBalances,
this.root3Alpha
);
const a = mulDown(invariant, this.root3Alpha);
const maxAmountInAssetInPool = divDown(
mulDown(
normalizedBalances[0].add(a),
normalizedBalances[1].add(a)
),
a
).sub(a); // (x + a)(y + a) / a - a
const limitAmountIn = maxAmountInAssetInPool.sub(
normalizedBalances[0]
);
const limitAmountInPlusSwapFee = divDown(
limitAmountIn,
ONE.sub(poolPairData.swapFee)
);
return bnum(
formatFixed(
mulDown(poolPairData.balanceIn, this.MAX_IN_RATIO),
poolPairData.decimalsIn
mulDown(limitAmountInPlusSwapFee, SWAP_LIMIT_FACTOR),
18
)
);
} else {
return bnum(
formatFixed(
mulDown(poolPairData.balanceOut, this.MAX_OUT_RATIO),
mulDown(poolPairData.balanceOut, SWAP_LIMIT_FACTOR),
poolPairData.decimalsOut
)
);
Expand Down
2 changes: 0 additions & 2 deletions src/pools/gyro3Pool/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { BigNumber, parseFixed } from '@ethersproject/bignumber';
import bn from 'bignumber.js';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import {
_MAX_IN_RATIO,
_MAX_OUT_RATIO,
SQRT_1E_NEG_1,
SQRT_1E_NEG_3,
SQRT_1E_NEG_5,
Expand Down
4 changes: 3 additions & 1 deletion src/routeProposal/filtering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ export function getBoostedPaths(
poolsAllDict: PoolDictionary,
config: SorConfig
): NewPath[] {
let maxPaths = BOOSTED_PATHS_MAX_LENGTH;
if (config.chainId === 137) maxPaths = 3;
const edgesFromNode = getBoostedGraph(
tokenIn,
tokenOut,
Expand Down Expand Up @@ -365,7 +367,7 @@ export function getBoostedPaths(
if (newTreeEdges.length == 0) {
iterate = false;
} else treeEdges.push(newTreeEdges);
if (n == BOOSTED_PATHS_MAX_LENGTH) iterate = false;
if (n == maxPaths) iterate = false;
}
return pathsInfoToPaths(pathsInfo, poolsAllDict);
}
Expand Down
4 changes: 2 additions & 2 deletions test/gyro2Pool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ describe('Gyro2Pool tests USDC > DAI', () => {
SwapTypes.SwapExactIn
);

expect(amount.toString()).to.eq('300');
expect(amount.toString()).to.eq('1243.74395782517101711');

amount = pool.getLimitAmountSwap(
poolPairData,
SwapTypes.SwapExactOut
);

expect(amount.toString()).to.eq('369.6');
expect(amount.toString()).to.eq('1231.998768');
});
});

Expand Down
7 changes: 4 additions & 3 deletions test/gyro3Pool.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// TS_NODE_PROJECT='tsconfig.testing.json' npx mocha -r ts-node/register test/gyro3Pool.spec.ts
import { expect } from 'chai';
import cloneDeep from 'lodash.clonedeep';
import { formatFixed, parseFixed, BigNumber } from '@ethersproject/bignumber';
import { formatFixed, parseFixed } from '@ethersproject/bignumber';
import { bnum } from '../src/utils/bignumber';
import { USDC, USDT } from './lib/constants';
import { SwapTypes } from '../src';
Expand Down Expand Up @@ -38,14 +39,14 @@ describe('Gyro3Pool tests USDC > DAI', () => {
SwapTypes.SwapExactIn
);

expect(amount.toString()).to.eq('24935.7');
expect(amount.toString()).to.eq('82089.998821185751004412');

amount = pool.getLimitAmountSwap(
poolPairData,
SwapTypes.SwapExactOut
);

expect(amount.toString()).to.eq('24445.5');
expect(amount.toString()).to.eq('81484.918515');
});
});

Expand Down
8 changes: 4 additions & 4 deletions test/testScripts/swapExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ function setUp(networkId: Network, provider: JsonRpcProvider): SOR {
}

export async function swap(): Promise<void> {
const networkId = Network.MAINNET;
const networkId = Network.POLYGON;
const provider = new JsonRpcProvider(PROVIDER_URLS[networkId]);
// gasPrice is used by SOR as a factor to determine how many pools to swap against.
// i.e. higher cost means more costly to trade against lots of different pools.
const gasPrice = BigNumber.from('40000000000');
// This determines the max no of pools the SOR will use to swap.
const maxPools = 4;
const tokenIn = ADDRESSES[networkId].WETH;
const tokenOut = ADDRESSES[networkId].DAI;
const tokenIn = ADDRESSES[networkId].USDT;
const tokenOut = ADDRESSES[networkId].USDC;
const swapType: SwapTypes = SwapTypes.SwapExactIn;
const swapAmount = parseFixed('1', 18);
const swapAmount = parseFixed('1000', 6);

const sor = setUp(networkId, provider);

Expand Down