Skip to content

Commit

Permalink
getLBP correctly handles abscence of lbpRaisingTokens in config
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioyuhjtman committed Mar 1, 2022
1 parent 9e4756c commit 48d93f7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
88 changes: 45 additions & 43 deletions src/routeProposal/filtering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export function getBoostedPaths(

const weth = config.weth.toLowerCase();
const bbausd = config.bbausd.address.toLowerCase();

// Letter 'i' in iTokenIn and iTokenOut stands for "internal",
// lacking of a better name for that so far.
const [lbpPathIn, iTokenIn] = getLBP(tokenIn, poolsAllDict, true, config);
Expand All @@ -266,7 +267,7 @@ export function getBoostedPaths(
config
);

// getLinearPools could receive an array of tokens so that we search
// getLinearPools might instead receive an array of tokens so that we search
// over poolsAllDict once instead of twice. Similarly for getPoolsWith
// and getLBP. This is a matter of code simplicity vs. efficiency.
const linearPoolsIn = getLinearPools(iTokenIn, poolsAllDict);
Expand Down Expand Up @@ -312,39 +313,37 @@ export function getBoostedPaths(

const paths1 = combineSemiPaths(semiPathsInToWeth, semiPathsWethToOut);
const paths2 = combineSemiPaths(semiPathsInToBBausd, semiPathsBBausdToOut);
if (!config.wethBBausd) {
const paths = paths1.concat(paths2);
// Every short path (short means length 1 and 2) is included in filterHopPools.
return removeShortPaths(paths);
let paths = paths1.concat(paths2);
if (config.wethBBausd) {
const WethBBausdPool = poolsAllDict[config.wethBBausd.id];
const WethBBausdPath = createPath(
[config.weth, config.bbausd.address],
[WethBBausdPool]
);
const BBausdWethPath = createPath(
[config.bbausd.address, config.weth],
[WethBBausdPool]
);
const paths3 = combineSemiPaths(
semiPathsInToWeth,
semiPathsBBausdToOut,
WethBBausdPath
);
const paths4 = combineSemiPaths(
semiPathsInToBBausd,
semiPathsWethToOut,
BBausdWethPath
);
paths = paths.concat(paths3, paths4);
}
const WethBBausdPool = poolsAllDict[config.wethBBausd.id];
const WethBBausdPath = createPath(
[config.weth, config.bbausd.address],
[WethBBausdPool]
);
const BBausdWethPath = createPath(
[config.bbausd.address, config.weth],
[WethBBausdPool]
);
const paths3 = combineSemiPaths(
semiPathsInToWeth,
semiPathsBBausdToOut,
WethBBausdPath
);
const paths4 = combineSemiPaths(
semiPathsInToBBausd,
semiPathsWethToOut,
BBausdWethPath
);
let paths = paths1.concat(paths2, paths3, paths4);

// If there is a nontrivial LBP path, compose every path with the lbp paths
// in and out. One of them might be the empty path.
if (lbpPathIn.pools.length > 0 || lbpPathOut.pools.length > 0) {
paths = paths.map((path) =>
composePaths([lbpPathIn, path, lbpPathOut])
);
}
// Every short path (short means length 1 and 2) is included in filterHopPools.
return removeShortPaths(paths);
}

Expand Down Expand Up @@ -944,23 +943,26 @@ function getLBP(
if (config.lbpRaisingTokens) {
if (config.lbpRaisingTokens.includes(token)) {
return [getEmptyPath(), token];
}
}
for (const id in poolsAllDict) {
const pool = poolsAllDict[id];
if (!pool.isLBP) continue;
const tokensList = pool.tokensList;
// We assume that the LBP has two tokens.
for (let i = 0; i < 2; i++) {
if (tokensList[i] == token) {
let path = createPath(
[tokensList[i], tokensList[1 - i]],
[pool]
);
if (!isInitial) path = reversePath(path);
return [path, tokensList[1 - i]];
} else {
for (const id in poolsAllDict) {
const pool = poolsAllDict[id];
if (!pool.isLBP) continue;
const tokensList = pool.tokensList;
// We assume that the LBP has two tokens.
for (let i = 0; i < 2; i++) {
if (tokensList[i] == token) {
const theOtherToken = tokensList[1 - i];
let path = createPath(
[tokensList[i], theOtherToken],
[pool]
);
if (!isInitial) path = reversePath(path);
if (config.lbpRaisingTokens.includes(theOtherToken))
return [path, theOtherToken];
}
}
}
return [getEmptyPath(), token];
}
}
return [getEmptyPath(), token];
} else return [getEmptyPath(), token];
}
10 changes: 5 additions & 5 deletions test/testScripts/swapExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const SOR_CONFIG: Record<Network, SorConfig> = {
chainId: Network.MAINNET, //1
vault: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
weth: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
staBal3Pool: {
bbausd: {
id: '0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb20000000000000000000000fe',
address: '0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb2',
},
Expand Down Expand Up @@ -654,11 +654,11 @@ async function makeRelayerTrade(
}

export async function simpleSwap() {
const networkId = Network.KOVAN;
const networkId = Network.MAINNET;
// Pools source can be Subgraph URL or pools data set passed directly
// Update pools list with most recent onchain balances
const tokenIn = ADDRESSES[networkId].DAI_from_AAVE;
const tokenOut = ADDRESSES[networkId].USDC_from_AAVE;
const tokenIn = ADDRESSES[networkId].DAI;
const tokenOut = ADDRESSES[networkId].USDC;
const swapType = SwapTypes.SwapExactIn;
const swapAmount = parseFixed('100', 18);
const executeTrade = true;
Expand Down Expand Up @@ -694,7 +694,7 @@ export async function simpleSwap() {

const swapInfo = await getSwap(
provider,
SOR_CONFIG[Network.KOVAN],
SOR_CONFIG[networkId],
subgraphPoolDataService,
// mockPoolDataService,
coingeckoTokenPriceService,
Expand Down

0 comments on commit 48d93f7

Please sign in to comment.