on master help sign in
Implement rekeyDateStore
0011f2dFang
src/store/lib/useBirthdaysStore.js+1−1
@@ -26,7 +26,7 @@ export default function useBirthdayStore() {
26 async point => {
26 async point => {
27 const _contracts = contracts.getOrElse(null);
27 const _contracts = contracts.getOrElse(null);
28 const _web3 = web3.getOrElse(null);
28 const _web3 = web3.getOrElse(null);
29− if (!_contracts || !web3) {
29+ if (!_contracts || !_web3) {
30 return;
30 return;
31 }
31 }
32
32
src/store/lib/usePointStore.js+5−2
@@ -2,6 +2,7 @@ import { useCallback } from 'react';
2
2
3import useDetailsStore from './useDetailsStore';
3import useDetailsStore from './useDetailsStore';
4import useBirthdaysStore from './useBirthdaysStore';
4import useBirthdaysStore from './useBirthdaysStore';
5+import useRekeyDateStore from './useRekeyDateStore';
5import useInvitesStore from './useInvitesStore';
6import useInvitesStore from './useInvitesStore';
6import useControlledPointsStore from './useControlledPointsStore';
7import useControlledPointsStore from './useControlledPointsStore';
7import useEclipticOwnerStore from './useEclipticOwnerStore';
8import useEclipticOwnerStore from './useEclipticOwnerStore';
@@ -9,6 +10,7 @@ import useEclipticOwnerStore from './useEclipticOwnerStore';
9export default function usePointStore() {
10export default function usePointStore() {
10 const { syncDetails, ...details } = useDetailsStore();
11 const { syncDetails, ...details } = useDetailsStore();
11 const { syncBirthday, ...birthdays } = useBirthdaysStore();
12 const { syncBirthday, ...birthdays } = useBirthdaysStore();
13+ const { syncRekeyDate, ...rekeyDates } = useRekeyDateStore();
12 const { syncInvites, ...invites } = useInvitesStore();
14 const { syncInvites, ...invites } = useInvitesStore();
13 const ecliptic = useEclipticOwnerStore();
15 const ecliptic = useEclipticOwnerStore();
14 const {
16 const {
@@ -18,8 +20,8 @@ export default function usePointStore() {
18
20
19 // sync all of the on-chain info required to display a known point
21 // sync all of the on-chain info required to display a known point
20 const syncKnownPoint = useCallback(
22 const syncKnownPoint = useCallback(
21− async point => Promise.all([syncBirthday(point)]),
23+ async point => Promise.all([syncBirthday(point), syncRekeyDate(point)]),
22− [syncBirthday]
24+ [syncBirthday, syncRekeyDate]
23 );
25 );
24
26
25 // sync all of the on-chain info required to display a foreign point
27 // sync all of the on-chain info required to display a foreign point
@@ -39,6 +41,7 @@ export default function usePointStore() {
39 return {
41 return {
40 ...details,
42 ...details,
41 ...birthdays,
43 ...birthdays,
44+ ...rekeyDates,
42 ...invites,
45 ...invites,
43 ...controlledPoints,
46 ...controlledPoints,
44 ...ecliptic,
47 ...ecliptic,
src/store/lib/useRekeyDateStore.jsnew file+59
@@ -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+}