forked from Uniswap/smart-order-router
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix default fallback issue. Refactor URI subgraph provider
- Loading branch information
Showing
15 changed files
with
180 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.