|
| 1 | +import { useQuery } from "@tanstack/react-query"; |
| 2 | + |
| 3 | +import { useGraphqlBatcher } from "context/GraphqlBatcher"; |
| 4 | +import { useMemo } from "react"; |
| 5 | + |
| 6 | +import { graphql } from "src/graphql"; |
| 7 | +import { CourtAllTimeQuery } from "src/graphql/graphql"; |
| 8 | +export type { CourtAllTimeQuery }; |
| 9 | + |
| 10 | +const courtAllTimeQuery = graphql(` |
| 11 | + query CourtAllTime { |
| 12 | + presentCourts: courts(orderBy: id, orderDirection: asc) { |
| 13 | + id |
| 14 | + parent { |
| 15 | + id |
| 16 | + } |
| 17 | + name |
| 18 | + numberDisputes |
| 19 | + numberVotes |
| 20 | + feeForJuror |
| 21 | + stake |
| 22 | + } |
| 23 | + } |
| 24 | +`); |
| 25 | + |
| 26 | +export const useCourtAllTimeQuery = () => { |
| 27 | + const { graphqlBatcher } = useGraphqlBatcher(); |
| 28 | + |
| 29 | + const usedQuery = useQuery({ |
| 30 | + queryKey: [`courtAllTimeQuery`], |
| 31 | + staleTime: Infinity, |
| 32 | + queryFn: async () => { |
| 33 | + const data = await graphqlBatcher.fetch({ |
| 34 | + id: crypto.randomUUID(), |
| 35 | + document: courtAllTimeQuery, |
| 36 | + }); |
| 37 | + return data; |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const courtActivityStats = useMemo(() => { |
| 42 | + if (usedQuery.data && !usedQuery.isFetching) { |
| 43 | + // 1. court with most disputes |
| 44 | + // we only iterate through past courts, since more courts might exist at the present |
| 45 | + // these diffCourts have: average stakes, and dispute diff |
| 46 | + const diffCourts = usedQuery.data.presentCourts.map((c) => ({ |
| 47 | + ...c, |
| 48 | + numberDisputes: c.numberDisputes, |
| 49 | + treeNumberDisputes: c.numberDisputes, |
| 50 | + numberVotes: c.numberVotes, |
| 51 | + treeNumberVotes: c.numberVotes, |
| 52 | + stake: c.stake, |
| 53 | + })); |
| 54 | + |
| 55 | + const mostDisputedCourt = diffCourts.sort((a, b) => b.numberDisputes - a.numberDisputes)[0]; |
| 56 | + // 2. biggest chances of getting drawn |
| 57 | + // fact a: getting drawn in a parent court also subjects you to its rewards |
| 58 | + // fact b: staking in children, stakes in parents. but subgraph at this date doesn't reflect this |
| 59 | + // so, stakes trickle up, rewards/disputes trickle down |
| 60 | + |
| 61 | + for (const parent of diffCourts) { |
| 62 | + for (const child of diffCourts) { |
| 63 | + if (parent.id === child.parent?.id) { |
| 64 | + child.treeNumberVotes = String(Number(parent.treeNumberVotes) + Number(child.treeNumberVotes)); |
| 65 | + child.treeNumberDisputes = String(Number(parent.treeNumberDisputes) + Number(child.treeNumberDisputes)); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + diffCourts.reverse(); |
| 70 | + for (const child of diffCourts) { |
| 71 | + for (const parent of diffCourts) { |
| 72 | + if (parent.id === child.parent?.id) { |
| 73 | + parent.stake = String(BigInt(parent.stake) + BigInt(child.stake)); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + diffCourts.reverse(); |
| 78 | + for (const c of diffCourts) { |
| 79 | + c.votesPerPnk = Number(c.numberVotes) / (Number(c.stake) / 1e18); |
| 80 | + c.treeVotesPerPnk = c.votesPerPnk; |
| 81 | + c.disputesPerPnk = Number(c.numberDisputes) / (Number(c.stake) / 1e18); |
| 82 | + c.treeDisputesPerPnk = c.disputesPerPnk; |
| 83 | + } |
| 84 | + for (const parent of diffCourts) { |
| 85 | + for (const child of diffCourts) { |
| 86 | + if (parent.id === child.parent?.id) { |
| 87 | + child.treeVotesPerPnk += parent.votesPerPnk; |
| 88 | + child.treeDisputesPerPnk += parent.disputesPerPnk; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + const bestDrawingChancesCourt = diffCourts.sort((a, b) => b.treeVotesPerPnk - a.treeVotesPerPnk)[0]; |
| 93 | + // 3. expected reward |
| 94 | + // since we isolated the exclusive disputes from the cumulative disputes |
| 95 | + // we can calculate the "isolated reward" of every court |
| 96 | + // after that's done, then just trickle the rewards down |
| 97 | + |
| 98 | + for (const c of diffCourts) { |
| 99 | + c.expectedRewardPerPnk = c.votesPerPnk * c.feeForJuror; |
| 100 | + c.treeExpectedRewardPerPnk = c.expectedRewardPerPnk; |
| 101 | + } |
| 102 | + for (const parent of diffCourts) { |
| 103 | + for (const child of diffCourts) { |
| 104 | + if (parent.id === child.parent?.id) { |
| 105 | + child.treeExpectedRewardPerPnk = parent.treeExpectedRewardPerPnk + child.treeExpectedRewardPerPnk; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + const bestExpectedRewardCourt = diffCourts.sort( |
| 110 | + (a, b) => b.treeExpectedRewardPerPnk - a.treeExpectedRewardPerPnk |
| 111 | + )[0]; |
| 112 | + |
| 113 | + return { mostDisputedCourt, bestDrawingChancesCourt, bestExpectedRewardCourt, diffCourts }; |
| 114 | + } else { |
| 115 | + return undefined; |
| 116 | + } |
| 117 | + }, [usedQuery]); |
| 118 | + |
| 119 | + return courtActivityStats; |
| 120 | +}; |
0 commit comments