on ac1828f help sign in
bridge/src/store/lib/useControlledPointsStore.js
129 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';
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) ? 1 : 2,
63 // };
64 // } catch (error) {
65 // console.warn(error);
66 // return { point: ship, layer: 1 };
67 // }
68 // })
69 // );
70 const pointsWithLayers = ownedPoints.map(point => ({
71 point: Number(point),
72 layer: 1,
73 }));
75 if (
76 ownedPoints === null &&
77 incomingPoints === null &&
78 managingPoints === null &&
79 votingPoints === null &&
80 spawningPoints === null
81 ) {
82 _setControlledPoints(
83 Just(Result.Error('Failed to read the blockchain.'))
84 );
85 } else {
86 _setControlledPoints(
87 Just(
88 Result.Ok({
89 ownedPoints: ownedPoints
90 .concat(ownedPointsL1)
91 .map(Number)
92 .filter(onlyUnique),
93 incomingPoints: incomingPoints
94 .concat(incomingPointsL1)
95 .map(Number)
96 .filter(onlyUnique),
97 managingPoints: managingPoints
98 .concat(managingPointsL1)
99 .map(Number)
100 .filter(onlyUnique),
101 votingPoints: votingPoints
102 .concat(votingPointsL1)
103 .map(Number)
104 .filter(onlyUnique),
105 spawningPoints: spawningPoints
106 .concat(spawningPointsL1)
107 .map(Number)
108 .filter(onlyUnique),
109 pointsWithLayers,
110 })
111 )
112 );
113 }
114 } catch (error) {
115 console.error('failed to fetch controlled points', error);
116 _setControlledPoints(Just(Result.Error(error)));
117 }
118 }, [contracts, wallet, getPoints, api]);
120 // sync controlled points whenever wallet or contracts changes
121 useEffect(() => {
122 syncControlledPoints();
123 }, [syncControlledPoints]);
125 return {
126 controlledPoints,
127 syncControlledPoints,
128 };