Skip to content

Commit 5ae9545

Browse files
authored
Merge pull request #1009 from kleros/fix(web)/dashboard-juror-rewards
fix(web): dashboard-juror-rewards
2 parents a2b2753 + 30e6d26 commit 5ae9545

File tree

4 files changed

+95
-12
lines changed

4 files changed

+95
-12
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import useSWR from "swr";
2+
import { gql } from "graphql-request";
3+
4+
const jurorRewardsQuery = gql`
5+
query JurorStakedCourtsQuery($address: ID!) {
6+
user(id: $address) {
7+
shifts {
8+
tokenAmount
9+
ethAmount
10+
}
11+
}
12+
}
13+
`;
14+
15+
export const useJurorRewardsQuery = (address: string): { data: typeof result; error: any; isValidating: boolean } => {
16+
const { data, error, isValidating } = useSWR({
17+
query: jurorRewardsQuery,
18+
variables: { address },
19+
});
20+
21+
const result = data ? data : undefined;
22+
return { data: result, error, isValidating };
23+
};

web/src/pages/Dashboard/JurorInfo/JurorRewards.tsx

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import React from "react";
22
import styled from "styled-components";
3+
import { formatUnits, formatEther } from "viem";
4+
import { useAccount } from "wagmi";
5+
import { KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS } from "src/consts/index";
36
import TokenRewards from "./TokenRewards";
47
import WithHelpTooltip from "../WithHelpTooltip";
8+
import { isUndefined } from "utils/index";
9+
import { useJurorRewardsQuery } from "hooks/queries/useJurorRewardsQuery";
10+
import { useCoinPrice } from "hooks/useCoinPrice";
511

12+
interface IReward {
13+
token: "ETH" | "PNK";
14+
coinId: number;
15+
getAmount: (amount: bigint) => string;
16+
getValue: (amount: bigint, coinPrice?: number) => string;
17+
}
618
const Container = styled.div`
719
display: flex;
820
flex-direction: column;
@@ -16,15 +28,62 @@ const tooltipMsg =
1628
"is coherent with the final ruling receive the Juror Rewards composed of " +
1729
"arbitration fees (ETH) + PNK redistribution between jurors.";
1830

31+
const coinIdToAddress = {
32+
0: KLEROS_CONTRACT_ADDRESS,
33+
1: WETH_CONTRACT_ADDRESS,
34+
};
35+
36+
const rewards: IReward[] = [
37+
{
38+
token: "ETH",
39+
coinId: 1,
40+
getAmount: (amount) => Number(formatEther(amount)).toFixed(3).toString(),
41+
getValue: (amount, coinPrice) => (Number(formatEther(amount)) * (coinPrice ?? 0)).toFixed(2).toString(),
42+
},
43+
{
44+
token: "PNK",
45+
coinId: 0,
46+
getAmount: (amount) => Number(formatUnits(amount, 18)).toFixed(3).toString(),
47+
getValue: (amount, coinPrice) => (Number(formatUnits(amount, 18)) * (coinPrice ?? 0)).toFixed(2).toString(),
48+
},
49+
];
50+
51+
const calculateTotalReward = (coinId: number, data: any) => {
52+
const total = data?.user.shifts
53+
.map((shift) => parseInt(coinId === 0 ? shift.tokenAmount : shift.ethAmount))
54+
.reduce((acc, curr) => acc + curr, 0);
55+
56+
return total;
57+
};
58+
1959
const Coherency: React.FC = () => {
60+
const { address } = useAccount();
61+
const { data } = useJurorRewardsQuery((address ?? "").toLowerCase());
62+
const { prices: pricesData } = useCoinPrice([KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS]);
63+
2064
return (
21-
<Container>
22-
<WithHelpTooltip place="bottom" {...{ tooltipMsg }}>
23-
<label> Juror Rewards </label>
24-
</WithHelpTooltip>
25-
<TokenRewards token="ETH" amount="3.66" value="208,783" />
26-
<TokenRewards token="PNK" amount="56,000" value="20,783" />
27-
</Container>
65+
<>
66+
{address && (
67+
<Container>
68+
<WithHelpTooltip place="bottom" {...{ tooltipMsg }}>
69+
<label> Juror Rewards </label>
70+
</WithHelpTooltip>
71+
72+
{rewards.map(({ token, coinId, getValue, getAmount }) => {
73+
const coinPrice = !isUndefined(pricesData) ? pricesData[coinIdToAddress[coinId]]?.price : undefined;
74+
const totalReward = calculateTotalReward(coinId, data);
75+
return (
76+
<TokenRewards
77+
key={coinId}
78+
{...{ token }}
79+
amount={data ? getAmount(totalReward) : "Fetching..."}
80+
value={data ? getValue(totalReward, coinPrice) : "Fetching..."}
81+
/>
82+
);
83+
})}
84+
</Container>
85+
)}
86+
</>
2887
);
2988
};
3089

web/src/pages/Home/index.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import CourtOverview from "./CourtOverview";
44
import LatestCases from "./LatestCases";
55
import Community from "./Community";
66
import { HomePageProvider } from "hooks/useHomePageContext";
7+
import { getOneYearAgoTimestamp } from "utils/date";
78

89
const Container = styled.div`
910
width: 100%;
@@ -12,11 +13,6 @@ const Container = styled.div`
1213
padding: 32px;
1314
`;
1415

15-
const getOneYearAgoTimestamp: () => number = () => {
16-
const currentTime = new Date().getTime() / 1000;
17-
return currentTime - 31556926; // One year in seconds
18-
};
19-
2016
const Home: React.FC = () => (
2117
<HomePageProvider timeframe={getOneYearAgoTimestamp()}>
2218
<Container>

web/src/utils/date.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ export function secondsToDayHourMinute(seconds: number): string {
1414
const s = seconds % 60;
1515
return d > 0 ? `${d}d ${h}h ${m}m` : `${h}h ${m}m ${s}s`;
1616
}
17+
18+
export function getOneYearAgoTimestamp(): number {
19+
const currentTime = new Date().getTime() / 1000;
20+
return currentTime - 31536000; // One year in seconds
21+
}

0 commit comments

Comments
 (0)