Skip to content

Commit

Permalink
Fix default fallback issue. Refactor URI subgraph provider
Browse files Browse the repository at this point in the history
  • Loading branch information
willpote committed Nov 10, 2021
1 parent 67b0953 commit 73dcce1
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 155 deletions.
2 changes: 2 additions & 0 deletions cli/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
V2StaticSubgraphProvider,
V3PoolProvider,
V3QuoteProvider,
V3SubgraphProvider,
} from '../src';

export abstract class BaseCommand extends Command {
Expand Down Expand Up @@ -277,6 +278,7 @@ export abstract class BaseCommand extends Command {
gasPriceCache
),
v2SubgraphProvider: new V2StaticSubgraphProvider(),
v3SubgraphProvider: new V3SubgraphProvider(chainId, undefined, 15000),
});

this._swapToRatioRouter = router;
Expand Down
29 changes: 17 additions & 12 deletions cli/commands/quote.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { flags } from '@oclif/command';
import { Protocol } from '@uniswap/router-sdk';
import { Currency, Ether, Percent, TradeType } from '@uniswap/sdk-core';
import dotenv from 'dotenv';
import { ethers } from 'ethers';
import _ from 'lodash';
import { ID_TO_CHAIN_ID, parseAmount, SwapRoute } from '../../src';
import { Protocol, TO_PROTOCOL } from '../../src/util/protocols';
import { TO_PROTOCOL } from '../../src/util/protocols';
import { BaseCommand } from '../base-command';

