-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(registry-subgraph): optimism (#846)
- Loading branch information
1 parent
8fbd2b0
commit b8f745a
Showing
6 changed files
with
67 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"mainnet": { | ||
"CannonRegistry": { | ||
"address": "0x8E5C7EFC9636A6A0408A46BB7F617094B81e5dba", | ||
"startBlock": 16512800 | ||
} | ||
}, | ||
"optimism": { | ||
"CannonRegistry": { | ||
"address": "0x8E5C7EFC9636A6A0408A46BB7F617094B81e5dba", | ||
"startBlock": 106756348 | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,26 +1,51 @@ | ||
import { useEffect } from 'react'; | ||
import { | ||
DocumentNode, | ||
NoInfer, | ||
OperationVariables, | ||
QueryHookOptions, | ||
QueryResult, | ||
TypedDocumentNode, | ||
useQuery, | ||
} from '@apollo/client'; | ||
import { useEffect, useState } from 'react'; | ||
import { ApolloError, DocumentNode, NoInfer, OperationVariables, QueryHookOptions, TypedDocumentNode } from '@apollo/client'; | ||
import apolloClient, { apolloClientOptimism } from '@/graphql/ApolloClient'; | ||
import { useLogs } from '@/providers/logsProvider'; | ||
import { merge } from 'lodash'; | ||
|
||
export function useQueryCannonSubgraphData<TData = any, TVariables extends OperationVariables = OperationVariables>( | ||
query: DocumentNode | TypedDocumentNode<TData, TVariables>, | ||
options?: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>> | ||
): QueryResult<TData, TVariables> { | ||
): any { | ||
const { addLog } = useLogs(); | ||
const [mergedData, setMergedData] = useState<TData | undefined>(undefined); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState<Error | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
addLog(`Querying Subgraph: ${(query.definitions[0] as any).name.value}(${JSON.stringify(options?.variables)})`); | ||
}, [query, JSON.stringify(options?.variables)]); | ||
const fetchSubgraphs = async () => { | ||
setLoading(true); | ||
try { | ||
// Query mainnet | ||
const { data: mainnetData } = await apolloClient.query<TData, TVariables>({ | ||
query, | ||
variables: options?.variables, | ||
}); | ||
|
||
// Query optimism | ||
const { data: optimismData } = await apolloClientOptimism.query<TData, TVariables>({ | ||
query, | ||
variables: options?.variables, | ||
}); | ||
|
||
const result = useQuery<TData, TVariables>(query, options); | ||
// Merge results, with optimism data taking precedence | ||
const merged = merge({}, mainnetData, optimismData); | ||
setMergedData(merged); | ||
} catch (e) { | ||
setError(e instanceof Error ? e : new Error('An error occurred')); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
addLog('Fetching subgraphs...'); | ||
void fetchSubgraphs(); | ||
}, [query, JSON.stringify(options?.variables)]); | ||
|
||
return result; | ||
return { | ||
data: mergedData, | ||
loading, | ||
error: error as ApolloError | undefined, | ||
}; | ||
} |