on abbbcbc help sign in
bridge/src/store/lib/useRekeyDateStore.js
68 lines 1.8 kB history raw
1import { useCallback, useState } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import Result from 'folktale/result';
5import { useNetwork } from '../network';
7export default function useRekeyDateStore() {
8 const { web3, contracts } = useNetwork();
9 const [rekeyDateCache, _setRekeyDateCache] = useState({});
11 const addToRekeyDateCache = useCallback(
12 entry =>
13 _setRekeyDateCache(cache => ({
14 ...cache,
15 ...entry,
16 })),
17 [_setRekeyDateCache]
18 );
20 /**
21 * @param {number} point
22 * @return {Maybe<Result<Date, String>>}
23 */
24 const getRekeyDate = useCallback(
25 point => rekeyDateCache[point] || Nothing(),
26 [rekeyDateCache]
27 );
29 const syncRekeyDate = useCallback(
30 async point => {
31 const _web3 = web3.getOrElse(null);
32 const _contracts = contracts.getOrElse(null);
33 if (!_web3 || !_contracts) {
34 return;
35 }
37 //TODO tighter search?
38 //TODO maybe move into azimuth-js?
39 const logs = await _contracts.azimuth.getPastEvents('ChangedKeys', {
40 fromBlock: 0,
41 toBlock: 'latest',
42 filter: {
43 point: [point],
44 },
45 });
47 if (logs.length === 0) {
48 //TODO: better encoding for "no rekeyDate" state?
49 addToRekeyDateCache({
50 [point]: Just(Result.Error('No rekey date available.')),
51 });
52 } else {
53 // last log is most recent
54 const blockNumber = logs[logs.length - 1].blockNumber;
55 const block = await _web3.eth.getBlock(blockNumber);
56 addToRekeyDateCache({
57 [point]: Just(Result.Ok(new Date(block.timestamp * 1000))),
58 });
59 }
60 },
61 [web3, contracts, addToRekeyDateCache]
62 );
64 return {
65 getRekeyDate,
66 syncRekeyDate,
67 };