dotenv.config();
Expand All @@ -21,7 +22,7 @@ export class Quote extends BaseCommand {
help: flags.help({ char: 'h' }),
tokenIn: flags.string({ char: 'i', required: true }),
tokenOut: flags.string({ char: 'o', required: true }),
recipient: flags.string({ required: true }),
recipient: flags.string({ required: false }),
amount: flags.string({ char: 'a', required: true }),
exactIn: flags.boolean({ required: false }),
exactOut: flags.boolean({ required: false }),
Expand Down Expand Up @@ -99,11 +100,13 @@ export class Quote extends BaseCommand {
amountIn,
tokenOut,
TradeType.EXACT_INPUT,
{
deadline: 100,
recipient,
slippageTolerance: new Percent(5, 10_000),
},
recipient
? {
deadline: 100,
recipient,
slippageTolerance: new Percent(5, 10_000),
}
: undefined,
{
blockNumber: this.blockNumber - 10,
v3PoolSelection: {
Expand All @@ -129,11 +132,13 @@ export class Quote extends BaseCommand {
amountOut,
tokenIn,
TradeType.EXACT_OUTPUT,
{
deadline: 100,
recipient,
slippageTolerance: new Percent(5, 10_000),
},
recipient
? {
deadline: 100,
recipient,
slippageTolerance: new Percent(5, 10_000),
}
: undefined,
{
blockNumber: this.blockNumber - 10,
v3PoolSelection: {
Expand Down
17 changes: 9 additions & 8 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
export * from './caching-gas-provider';
export * from './cache';
export * from './cache-node';
export * from './v3/caching-pool-provider';
export * from './v3/caching-subgraph-provider';
export * from './caching-token-provider';
export * from './caching-gas-provider';
export * from './caching-token-list-provider';
export * from './caching-token-provider';
export * from './eip-1559-gas-price-provider';
export * from './eth-gas-station-info-gas-price-provider';
export * from './gas-price-provider';
export * from './multicall-provider';
export * from './multicall-uniswap-provider';
export * from './token-provider';
export * from './uri-subgraph-provider';
export * from './v2/quote-provider';
export * from './v2/static-subgraph-provider';
export * from './v2/subgraph-provider';
export * from './v3/caching-pool-provider';
export * from './v3/caching-subgraph-provider';
export * from './v3/pool-provider';
export * from './v3/quote-provider';
export * from './v3/subgraph-provider';
export * from './token-provider';
export * from './v3/uri-subgraph-provider';
export * from './v2/subgraph-provider';
export * from './v2/static-subgraph-provider';
export * from './v2/quote-provider';
74 changes: 74 additions & 0 deletions src/providers/uri-subgraph-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { default as retry } from 'async-retry';
import Timeout from 'await-timeout';
import axios from 'axios';
import { ChainId } from '../util/chains';
import { log } from '../util/log';
import { V2SubgraphPool } from './v2/subgraph-provider';
import { V3SubgraphPool } from './v3/subgraph-provider';

export class URISubgraphProvider<
TSubgraphPool extends V2SubgraphPool | V3SubgraphPool
> {
constructor(
private chainId: ChainId,
private uri: string,
private timeout = 3000,
private retries = 2
) {}

public async getPools(): Promise<TSubgraphPool[]> {
log.info(
{ uri: this.uri },
`About to get subgraph pools from URI ${this.uri}`
);

await retry(
async () => {
const timeout = new Timeout();
const timerPromise = timeout.set(this.timeout).then(() => {
throw new Error(
`Timed out getting pools from subgraph: ${this.timeout}`
);
});

let response;

try {
response = await Promise.race([axios.get(this.uri), timerPromise]);
} catch (err) {
throw err;
} finally {
timeout.clear();
}

const { data: poolsBuffer, status } = response;

if (status != 200) {
log.error({ response }, `Unabled to get pools from ${this.uri}.`);

throw new Error(`Unable to get pools from ${this.uri}`);
}

const pools = poolsBuffer as TSubgraphPool[];

log.info(
{ uri: this.uri, chain: this.chainId },
`Got subgraph pools from uri. Num: ${pools.length}`
);

return pools;
},
{
retries: this.retries,
onRetry: (err, retry) => {
log.info(
{ err },
`Failed to get pools from uri ${this.uri}. Retry attempt: ${retry}`
);
},
}
);

throw new Error(`Unable to get pools from ${this.uri}`);
}
}
53 changes: 28 additions & 25 deletions src/providers/v2/pool-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ export interface IV2PoolProvider {
): Promise<V2PoolAccessor>;
getPoolAddress(
tokenA: Token,
tokenB: Token,
tokenB: Token
): { poolAddress: string; token0: Token; token1: Token };
}

export type V2PoolAccessor = {
getPool: (
tokenA: Token,
tokenB: Token,
) => Pair | undefined;
getPool: (tokenA: Token, tokenB: Token) => Pair | undefined;
getPoolByAddress: (address: string) => Pair | undefined;
getAllPools: () => Pair[];
};
Expand All @@ -50,7 +47,7 @@ export class V2PoolProvider implements IV2PoolProvider {
retries: 2,
minTimeout: 50,
maxTimeout: 500,
},
}
) {}

public async getPools(
Expand All @@ -66,7 +63,7 @@ export class V2PoolProvider implements IV2PoolProvider {

const { poolAddress, token0, token1 } = this.getPoolAddress(
tokenA,
tokenB,
tokenB
);

if (poolAddressSet.has(poolAddress)) {
Expand All @@ -86,10 +83,18 @@ export class V2PoolProvider implements IV2PoolProvider {
`About to get reserves for ${poolAddressSet.size} pools as of block: ${providerConfig?.blockNumber}.`
);

const reservesResults = await this.getPoolsData<IReserves>(sortedPoolAddresses, 'getReserves', providerConfig);
const reservesResults = await this.getPoolsData<IReserves>(
sortedPoolAddresses,
'getReserves',
providerConfig
);

log.info(
`Got reserves for ${poolAddressSet.size} pools as of block: ${providerConfig?.blockNumber}.`
`Got reserves for ${poolAddressSet.size} pools ${
providerConfig?.blockNumber
? `as of block: ${await providerConfig?.blockNumber}.`
: ``
}`
);

const poolAddressToPool: { [poolAddress: string]: Pair } = {};
Expand All @@ -111,34 +116,32 @@ export class V2PoolProvider implements IV2PoolProvider {

const pool = new Pair(
CurrencyAmount.fromRawAmount(token0, reserve0.toString()),
CurrencyAmount.fromRawAmount(token1, reserve1.toString()),
CurrencyAmount.fromRawAmount(token1, reserve1.toString())
);

const poolAddress = sortedPoolAddresses[i]!;

poolAddressToPool[poolAddress] = pool;
}

log.info(
{
invalidPools: _.map(
invalidPools,
([token0, token1]) =>
`${token0.symbol}/${token1.symbol}`
),
},
`${invalidPools.length} pools invalid after checking their slot0 and liquidity results. Dropping.`
);
if (invalidPools) {
log.info(
{
invalidPools: _.map(
invalidPools,
([token0, token1]) => `${token0.symbol}/${token1.symbol}`
),
},
`${invalidPools.length} pools invalid after checking their slot0 and liquidity results. Dropping.`
);
}

const poolStrs = _.map(Object.values(poolAddressToPool), poolToString);

log.debug({ poolStrs }, `Found ${poolStrs.length} valid pools`);

return {
getPool: (
tokenA: Token,
tokenB: Token,
): Pair | undefined => {
getPool: (tokenA: Token, tokenB: Token): Pair | undefined => {
const { poolAddress } = this.getPoolAddress(tokenA, tokenB);
return poolAddressToPool[poolAddress];
},
Expand All @@ -150,7 +153,7 @@ export class V2PoolProvider implements IV2PoolProvider {

public getPoolAddress(
tokenA: Token,
tokenB: Token,
tokenB: Token
): { poolAddress: string; token0: Token; token1: Token } {
const [token0, token1] = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
Expand Down
42 changes: 4 additions & 38 deletions src/providers/v2/uri-subgraph-provider.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
import { ChainId } from '../../util/chains';
import { log } from '../../util/log';
import axios from 'axios';
import { URISubgraphProvider } from '../uri-subgraph-provider';
import { IV2SubgraphProvider, V2SubgraphPool } from './subgraph-provider';

export class V2URISubgraphProvider implements IV2SubgraphProvider {
constructor(private chainId: ChainId, private uri: string) {}

public async getPools(): Promise<V2SubgraphPool[]> {
try {
const response = await axios.get(this.uri);
const { data: poolsBuffer, status } = response;

if (status != 200) {
log.error(
{ response },
`Unabled to get pools from ${this.uri}.`
);

throw new Error(`Unable to get pools from ${this.uri}`);
}

const pools = poolsBuffer as V2SubgraphPool[];

log.info(
{ uri: this.uri, chain: this.chainId },
`Got subgraph pools from uri. Num: ${pools.length}`
);

return pools;
} catch (err) {
log.info(
{ uri: this.uri, chain: this.chainId },
`Failed to get subgraph pools from uri.`
);

throw err;
}
}
}
export class V2URISubgraphProvider
extends URISubgraphProvider<V2SubgraphPool>
implements IV2SubgraphProvider {}
8 changes: 6 additions & 2 deletions src/providers/v3/pool-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class V3PoolProvider implements IV3PoolProvider {
retries: 2,
minTimeout: 50,
maxTimeout: 500,
},
}
) {}

public async getPools(
Expand Down Expand Up @@ -106,7 +106,11 @@ export class V3PoolProvider implements IV3PoolProvider {
]);

log.info(
`Got liquidity and slot0s for ${poolAddressSet.size} pools as of block: ${providerConfig?.blockNumber}.`
`Got liquidity and slot0s for ${poolAddressSet.size} pools ${
providerConfig?.blockNumber
? `as of block: ${providerConfig?.blockNumber}.`
: ``
}`
);

const poolAddressToPool: { [poolAddress: string]: Pool } = {};
Expand Down
1 change: 1 addition & 0 deletions src/providers/v3/subgraph-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export class V3SubgraphProvider implements IV3SubgraphProvider {
tvlUSD: parseFloat(totalValueLockedUSD),
};
});

return poolsSanitized;
}
}
Loading

0 comments on commit 73dcce1

Please sign in to comment.