Skip to content

Commit b82ea0d

Browse files
committed
feat: refactor stat names, values, add homeblock query, add chart icon, add time selector, style
1 parent 441ec60 commit b82ea0d

File tree

7 files changed

+205
-191
lines changed

7 files changed

+205
-191
lines changed

web/src/assets/svgs/icons/chart.svg

+15
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { arbitrum, arbitrumSepolia } from "viem/chains";
2+
3+
export const averageBlockTimeInSeconds = { [arbitrum.id]: 0.26, [arbitrumSepolia.id]: 0.268 };

web/src/hooks/queries/useCourtAllTimeQuery.ts

-120
This file was deleted.

web/src/hooks/queries/useHomePageBlockQuery.ts

+77-44
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const homePageBlockQuery = graphql(`
1818
numberDisputes
1919
numberVotes
2020
feeForJuror
21-
stake
21+
effectiveStake
2222
}
2323
pastCourts: courts(orderBy: id, orderDirection: asc, block: { number: $blockNumber }) {
2424
id
@@ -29,7 +29,7 @@ const homePageBlockQuery = graphql(`
2929
numberDisputes
3030
numberVotes
3131
feeForJuror
32-
stake
32+
effectiveStake
3333
}
3434
}
3535
`);
@@ -54,31 +54,78 @@ export const useHomePageBlockQuery = (blockNumber: number | null, allTime: boole
5454

5555
const courtActivityStats = useMemo(() => {
5656
if (usedQuery.data && !usedQuery.isFetching) {
57-
// 1. court with most disputes
58-
// we only iterate through past courts, since more courts might exist at the present
59-
// these diffCourts have: average stakes, and dispute diff
6057
const diffCourts = allTime
61-
? usedQuery.data.presentCourts.map((c) => ({
62-
...c,
63-
numberDisputes: c.numberDisputes,
64-
treeNumberDisputes: c.numberDisputes,
65-
numberVotes: c.numberVotes,
66-
treeNumberVotes: c.numberVotes,
67-
stake: c.stake,
58+
? usedQuery.data.presentCourts.map((presentCourt) => ({
59+
...presentCourt,
60+
numberDisputes: presentCourt.numberDisputes,
61+
treeNumberDisputes: presentCourt.numberDisputes,
62+
numberVotes: presentCourt.numberVotes,
63+
treeNumberVotes: presentCourt.numberVotes,
64+
effectiveStake: presentCourt.effectiveStake,
65+
votesPerPnk: Number(presentCourt.numberVotes) / (Number(presentCourt.effectiveStake) / 1e18),
66+
treeVotesPerPnk: Number(presentCourt.numberVotes) / (Number(presentCourt.effectiveStake) / 1e18),
67+
disputesPerPnk: Number(presentCourt.numberDisputes) / (Number(presentCourt.effectiveStake) / 1e18),
68+
treeDisputesPerPnk: Number(presentCourt.numberDisputes) / (Number(presentCourt.effectiveStake) / 1e18),
6869
}))
69-
: usedQuery.data.pastCourts.map((c, i) => ({
70-
...c,
71-
numberDisputes: usedQuery.data.presentCourts[i].numberDisputes - c.numberDisputes,
72-
treeNumberDisputes: usedQuery.data.presentCourts[i].numberDisputes - c.numberDisputes,
73-
numberVotes: usedQuery.data.presentCourts[i].numberVotes - c.numberVotes,
74-
treeNumberVotes: usedQuery.data.presentCourts[i].numberVotes - c.numberVotes,
75-
stake: (BigInt(usedQuery.data.presentCourts[i].stake) + BigInt(c.stake)) / 2n,
76-
}));
77-
const mostDisputedCourt = diffCourts.sort((a, b) => b.numberDisputes - a.numberDisputes)[0];
78-
// 2. biggest chances of getting drawn
79-
// fact a: getting drawn in a parent court also subjects you to its rewards
80-
// fact b: staking in children, stakes in parents. but subgraph at this date doesn't reflect this
81-
// so, stakes trickle up, rewards/disputes trickle down
70+
: usedQuery.data.presentCourts.map((presentCourt) => {
71+
const pastCourt = usedQuery.data.pastCourts.find((pastCourt) => pastCourt.id === presentCourt.id);
72+
73+
return {
74+
...presentCourt,
75+
numberDisputes: pastCourt
76+
? presentCourt.numberDisputes - pastCourt.numberDisputes
77+
: presentCourt.numberDisputes,
78+
treeNumberDisputes: pastCourt
79+
? presentCourt.numberDisputes - pastCourt.numberDisputes
80+
: presentCourt.numberDisputes,
81+
numberVotes: pastCourt ? presentCourt.numberVotes - pastCourt.numberVotes : presentCourt.numberVotes,
82+
treeNumberVotes: pastCourt ? presentCourt.numberVotes - pastCourt.numberVotes : presentCourt.numberVotes,
83+
effectiveStake: pastCourt
84+
? (BigInt(presentCourt.effectiveStake) + BigInt(pastCourt.effectiveStake)) / 2n
85+
: presentCourt.effectiveStake,
86+
votesPerPnk:
87+
Number(pastCourt ? presentCourt.numberVotes - pastCourt.numberVotes : presentCourt.numberVotes) /
88+
(Number(
89+
pastCourt
90+
? (BigInt(presentCourt.effectiveStake) + BigInt(pastCourt.effectiveStake)) / 2n
91+
: presentCourt.effectiveStake
92+
) /
93+
1e18),
94+
treeVotesPerPnk:
95+
Number(pastCourt ? presentCourt.numberVotes - pastCourt.numberVotes : presentCourt.numberVotes) /
96+
(Number(
97+
pastCourt
98+
? (BigInt(presentCourt.effectiveStake) + BigInt(pastCourt.effectiveStake)) / 2n
99+
: presentCourt.effectiveStake
100+
) /
101+
1e18),
102+
disputesPerPnk:
103+
Number(
104+
pastCourt ? presentCourt.numberDisputes - pastCourt.numberDisputes : presentCourt.numberDisputes
105+
) /
106+
(Number(
107+
pastCourt
108+
? (BigInt(presentCourt.effectiveStake) + BigInt(pastCourt.effectiveStake)) / 2n
109+
: presentCourt.effectiveStake
110+
) /
111+
1e18),
112+
treeDisputesPerPnk:
113+
Number(
114+
pastCourt ? presentCourt.numberDisputes - pastCourt.numberDisputes : presentCourt.numberDisputes
115+
) /
116+
(Number(
117+
pastCourt
118+
? (BigInt(presentCourt.effectiveStake) + BigInt(pastCourt.effectiveStake)) / 2n
119+
: presentCourt.effectiveStake
120+
) /
121+
1e18),
122+
};
123+
});
124+
125+
const mostDisputedCourt = diffCourts.toSorted((a, b) => b.numberDisputes - a.numberDisputes)[0];
126+
// 1. biggest chances of getting drawn
127+
// fact: getting drawn in a parent court also subjects you to its rewards
128+
// so, rewards/disputes trickle down
82129

83130
for (const parent of diffCourts) {
84131
for (const child of diffCourts) {
@@ -88,21 +135,7 @@ export const useHomePageBlockQuery = (blockNumber: number | null, allTime: boole
88135
}
89136
}
90137
}
91-
diffCourts.reverse();
92-
for (const child of diffCourts) {
93-
for (const parent of diffCourts) {
94-
if (parent.id === child.parent?.id) {
95-
parent.stake = String(BigInt(parent.stake) + BigInt(child.stake));
96-
}
97-
}
98-
}
99-
diffCourts.reverse();
100-
for (const c of diffCourts) {
101-
c.votesPerPnk = Number(c.numberVotes) / (Number(c.stake) / 1e18);
102-
c.treeVotesPerPnk = c.votesPerPnk;
103-
c.disputesPerPnk = Number(c.numberDisputes) / (Number(c.stake) / 1e18);
104-
c.treeDisputesPerPnk = c.disputesPerPnk;
105-
}
138+
106139
for (const parent of diffCourts) {
107140
for (const child of diffCourts) {
108141
if (parent.id === child.parent?.id) {
@@ -111,8 +144,8 @@ export const useHomePageBlockQuery = (blockNumber: number | null, allTime: boole
111144
}
112145
}
113146
}
114-
const bestDrawingChancesCourt = diffCourts.sort((a, b) => b.treeVotesPerPnk - a.treeVotesPerPnk)[0];
115-
// 3. expected reward
147+
const bestDrawingChancesCourt = diffCourts.toSorted((a, b) => b.treeVotesPerPnk - a.treeVotesPerPnk)[0];
148+
// 2. expected reward
116149
// since we isolated the exclusive disputes from the cumulative disputes
117150
// we can calculate the "isolated reward" of every court
118151
// after that's done, then just trickle the rewards down
@@ -128,11 +161,11 @@ export const useHomePageBlockQuery = (blockNumber: number | null, allTime: boole
128161
}
129162
}
130163
}
131-
const bestExpectedRewardCourt = diffCourts.sort(
164+
const bestExpectedRewardCourt = diffCourts.toSorted(
132165
(a, b) => b.treeExpectedRewardPerPnk - a.treeExpectedRewardPerPnk
133166
)[0];
134167

135-
return { mostDisputedCourt, bestDrawingChancesCourt, bestExpectedRewardCourt };
168+
return { mostDisputedCourt, bestDrawingChancesCourt, bestExpectedRewardCourt, diffCourts };
136169
} else {
137170
return undefined;
138171
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useEffect, useState } from "react";
2+
import { DEFAULT_CHAIN } from "consts/chains";
3+
import { useHomePageBlockQuery } from "./useHomePageBlockQuery";
4+
import { useBlockNumber } from "wagmi";
5+
import { averageBlockTimeInSeconds } from "consts/averageBlockTimeInSeconds";
6+
7+
export const useHomePageExtraStats = (days: number | string) => {
8+
const [pastBlockNumber, setPastBlockNumber] = useState<number>();
9+
const currentBlockNumber = useBlockNumber({ chainId: DEFAULT_CHAIN });
10+
11+
useEffect(() => {
12+
if (typeof days !== "string" && currentBlockNumber?.data) {
13+
const timeInBlocks = Math.floor((days * 24 * 3600) / averageBlockTimeInSeconds[DEFAULT_CHAIN]);
14+
setPastBlockNumber(Number(currentBlockNumber.data) - timeInBlocks);
15+
}
16+
}, [DEFAULT_CHAIN, currentBlockNumber, days]);
17+
18+
const data = useHomePageBlockQuery(pastBlockNumber, days === "allTime");
19+
20+
return data;
21+
};

0 commit comments

Comments
 (0)