Skip to content

Commit

Permalink
Added disabled token filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Sep 9, 2020
1 parent 579750f commit 4184d24
Show file tree
Hide file tree
Showing 8 changed files with 561 additions and 37 deletions.
8 changes: 8 additions & 0 deletions src/disabled-tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tokens": [
{
"address": "0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"symbol": "OCEAN"
}
]
}
32 changes: 27 additions & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { BigNumber } from './utils/bignumber';
import { ethers } from 'ethers';
import { PoolPairData, Path, Pool, PoolDictionary, Swap } from './types';
import {
PoolPairData,
Path,
Pool,
PoolDictionary,
Swap,
DisabledToken,
} from './types';
import {
BONE,
TWOBONE,
Expand Down Expand Up @@ -641,13 +648,16 @@ export function getTokenPairsMultiHop(token: string, poolsTokensListSet: any) {
export function filterPoolsWithTokensDirect(
allPools: Pool[], // The complete information of the pools
tokenIn: string,
tokenOut: string
tokenOut: string,
disabledTokens: DisabledToken[] = []
): PoolDictionary {
// let poolsWithTokens: { [poolId: string]: Pool} = {};
let poolsWithTokens: PoolDictionary = {};
// If pool contains token add all its tokens to direct list
allPools.forEach(pool => {
let tokenListSet = new Set(pool.tokensList);
disabledTokens.forEach(token => tokenListSet.delete(token.address));

if (tokenListSet.has(tokenIn) && tokenListSet.has(tokenOut)) {
poolsWithTokens[pool.id] = pool;
}
Expand All @@ -660,7 +670,8 @@ export function filterPoolsWithTokensDirect(
export function filterPoolsWithoutMutualTokens(
allPools: Pool[],
tokenOne: string,
tokenTwo: string
tokenTwo: string,
disabledTokens: DisabledToken[] = []
): [PoolDictionary, Set<string>, PoolDictionary, Set<string>] {
let tokenOnePools: PoolDictionary = {};
let tokenTwoPools: PoolDictionary = {};
Expand All @@ -669,6 +680,11 @@ export function filterPoolsWithoutMutualTokens(

allPools.forEach(pool => {
let poolTokensSET = new Set(pool.tokensList);
disabledTokens.forEach(token => poolTokensSET.delete(token.address));
if (poolTokensSET.size < 2) {
return;
}

let containsTokenOne = poolTokensSET.has(tokenOne);
let containsTokenTwo = poolTokensSET.has(tokenTwo);

Expand Down Expand Up @@ -699,7 +715,8 @@ export function filterPoolsWithoutMutualTokens(
export async function filterPoolsWithTokensMultihop(
allPools: Pool[], // Just the list of pool tokens
tokenIn: string,
tokenOut: string
tokenOut: string,
disabledTokens: DisabledToken[] = []
): Promise<[Pool[], Pool[], string[]]> {
//// Multi-hop trades: we find the best pools that connect tokenIn and tokenOut through a multi-hop (intermediate) token
// First: we get all tokens that can be used to be traded with tokenIn excluding
Expand All @@ -715,7 +732,12 @@ export async function filterPoolsWithTokensMultihop(
tokenInHopTokens,
poolsTokenOutNoTokenIn,
tokenOutHopTokens,
] = filterPoolsWithoutMutualTokens(allPools, tokenIn, tokenOut);
] = filterPoolsWithoutMutualTokens(
allPools,
tokenIn,
tokenOut,
disabledTokens
);

// Third: we find the intersection of the two previous sets so we can trade tokenIn for tokenOut with 1 multi-hop
const hopTokensSet = [...tokenInHopTokens].filter(x =>
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ export interface SubGraphToken {
export interface PoolDictionary {
[poolId: string]: Pool;
}

export interface DisabledToken {
address: string;
symbol: string;
}
158 changes: 158 additions & 0 deletions test/allPools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,164 @@
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
],
"totalWeight": "50"
},
{
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad6",
"publicSwap": true,
"swapFee": "0.012",
"tokens": [
{
"address": "0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"balance": "9999999999.1111111",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad6-0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"symbol": "OCEAN"
},
{
"address": "0x6b175474e89094c44da98b954eedeac495271d0f",
"balance": "8888888888.1111111",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad6-0x6b175474e89094c44da98b954eedeac495271d0f",
"symbol": "DAI"
}
],
"tokensList": [
"0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"0x6b175474e89094c44da98b954eedeac495271d0f"
],
"totalWeight": "50"
},
{
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad7",
"publicSwap": true,
"swapFee": "0.012",
"tokens": [
{
"address": "0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"balance": "9999999999.2222222",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad7-0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"symbol": "OCEAN"
},
{
"address": "0x960b236a07cf122663c4303350609a66a7b288c0",
"balance": "8888888888.2222222",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad7-0x960b236a07cf122663c4303350609a66a7b288c0",
"symbol": "ANT"
}
],
"tokensList": [
"0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"0x960b236a07cf122663c4303350609a66a7b288c0"
],
"totalWeight": "50"
},
{
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad8",
"publicSwap": true,
"swapFee": "0.012",
"tokens": [
{
"address": "0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"balance": "9999999999.3333333",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad8-0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"symbol": "OCEAN"
},
{
"address": "0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2",
"balance": "8888888888.3333333",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad8-0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2",
"symbol": "MKR"
}
],
"tokensList": [
"0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2"
],
"totalWeight": "50"
},
{
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad9",
"publicSwap": true,
"swapFee": "0.012",
"tokens": [
{
"address": "0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"balance": "9999999999.3333333",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad9-0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"symbol": "OCEAN"
},
{
"address": "0x9f8f72aa9304c8b593d555f12ef6589cc3a579a1",
"balance": "8888888888.3333333",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad9-0x9f8f72aa9304c8b593d555f12ef6589cc3a579a1",
"symbol": "TEST1"
},
{
"address": "0x9f8f72aa9304c8b593d555f12ef6589cc3a579a3",
"balance": "88.3333333",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad9-0x9f8f72aa9304c8b593d555f12ef6589cc3a579a3",
"symbol": "TEST2"
}
],
"tokensList": [
"0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a1",
"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a3"
],
"totalWeight": "75"
},
{
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad0",
"publicSwap": true,
"swapFee": "0.012",
"tokens": [
{
"address": "0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"balance": "9999999999.3333333",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad0-0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"symbol": "OCEAN"
},
{
"address": "0x9f8f72aa9304c8b593d555f12ef6589cc3a579a3",
"balance": "88.3333333",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad0-0x9f8f72aa9304c8b593d555f12ef6589cc3a579a3",
"symbol": "TEST2"
},
{
"address": "0x9f8f72aa9304c8b593d555f12ef6589cc3a579a4",
"balance": "8888888888.3333333",
"decimals": 18,
"denormWeight": "25",
"id": "0xf218fe414c6b1c6b42e79b7690f1509a634baad0-0x9f8f72aa9304c8b593d555f12ef6589cc3a579a4",
"symbol": "TEST3"
}
],
"tokensList": [
"0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a3",
"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a4"
],
"totalWeight": "75"
}
]
}
4 changes: 2 additions & 2 deletions test/direct-sor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ describe('Test direct SOR (legacy version with direct pools only) using allPools
});

it('Saved pool check', async () => {
// Compares saved pools @25/05/20 to current Subgraph pools.
assert.equal(allPools.pools.length, 59, 'Should be 59 pools');
// Compares saved pools @09/09/20 to current Subgraph pools.
assert.equal(allPools.pools.length, 64, 'Should be 64 pools');
});

it('Direct SOR - WETH->DAI, swapExactIn', async () => {
Expand Down
8 changes: 8 additions & 0 deletions test/disabled-tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tokens": [
{
"address": "0x985dd3d42de1e256d09e1c10f112bccb8015ad41",
"symbol": "OCEAN"
}
]
}
Loading

0 comments on commit 4184d24

Please sign in to comment.