on afdb680 help sign in
bridge/src/store/lib/useControlledPointsStore.js
128 lines 4.0 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';
11export default function useControlledPointsStore() {
12 const { contracts } = useNetwork();
13 const { wallet } = useWallet();
14 const { getPoints, getPointDetails } = useRoller();
16 const [controlledPoints, _setControlledPoints] = useState(Nothing());
18 const syncControlledPoints = useCallback(async () => {
19 const _contracts = contracts.getOrElse(null);
20 const _wallet = wallet.getOrElse(null);
21 if (!_contracts || !_wallet) {
22 return;
23 }
25 _setControlledPoints(Nothing());
27 const address = _wallet.address;
29 try {
30 const [
31 ownedPointsL1,
32 incomingPointsL1,
33 managingPointsL1,
34 votingPointsL1,
35 spawningPointsL1,
36 ownedPointsL2,
37 incomingPointsL2,
38 managingPointsL2,
39 votingPointsL2,
40 spawningPointsL2,
41 ] = await Promise.all([
42 azimuth.azimuth.getOwnedPoints(_contracts, address),
43 azimuth.azimuth.getTransferringFor(_contracts, address),
44 azimuth.azimuth.getManagerFor(_contracts, address),
45 azimuth.azimuth.getVotingFor(_contracts, address),
46 azimuth.azimuth.getSpawningFor(_contracts, address),
47 getPoints('own', address),
48 getPoints('transfer', address),
49 getPoints('manage', address),
50 getPoints('vote', address),
51 getPoints('spawn', address),
52 ]);
54 if (
55 ownedPointsL1 === null &&
56 incomingPointsL1 === null &&
57 managingPointsL1 === null &&
58 votingPointsL1 === null &&
59 spawningPointsL1 === null &&
60 ownedPointsL2 === null &&
61 incomingPointsL2 === null &&
62 managingPointsL2 === null &&
63 votingPointsL2 === null &&
64 spawningPointsL2 === null
65 ) {
66 _setControlledPoints(
67 Just(Result.Error('Failed to read the blockchain.'))
68 );
69 } else {
70 getPointDetails(
71 (ownedPointsL1 || []).concat(ownedPointsL2 || []).filter(onlyUnique),
72 (incomingPointsL1 || [])
73 .concat(incomingPointsL2 || [])
74 .filter(onlyUnique),
75 (managingPointsL1 || [])
76 .concat(managingPointsL2 || [])
77 .filter(onlyUnique),
78 (votingPointsL1 || [])
79 .concat(votingPointsL2 || [])
80 .filter(onlyUnique),
81 (spawningPointsL1 || [])
82 .concat(spawningPointsL2 || [])
83 .filter(onlyUnique)
84 );
86 _setControlledPoints(
87 Just(
88 Result.Ok({
89 ownedPoints: ownedPointsL2
90 .concat(ownedPointsL1)
91 .map(Number)
92 .filter(onlyUnique),
93 incomingPoints: incomingPointsL2
94 .concat(incomingPointsL1)
95 .map(Number)
96 .filter(onlyUnique),
97 managingPoints: managingPointsL2
98 .concat(managingPointsL1)
99 .map(Number)
100 .filter(onlyUnique),
101 votingPoints: votingPointsL2
102 .concat(votingPointsL1)
103 .map(Number)
104 .filter(onlyUnique),
105 spawningPoints: spawningPointsL2
106 .concat(spawningPointsL1)
107 .map(Number)
108 .filter(onlyUnique),
109 })
110 )
111 );
112 }
113 } catch (error) {
114 console.error('failed to fetch controlled points', error);
115 _setControlledPoints(Just(Result.Error(error)));
116 }
117 }, [contracts, wallet, getPoints, getPointDetails]);
119 // sync controlled points whenever wallet or contracts changes
120 useEffect(() => {
121 syncControlledPoints();
122 }, [syncControlledPoints]);
124 return {
125 controlledPoints,
126 syncControlledPoints,
127 };