1import { useState, useEffect, useCallback } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import Result from 'folktale/result';
4import * as azimuth from 'azimuth-js';
6import { useWallet } from 'store/wallet';
7import useRoller from 'lib/useRoller';
8import { onlyUnique } from 'lib/utils/array';
9import { useNetwork } from '../network';
10import { ONE_MINUTE } from 'lib/constants';
12export default function useControlledPointsStore() {
13 const { contracts, web3 } = useNetwork();
14 const { wallet } = useWallet();
15 const { getPoints, getPointsDetails } = useRoller();
17 const [controlledPoints, _setControlledPoints] = useState(Nothing());
19 const syncControlledPoints = useCallback(async () => {
20 const _contracts = contracts.getOrElse(null);
21 const _wallet = wallet.getOrElse(null);
22 if (!web3 || Nothing.hasInstance(web3) || !_contracts || !_wallet) {
26 _setControlledPoints(Nothing());
28 const address = _wallet.address;
42 ] = await Promise.all([
43 azimuth.azimuth.getOwnedPoints(_contracts, address),
44 azimuth.azimuth.getTransferringFor(_contracts, address),
45 azimuth.azimuth.getManagerFor(_contracts, address),
46 azimuth.azimuth.getVotingFor(_contracts, address),
47 azimuth.azimuth.getSpawningFor(_contracts, address),
48 getPoints('own', address),
49 getPoints('transfer', address),
50 getPoints('manage', address),
51 getPoints('vote', address),
52 getPoints('spawn', address),
56 ownedPointsL1 === null &&
57 incomingPointsL1 === null &&
58 managingPointsL1 === null &&
59 votingPointsL1 === null &&
60 spawningPointsL1 === null &&
61 ownedPointsL2 === null &&
62 incomingPointsL2 === null &&
63 managingPointsL2 === null &&
64 votingPointsL2 === null &&
65 spawningPointsL2 === null
68 Just(Result.Error('Failed to read the blockchain.'))
71 await getPointsDetails(
72 (ownedPointsL1 || []).concat(ownedPointsL2 || []).filter(onlyUnique),
73 (incomingPointsL1 || [])
74 .concat(incomingPointsL2 || [])
76 (managingPointsL1 || [])
77 .concat(managingPointsL2 || [])
79 (votingPointsL1 || [])
80 .concat(votingPointsL2 || [])
82 (spawningPointsL1 || [])
83 .concat(spawningPointsL2 || [])
90 ownedPoints: ownedPointsL2
91 .concat(ownedPointsL1)
94 incomingPoints: incomingPointsL2
95 .concat(incomingPointsL1)
98 managingPoints: managingPointsL2
99 .concat(managingPointsL1)
102 votingPoints: votingPointsL2
103 .concat(votingPointsL1)
106 spawningPoints: spawningPointsL2
107 .concat(spawningPointsL1)
115 console.error('failed to fetch controlled points', error);
116 _setControlledPoints(Just(Result.Error(JSON.stringify(error))));
118 }, [web3, contracts, wallet, getPoints, getPointsDetails]);
120 // sync controlled points whenever wallet or contracts changes
122 syncControlledPoints();
123 }, [syncControlledPoints]);
125 // Poll the roller every minute for changes to the points
128 syncControlledPoints();
130 }, []); // eslint-disable-line react-hooks/exhaustive-deps
134 syncControlledPoints,
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list