11import { useQuery } from "@tanstack/react-query" ;
2- import { getContract , HttpRequestError , PublicClient , RpcError } from "viem" ;
3- import { usePublicClient } from "wagmi" ;
2+ import { HttpRequestError , RpcError } from "viem" ;
43
54import { executeActions } from "@kleros/kleros-sdk/src/dataMappings/executeActions" ;
65import { DisputeDetails } from "@kleros/kleros-sdk/src/dataMappings/utils/disputeDetailsTypes" ;
76import { populateTemplate } from "@kleros/kleros-sdk/src/dataMappings/utils/populateTemplate" ;
87
9- import { GENESIS_BLOCK_ARBSEPOLIA } from "consts/index" ;
108import { useGraphqlBatcher } from "context/GraphqlBatcher" ;
11- import { iArbitrableV2Abi } from "hooks/contracts/generated" ;
12- import { useEvidenceGroup } from "queries/useEvidenceGroup" ;
139import { debounceErrorToast } from "utils/debounceErrorToast" ;
1410import { isUndefined } from "utils/index" ;
15- import { DEFAULT_CHAIN } from "consts/chains" ;
1611
1712import { graphql } from "src/graphql" ;
1813
19- import { useIsCrossChainDispute } from "../useIsCrossChainDispute " ;
14+ import { useDisputeDetailsQuery } from "./useDisputeDetailsQuery " ;
2015
2116const disputeTemplateQuery = graphql ( `
2217 query DisputeTemplate($id: ID!) {
@@ -30,33 +25,27 @@ const disputeTemplateQuery = graphql(`
3025` ) ;
3126
3227export const usePopulatedDisputeData = ( disputeID ?: string , arbitrableAddress ?: `0x${string } `) => {
33- const publicClient = usePublicClient ( ) ;
34- const { data : crossChainData , isError } = useIsCrossChainDispute ( disputeID , arbitrableAddress ) ;
28+ const { data : disputeData } = useDisputeDetailsQuery ( disputeID ) ;
3529 const { graphqlBatcher } = useGraphqlBatcher ( ) ;
36- const { data : externalDisputeID } = useEvidenceGroup ( disputeID , arbitrableAddress ) ;
3730 const isEnabled =
3831 ! isUndefined ( disputeID ) &&
39- ! isUndefined ( crossChainData ) &&
40- ! isUndefined ( arbitrableAddress ) &&
41- ! isUndefined ( externalDisputeID ) ;
32+ ! isUndefined ( disputeData ) &&
33+ ! isUndefined ( disputeData ?. dispute ) &&
34+ ! isUndefined ( disputeData . dispute ?. arbitrableChainId ) &&
35+ ! isUndefined ( disputeData . dispute ?. externalDisputeId ) &&
36+ ! isUndefined ( disputeData . dispute ?. templateId ) ;
4237
4338 return useQuery < DisputeDetails > ( {
44- queryKey : [ `DisputeTemplate${ disputeID } ${ arbitrableAddress } ${ externalDisputeID } ` ] ,
39+ queryKey : [ `DisputeTemplate${ disputeID } ${ arbitrableAddress } ${ disputeData ?. dispute ?. externalDisputeId } ` ] ,
4540 enabled : isEnabled ,
4641 staleTime : Infinity ,
4742 queryFn : async ( ) => {
48- if ( isEnabled && ! isError ) {
43+ if ( isEnabled ) {
4944 try {
50- const { isCrossChainDispute, crossChainTemplateId } = crossChainData ;
51-
52- const templateId = isCrossChainDispute
53- ? crossChainTemplateId
54- : await getTemplateId ( arbitrableAddress , disputeID , publicClient ) ;
55-
5645 const { disputeTemplate } = await graphqlBatcher . fetch ( {
5746 id : crypto . randomUUID ( ) ,
5847 document : disputeTemplateQuery ,
59- variables : { id : templateId . toString ( ) } ,
48+ variables : { id : disputeData . dispute ?. templateId . toString ( ) } ,
6049 isDisputeTemplate : true ,
6150 } ) ;
6251
@@ -66,10 +55,10 @@ export const usePopulatedDisputeData = (disputeID?: string, arbitrableAddress?:
6655 const initialContext = {
6756 disputeID : disputeID ,
6857 arbitrableAddress : arbitrableAddress ,
69- arbitrableChainID : isCrossChainDispute ? crossChainData . crossChainId . toString ( ) : DEFAULT_CHAIN . toString ( ) ,
58+ arbitrableChainID : disputeData . dispute ?. arbitrableChainId ,
7059 graphApiKey : import . meta. env . REACT_APP_GRAPH_API_KEY ,
7160 alchemyApiKey : import . meta. env . ALCHEMY_API_KEY ,
72- externalDisputeID : externalDisputeID ,
61+ externalDisputeID : disputeData . dispute ?. externalDisputeId ,
7362 } ;
7463
7564 const data = dataMappings ? await executeActions ( JSON . parse ( dataMappings ) , initialContext ) : { } ;
@@ -88,28 +77,3 @@ export const usePopulatedDisputeData = (disputeID?: string, arbitrableAddress?:
8877 } ,
8978 } ) ;
9079} ;
91-
92- const getTemplateId = async (
93- arbitrableAddress : `0x${string } `,
94- disputeID : string ,
95- publicClient : PublicClient
96- ) : Promise < bigint > => {
97- const arbitrable = getContract ( {
98- abi : iArbitrableV2Abi ,
99- address : arbitrableAddress ,
100- client : { public : publicClient } ,
101- } ) ;
102- const disputeFilter = await arbitrable . createEventFilter . DisputeRequest (
103- {
104- _arbitrableDisputeID : BigInt ( disputeID ) ,
105- } ,
106- {
107- fromBlock : GENESIS_BLOCK_ARBSEPOLIA ,
108- toBlock : "latest" ,
109- }
110- ) ;
111- const disputeEvents = await publicClient . getFilterLogs ( {
112- filter : disputeFilter ,
113- } ) ;
114- return disputeEvents [ 0 ] . args . _templateId ?? 0n ;
115- } ;
0 commit comments