@@ -10,6 +10,8 @@ import { klerosCoreAbi, klerosCoreAddress } from "hooks/contracts/generated";
10
10
import useTransactionBatcher , { type TransactionBatcherConfig } from "hooks/useTransactionBatcher" ;
11
11
import { wrapWithToast } from "utils/wrapWithToast" ;
12
12
13
+ import useDisputeMaintenanceQuery from "queries/useDisputeMaintenanceQuery" ;
14
+
13
15
import { Period } from "src/graphql/graphql" ;
14
16
import { isUndefined } from "src/utils" ;
15
17
@@ -20,19 +22,26 @@ const StyledButton = styled(Button)`
20
22
` ;
21
23
22
24
interface IDistributeRewards extends IBaseMaintenanceButton {
23
- numberOfVotes ?: string ;
24
25
roundIndex ?: string ;
25
26
period ?: string ;
26
27
}
27
28
28
- const DistributeRewards : React . FC < IDistributeRewards > = ( { id, numberOfVotes , roundIndex, setIsOpen, period } ) => {
29
+ const DistributeRewards : React . FC < IDistributeRewards > = ( { id, roundIndex, setIsOpen, period } ) => {
29
30
const [ isSending , setIsSending ] = useState ( false ) ;
30
31
const [ contractConfigs , setContractConfigs ] = useState < TransactionBatcherConfig > ( ) ;
31
32
const publicClient = usePublicClient ( ) ;
32
33
const { chainId } = useAccount ( ) ;
33
34
35
+ const { data : maintenanceData } = useDisputeMaintenanceQuery ( id ) ;
36
+
37
+ const rewardsDispersed = useMemo (
38
+ ( ) => maintenanceData ?. dispute ?. rounds . every ( ( round ) => round . jurorRewardsDispersed ) ,
39
+ [ maintenanceData ]
40
+ ) ;
41
+
34
42
useEffect ( ( ) => {
35
- if ( ! id || ! roundIndex || ! numberOfVotes ) return ;
43
+ const rounds = maintenanceData ?. dispute ?. rounds ;
44
+ if ( ! id || ! roundIndex || ! rounds ) return ;
36
45
37
46
const baseArgs = {
38
47
abi : klerosCoreAbi ,
@@ -41,40 +50,37 @@ const DistributeRewards: React.FC<IDistributeRewards> = ({ id, numberOfVotes, ro
41
50
} ;
42
51
43
52
const argsArr : TransactionBatcherConfig = [ ] ;
44
- let nbVotes = parseInt ( numberOfVotes ) ;
45
-
46
- // each previous round has (n - 1)/2 jurors
47
- for ( let i = parseInt ( roundIndex ) ; i >= 0 ; i -- ) {
48
- argsArr . push ( { ...baseArgs , args : [ BigInt ( id ) , BigInt ( i ) , BigInt ( nbVotes ) ] } ) ;
49
53
50
- nbVotes = ( nbVotes - 1 ) / 2 ;
54
+ for ( const round of rounds ) {
55
+ argsArr . push ( { ...baseArgs , args : [ BigInt ( id ) , BigInt ( round . id . split ( "-" ) [ 1 ] ) , BigInt ( round . nbVotes ) ] } ) ;
51
56
}
52
57
53
58
setContractConfigs ( argsArr ) ;
54
- } , [ id , roundIndex , numberOfVotes , chainId ] ) ;
59
+ } , [ id , roundIndex , chainId , maintenanceData ] ) ;
55
60
56
61
const {
57
62
executeBatch,
58
63
isLoading : isLoadingConfig ,
59
64
isError,
60
65
} = useTransactionBatcher ( contractConfigs , {
61
- enabled : ! isUndefined ( period ) && period === Period . Execution ,
66
+ enabled : ! isUndefined ( period ) && period === Period . Execution && ! rewardsDispersed ,
62
67
} ) ;
63
68
64
69
const isLoading = useMemo ( ( ) => isLoadingConfig || isSending , [ isLoadingConfig , isSending ] ) ;
65
70
const isDisabled = useMemo (
66
- ( ) => isUndefined ( id ) || isUndefined ( numberOfVotes ) || isError || isLoading || period !== Period . Execution ,
67
- [ id , numberOfVotes , isError , isLoading , period ]
71
+ ( ) => isUndefined ( id ) || isError || isLoading || period !== Period . Execution || rewardsDispersed ,
72
+ [ id , isError , isLoading , period , rewardsDispersed ]
68
73
) ;
69
74
70
75
const handleClick = ( ) => {
76
+ if ( ! publicClient ) return ;
71
77
setIsSending ( true ) ;
72
78
73
79
wrapWithToast ( async ( ) => await executeBatch ( ) , publicClient ) . finally ( ( ) => {
74
80
setIsOpen ( false ) ;
75
81
} ) ;
76
82
} ;
77
- return < StyledButton text = "Rewards" small isLoading = { isLoading } disabled = { isDisabled } onClick = { handleClick } /> ;
83
+ return < StyledButton text = "Juror Rewards" small isLoading = { isLoading } disabled = { isDisabled } onClick = { handleClick } /> ;
78
84
} ;
79
85
80
86
export default DistributeRewards ;
0 commit comments