forked from renproject/command-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphContainer.ts
181 lines (159 loc) · 6.52 KB
/
graphContainer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import Axios from "axios";
import { useEffect, useRef, useState } from "react";
import { createContainer } from "unstated-next";
import { SECONDS } from "../controllers/common/BackgroundTasks";
import { useMemoizeWithExpiry } from "../hooks/useMemoizeWithExpiry";
import { useTaskSchedule } from "../hooks/useTaskSchedule";
import { GraphClientContainer } from "../lib/graphQL/ApolloWithNetwork";
import {
bscSubgraphUrl,
ethereumSubgraphUrl,
fantomSubgraphUrl,
polygonSubgraphUrl,
} from "../lib/graphQL/client";
import { queryRenVMSubgraph, RenVM } from "../lib/graphQL/queries/renVM";
import { catchBackgroundException } from "../lib/react/errors";
import { NotificationsContainer } from "./notificationsContainer";
import { Web3Container } from "./web3Container";
// If the subgraph is behind by more than 30 blocks, show a warning and fall
// back to infura for fetching rewards.
const UNSYNCED_THRESHOLD = 30;
const useGraphContainer = () => {
const { web3, renNetwork } = Web3Container.useContainer();
const {
showSuccess,
showError,
showHint,
} = NotificationsContainer.useContainer();
const { ethereumSubgraph } = GraphClientContainer.useContainer();
const [renVM, setRenVM] = useState<RenVM | null>(null);
const shownEpochNotification = useRef(false);
const shownGraphError = useRef(false);
const updater = async () => {
try {
const newRenVM = await queryRenVMSubgraph(ethereumSubgraph);
setRenVM(newRenVM);
// Get seconds since start of epoch.
const epochStart =
Date.now() / 1000 - newRenVM.currentEpoch.timestamp.toNumber();
// Check if the epochhash is different or the epoch started less
// than 5 minutes ago.
const newEpoch =
(renVM &&
newRenVM.currentEpoch.epochhash !==
renVM.currentEpoch.epochhash) ||
epochStart < 600;
// Show a notification about the new epoch.
if (newEpoch && !shownEpochNotification.current) {
shownEpochNotification.current = true;
const darknodeCount = newRenVM.numberOfDarknodes.toFixed
? newRenVM.numberOfDarknodes.toFixed()
: newRenVM.numberOfDarknodes.toString();
showSuccess(
`A new epoch has just started. There are now ${darknodeCount} darknodes registered. Darknode rewards will appear shortly.`,
30 * SECONDS,
);
}
return { timeout: 15, result: newRenVM };
} catch (error) {
catchBackgroundException(error, "Error in graphStore: updater");
if (!renVM && !shownGraphError.current) {
shownGraphError.current = true;
showError("Failed to load RenVM data from subgraph.");
}
return { timeout: 15, result: renVM };
}
};
const [fetchRenVM] = useTaskSchedule(updater);
const getLatestSyncedBlock = useMemoizeWithExpiry(
async () =>
Axios.post<{
// eslint-disable-next-line id-blacklist
data: { _meta: { block: { number: number } } };
}>(ethereumSubgraphUrl(renNetwork), {
query:
"{\n _meta {\n block {\n number\n }\n }\n}",
}).then((response) => response.data.data._meta.block.number),
60 * SECONDS,
[renNetwork],
);
// TODO: Refactor into above function.
const getLatestSyncedBlockBSC = useMemoizeWithExpiry(
async () =>
Axios.post<{
// eslint-disable-next-line id-blacklist
data: { _meta: { block: { number: number } } };
}>(bscSubgraphUrl(renNetwork), {
query:
"{\n _meta {\n block {\n number\n }\n }\n}",
}).then((response) => response.data.data._meta.block.number),
60 * SECONDS,
[renNetwork],
);
const getLatestSyncedBlockFantom = useMemoizeWithExpiry(
async () =>
Axios.post<{
// eslint-disable-next-line id-blacklist
data: { _meta: { block: { number: number } } };
}>(fantomSubgraphUrl(renNetwork), {
query:
"{\n _meta {\n block {\n number\n }\n }\n}",
}).then((response) => response.data.data._meta.block.number),
// .then(() => 7995685),
60 * SECONDS,
[renNetwork],
);
const getLatestSyncedBlockPolygon = useMemoizeWithExpiry(
async () =>
Axios.post<{
// eslint-disable-next-line id-blacklist
data: { _meta: { block: { number: number } } };
}>(polygonSubgraphUrl(renNetwork), {
query:
"{\n _meta {\n block {\n number\n }\n }\n}",
}).then((response) => response.data.data._meta.block.number),
// .then(() => 7995726),
60 * SECONDS,
[renNetwork],
);
const [subgraphOutOfSync, setSubgraphOutOfSync] = useState(false);
const [checkedBlock, setCheckedBlock] = useState(false);
useEffect(() => {
if (checkedBlock) {
return;
}
setCheckedBlock(true);
(async () => {
try {
const subgraphBlock = await getLatestSyncedBlock();
const web3Block = await web3.eth.getBlockNumber();
if (
subgraphBlock &&
web3Block &&
subgraphBlock < web3Block - UNSYNCED_THRESHOLD
) {
setSubgraphOutOfSync(true);
showHint(
`The RenVM subgraph is ${
web3Block - subgraphBlock
} blocks behind - data may be out of date. New epochs can take a few hours to process.`,
0,
);
}
} catch (error) {
// Ignore error;
}
})().catch(console.error);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [checkedBlock]);
return {
renVM,
fetchRenVM,
subgraphOutOfSync,
getLatestSyncedBlock,
getLatestSyncedBlockBSC,
getLatestSyncedBlockFantom,
getLatestSyncedBlockPolygon,
};
};
export const GraphContainer = createContainer(useGraphContainer);