@@ -0,0 +1,59 @@
1+import { useCallback, useState } from 'react';
2+import { Just, Nothing } from 'folktale/maybe';
3+import * as azimuth from 'azimuth-js';
4+
5+import { useNetwork } from '../network';
6+
7+export default function useRekeyDateStore() {
8+ const { web3 } = useNetwork();
9+ const [rekeyDateCache, _setRekeyDateCache] = useState({});
10+
11+ const addToRekeyDateCache = useCallback(
12+ entry =>
13+ _setRekeyDateCache(cache => ({
14+ ...cache,
15+ ...entry,
16+ })),
17+ [_setRekeyDateCache]
18+ );
19+
20+ const getRekeyDate = useCallback(
21+ point => rekeyDateCache[point] || Nothing(),
22+ [rekeyDateCache]
23+ );
24+
25+ const syncRekeyDate = useCallback(
26+ async point => {
27+ const _web3 = web3.getOrElse(null);
28+ if (!_web3) {
29+ return;
30+ }
31+
32+ //TODO tighter search?
33+ //TODO maybe move into azimuth-js?
34+ const logs = await azimuth.azimuth.getPastEvents('ChangedKeys', {
35+ fromBlock: 0,
36+ toBlock: 'latest',
37+ filter: { point: [point] },
38+ });
39+
40+ if (logs.length === 0) {
41+ //TODO: better encoding for "no rekeyDate" state?
42+ addToRekeyDateCache({
43+ [point]: Just('unknown'),
44+ });
45+ } else {
46+ // last log is most recent
47+ const blockNumber = logs[logs.length - 1].blockNumber;
48+ const block = await _web3.eth.getBlock(blockNumber);
49+ addToRekeyDateCache({
50+ //TODO date format
51+ [point]: Just(new Date(block.timestamp * 1000).toString()),
52+ });
53+ }
54+ },
55+ [web3, addToRekeyDateCache]
56+ );
57+
58+ return { getRekeyDate, syncRekeyDate };
59+}