Skip to content

Commit 8298bfa

Browse files
committedDec 20, 2018
feat: new contract views and charts
1 parent 8426251 commit 8298bfa

File tree

4 files changed

+122
-27
lines changed

4 files changed

+122
-27
lines changed
 

‎.env

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
REACT_APP_KLEROS_LIQUID_KOVAN_ADDRESS=0xa80116b49125f8a7466e995d46a4abdb0d98a199
2-
REACT_APP_PINAKION_KOVAN_ADDRESS=0x35c3F474d9d1B72D918b556F92ba934EA30E559D
1+
REACT_APP_KLEROS_LIQUID_KOVAN_ADDRESS=0x5f34bd5da181189f897f41ae1dae3ea108d85b91
2+
REACT_APP_PINAKION_KOVAN_ADDRESS=0x1ee318dbc19267dbce08f54a66ab198f73ede356
33
REACT_APP_POLICY_REGISTRY_KOVAN_ADDRESS=0x55aa06760990a6418a89d2177de367fe1cb5c3cb
44
REACT_APP_WEB3_FALLBACK_URL=wss://mainnet.infura.io/ws

‎src/components/eth-amount.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ const ETHAmount = ({ amount }) => {
2222
}
2323

2424
ETHAmount.propTypes = {
25-
amount: PropTypes.string
25+
amount: PropTypes.oneOfType([
26+
PropTypes.string.isRequired,
27+
PropTypes.number.isRequired,
28+
PropTypes.object.isRequired
29+
])
2630
}
2731

