1- import React , { useMemo , useState } from "react" ;
1+ import React , { useEffect , useMemo , useState } from "react" ;
22import styled from "styled-components" ;
33
4- import { usePublicClient } from "wagmi" ;
4+ import { useAccount , usePublicClient } from "wagmi" ;
55
66import { Button } from "@kleros/ui-components-library" ;
77
8- import { useSimulateKlerosCoreExecute , useWriteKlerosCoreExecute } from "hooks/contracts/generated" ;
8+ import { DEFAULT_CHAIN } from "consts/chains" ;
9+ import { klerosCoreAbi , klerosCoreAddress } from "hooks/contracts/generated" ;
10+ import useTransactionBatcher , { type TransactionBatcherConfig , useBatchWrite } from "hooks/useTransactionBatcher" ;
911import { wrapWithToast } from "utils/wrapWithToast" ;
1012
1113import { isUndefined } from "src/utils" ;
@@ -23,32 +25,48 @@ interface IDistributeRewards extends IBaseMaintenaceButton {
2325
2426const DistributeRewards : React . FC < IDistributeRewards > = ( { id, numberOfVotes, roundIndex, setIsOpen } ) => {
2527 const [ isSending , setIsSending ] = useState ( false ) ;
28+ const [ contractConfigs , setContractConfigs ] = useState < TransactionBatcherConfig > ( ) ;
2629 const publicClient = usePublicClient ( ) ;
30+ const { chainId } = useAccount ( ) ;
2731
28- const {
29- data : executeConfig ,
30- isLoading : isLoadingConfig ,
31- isError,
32- } = useSimulateKlerosCoreExecute ( {
33- query : {
34- enabled : ! isUndefined ( id ) && ! isUndefined ( numberOfVotes ) && ! isUndefined ( roundIndex ) ,
35- } ,
36- args : [ BigInt ( id ?? 0 ) , BigInt ( roundIndex ?? 0 ) , BigInt ( numberOfVotes ?? 0 ) ] ,
37- } ) ;
32+ useEffect ( ( ) => {
33+ if ( ! id || ! roundIndex || ! numberOfVotes ) return ;
3834
39- const { writeContractAsync : execute } = useWriteKlerosCoreExecute ( ) ;
35+ const baseArgs = {
36+ abi : klerosCoreAbi ,
37+ address : klerosCoreAddress [ chainId ?? DEFAULT_CHAIN ] ,
38+ functionName : "execute" ,
39+ } ;
40+
41+ const argsArr : TransactionBatcherConfig = [ ] ;
42+ let nbVotes = parseInt ( numberOfVotes ) ;
43+
44+ // each previous round has (n - 1)/2 jurors
45+ for ( let i = parseInt ( roundIndex ) ; i >= 0 ; i -- ) {
46+ argsArr . push ( { ...baseArgs , args : [ BigInt ( id ) , BigInt ( i ) , BigInt ( nbVotes ) ] } ) ;
47+
48+ nbVotes = ( nbVotes - 1 ) / 2 ;
49+ }
50+
51+ setContractConfigs ( argsArr ) ;
52+ } , [ id , roundIndex , numberOfVotes , chainId ] ) ;
53+
54+ const { batchConfig, isLoading : isLoadingConfig , isError } = useTransactionBatcher ( contractConfigs ) ;
55+
56+ const { writeContractAsync : executeBatch } = useBatchWrite ( ) ;
4057
4158 const isLoading = useMemo ( ( ) => isLoadingConfig || isSending , [ isLoadingConfig , isSending ] ) ;
4259 const isDisabled = useMemo (
4360 ( ) => isUndefined ( id ) || isUndefined ( numberOfVotes ) || isError || isLoading ,
4461 [ id , numberOfVotes , isError , isLoading ]
4562 ) ;
63+
4664 const handleClick = ( ) => {
47- if ( ! executeConfig ) return ;
65+ if ( ! batchConfig ) return ;
4866
4967 setIsSending ( true ) ;
5068
51- wrapWithToast ( async ( ) => await execute ( executeConfig . request ) , publicClient ) . finally ( ( ) => {
69+ wrapWithToast ( async ( ) => await executeBatch ( batchConfig . request ) , publicClient ) . finally ( ( ) => {
5270 setIsOpen ( false ) ;
5371 } ) ;
5472 } ;
0 commit comments