11import { useQuery } from "@tanstack/react-query" ;
22
33import { useGraphqlBatcher } from "context/GraphqlBatcher" ;
4+ import { useMemo } from "react" ;
45
56import { graphql } from "src/graphql" ;
67import { HomePageBlockQuery } from "src/graphql/graphql" ;
78export type { HomePageBlockQuery } ;
89
910const homePageBlockQuery = graphql ( `
1011 query HomePageBlock($blockNumber: Int) {
11- courts(orderBy: id, orderDirection: asc, block: { number: $blockNumber } ) {
12+ presentCourts: courts(orderBy: id, orderDirection: asc) {
1213 id
14+ parent {
15+ id
16+ }
17+ name
18+ numberDisputes
19+ feeForJuror
20+ stake
21+ }
22+ pastCourts: courts(orderBy: id, orderDirection: asc, block: { number: $blockNumber }) {
23+ id
24+ parent {
25+ id
26+ }
1327 name
1428 numberDisputes
1529 feeForJuror
@@ -22,7 +36,7 @@ export const useHomePageBlockQuery = (blockNumber: number) => {
2236 const isEnabled = blockNumber != null ;
2337 const { graphqlBatcher } = useGraphqlBatcher ( ) ;
2438
25- return useQuery ( {
39+ const usedQuery = useQuery ( {
2640 queryKey : [ `homePageBlockQuery${ blockNumber } ` ] ,
2741 enabled : isEnabled ,
2842 queryFn : async ( ) => {
@@ -34,4 +48,79 @@ export const useHomePageBlockQuery = (blockNumber: number) => {
3448 return data ;
3549 } ,
3650 } ) ;
51+
52+ const courtActivityStats = useMemo ( ( ) => {
53+ if ( usedQuery . data && ! usedQuery . isFetching ) {
54+ // 1. court with most disputes
55+ // we only iterate through past courts, since more courts might exist at the present
56+ // these diffCourts have: average stakes, and dispute diff
57+
58+ const diffCourts = usedQuery . data . pastCourts . map ( ( c , i ) => ( {
59+ ...c ,
60+ numberDisputes : usedQuery . data . presentCourts [ i ] . numberDisputes - c . numberDisputes ,
61+ treeNumberDisputes : usedQuery . data . presentCourts [ i ] . numberDisputes - c . numberDisputes ,
62+ stake : ( BigInt ( usedQuery . data . presentCourts [ i ] . stake ) + BigInt ( c . stake ) ) / 2n ,
63+ } ) ) ;
64+ const mostDisputedCourt = diffCourts . sort ( ( a , b ) => b . numberDisputes - a . numberDisputes ) [ 0 ] ;
65+ // 2. biggest chances of getting drawn
66+ // fact a: getting drawn in a parent court also subjects you to its rewards
67+ // fact b: staking in children, stakes in parents. but subgraph at this date doesn't reflect this
68+ // so, stakes trickle up, rewards/disputes trickle down
69+
70+ for ( const parent of diffCourts ) {
71+ for ( const child of diffCourts ) {
72+ if ( parent . id === child . parent ?. id ) {
73+ child . treeNumberDisputes = String ( Number ( parent . treeNumberDisputes ) + Number ( child . treeNumberDisputes ) ) ;
74+ }
75+ }
76+ }
77+ diffCourts . reverse ( ) ;
78+ for ( const child of diffCourts ) {
79+ for ( const parent of diffCourts ) {
80+ if ( parent . id === child . parent ?. id ) {
81+ parent . stake = String ( BigInt ( parent . stake ) + BigInt ( child . stake ) ) ;
82+ }
83+ }
84+ }
85+ diffCourts . reverse ( ) ;
86+ //
87+ for ( const c of diffCourts ) {
88+ c . disputesPerPnk = Number ( c . numberDisputes ) / ( Number ( c . stake ) / 1e18 ) ;
89+ c . treeDisputesPerPnk = c . disputesPerPnk ;
90+ }
91+ for ( const parent of diffCourts ) {
92+ for ( const child of diffCourts ) {
93+ if ( parent . id === child . parent ?. id ) {
94+ child . treeDisputesPerPnk += parent . disputesPerPnk ;
95+ }
96+ }
97+ }
98+ const bestDrawingChancesCourt = diffCourts . sort ( ( a , b ) => b . treeDisputesPerPnk - a . treeDisputesPerPnk ) [ 0 ] ;
99+ // 3. expected reward
100+ // since we isolated the exclusive disputes from the cumulative disputes
101+ // we can calculate the "isolated reward" of every court
102+ // after that's done, then just trickle the rewards down
103+
104+ for ( const c of diffCourts ) {
105+ c . expectedRewardPerPnk = c . disputesPerPnk * c . feeForJuror ;
106+ c . treeExpectedRewardPerPnk = c . expectedRewardPerPnk ;
107+ }
108+ for ( const parent of diffCourts ) {
109+ for ( const child of diffCourts ) {
110+ if ( parent . id === child . parent ?. id ) {
111+ child . treeExpectedRewardPerPnk = parent . treeExpectedRewardPerPnk + child . treeExpectedRewardPerPnk ;
112+ }
113+ }
114+ }
115+ const bestExpectedRewardCourt = diffCourts . sort (
116+ ( a , b ) => b . treeExpectedRewardPerPnk - a . treeExpectedRewardPerPnk
117+ ) [ 0 ] ;
118+
119+ return { mostDisputedCourt, bestDrawingChancesCourt, bestExpectedRewardCourt } ;
120+ } else {
121+ return undefined ;
122+ }
123+ } , [ usedQuery ] ) ;
124+
125+ return courtActivityStats ;
37126} ;
0 commit comments