1
- import React , { useMemo , useState } from "react" ;
1
+ import React , { useEffect , useMemo , useState } from "react" ;
2
2
import styled from "styled-components" ;
3
3
4
- import { usePublicClient } from "wagmi" ;
4
+ import { useAccount , usePublicClient } from "wagmi" ;
5
5
6
6
import { Button } from "@kleros/ui-components-library" ;
7
7
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" ;
9
11
import { wrapWithToast } from "utils/wrapWithToast" ;
10
12
11
13
import { isUndefined } from "src/utils" ;
@@ -23,32 +25,48 @@ interface IDistributeRewards extends IBaseMaintenaceButton {
23
25
24
26
const DistributeRewards : React . FC < IDistributeRewards > = ( { id, numberOfVotes, roundIndex, setIsOpen } ) => {
25
27
const [ isSending , setIsSending ] = useState ( false ) ;
28
+ const [ contractConfigs , setContractConfigs ] = useState < TransactionBatcherConfig > ( ) ;
26
29
const publicClient = usePublicClient ( ) ;
30
+ const { chainId } = useAccount ( ) ;
27
31
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 ;
38
34
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 ( ) ;
40
57
41
58
const isLoading = useMemo ( ( ) => isLoadingConfig || isSending , [ isLoadingConfig , isSending ] ) ;
42
59
const isDisabled = useMemo (
43
60
( ) => isUndefined ( id ) || isUndefined ( numberOfVotes ) || isError || isLoading ,
44
61
[ id , numberOfVotes , isError , isLoading ]
45
62
) ;
63
+
46
64
const handleClick = ( ) => {
47
- if ( ! executeConfig ) return ;
65
+ if ( ! batchConfig ) return ;
48
66
49
67
setIsSending ( true ) ;
50
68
51
- wrapWithToast ( async ( ) => await execute ( executeConfig . request ) , publicClient ) . finally ( ( ) => {
69
+ wrapWithToast ( async ( ) => await executeBatch ( batchConfig . request ) , publicClient ) . finally ( ( ) => {
52
70
setIsOpen ( false ) ;
53
71
} ) ;
54
72
} ;
0 commit comments