on master help sign in
Implement rekeyDateStore
commit0011f2d28f7cec501124b1f143c197a7e0159819 authorFang <git@fang.io> dateFri 5 Jul 2019, 12:55 (7 years ago) parenta9887cf
3 files changed+65−3
src/store/lib/useBirthdaysStore.js+1−1 src/store/lib/usePointStore.js+5−2 src/store/lib/useRekeyDateStore.js+59
src/store/lib/useBirthdaysStore.js+1−1
@@ -26,7 +26,7 @@ export default function useBirthdayStore() {
2626 async point => {
2727 const _contracts = contracts.getOrElse(null);
2828 const _web3 = web3.getOrElse(null);
29− if (!_contracts || !web3) {
29+ if (!_contracts || !_web3) {
3030 return;
3131 }
3232
src/store/lib/usePointStore.js+5−2
@@ -2,6 +2,7 @@ import { useCallback } from 'react';
22
33import useDetailsStore from './useDetailsStore';
44import useBirthdaysStore from './useBirthdaysStore';
5+import useRekeyDateStore from './useRekeyDateStore';
56import useInvitesStore from './useInvitesStore';
67import useControlledPointsStore from './useControlledPointsStore';
78import useEclipticOwnerStore from './useEclipticOwnerStore';
@@ -9,6 +10,7 @@ import useEclipticOwnerStore from './useEclipticOwnerStore';
910export default function usePointStore() {
1011 const { syncDetails, ...details } = useDetailsStore();
1112 const { syncBirthday, ...birthdays } = useBirthdaysStore();
13+ const { syncRekeyDate, ...rekeyDates } = useRekeyDateStore();
1214 const { syncInvites, ...invites } = useInvitesStore();
1315 const ecliptic = useEclipticOwnerStore();
1416 const {
@@ -18,8 +20,8 @@ export default function usePointStore() {
1820
1921 // sync all of the on-chain info required to display a known point
2022 const syncKnownPoint = useCallback(
21− async point => Promise.all([syncBirthday(point)]),
22− [syncBirthday]
23+ async point => Promise.all([syncBirthday(point), syncRekeyDate(point)]),
24+ [syncBirthday, syncRekeyDate]
2325 );
2426
2527 // sync all of the on-chain info required to display a foreign point
@@ -39,6 +41,7 @@ export default function usePointStore() {
3941 return {
4042 ...details,
4143 ...birthdays,
44+ ...rekeyDates,
4245 ...invites,
4346 ...controlledPoints,
4447 ...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+}