-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathDistributeRewards.tsx
91 lines (70 loc) · 2.9 KB
/
DistributeRewards.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, { useEffect, useMemo, useState } from "react";
import styled from "styled-components";
import { useAccount, usePublicClient } from "wagmi";
import { Button } from "@kleros/ui-components-library";
import { DEFAULT_CHAIN } from "consts/chains";
import { klerosCoreAbi, klerosCoreAddress } from "hooks/contracts/generated";
import useTransactionBatcher, { type TransactionBatcherConfig } from "hooks/useTransactionBatcher";
import { wrapWithToast } from "utils/wrapWithToast";
import useDisputeMaintenanceQuery from "queries/useDisputeMaintenanceQuery";
import { Period } from "src/graphql/graphql";
import { isUndefined } from "src/utils";
import { IBaseMaintenanceButton } from ".";
const StyledButton = styled(Button)`
width: 100%;
`;
interface IDistributeRewards extends IBaseMaintenanceButton {
roundIndex?: string;
period?: string;
}
const DistributeRewards: React.FC<IDistributeRewards> = ({ id, roundIndex, setIsOpen, period }) => {
const [isSending, setIsSending] = useState(false);
const [contractConfigs, setContractConfigs] = useState<TransactionBatcherConfig>();
const publicClient = usePublicClient();
const { chainId } = useAccount();
const { data: maintenanceData } = useDisputeMaintenanceQuery(id);
const rewardsDispersed = useMemo(
() => maintenanceData?.dispute?.rounds.every((round) => round.jurorRewardsDispersed),
[maintenanceData]
);
useEffect(() => {
const rounds = maintenanceData?.dispute?.rounds;
if (isUndefined(id) || isUndefined(roundIndex) || isUndefined(rounds)) return;
const baseArgs = {
abi: klerosCoreAbi,
address: klerosCoreAddress[chainId ?? DEFAULT_CHAIN],
functionName: "execute",
};
const argsArr: TransactionBatcherConfig = [];
for (const round of rounds) {
argsArr.push({
...baseArgs,
args: [BigInt(id), BigInt(round.id.split("-")[1]), BigInt(round.nbVotes) * BigInt(2)],
});
}
setContractConfigs(argsArr);
}, [id, roundIndex, chainId, maintenanceData]);
const {
executeBatch,
batchConfig,
isLoading: isLoadingConfig,
isError,
} = useTransactionBatcher(contractConfigs, {
enabled: !isUndefined(period) && period === Period.Execution && !rewardsDispersed,
});
const isLoading = useMemo(() => isLoadingConfig || isSending, [isLoadingConfig, isSending]);
const isDisabled = useMemo(
() => isUndefined(id) || isError || isLoading || period !== Period.Execution || rewardsDispersed,
[id, isError, isLoading, period, rewardsDispersed]
);
const handleClick = () => {
if (!publicClient || !batchConfig) return;
setIsSending(true);
wrapWithToast(async () => await executeBatch(batchConfig), publicClient).finally(() => {
setIsSending(false);
setIsOpen(false);
});
};
return <StyledButton text="Juror Rewards" small isLoading={isLoading} disabled={isDisabled} onClick={handleClick} />;
};
export default DistributeRewards;