on 8a36573 help sign in
bridge/src/store/lib/useControlledPointsStore.js
121 lines 3.9 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 { useNetwork } from '../network';
7import { useWallet } from 'store/wallet';
8import useRoller from 'lib/useRoller';
10const onlyUnique = (value, index, self) => self.indexOf(value) === index;
12export default function useControlledPointsStore() {
13 const { contracts } = useNetwork();
14 const { wallet } = useWallet();
15 const { getPoints, getPointDetails } = 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 (!_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 getPointDetails(
72 (ownedPointsL1 || []).concat(ownedPointsL2 || []),
73 (incomingPointsL1 || []).concat(incomingPointsL2 || []),
74 (managingPointsL1 || []).concat(managingPointsL2 || []),
75 (votingPointsL1 || []).concat(votingPointsL2 || []),
76 (spawningPointsL1 || []).concat(spawningPointsL2 || [])
77 );
79 _setControlledPoints(
80 Just(
81 Result.Ok({
82 ownedPoints: ownedPointsL2
83 .concat(ownedPointsL1)
84 .map(Number)
85 .filter(onlyUnique),
86 incomingPoints: incomingPointsL2
87 .concat(incomingPointsL1)
88 .map(Number)
89 .filter(onlyUnique),
90 managingPoints: managingPointsL2
91 .concat(managingPointsL1)
92 .map(Number)
93 .filter(onlyUnique),
94 votingPoints: votingPointsL2
95 .concat(votingPointsL1)
96 .map(Number)
97 .filter(onlyUnique),
98 spawningPoints: spawningPointsL2
99 .concat(spawningPointsL1)
100 .map(Number)
101 .filter(onlyUnique),
102 })
103 )
104 );
105 }
106 } catch (error) {
107 console.error('failed to fetch controlled points', error);
108 _setControlledPoints(Just(Result.Error(error)));
109 }
110 }, [contracts, wallet, getPoints, getPointDetails]);
112 // sync controlled points whenever wallet or contracts changes
113 useEffect(() => {
114 syncControlledPoints();
115 }, [syncControlledPoints]);
117 return {
118 controlledPoints,
119 syncControlledPoints,
120 };