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