on master help sign in
bridge/src/store/lib/useControlledPointsStore.js
136 lines 4.4 kB history raw
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) {
23 return;
24 }
26 _setControlledPoints(Nothing());
28 const address = _wallet.address;
30 try {
31 const [
32 ownedPointsL1,
33 incomingPointsL1,
34 managingPointsL1,
35 votingPointsL1,
36 spawningPointsL1,
37 ownedPointsL2,
38 incomingPointsL2,
39 managingPointsL2,
40 votingPointsL2,
41 spawningPointsL2,
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),
53 ]);
55 if (
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
66 ) {
67 _setControlledPoints(
68 Just(Result.Error('Failed to read the blockchain.'))
69 );
70 } else {
71 await getPointsDetails(
72 (ownedPointsL1 || []).concat(ownedPointsL2 || []).filter(onlyUnique),
73 (incomingPointsL1 || [])
74 .concat(incomingPointsL2 || [])
75 .filter(onlyUnique),
76 (managingPointsL1 || [])
77 .concat(managingPointsL2 || [])
78 .filter(onlyUnique),
79 (votingPointsL1 || [])
80 .concat(votingPointsL2 || [])
81 .filter(onlyUnique),
82 (spawningPointsL1 || [])
83 .concat(spawningPointsL2 || [])
84 .filter(onlyUnique)
85 );
87 _setControlledPoints(
88 Just(
89 Result.Ok({
90 ownedPoints: ownedPointsL2
91 .concat(ownedPointsL1)
92 .map(Number)
93 .filter(onlyUnique),
94 incomingPoints: incomingPointsL2
95 .concat(incomingPointsL1)
96 .map(Number)
97 .filter(onlyUnique),
98 managingPoints: managingPointsL2
99 .concat(managingPointsL1)
100 .map(Number)
101 .filter(onlyUnique),
102 votingPoints: votingPointsL2
103 .concat(votingPointsL1)
104 .map(Number)
105 .filter(onlyUnique),
106 spawningPoints: spawningPointsL2
107 .concat(spawningPointsL1)
108 .map(Number)
109 .filter(onlyUnique),
110 })
111 )
112 );
113 }
114 } catch (error) {
115 console.error('failed to fetch controlled points', error);
116 _setControlledPoints(Just(Result.Error(JSON.stringify(error))));
117 }
118 }, [web3, contracts, wallet, getPoints, getPointsDetails]);
120 // sync controlled points whenever wallet or contracts changes
121 useEffect(() => {
122 syncControlledPoints();
123 }, [syncControlledPoints]);
125 // Poll the roller every minute for changes to the points
126 useEffect(() => {
127 setInterval(() => {
128 syncControlledPoints();
129 }, ONE_MINUTE);
130 }, []); // eslint-disable-line react-hooks/exhaustive-deps
132 return {
133 controlledPoints,
134 syncControlledPoints,
135 };