Skip to content

Commit

Permalink
Merge pull request #227 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
John Grant authored Jan 12, 2022
2 parents 4c1bb21 + 5657c05 commit 7603520
Show file tree
Hide file tree
Showing 36 changed files with 1,208 additions and 880 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": "2.1.0-beta.4",
"version": "3.0.0-beta.0",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
76 changes: 0 additions & 76 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,6 @@
import { Zero } from '@ethersproject/constants';
import { SwapInfo } from './types';

export const WETHADDR: { [chainId: number]: string } = {
1: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
3: '0xdFCeA9088c8A88A76FF74892C1457C17dfeef9C1',
4: '0xdFCeA9088c8A88A76FF74892C1457C17dfeef9C1',
5: '0x9A1000D492d40bfccbc03f413A48F5B6516Ec0Fd',
42: '0xdFCeA9088c8A88A76FF74892C1457C17dfeef9C1',
137: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', // For Polygon this is actually wrapped MATIC
42161: '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1',
99: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
};

export const MULTIADDR: { [chainId: number]: string } = {
1: '0xeefba1e63905ef1d7acba5a8513c70307c1ce441',
3: '0x53c43764255c17bd724f74c4ef150724ac50a3ed',
4: '0x42ad527de7d4e9d9d011ac45b31d8551f8fe9821',
5: '0x3b2A02F22fCbc872AF77674ceD303eb269a46ce3',
42: '0x2cc8688C5f75E365aaEEb4ea8D6a480405A48D2A',
137: '0xa1B2b503959aedD81512C37e9dce48164ec6a94d',
42161: '0x269ff446d9892c9e19082564df3f5e8741e190a1',
99: '0xeefba1e63905ef1d7acba5a8513c70307c1ce441',
};

export const VAULTADDR: { [chainId: number]: string } = {
1: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
3: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
4: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
5: '0x65748E8287Ce4B9E6D83EE853431958851550311',
42: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
137: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
42161: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
99: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
};

// Currently only used for Polygon
// ID of USDC Connecting Pool & USDC token address
export const USDCCONNECTINGPOOL: {
[chainId: number]: { id: string; usdc: string };
} = {
99: {
id: 'usdcConnecting',
usdc: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
},
};

export const STABAL3POOL: {
[chainId: number]: { id: string; address: string };
} = {
// TO DO - Add Mainnet info
// TO DO - Add for Polygon for staBAL3 pairs
1: {
id: '0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb20000000000000000000000fe',
address: '0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb2',
},
42: {
id: '0x8fd162f338b770f7e879030830cde9173367f3010000000000000000000004d8',
address: '0x8fd162f338b770f7e879030830cde9173367f301',
},
99: {
id: 'staBal3Id',
address: '0x06df3b2bbb68adc8b0e302443692037ed9f91b42',
},
};

export const WETHSTABAL3: {
[chainId: number]: { id: string; address: string };
} = {
42: {
id: '0x6be79a54f119dbf9e8ebd9ded8c5bd49205bc62d00020000000000000000033c',
address: '0x6be79a54f119dbf9e8ebd9ded8c5bd49205bc62d',
},
99: {
id: 'weightedWethStaBal3Id',
address: 'weightedWethStaBal3',
},
};

