|
| 1 | +import { useQuery } from "@tanstack/react-query"; |
| 2 | + |
| 3 | +import { useGraphqlBatcher } from "context/GraphqlBatcher"; |
| 4 | +import { isUndefined } from "utils/index"; |
| 5 | + |
| 6 | +import { graphql } from "src/graphql"; |
| 7 | +import { HomePageBlockQuery } from "src/graphql/graphql"; |
| 8 | +export type { HomePageBlockQuery }; |
| 9 | + |
| 10 | +const homePageBlockQuery = graphql(` |
| 11 | + query HomePageBlock($blockNumber: Int) { |
| 12 | + presentCourts: courts(orderBy: id, orderDirection: asc) { |
| 13 | + id |
| 14 | + parent { |
| 15 | + id |
| 16 | + } |
| 17 | + name |
| 18 | + numberDisputes |
| 19 | + numberVotes |
| 20 | + feeForJuror |
| 21 | + effectiveStake |
| 22 | + } |
| 23 | + pastCourts: courts(orderBy: id, orderDirection: asc, block: { number: $blockNumber }) { |
| 24 | + id |
| 25 | + parent { |
| 26 | + id |
| 27 | + } |
| 28 | + name |
| 29 | + numberDisputes |
| 30 | + numberVotes |
| 31 | + feeForJuror |
| 32 | + effectiveStake |
| 33 | + } |
| 34 | + } |
| 35 | +`); |
| 36 | + |
| 37 | +type Court = HomePageBlockQuery["presentCourts"][number]; |
| 38 | +type CourtWithTree = Court & { |
| 39 | + numberDisputes: number; |
| 40 | + numberVotes: number; |
| 41 | + feeForJuror: bigint; |
| 42 | + effectiveStake: bigint; |
| 43 | + treeNumberDisputes: number; |
| 44 | + treeNumberVotes: number; |
| 45 | + votesPerPnk: number; |
| 46 | + treeVotesPerPnk: number; |
| 47 | + expectedRewardPerPnk: number; |
| 48 | + treeExpectedRewardPerPnk: number; |
| 49 | +}; |
| 50 | + |
| 51 | +export type HomePageBlockStats = { |
| 52 | + mostDisputedCourt: CourtWithTree; |
| 53 | + bestDrawingChancesCourt: CourtWithTree; |
| 54 | + bestExpectedRewardCourt: CourtWithTree; |
| 55 | + courts: CourtWithTree[]; |
| 56 | +}; |
| 57 | + |
| 58 | +export const useHomePageBlockQuery = (blockNumber: number | undefined, allTime: boolean) => { |
| 59 | + const isEnabled = !isUndefined(blockNumber) || allTime; |
| 60 | + const { graphqlBatcher } = useGraphqlBatcher(); |
| 61 | + |
| 62 | + return useQuery<HomePageBlockStats>({ |
| 63 | + queryKey: [`homePageBlockQuery${blockNumber}-${allTime}`], |
| 64 | + enabled: isEnabled, |
| 65 | + staleTime: Infinity, |
| 66 | + queryFn: async () => { |
| 67 | + const data = await graphqlBatcher.fetch({ |
| 68 | + id: crypto.randomUUID(), |
| 69 | + document: homePageBlockQuery, |
| 70 | + variables: { blockNumber }, |
| 71 | + }); |
| 72 | + |
| 73 | + return processData(data, allTime); |
| 74 | + }, |
| 75 | + }); |
| 76 | +}; |
| 77 | + |
| 78 | +const processData = (data: HomePageBlockQuery, allTime: boolean) => { |
| 79 | + const presentCourts = data.presentCourts; |
| 80 | + const pastCourts = data.pastCourts; |
| 81 | + const processedCourts: CourtWithTree[] = Array(presentCourts.length); |
| 82 | + const processed = new Set(); |
| 83 | + |
| 84 | + const processCourt = (id: number): CourtWithTree => { |
| 85 | + if (processed.has(id)) return processedCourts[id]; |
| 86 | + |
| 87 | + processed.add(id); |
| 88 | + const court = |
| 89 | + !allTime && id < data.pastCourts.length |
| 90 | + ? addTreeValuesWithDiff(presentCourts[id], pastCourts[id]) |
| 91 | + : addTreeValues(presentCourts[id]); |
| 92 | + const parentIndex = court.parent ? Number(court.parent.id) - 1 : 0; |
| 93 | + |
| 94 | + if (id === parentIndex) { |
| 95 | + processedCourts[id] = court; |
| 96 | + return court; |
| 97 | + } |
| 98 | + |
| 99 | + processedCourts[id] = { |
| 100 | + ...court, |
| 101 | + treeNumberDisputes: court.treeNumberDisputes + processCourt(parentIndex).treeNumberDisputes, |
| 102 | + treeNumberVotes: court.treeNumberVotes + processCourt(parentIndex).treeNumberVotes, |
| 103 | + treeVotesPerPnk: court.treeVotesPerPnk + processCourt(parentIndex).treeVotesPerPnk, |
| 104 | + treeExpectedRewardPerPnk: court.treeExpectedRewardPerPnk + processCourt(parentIndex).treeExpectedRewardPerPnk, |
| 105 | + }; |
| 106 | + |
| 107 | + return processedCourts[id]; |
| 108 | + }; |
| 109 | + |
| 110 | + for (const court of presentCourts.toReversed()) { |
| 111 | + processCourt(Number(court.id) - 1); |
| 112 | + } |
| 113 | + |
| 114 | + processedCourts.reverse(); |
| 115 | + |
| 116 | + return { |
| 117 | + mostDisputedCourt: getCourtMostDisputes(processedCourts), |
| 118 | + bestDrawingChancesCourt: getCourtBestDrawingChances(processedCourts), |
| 119 | + bestExpectedRewardCourt: getBestExpectedRewardCourt(processedCourts), |
| 120 | + courts: processedCourts, |
| 121 | + }; |
| 122 | +}; |
| 123 | + |
| 124 | +const addTreeValues = (court: Court): CourtWithTree => { |
| 125 | + const votesPerPnk = Number(court.numberVotes) / (Number(court.effectiveStake) / 1e18); |
| 126 | + const expectedRewardPerPnk = votesPerPnk * (Number(court.feeForJuror) / 1e18); |
| 127 | + return { |
| 128 | + ...court, |
| 129 | + numberDisputes: Number(court.numberDisputes), |
| 130 | + numberVotes: Number(court.numberVotes), |
| 131 | + feeForJuror: BigInt(court.feeForJuror) / BigInt(1e18), |
| 132 | + effectiveStake: BigInt(court.effectiveStake), |
| 133 | + treeNumberDisputes: Number(court.numberDisputes), |
| 134 | + treeNumberVotes: Number(court.numberVotes), |
| 135 | + votesPerPnk, |
| 136 | + treeVotesPerPnk: votesPerPnk, |
| 137 | + expectedRewardPerPnk, |
| 138 | + treeExpectedRewardPerPnk: expectedRewardPerPnk, |
| 139 | + }; |
| 140 | +}; |
| 141 | + |
| 142 | +const addTreeValuesWithDiff = (presentCourt: Court, pastCourt: Court): CourtWithTree => { |
| 143 | + const presentCourtWithTree = addTreeValues(presentCourt); |
| 144 | + const pastCourtWithTree = addTreeValues(pastCourt); |
| 145 | + const diffNumberVotes = presentCourtWithTree.numberVotes - pastCourtWithTree.numberVotes; |
| 146 | + const avgEffectiveStake = (presentCourtWithTree.effectiveStake + pastCourtWithTree.effectiveStake) / 2n; |
| 147 | + const votesPerPnk = diffNumberVotes / Number(avgEffectiveStake); |
| 148 | + const expectedRewardPerPnk = votesPerPnk * Number(presentCourt.feeForJuror); |
| 149 | + return { |
| 150 | + ...presentCourt, |
| 151 | + numberDisputes: presentCourtWithTree.numberDisputes - pastCourtWithTree.numberDisputes, |
| 152 | + treeNumberDisputes: presentCourtWithTree.treeNumberDisputes - pastCourtWithTree.treeNumberDisputes, |
| 153 | + numberVotes: diffNumberVotes, |
| 154 | + treeNumberVotes: presentCourtWithTree.treeNumberVotes - pastCourtWithTree.treeNumberVotes, |
| 155 | + effectiveStake: avgEffectiveStake, |
| 156 | + votesPerPnk, |
| 157 | + treeVotesPerPnk: votesPerPnk, |
| 158 | + expectedRewardPerPnk, |
| 159 | + treeExpectedRewardPerPnk: expectedRewardPerPnk, |
| 160 | + }; |
| 161 | +}; |
| 162 | + |
| 163 | +const getCourtMostDisputes = (courts: CourtWithTree[]) => |
| 164 | + courts.toSorted((a: CourtWithTree, b: CourtWithTree) => b.numberDisputes - a.numberDisputes)[0]; |
| 165 | +const getCourtBestDrawingChances = (courts: CourtWithTree[]) => |
| 166 | + courts.toSorted((a, b) => b.treeVotesPerPnk - a.treeVotesPerPnk)[0]; |
| 167 | +const getBestExpectedRewardCourt = (courts: CourtWithTree[]) => |
| 168 | + courts.toSorted((a, b) => b.treeExpectedRewardPerPnk - a.treeExpectedRewardPerPnk)[0]; |
0 commit comments