Skip to content

Commit

Permalink
Merge pull request #351 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Release 4.1.0-beta.0
  • Loading branch information
John Grant authored Jan 25, 2023
2 parents 5a79b0e + c6b4050 commit 5f6f9df
Show file tree
Hide file tree
Showing 15 changed files with 387 additions and 416 deletions.
34 changes: 0 additions & 34 deletions .circleci/config.yml

This file was deleted.

70 changes: 70 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Checks

on:
- pull_request

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install node
uses: actions/setup-node@v3
with:
node-version: 14.16.1
- name: Cache
uses: actions/cache@v2
id: cache
with:
path: '**/node_modules'
key: yarn-v1-${{ hashFiles('**/yarn.lock') }}
- name: Install
run: yarn --immutable
if: steps.cache.outputs.cache-hit != 'true'
- name: Lint
run: yarn lint

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install node
uses: actions/setup-node@v3
with:
node-version: 14.16.1
- name: Cache
uses: actions/cache@v2
id: cache
with:
path: '**/node_modules'
key: yarn-v1-${{ hashFiles('**/yarn.lock') }}
- name: Install
run: yarn --immutable
if: steps.cache.outputs.cache-hit != 'true'
- name: Build
run: yarn build

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install node
uses: actions/setup-node@v3
with:
node-version: 14.16.1
- name: Cache
uses: actions/cache@v2
id: cache
with:
path: '**/node_modules'
key: yarn-v1-${{ hashFiles('**/yarn.lock') }}
- name: Install
run: yarn --immutable
if: steps.cache.outputs.cache-hit != 'true'
- name: Compile
run: yarn build
- name: Test
run: yarn test
env:
CI: true
INFURA: ${{ secrets.INFURA }}
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<p align="center">
<a href="https://circleci.com/gh/balancer-labs/balancer-sor">
<img src="https://circleci.com/gh/balancer-labs/balancer-sor.svg?style=svg&circle-token=33636208d3161f79ff283b29c8dba9841bda8931" />
</a>
<a href="https://coveralls.io/github/balancer-labs/balancer-sor">
<img src="https://coveralls.io/repos/github/balancer-labs/balancer-sor/badge.svg?t=7avwwt" />
</a>
<a href="https://www.gnu.org/licenses/gpl-3.0">
<img src="https://img.shields.io/badge/License-GPLv3-green.svg" />
</a>
Expand Down
4 changes: 1 addition & 3 deletions 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.18",
"version": "4.1.0-beta.0",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand All @@ -9,7 +9,6 @@
"build": "rollup -c",
"prepack": "yarn build",
"test": "TS_NODE_PROJECT='tsconfig.testing.json' nyc mocha -r ts-node/register test/*.spec.ts --timeout 20000",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint ./src ./test --ext .ts --max-warnings 0",
"node": "npx hardhat node --tsconfig tsconfig.testing.json --fork $(grep ALCHEMY_URL .env | cut -d '=' -f2) --fork-block-number 14828550"
},
Expand Down Expand Up @@ -48,7 +47,6 @@
"@typescript-eslint/parser": "^4.29.2",
"bignumber.js": "^9.0.1",
"chai": "^4.2.0",
"coveralls": "^3.0.9",
"dotenv": "^8.2.0",
"eslint": "^7.32.0",
"eslint-plugin-mocha-no-only": "^1.1.1",
Expand Down
179 changes: 104 additions & 75 deletions src/routeProposal/filtering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ import { Zero } from '@ethersproject/constants';

const BOOSTED_PATHS_MAX_LENGTH = 7;

interface edgeDict {
[node: string]: [string, string, string][];
}

interface treeEdge {
edge: [string, string, string];
parentIndices: [number, number];
visitedNodes: string[];
}