export const EMPTY_SWAPINFO: SwapInfo = {
tokenAddresses: [],
swaps: [],
Expand Down
35 changes: 35 additions & 0 deletions src/poolCacher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import cloneDeep from 'lodash.clonedeep';
import { PoolDataService, SubgraphPoolBase } from './types';

export class PoolCacher {
private pools: SubgraphPoolBase[] = [];
private _finishedFetching = false;

constructor(private readonly poolDataService: PoolDataService) {}

public get finishedFetching(): boolean {
return this._finishedFetching;
}

public getPools(): SubgraphPoolBase[] {
return cloneDeep(this.pools);
}

/*
* Saves updated pools data to internal cache.
*/
public async fetchPools(): Promise<boolean> {
try {
this.pools = await this.poolDataService.getPools();
this._finishedFetching = true;

return true;
} catch (err) {
// On error clear all caches and return false so user knows to try again.
this._finishedFetching = false;
this.pools = [];
console.error(`Error: fetchPools(): ${err.message}`);
return false;
}
}
}
103 changes: 0 additions & 103 deletions src/poolCaching/index.ts

This file was deleted.

29 changes: 12 additions & 17 deletions src/routeProposal/filtering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ import {
PoolFilter,
PoolTypes,
PoolPairBase,
SorConfig,
} from '../types';
import { MetaStablePool } from '../pools/metaStablePool/metaStablePool';
import { ZERO } from '../utils/bignumber';
import {
USDCCONNECTINGPOOL,
STABAL3POOL,
WETHSTABAL3,
WETHADDR,
} from '../constants';
import { parseNewPool } from '../pools';
import { Zero } from '@ethersproject/constants';

Expand Down Expand Up @@ -225,10 +220,10 @@ export function getLinearStaBal3Paths(
tokenOut: string,
poolsAllDict: PoolDictionary,
poolsFilteredDict: PoolDictionary,
chainId: number
config: SorConfig
): NewPath[] {
// This is the top level Metastable pool containing bUSDC/bDAI/bUSDT
const staBal3PoolInfo = STABAL3POOL[chainId];
const staBal3PoolInfo = config.staBal3Pool;
if (!staBal3PoolInfo) return [];
const staBal3Pool: MetaStablePool = poolsAllDict[
staBal3PoolInfo.id
Expand Down Expand Up @@ -291,13 +286,13 @@ export function getLinearStaBal3Paths(

// Creates a path through most liquid WETH/TokenOut pool via WETH/staBal3 connecting pool
// staBal3Bpt>[staBal3Bpt-WETH]>WETH>[WETH-TokenOut]>TokenOut
const wethStaBal3Info = WETHSTABAL3[chainId];
const wethStaBal3Info = config.wethStaBal3;
if (!wethStaBal3Info) return pathsUsingLinear;
const wethStaBal3Pool = poolsAllDict[wethStaBal3Info.id];
if (!wethStaBal3Pool) return pathsUsingLinear;

const wethTokenOutPath = getMostLiquidPath(
[staBal3Pool.address, WETHADDR[chainId], tokenOut],
[staBal3Pool.address, config.weth, tokenOut],
1,
[wethStaBal3Pool],
poolsFilteredDict,
Expand Down Expand Up @@ -334,13 +329,13 @@ export function getLinearStaBal3Paths(

// Creates a path through most liquid WETH paired pool and staBal3/WETH pool
// TokenIn>[WETH-TokenIn]>WETH>[staBal3Bpt-WETH]>staBal3Bpt>
const wethStaBal3Info = WETHSTABAL3[chainId];
const wethStaBal3Info = config.wethStaBal3;
if (!wethStaBal3Info) return pathsUsingLinear;
const wethStaBal3Pool = poolsAllDict[wethStaBal3Info.id];
if (!wethStaBal3Pool) return pathsUsingLinear;

const tokenInWethPath = getMostLiquidPath(
[tokenIn, WETHADDR[chainId], staBal3Pool.address],
[tokenIn, config.weth, staBal3Pool.address],
0,
[wethStaBal3Pool],
poolsFilteredDict,
Expand Down Expand Up @@ -530,17 +525,17 @@ export function getPathsUsingStaBalPool(
tokenOut: string,
poolsAll: PoolDictionary,
poolsFiltered: PoolDictionary,
chainId: number
config: SorConfig
): NewPath[] {
// This will be the USDC/staBAL Connecting pool used in Polygon
const usdcConnectingPoolInfo = USDCCONNECTINGPOOL[chainId];
const usdcConnectingPoolInfo = config.usdcConnectingPool;
if (!usdcConnectingPoolInfo) return [];

const usdcConnectingPool = poolsAll[usdcConnectingPoolInfo.id];
if (!usdcConnectingPool) return [];

// staBal BPT token is the hop token between token and USDC connecting pool
const hopTokenStaBal = STABAL3POOL[chainId].address;
const hopTokenStaBal = config.staBal3Pool?.address;

if (!usdcConnectingPool || !hopTokenStaBal) return [];

// Finds the best metastable Pool with tokenIn/staBal3Bpt or returns null if doesn't exist
const metastablePoolIdIn = getHighestLiquidityPool(
Expand Down
17 changes: 12 additions & 5 deletions src/routeProposal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import {
parseToPoolsDict,
} from './filtering';
import { calculatePathLimits } from './pathLimits';
import { SwapOptions, SwapTypes, NewPath, SubgraphPoolBase } from '../types';
import {
SwapOptions,
SwapTypes,
NewPath,
SubgraphPoolBase,
SorConfig,
} from '../types';

export class RouteProposer {
cache: Record<string, { paths: NewPath[] }> = {};

constructor(private readonly config: SorConfig) {}

/**
* Given a list of pools and a desired input/output, returns a set of possible paths to route through
*/
Expand All @@ -19,8 +27,7 @@ export class RouteProposer {
tokenOut: string,
swapType: SwapTypes,
pools: SubgraphPoolBase[],
swapOptions: SwapOptions,
chainId: number
swapOptions: SwapOptions
): NewPath[] {
if (pools.length === 0) return [];

Expand Down Expand Up @@ -57,15 +64,15 @@ export class RouteProposer {
tokenOut,
poolsAllDict,
poolsFilteredDict,
chainId
this.config
);

const pathsUsingStaBal = getPathsUsingStaBalPool(
tokenIn,
tokenOut,
poolsAllDict,
poolsFilteredDict,
chainId
this.config
);

const combinedPathData = pathData
Expand Down
Loading

0 comments on commit 7603520

Please sign in to comment.