1
1
import { useQuery } from "@tanstack/react-query" ;
2
- import { getContract , HttpRequestError , PublicClient , RpcError } from "viem" ;
3
- import { usePublicClient } from "wagmi" ;
2
+ import { HttpRequestError , RpcError } from "viem" ;
4
3
5
4
import { executeActions } from "@kleros/kleros-sdk/src/dataMappings/executeActions" ;
6
5
import { DisputeDetails } from "@kleros/kleros-sdk/src/dataMappings/utils/disputeDetailsTypes" ;
7
6
import { populateTemplate } from "@kleros/kleros-sdk/src/dataMappings/utils/populateTemplate" ;
8
7
9
- import { GENESIS_BLOCK_ARBSEPOLIA } from "consts/index" ;
10
8
import { useGraphqlBatcher } from "context/GraphqlBatcher" ;
11
- import { iArbitrableV2Abi } from "hooks/contracts/generated" ;
12
- import { useEvidenceGroup } from "queries/useEvidenceGroup" ;
13
9
import { debounceErrorToast } from "utils/debounceErrorToast" ;
14
10
import { isUndefined } from "utils/index" ;
15
- import { DEFAULT_CHAIN } from "consts/chains" ;
16
11
17
12
import { graphql } from "src/graphql" ;
18
13
19
- import { useIsCrossChainDispute } from "../useIsCrossChainDispute " ;
14
+ import { useDisputeDetailsQuery } from "./useDisputeDetailsQuery " ;
20
15
21
16
const disputeTemplateQuery = graphql ( `
22
17
query DisputeTemplate($id: ID!) {
@@ -30,33 +25,27 @@ const disputeTemplateQuery = graphql(`
30
25
` ) ;
31
26
32
27
export 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 ) ;
35
29
const { graphqlBatcher } = useGraphqlBatcher ( ) ;
36
- const { data : externalDisputeID } = useEvidenceGroup ( disputeID , arbitrableAddress ) ;
37
30
const isEnabled =
38
31
! 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 ) ;
42
37
43
38
return useQuery < DisputeDetails > ( {
44
- queryKey : [ `DisputeTemplate${ disputeID } ${ arbitrableAddress } ${ externalDisputeID } ` ] ,
39
+ queryKey : [ `DisputeTemplate${ disputeID } ${ arbitrableAddress } ${ disputeData ?. dispute ?. externalDisputeId } ` ] ,
45
40
enabled : isEnabled ,
46
41
staleTime : Infinity ,
47
42
queryFn : async ( ) => {
48
- if ( isEnabled && ! isError ) {
43
+ if ( isEnabled ) {
49
44
try {
50
- const { isCrossChainDispute, crossChainTemplateId } = crossChainData ;
51
-
52
- const templateId = isCrossChainDispute
53
- ? crossChainTemplateId
54
- : await getTemplateId ( arbitrableAddress , disputeID , publicClient ) ;
55
-
56
45
const { disputeTemplate } = await graphqlBatcher . fetch ( {
57
46
id : crypto . randomUUID ( ) ,
58
47
document : disputeTemplateQuery ,
59
- variables : { id : templateId . toString ( ) } ,
48
+ variables : { id : disputeData . dispute ?. templateId . toString ( ) } ,
60
49
isDisputeTemplate : true ,
61
50
} ) ;
62
51
@@ -66,10 +55,10 @@ export const usePopulatedDisputeData = (disputeID?: string, arbitrableAddress?:
66
55
const initialContext = {
67
56
disputeID : disputeID ,
68
57
arbitrableAddress : arbitrableAddress ,
69
- arbitrableChainID : isCrossChainDispute ? crossChainData . crossChainId . toString ( ) : DEFAULT_CHAIN . toString ( ) ,
58
+ arbitrableChainID : disputeData . dispute ?. arbitrableChainId ,
70
59
graphApiKey : import . meta. env . REACT_APP_GRAPH_API_KEY ,
71
60
alchemyApiKey : import . meta. env . ALCHEMY_API_KEY ,
72
- externalDisputeID : externalDisputeID ,
61
+ externalDisputeID : disputeData . dispute ?. externalDisputeId ,
73
62
} ;
74
63
75
64
const data = dataMappings ? await executeActions ( JSON . parse ( dataMappings ) , initialContext ) : { } ;
@@ -88,28 +77,3 @@ export const usePopulatedDisputeData = (disputeID?: string, arbitrableAddress?:
88
77
} ,
89
78
} ) ;
90
79
} ;
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