forked from Uniswap/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate relay (Uniswap#4320)
* setup relay compiler * refactored to use polling interval, fixed PR comments * fixes, readded uninitialized state for liquidity chart * updated cypress test * reorganized graphql files into src/graphql
- Loading branch information
Showing
23 changed files
with
395 additions
and
295 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 +1 @@ | ||
/src/state/data/generated.ts | ||
/src/schema/schema.graphql |
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
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
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,53 @@ | ||
import graphql from 'babel-plugin-relay/macro' | ||
import useInterval from 'lib/hooks/useInterval' | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { fetchQuery, useRelayEnvironment } from 'relay-hooks' | ||
import { useAppSelector } from 'state/hooks' | ||
|
||
import type { | ||
AllV3TicksQuery as AllV3TicksQueryType, | ||
AllV3TicksQuery$data, | ||
} from './__generated__/AllV3TicksQuery.graphql' | ||
|
||
const query = graphql` | ||
query AllV3TicksQuery($poolAddress: String!, $skip: Int!) { | ||
ticks(first: 1000, skip: $skip, where: { poolAddress: $poolAddress }, orderBy: tickIdx) { | ||
tick: tickIdx | ||
liquidityNet | ||
price0 | ||
price1 | ||
} | ||
} | ||
` | ||
|
||
export type Ticks = AllV3TicksQuery$data['ticks'] | ||
export type TickData = Ticks[number] | ||
|
||
export default function useAllV3TicksQuery(poolAddress: string | undefined, skip: number, interval: number) { | ||
const [data, setData] = useState<AllV3TicksQuery$data | null>(null) | ||
const [error, setError] = useState<any>(null) | ||
const [isLoading, setIsLoading] = useState(true) | ||
const chainId = useAppSelector((state) => state.application.chainId) | ||
const environment = useRelayEnvironment() | ||
|
||
const refreshData = useCallback(() => { | ||
if (poolAddress && chainId) { | ||
fetchQuery<AllV3TicksQueryType>(environment, query, { | ||
poolAddress: poolAddress.toLowerCase(), | ||
skip, | ||
}).subscribe({ | ||
next: setData, | ||
error: setError, | ||
complete: () => setIsLoading(false), | ||
}) | ||
} else { | ||
setIsLoading(false) | ||
} | ||
}, [poolAddress, skip, chainId, environment]) | ||
|
||
// Trigger fetch on first load | ||
useEffect(refreshData, [refreshData, poolAddress, skip]) | ||
|
||
useInterval(refreshData, interval, true) | ||
return { error, isLoading, data } | ||
} |
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,69 @@ | ||
import graphql from 'babel-plugin-relay/macro' | ||
import useInterval from 'lib/hooks/useInterval' | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { fetchQuery, useRelayEnvironment } from 'relay-hooks' | ||
import { useAppSelector } from 'state/hooks' | ||
|
||
import type { | ||
FeeTierDistributionQuery as FeeTierDistributionQueryType, | ||
FeeTierDistributionQuery$data, | ||
} from './__generated__/FeeTierDistributionQuery.graphql' | ||
|
||
const query = graphql` | ||
query FeeTierDistributionQuery($token0: String!, $token1: String!) { | ||
_meta { | ||
block { | ||
number | ||
} | ||
} | ||
asToken0: pools( | ||
orderBy: totalValueLockedToken0 | ||
orderDirection: desc | ||
where: { token0: $token0, token1: $token1 } | ||
) { | ||
feeTier | ||
totalValueLockedToken0 | ||
totalValueLockedToken1 | ||
} | ||
asToken1: pools( | ||
orderBy: totalValueLockedToken0 | ||
orderDirection: desc | ||
where: { token0: $token1, token1: $token0 } | ||
) { | ||
feeTier | ||
totalValueLockedToken0 | ||
totalValueLockedToken1 | ||
} | ||
} | ||
` | ||
|
||
export default function useFeeTierDistributionQuery( | ||
token0: string | undefined, | ||
token1: string | undefined, | ||
interval: number | ||
) { | ||
const [data, setData] = useState<FeeTierDistributionQuery$data | null>(null) | ||
const [error, setError] = useState<any>(null) | ||
const [isLoading, setIsLoading] = useState(true) | ||
const environment = useRelayEnvironment() | ||
const chainId = useAppSelector((state) => state.application.chainId) | ||
|
||
const refreshData = useCallback(() => { | ||
if (token0 && token1 && chainId) { | ||
fetchQuery<FeeTierDistributionQueryType>(environment, query, { | ||
token0: token0.toLowerCase(), | ||
token1: token1.toLowerCase(), | ||
}).subscribe({ | ||
next: setData, | ||
error: setError, | ||
complete: () => setIsLoading(false), | ||
}) | ||
} | ||
}, [token0, token1, chainId, environment]) | ||
|
||
// Trigger fetch on first load | ||
useEffect(refreshData, [refreshData, token0, token1]) | ||
|
||
useInterval(refreshData, interval, true) | ||
return { error, isLoading, data } | ||
} |
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,9 @@ | ||
import { Environment, Network, RecordSource, Store } from 'relay-runtime' | ||
|
||
import fetchGraphQL from './fetchGraphQL' | ||
|
||
// Export a singleton instance of Relay Environment configured with our network function: | ||
export default new Environment({ | ||
network: Network.create(fetchGraphQL), | ||
store: new Store(new RecordSource()), | ||
}) |
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,53 @@ | ||
/** | ||
* Helpful Resources | ||
* https://github.com/sibelius/create-react-app-relay-modern/blob/master/src/relay/fetchQuery.js | ||
* https://github.com/relay-tools/relay-compiler-language-typescript/blob/master/example/ts/app.tsx | ||
*/ | ||
|
||
import { SupportedChainId } from 'constants/chains' | ||
import { Variables } from 'react-relay' | ||
import { GraphQLResponse, ObservableFromValue, RequestParameters } from 'relay-runtime' | ||
|
||
import store, { AppState } from '../state/index' | ||
|
||
const CHAIN_SUBGRAPH_URL: Record<number, string> = { | ||
[SupportedChainId.MAINNET]: 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3', | ||
[SupportedChainId.RINKEBY]: 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3', | ||
|
||
[SupportedChainId.ARBITRUM_ONE]: 'https://api.thegraph.com/subgraphs/name/ianlapham/arbitrum-minimal', | ||
|
||
[SupportedChainId.OPTIMISM]: 'https://api.thegraph.com/subgraphs/name/ianlapham/optimism-post-regenesis', | ||
|
||
[SupportedChainId.POLYGON]: 'https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-v3-polygon', | ||
|
||
[SupportedChainId.CELO]: 'https://api.thegraph.com/subgraphs/name/jesse-sawa/uniswap-celo', | ||
} | ||
|
||
const headers = { | ||
Accept: 'application/json', | ||
'Content-type': 'application/json', | ||
} | ||
|
||
// Define a function that fetches the results of a request (query/mutation/etc) | ||
// and returns its results as a Promise: | ||
const fetchQuery = (params: RequestParameters, variables: Variables): ObservableFromValue<GraphQLResponse> => { | ||
const chainId = (store.getState() as AppState).application.chainId | ||
|
||
const subgraphUrl = | ||
chainId && CHAIN_SUBGRAPH_URL[chainId] ? CHAIN_SUBGRAPH_URL[chainId] : CHAIN_SUBGRAPH_URL[SupportedChainId.MAINNET] | ||
|
||
const body = JSON.stringify({ | ||
query: params.text, // GraphQL text from input | ||
variables, | ||
}) | ||
|
||
const response = fetch(subgraphUrl, { | ||
method: 'POST', | ||
headers, | ||
body, | ||
}).then((res) => res.json()) | ||
|
||
return response | ||
} | ||
|
||
export default fetchQuery |
Oops, something went wrong.