1
1
import React from "react" ;
2
2
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" ;
3
6
import TokenRewards from "./TokenRewards" ;
4
7
import WithHelpTooltip from "../WithHelpTooltip" ;
8
+ import { isUndefined } from "utils/index" ;
9
+ import { useJurorRewardsQuery } from "hooks/queries/useJurorRewardsQuery" ;
10
+ import { useCoinPrice } from "hooks/useCoinPrice" ;
5
11
12
+ interface IReward {
13
+ token : "ETH" | "PNK" ;
14
+ coinId : number ;
15
+ getAmount : ( amount : bigint ) => string ;
16
+ getValue : ( amount : bigint , coinPrice ?: number ) => string ;
17
+ }
6
18
const Container = styled . div `
7
19
display: flex;
8
20
flex-direction: column;
@@ -16,15 +28,63 @@ const tooltipMsg =
16
28
"is coherent with the final ruling receive the Juror Rewards composed of " +
17
29
"arbitration fees (ETH) + PNK redistribution between jurors." ;
18
30
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 =
53
+ data &&
54
+ data . user . shifts
55
+ . map ( ( shift ) => parseInt ( coinId === 0 ? shift . tokenAmount : shift . ethAmount ) )
56
+ . reduce ( ( acc , curr ) => acc + curr , 0 ) ;
57
+ return total ;
58
+ } ;
59
+
19
60
const Coherency : React . FC = ( ) => {
61
+ const { address } = useAccount ( ) ;
62
+ const { data } = useJurorRewardsQuery ( ( address ?? "" ) . toLowerCase ( ) ) ;
63
+ const { prices : pricesData } = useCoinPrice ( [ KLEROS_CONTRACT_ADDRESS , WETH_CONTRACT_ADDRESS ] ) ;
64
+
20
65
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 >
66
+ < >
67
+ { address && (
68
+ < Container >
69
+ < WithHelpTooltip place = "bottom" { ...{ tooltipMsg } } >
70
+ < label > Juror Rewards </ label >
71
+ </ WithHelpTooltip >
72
+
73
+ { rewards . map ( ( { token, coinId, getValue, getAmount } , i ) => {
74
+ const coinPrice = ! isUndefined ( pricesData ) ? pricesData [ coinIdToAddress [ coinId ! ] ] ?. price : undefined ;
75
+ const totalReward = calculateTotalReward ( coinId , data ) ;
76
+ return (
77
+ < TokenRewards
78
+ key = { i }
79
+ { ...{ token } }
80
+ amount = { data ? getAmount ( totalReward ) : "Fetching..." }
81
+ value = { data ? getValue ( totalReward , coinPrice ) : "Fetching..." }
82
+ />
83
+ ) ;
84
+ } ) }
85
+ </ Container >
86
+ ) }
87
+ </ >
28
88
) ;
29
89
} ;
30
90
0 commit comments