2832
ETHAmount.defaultProps = {

‎src/components/pie-chart.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ import ReactMinimalPieChart from 'react-minimal-pie-chart'
44
import styled from 'styled-components/macro'
55

66
const StyledDiv = styled.div`
7+
padding: 65px 45px 45px;
78
position: relative;
89
`
10+
const StyledTitleDiv = styled.div`
11+
font-weight: medium;
12+
left: 50%;
13+
position: absolute;
14+
top: 20px;
15+
transform: translateX(-50%);
16+
`
917
const StyledTooltipDiv = styled.div.attrs(({ x, y }) => ({
1018
style: { left: `${x}px`, top: `${y - 60}px` }
1119
}))`
@@ -15,8 +23,9 @@ const StyledTooltipDiv = styled.div.attrs(({ x, y }) => ({
1523
padding: 10px 8px;
1624
position: absolute;
1725
white-space: nowrap;
26+
z-index: 1;
1827
`
19-
const PieChart = ({ data }) => {
28+
const PieChart = ({ data, title }) => {
2029
const [state, setState] = useState({ dataIndex: null, x: null, y: null })
2130
const onMouseMove = useCallback(event => {
2231
const bounds = event.currentTarget.getBoundingClientRect()
@@ -35,6 +44,7 @@ const PieChart = ({ data }) => {
3544
const inPie = state.dataIndex !== null
3645
return (
3746
<StyledDiv onMouseMove={inPie ? onMouseMove : undefined}>
47+
<StyledTitleDiv>{title}</StyledTitleDiv>
3848
<ReactMinimalPieChart
3949
className="ReactMinimalPieChart"
4050
data={data}
@@ -61,7 +71,8 @@ PieChart.propTypes = {
6171
tooltip: PropTypes.node.isRequired,
6272
value: PropTypes.number.isRequired
6373
}).isRequired
64-
).isRequired
74+
).isRequired,
75+
title: PropTypes.node.isRequired
6576
}
6677

6778
export default PieChart

‎src/components/pnk-stats-list-card.js

+102-22
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import styled from 'styled-components/macro'
77
import { useDataloader } from '../bootstrap/dataloader'
88
import { useDrizzle } from '../temp/drizzle-react-hooks'
99

10+
const loadingPieChartData = [{ tooltip: 'Loading...', value: 1 }]
11+
const StyledDiv = styled.div`
12+
display: flex;
13+
`
1014
const StyledAmountSpan = styled.span`
1115
font-weight: bold;
1216
`
1317
const StyledTitleSpan = styled.span`
1418
font-style: italic;
1519
`
1620
const PNKStatsListCard = () => {
17-
const { cacheCall, drizzleState } = useDrizzle()
21+
const { cacheCall, drizzle, drizzleState, useCacheEvents } = useDrizzle()
1822
const load = useDataloader()
1923
const subcourtIDs = cacheCall(
2024
'KlerosLiquid',
@@ -38,28 +42,104 @@ const PNKStatsListCard = () => {
3842
}
3943
return subcourt
4044
})
41-
const loading = !subcourts || subcourts.some(s => s.stake === undefined)
45+
const loadingSubcourts =
46+
!subcourts || subcourts.some(s => s.stake === undefined)
47+
const draws = useCacheEvents(
48+
'KlerosLiquid',
49+
'Draw',
50+
useMemo(
51+
() => ({
52+
filter: { _address: drizzleState.accounts[0] },
53+
fromBlock: 0
54+
}),
55+
[drizzleState.accounts[0]]
56+
)
57+
)
58+
const disputes = draws
59+
? draws.reduce(
60+
(acc, d) => {
61+
if (acc.atStakePerVoteByID[d.returnValues._disputeID] === undefined) {
62+
acc.atStakePerVoteByID[d.returnValues._disputeID] = null
63+
const dispute = cacheCall(
64+
'KlerosLiquid',
65+
'disputes',
66+
d.returnValues._disputeID
67+
)
68+
const dispute2 = cacheCall(
69+
'KlerosLiquid',
70+
'getDispute',
71+
d.returnValues._disputeID
72+
)
73+
if (dispute && dispute2) {
74+
if (!dispute.period !== '4') {
75+
acc.atStakePerVoteByID[
76+
d.returnValues._disputeID
77+
] = drizzle.web3.utils.toBN(
78+
dispute2.jurorAtStake[dispute2.jurorAtStake.length - 1]
79+
)
80+
acc.atStakeByID[
81+
d.returnValues._disputeID
82+
] = drizzle.web3.utils.toBN(0)
83+
}
84+
} else acc.loading = true
85+
}
86+
if (acc.atStakePerVoteByID[d.returnValues._disputeID] !== null)
87+
acc.atStakeByID[d.returnValues._disputeID] = acc.atStakeByID[
88+
d.returnValues._disputeID
89+
].add(acc.atStakePerVoteByID[d.returnValues._disputeID])
90+
return acc
91+
},
92+
{
93+
atStakeByID: {},
94+
atStakePerVoteByID: {},
95+
loading: false
96+
}
97+
)
98+
: { loading: true }
4299
return (
43-
<TitledListCard loading={loading} prefix="PNK" title="Stats">
44-
{!loading && (
45-
<PieChart
46-
data={useMemo(
47-
() =>
48-
subcourts.map(s => ({
49-
tooltip: (
50-
<Spin spinning={s.name === undefined}>
51-
<StyledAmountSpan>
52-
<ETHAmount amount={s.stake} /> PNK
53-
</StyledAmountSpan>
54-
<StyledTitleSpan> - {s.name || '...'}</StyledTitleSpan>
55-
</Spin>
56-
),
57-
value: Number(s.stake)
58-
})),
59-
[...subcourts.map(s => s.stake), ...subcourts.map(s => s.name)]
60-
)}
61-
/>
62-
)}
100+
<TitledListCard prefix="PNK" title="Stats">
101+
<StyledDiv>
102+
<Spin spinning={loadingSubcourts}>
103+
<PieChart
104+
data={
105+
loadingSubcourts
106+
? loadingPieChartData
107+
: subcourts.map(s => ({
108+
tooltip: (
109+
<Spin spinning={s.name === undefined}>
110+
<StyledAmountSpan>
111+
<ETHAmount amount={s.stake} /> PNK
112+
</StyledAmountSpan>
113+
<StyledTitleSpan> - {s.name || '...'}</StyledTitleSpan>
114+
</Spin>
115+
),
116+
value: Number(s.stake)
117+
}))
118+
}
119+
title="Staked Tokens"
120+
/>
121+
</Spin>
122+
<Spin spinning={disputes.loading}>
123+
<PieChart
124+
data={
125+
disputes.loading
126+
? loadingPieChartData
127+
: Object.entries(disputes.atStakeByID).map(s => ({
128+
tooltip: (
129+
<>
130+
<StyledAmountSpan>
131+
<ETHAmount amount={s[1]} /> PNK
132+
</StyledAmountSpan>
133+
<StyledTitleSpan> - Case {s[0]}</StyledTitleSpan>
134+
</>
135+
),
136+
value: Number(s[1])
137+
}))
138+
}
139+
title="Locked Tokens"
140+
/>
141+
</Spin>
142+
</StyledDiv>
63143
</TitledListCard>
64144
)
65145
}

0 commit comments

Comments
 (0)
Please sign in to comment.