@@ -10,6 +10,8 @@ import { klerosCoreAbi, klerosCoreAddress } from "hooks/contracts/generated";
1010import useTransactionBatcher , { type TransactionBatcherConfig } from "hooks/useTransactionBatcher" ;
1111import { wrapWithToast } from "utils/wrapWithToast" ;
1212
13+ import useDisputeMaintenanceQuery from "queries/useDisputeMaintenanceQuery" ;
14+
1315import { Period } from "src/graphql/graphql" ;
1416import { isUndefined } from "src/utils" ;
1517
@@ -20,19 +22,26 @@ const StyledButton = styled(Button)`
2022` ;
2123
2224interface IDistributeRewards extends IBaseMaintenanceButton {
23- numberOfVotes ?: string ;
2425 roundIndex ?: string ;
2526 period ?: string ;
2627}
2728
28- const DistributeRewards : React . FC < IDistributeRewards > = ( { id, numberOfVotes , roundIndex, setIsOpen, period } ) => {
29+ const DistributeRewards : React . FC < IDistributeRewards > = ( { id, roundIndex, setIsOpen, period } ) => {
2930 const [ isSending , setIsSending ] = useState ( false ) ;
3031 const [ contractConfigs , setContractConfigs ] = useState < TransactionBatcherConfig > ( ) ;
3132 const publicClient = usePublicClient ( ) ;
3233 const { chainId } = useAccount ( ) ;
3334
35+ const { data : maintenanceData } = useDisputeMaintenanceQuery ( id ) ;
36+
37+ const rewardsDispersed = useMemo (
38+ ( ) => maintenanceData ?. dispute ?. rounds . every ( ( round ) => round . jurorRewardsDispersed ) ,
39+ [ maintenanceData ]
40+ ) ;
41+
3442 useEffect ( ( ) => {
35- if ( ! id || ! roundIndex || ! numberOfVotes ) return ;
43+ const rounds = maintenanceData ?. dispute ?. rounds ;
44+ if ( ! id || ! roundIndex || ! rounds ) return ;
3645
3746 const baseArgs = {
3847 abi : klerosCoreAbi ,
@@ -41,40 +50,37 @@ const DistributeRewards: React.FC<IDistributeRewards> = ({ id, numberOfVotes, ro
4150 } ;
4251
4352 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 ) ] } ) ;
4953
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 ) ] } ) ;
5156 }
5257
5358 setContractConfigs ( argsArr ) ;
54- } , [ id , roundIndex , numberOfVotes , chainId ] ) ;
59+ } , [ id , roundIndex , chainId , maintenanceData ] ) ;
5560
5661 const {
5762 executeBatch,
5863 isLoading : isLoadingConfig ,
5964 isError,
6065 } = useTransactionBatcher ( contractConfigs , {
61- enabled : ! isUndefined ( period ) && period === Period . Execution ,
66+ enabled : ! isUndefined ( period ) && period === Period . Execution && ! rewardsDispersed ,
6267 } ) ;
6368
6469 const isLoading = useMemo ( ( ) => isLoadingConfig || isSending , [ isLoadingConfig , isSending ] ) ;
6570 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 ]
6873 ) ;
6974
7075 const handleClick = ( ) => {
76+ if ( ! publicClient ) return ;
7177 setIsSending ( true ) ;
7278
7379 wrapWithToast ( async ( ) => await executeBatch ( ) , publicClient ) . finally ( ( ) => {
7480 setIsOpen ( false ) ;
7581 } ) ;
7682 } ;
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 } /> ;
7884} ;
7985
8086export default DistributeRewards ;
0 commit comments