Skip to content

Commit

Permalink
Merge pull request #270 from balancer-labs/develop
Browse files Browse the repository at this point in the history
4.0.1-beta.0
  • Loading branch information
John Grant authored Jun 23, 2022
2 parents 5ae7506 + 727d0a3 commit 4a159c3
Show file tree
Hide file tree
Showing 15 changed files with 17,639 additions and 1,213 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.0-beta.9",
"version": "4.0.1-beta.0",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
608 changes: 235 additions & 373 deletions src/routeProposal/filtering.ts

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/routeProposal/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
getPathsUsingStaBalPool,
parseToPoolsDict,
getBoostedPaths,
filterPoolsOfInterest,
producePaths,
getBoostedPaths,
} from './filtering';
import { calculatePathLimits } from './pathLimits';
import {
Expand All @@ -30,6 +30,8 @@ export class RouteProposer {
pools: SubgraphPoolBase[],
swapOptions: SwapOptions
): NewPath[] {
tokenIn = tokenIn.toLowerCase();
tokenOut = tokenOut.toLowerCase();
if (pools.length === 0) return [];

// If token pair has been processed before that info can be reused to speed up execution
Expand Down Expand Up @@ -86,7 +88,6 @@ export class RouteProposer {
{
paths: paths,
};

return paths;
}

Expand All @@ -106,6 +107,8 @@ export class RouteProposer {
poolsAllDict: PoolDictionary,
maxPools: number
): NewPath[] {
tokenIn = tokenIn.toLowerCase();
tokenOut = tokenOut.toLowerCase();
if (Object.keys(poolsAllDict).length === 0) return [];

const [directPools, hopsIn, hopsOut] = filterPoolsOfInterest(
Expand Down
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ export interface SorConfig {
chainId: number;
vault: string;
weth: string;
bbausd?: { id: string; address: string };
wethBBausd?: { id: string; address: string };
staBal3Pool?: { id: string; address: string };
wethStaBal3?: { id: string; address: string };
usdcConnectingPool?: { id: string; usdc: string };
wETHwstETH?: { id: string; address: string };
lbpRaisingTokens?: string[];
}

Expand Down
139 changes: 102 additions & 37 deletions test/boostedPaths.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TS_NODE_PROJECT='tsconfig.testing.json' npx mocha -r ts-node/register test/linear.spec.ts
import { assert, expect } from 'chai';
// TS_NODE_PROJECT='tsconfig.testing.json' npx mocha -r ts-node/register test/boostedPaths.spec.ts
import { assert } from 'chai';
import cloneDeep from 'lodash.clonedeep';
import {
PoolDictionary,
Expand All @@ -15,33 +15,23 @@ import {
getBoostedPaths,
} from '../src/routeProposal/filtering';
import { bnum } from '../src/utils/bignumber';
import { calculatePathLimits } from '../src/routeProposal/pathLimits';
import { getFullSwap, simpleCheckPath } from './lib/testHelpers';
import {
DAI,
aDAI,
bbaDAI,
USDC,
BAL,
TestToken,
MKR,
GUSD,
WETH,
TUSD,
USDT,
LINEAR_AUSDT,
LINEAR_ADAI,
aUSDT,
KOVAN_BAL,
AAVE_USDT,
sorConfigTestBoosted,
sorConfigTest,
bbaUSD,
FEI,
} from './lib/constants';

// Multiple boosted pools
import boostedPools from './testData/boostedPools/multipleBoosted.json';
import genericBoostedPools from './testData/boostedPools/genericBoosted.json';
import boostedPoolsWithWstETH from './testData/boostedPools/boostedPoolsWithWstETH.json';
import { BigNumber, parseFixed } from '@ethersproject/bignumber';
import { getOutputAmountSwapForPath } from '../src/router/helpersClass';
import { JsonRpcProvider } from '@ethersproject/providers';
Expand All @@ -59,7 +49,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
const tokensChains = [
[TUSD.address, WETH.address, BAL.address],
Expand Down Expand Up @@ -90,7 +80,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
assert.equal(paths.length, 4);
assert.isTrue(checkNoDuplicate(paths));
Expand All @@ -106,7 +96,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
assert.equal(paths.length, 4);
assert.isTrue(checkNoDuplicate(paths));
Expand All @@ -120,7 +110,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
assert.equal(paths.length, 4);
const bbfDaiAddress = '0x0000000000000000000000000000000000000000';
Expand Down Expand Up @@ -179,19 +169,21 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
assert.equal(paths.length, 3);
assert.equal(boostedPaths.length, 0);
});
});
context('bbausd and Weth to Dai', () => {
it('four combinations', () => {
const binaryOption = [true, false];
if (!sorConfigTestBoosted.bbausd) return;
const binaryOption = [false, true];
for (const reverse of binaryOption) {
const tokens = [
[WETH.address, sorConfigTestBoosted.bbausd.address],
[
WETH.address,
'0x8fd162f338b770f7e879030830cde9173367f301',
],
[DAI.address],
];
if (reverse) tokens.reverse();
Expand All @@ -203,7 +195,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
assert.equal(paths.length, 2);
assert.isTrue(checkNoDuplicate(paths));
Expand Down Expand Up @@ -231,7 +223,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
91.23098,
boostedPools.pools,
sorConfigTestBoosted
sorConfigTest
),
'SOR path is not the best one'
);
Expand All @@ -242,7 +234,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
910000.23098,
boostedPools.pools,
sorConfigTestBoosted
sorConfigTest
),
'SOR path is not the best one'
);
Expand All @@ -259,15 +251,26 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
console.log('paths ids');
console.log(paths[0].id);
console.log(paths[1].id);
assert.equal(
paths[0].id,
boostedPaths[0].id,
'LBPweightedTusdOhmBBaUSD-TUSDbbaUSD-BAL'
);
assert.equal(
boostedPaths[1].id,
'LBPweightedTusdOhmweightedTusdWethweightedBalWeth'
);
assert.equal(
paths[3].id,
'LBPweightedTusdOhmBBaUSD-TUSDbbaUSD-BAL'
boostedPaths[2].id,
'LBPweightedTusdOhmBBaUSD-TUSDweightedWeth-BBausdweightedBalWeth'
);
assert.equal(
boostedPaths[3].id,
'LBPweightedTusdOhmweightedTusdWethweightedWeth-BBausdbbaUSD-BAL'
);
const OHM = tokenIn;
const tokensChains = [
Expand Down Expand Up @@ -313,7 +316,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
const tokensChains = [
[
Expand Down Expand Up @@ -361,7 +364,7 @@ describe('multiple boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
boostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
for (const path of paths) {
console.log(path.id);
Expand All @@ -378,7 +381,7 @@ describe('multiple boosted pools, path creation test', () => {
assert.equal(paths.length, 2);
});
it('Test correctness in absence of LBP raising info at config', () => {
const sorConfigNoLbpRaising = cloneDeep(sorConfigTestBoosted);
const sorConfigNoLbpRaising = cloneDeep(sorConfigTest);
delete sorConfigNoLbpRaising['lbpRaisingTokens'];
const sorConfigNoRaisingTusd = cloneDeep(sorConfigNoLbpRaising);
sorConfigNoRaisingTusd['lbpRaisingTokens'] = [
Expand All @@ -388,7 +391,7 @@ describe('multiple boosted pools, path creation test', () => {
const tokenOut = BAL.address;
const pathsCases: NewPath[][] = [];
const sorConfigCases: SorConfig[] = [
sorConfigTestBoosted,
sorConfigTest,
sorConfigNoLbpRaising,
sorConfigNoRaisingTusd,
];
Expand Down Expand Up @@ -424,7 +427,7 @@ describe('generic boosted pools, path creation test', () => {
SwapTypes.SwapExactIn,
genericBoostedPools.pools,
maxPools,
sorConfigTestBoosted
sorConfigTest
);
const bbfFEIAddress = '0x0000000000000000000000000000000000111111';
const bbfDAIAddress = '0x0000000000000000000000000000000000000000';
Expand All @@ -436,11 +439,72 @@ describe('generic boosted pools, path creation test', () => {
];
for (let i = 0; i < 1; i++) {
assert.isTrue(
simpleCheckPath(paths[i], poolsChains[i], tokensChains[i])
simpleCheckPath(
boostedPaths[i],
poolsChains[i],
tokensChains[i]
)
);
}
assert.equal(boostedPaths.length, 1);
assert.equal(paths.length, 1);
assert.equal(boostedPaths.length, 4);
assert.equal(paths.length, 4);
});
});
});

describe('generic boosted pools with wstETH, path creation test', () => {
context('...', () => {
it('FEI-bbaUSD', () => {
const tokenIn = FEI.address;
const tokenOut = bbaUSD.address;
const [paths, , boostedPaths] = getPaths(
tokenIn,
tokenOut,
SwapTypes.SwapExactIn,
boostedPoolsWithWstETH.pools,
maxPools,
sorConfigTest
);
const pathIds = [
'FuseLinearFeibbfUSD-PoolweightedWstETH-BBfusdweightedWstETH-BBausd',
'FuseLinearFeibbfUSD-PoolbbaUSD-bbfUSD',
'FuseLinearFeibbfUSD-PoolbbaUSD-bbfDAI',
'FuseLinearFeibbfUSD-PoolFuseLinearDaiAaveLinearDaibbaUSD-Pool',
];
for (let i = 0; i < 4; i++) {
assert.equal(paths[i].id, pathIds[i], 'unexpected path');
}
assert.equal(boostedPaths.length, 4);
assert.equal(paths.length, 4);
});
it('FEI-BAL', () => {
const tokenIn = FEI.address;
const tokenOut = BAL.address;
const [paths, , boostedPaths] = getPaths(
tokenIn,
tokenOut,
SwapTypes.SwapExactIn,
boostedPoolsWithWstETH.pools,
maxPools,
sorConfigTest
);
const pathsIds = [
'FuseLinearFeibbfUSD-PoolweightedWstETH-BBfusdwETH-wstETHweightedBalWeth',
'FuseLinearFeibbfUSD-PoolweightedWstETH-BBfusdweightedWstETH-BBausdbbaUSD-BAL',
'FuseLinearFeibbfUSD-PoolbbaUSD-bbfUSDweightedWstETH-BBausdwETH-wstETHweightedBalWeth',
'FuseLinearFeibbfUSD-PoolbbaUSD-bbfUSDbbaUSD-BAL',
'FuseLinearFeibbfUSD-PoolbbaUSD-bbfDAIbbaUSD-bbfUSDweightedWstETH-BBfusdwETH-wstETHweightedBalWeth',
'FuseLinearFeibbfUSD-PoolbbaUSD-bbfDAIweightedWstETH-BBausdwETH-wstETHweightedBalWeth',
'FuseLinearFeibbfUSD-PoolbbaUSD-bbfDAIbbaUSD-BAL',
'FuseLinearFeibbfUSD-PoolFuseLinearDaiAaveLinearDaibbaUSD-PoolweightedWstETH-BBausdwETH-wstETHweightedBalWeth',
'FuseLinearFeibbfUSD-PoolFuseLinearDaiAaveLinearDaibbaUSD-PoolbbaUSD-bbfUSDweightedWstETH-BBfusdwETH-wstETHweightedBalWeth',
'FuseLinearFeibbfUSD-PoolFuseLinearDaiAaveLinearDaibbaUSD-PoolbbaUSD-BAL',
];
for (let i = 0; i < 10; i++) {
assert.equal(paths[i].id, pathsIds[i], 'unexpected path');
}
assert.equal(boostedPaths.length, 10);
assert.equal(paths.length, 10);
});
});
});
Expand Down Expand Up @@ -482,6 +546,7 @@ function getPaths(
swapOptions
);

// const boostedPaths = getBoostedPaths(tokenIn, tokenOut, poolsAll, config);
const boostedPaths = getBoostedPaths(tokenIn, tokenOut, poolsAll, config);
return [paths, poolsAll, boostedPaths];
}
Expand Down
Loading

0 comments on commit 4a159c3

Please sign in to comment.