-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathuseHomePageExtraStats.ts
70 lines (52 loc) · 2.52 KB
/
useHomePageExtraStats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { useEffect, useMemo, useState } from "react";
import { DEFAULT_CHAIN } from "consts/chains";
import { HomePageBlockQuery } from "src/graphql/graphql";
import { isUndefined } from "src/utils";
import { useHomePageContext } from "../useHomePageContext";
import { useHomePageBlockQuery } from "./useHomePageBlockQuery";
import { useBlockNumber } from "wagmi";
import { averageBlockTimeInSeconds } from "consts/averageBlockTimeInSeconds";
type Court = HomePageBlockQuery["courts"][number];
const getCourtWithMaxDifference = (lastWeekCourts: Court[], currentCourts: Court[]): Court => {
const diffs = lastWeekCourts.map((court, idx) => {
return Number(currentCourts[idx].numberDisputes) - Number(court.numberDisputes);
});
const maxDiffCourtId = diffs.reduce((a, b) => (a > b ? a : b));
return lastWeekCourts[diffs.indexOf(maxDiffCourtId)];
};
const getCourtWithMaxDrawingChance = (currentCourts: Court[]): Court => {
return currentCourts.reduce((a, b) => (Number(a.stake) > Number(b.feeForJuror) ? b : a));
};
const getCourtWithMaxRewardChance = (currentCourts: Court[]): Court => {
return currentCourts.reduce((a, b) => (Number(a.feeForJuror) > Number(b.feeForJuror) ? a : b));
};
export interface HomePageExtraStatsType {
MostActiveCourt: string | null;
HighestDrawingChance: string | null;
HighestRewardChance: string | null;
}
export const useHomePageExtraStats = (): HomePageExtraStatsType => {
const { data } = useHomePageContext();
const [oneWeekAgoBlockNumber, setOneWeekAgoBlockNumber] = useState<number>();
const currentBlockNumber = useBlockNumber({ chainId: DEFAULT_CHAIN });
useEffect(() => {
if (currentBlockNumber?.data) {
const oneWeekInBlocks = Math.floor((7 * 24 * 3600) / averageBlockTimeInSeconds[DEFAULT_CHAIN]);
setOneWeekAgoBlockNumber(Number(currentBlockNumber.data) - oneWeekInBlocks);
}
}, [DEFAULT_CHAIN, currentBlockNumber]);
const { data: relData } = useHomePageBlockQuery(oneWeekAgoBlockNumber!);
const MostActiveCourt = useMemo(() => {
if (isUndefined(relData) || isUndefined(data)) {
return null;
}
return getCourtWithMaxDifference(relData.courts, data.courts).name ?? null;
}, [relData, data]);
const HighestDrawingChance = useMemo(() => {
return data ? getCourtWithMaxDrawingChance(data.courts).name ?? null : null;
}, [data]);
const HighestRewardChance = useMemo(() => {
return data ? getCourtWithMaxRewardChance(data.courts).name ?? null : null;
}, [data]);
return { MostActiveCourt, HighestDrawingChance, HighestRewardChance };
};