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,62 @@ 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 = 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
+
19
59
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
+
20
64
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
+ </ >
28
87
) ;
29
88
} ;
30
89
0 commit comments