1
1
import { useQuery } from "@tanstack/react-query" ;
2
2
3
3
import { useGraphqlBatcher } from "context/GraphqlBatcher" ;
4
+ import { useMemo } from "react" ;
4
5
5
6
import { graphql } from "src/graphql" ;
6
7
import { HomePageBlockQuery } from "src/graphql/graphql" ;
7
8
export type { HomePageBlockQuery } ;
8
9
9
10
const homePageBlockQuery = graphql ( `
10
11
query HomePageBlock($blockNumber: Int) {
11
- courts(orderBy: id, orderDirection: asc, block: { number: $blockNumber } ) {
12
+ presentCourts: courts(orderBy: id, orderDirection: asc) {
12
13
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
+ }
13
27
name
14
28
numberDisputes
15
29
feeForJuror
@@ -22,7 +36,7 @@ export const useHomePageBlockQuery = (blockNumber: number) => {
22
36
const isEnabled = blockNumber != null ;
23
37
const { graphqlBatcher } = useGraphqlBatcher ( ) ;
24
38
25
- return useQuery ( {
39
+ const usedQuery = useQuery ( {
26
40
queryKey : [ `homePageBlockQuery${ blockNumber } ` ] ,
27
41
enabled : isEnabled ,
28
42
queryFn : async ( ) => {
@@ -34,4 +48,79 @@ export const useHomePageBlockQuery = (blockNumber: number) => {
34
48
return data ;
35
49
} ,
36
50
} ) ;
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 ;
37
126
} ;
0 commit comments