Skip to content

Commit 32672d2

Browse files
committed
refactor(web): update-juror-rewards-button-logic
1 parent 1149b6f commit 32672d2

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

web/src/hooks/queries/useDisputeMaintenanceQuery.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const disputeMaintenance = graphql(`
1313
jurorsDrawn
1414
}
1515
rounds {
16+
id
1617
jurorRewardsDispersed
1718
nbVotes
1819
}

web/src/pages/Cases/CaseDetails/MaintenanceButtons/DistributeRewards.tsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { klerosCoreAbi, klerosCoreAddress } from "hooks/contracts/generated";
1010
import useTransactionBatcher, { type TransactionBatcherConfig } from "hooks/useTransactionBatcher";
1111
import { wrapWithToast } from "utils/wrapWithToast";
1212

13+
import useDisputeMaintenanceQuery from "queries/useDisputeMaintenanceQuery";
14+
1315
import { Period } from "src/graphql/graphql";
1416
import { isUndefined } from "src/utils";
1517

@@ -20,19 +22,26 @@ const StyledButton = styled(Button)`
2022
`;
2123

2224
interface 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

8086
export default DistributeRewards;

web/src/pages/Cases/CaseDetails/MaintenanceButtons/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ const MaintenanceButtons: React.FC = () => {
113113
<DistributeRewards
114114
{...{ id, setIsOpen }}
115115
roundIndex={dispute?.currentRoundIndex}
116-
numberOfVotes={dispute?.currentRound.nbVotes}
117116
period={dispute?.period}
118117
/>
119118
<ExecuteRulingButton {...{ id, setIsOpen }} period={dispute?.period} ruled={dispute?.ruled} />

0 commit comments

Comments
 (0)