export const filterPoolsByType = (
pools: SubgraphPoolBase[],
poolTypeFilter: PoolFilter
Expand Down Expand Up @@ -166,17 +176,20 @@ export function producePaths(
// We build this tree by adding at each step all the possible continuations for
// each branch. When a branch reaches tokenOut, we write down the corresponding path.
// We only allow paths up to length BOOSTED_PATHS_MAX_LENGTH = 7

export function getBoostedGraph(
tokenIn: string,
tokenOut: string,
poolsAllDict: PoolDictionary,
config: SorConfig
): edgeDict {
const wethAddress: string = config.weth.toLowerCase();
const graphPoolsSet: Set<PoolBase> = new Set();
const linearPools: PoolBase[] = [];
const phantomPools: PoolBase[] = [];
const connectingTokens = config.connectingTokens
? config.connectingTokens.map(
(connectingToken) => connectingToken.address
)
: [];
// Here we add all linear pools, take note of phantom pools,
// add LBP pools with tokenIn or tokenOut and their corresponding
// highest liquidity WETH connections
Expand All @@ -194,73 +207,34 @@ export function getBoostedGraph(
if (tokensList.includes(pool.address)) {
phantomPools.push(pool);
}
if (config.lbpRaisingTokens) {
const raisingTokens = config.lbpRaisingTokens.map((address) =>
address.toLowerCase()
if (config.lbpRaisingTokens && pool.isLBP) {
handleLBPCase(
graphPoolsSet,
config.lbpRaisingTokens,
pool,
tokenIn,
tokenOut,
connectingTokens,
poolsAllDict
);
if (pool.isLBP) {
const raisingTokenIn: string | undefined = getRaisingToken(
pool,
raisingTokens,
tokenIn
);
if (raisingTokenIn) {
graphPoolsSet.add(pool);
if (raisingTokenIn !== wethAddress) {
const bestRaisingTokenInToWeth =
getHighestLiquidityPool(
raisingTokenIn,
wethAddress,
poolsAllDict
);
if (bestRaisingTokenInToWeth) {
graphPoolsSet.add(
poolsAllDict[bestRaisingTokenInToWeth]
);
}
}
}
const raisingTokenOut: string | undefined = getRaisingToken(
pool,
raisingTokens,
tokenOut
);
if (raisingTokenOut) {
graphPoolsSet.add(pool);
if (raisingTokenOut !== wethAddress) {
const bestWethToRaisingTokenOut =
getHighestLiquidityPool(
wethAddress,
raisingTokenOut,
poolsAllDict
);
if (bestWethToRaisingTokenOut) {
graphPoolsSet.add(
poolsAllDict[bestWethToRaisingTokenOut]
);
}
}
}
}
}
}
}
// add highest liquidity pools with tokenIn and weth or tokenOut and weth
const bestTokenInToWeth = getHighestLiquidityPool(
tokenIn,
wethAddress,
poolsAllDict
);
if (bestTokenInToWeth) {
graphPoolsSet.add(poolsAllDict[bestTokenInToWeth]);
}
const bestWethToTokenOut = getHighestLiquidityPool(
wethAddress,
tokenOut,
poolsAllDict
);
if (bestWethToTokenOut) {
graphPoolsSet.add(poolsAllDict[bestWethToTokenOut]);
// add best pools tokenIn -> connectingToken and connectingToken -> tokenOut
// these can be part of a longer path so do not rely on being directly connected
for (const connectingToken of connectingTokens) {
addMostLiquidPoolToSet(
tokenIn,
connectingToken,
poolsAllDict,
graphPoolsSet
);
addMostLiquidPoolToSet(
connectingToken,
tokenOut,
poolsAllDict,
graphPoolsSet
);
}
if (linearPools.length == 0) return {};
const linearPoolsAddresses = linearPools.map((pool) => pool.address);
Expand Down Expand Up @@ -294,10 +268,6 @@ export function getBoostedGraph(
return edgeDict;
}

interface edgeDict {
[node: string]: [string, string, string][];
}

function getNodesAndEdges(pools: PoolBase[]): edgeDict {
const edgesFromNode: edgeDict = {};
for (const pool of pools) {
Expand All @@ -319,12 +289,6 @@ function getNodesAndEdges(pools: PoolBase[]): edgeDict {
return edgesFromNode;
}

interface treeEdge {
edge: [string, string, string];
parentIndices: [number, number];
visitedNodes: string[];
}

export function getBoostedPaths(
tokenIn: string,
tokenOut: string,
Expand Down Expand Up @@ -645,3 +609,68 @@ function getRaisingToken(
}
return theOtherToken;
}

function handleLBPCase(
graphPoolsSet: Set<PoolBase>,
lbpRaisingTokens: string[],
poolLbp: PoolBase,
tokenIn: string,
tokenOut: string,
connectingTokens: string[],
poolsAllDict: PoolDictionary
) {
// Tokens that will be used as LBP Base Token, e.g. USDC/DAI/WETH
const raisingTokens = lbpRaisingTokens.map((address) =>
address.toLowerCase()
);

if (raisingTokens.length === 0) return;

// Assuming tokenIn is the lbpToken find the corresponding base token if it exists
const raisingTokenIn: string | undefined = getRaisingToken(
poolLbp,
raisingTokens,
tokenIn
);
const raisingTokenOut: string | undefined = getRaisingToken(
poolLbp,
raisingTokens,
tokenOut
);
if (!raisingTokenIn && !raisingTokenOut) return;

// Add the LBP pool to the graph
graphPoolsSet.add(poolLbp);

// For each connecting token add most liquid pools with raisingToken and raisingTokenOut
for (const connectingToken of connectingTokens) {
if (raisingTokenIn && raisingTokenIn !== connectingToken) {
// raisingToken>[Pool]>connectingToken
addMostLiquidPoolToSet(
raisingTokenIn,
connectingToken,
poolsAllDict,
graphPoolsSet
);
}
if (raisingTokenOut && raisingTokenOut !== connectingToken) {
// connectingToken>[Pool]>raisingToken
addMostLiquidPoolToSet(
connectingToken,
raisingTokenOut,
poolsAllDict,
graphPoolsSet
);
}
}
}

function addMostLiquidPoolToSet(
tokenIn: string,
tokenOut: string,
pools: PoolDictionary,
graphPools: Set<PoolBase>
): void {
const pool = getHighestLiquidityPool(tokenIn, tokenOut, pools);
if (pool) graphPools.add(pools[pool]);
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface SorConfig {
chainId: number;
vault: string;
weth: string;
connectingTokens?: { symbol: string; address: string }[];
staBal3Pool?: { id: string; address: string };
usdcConnectingPool?: { id: string; usdc: string };
wETHwstETH?: { id: string; address: string };
Expand Down
Loading

0 comments on commit 5f6f9df

Please sign in to comment.