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