on dee2cd5 help sign in
bridge/src/store/lib/useControlledPointsStore.js
64 lines 1.8 kB history raw
1import { useState, useCallback } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import * as azimuth from 'azimuth-js';
5import { useNetwork } from '../network';
6import { useWallet } from 'store/wallet';
8const EMPTY_CONTROLLED_POINTS = {
9 ownedPoints: Nothing(),
10 incomingPoints: Nothing(),
11 managingPoints: Nothing(),
12 votingPoints: Nothing(),
13 spawningPoints: Nothing(),
14};
16export default function useControlledPointsStore() {
17 const { contracts } = useNetwork();
18 const { wallet } = useWallet();
19 const [controlledPointsCache, _setControlledPointsCache] = useState(
20 EMPTY_CONTROLLED_POINTS
21 );
23 const getControlledPoints = useCallback(
24 () => controlledPointsCache || EMPTY_CONTROLLED_POINTS,
25 [controlledPointsCache]
26 );
28 const syncControlledPoints = useCallback(async () => {
29 const _contracts = contracts.getOrElse(null);
30 const _wallet = wallet.getOrElse(null);
31 if (!_contracts || !_wallet) {
32 return;
33 }
35 const address = _wallet.address;
37 const [
38 ownedPoints,
39 incomingPoints,
40 managingPoints,
41 votingPoints,
42 spawningPoints,
43 ] = await Promise.all([
44 azimuth.azimuth.getOwnedPoints(_contracts, address),
45 azimuth.azimuth.getTransferringFor(_contracts, address),
46 azimuth.azimuth.getManagerFor(_contracts, address),
47 azimuth.azimuth.getVotingFor(_contracts, address),
48 azimuth.azimuth.getSpawningFor(_contracts, address),
49 ]);
51 _setControlledPointsCache({
52 ownedPoints: Just(ownedPoints),
53 incomingPoints: Just(incomingPoints),
54 managingPoints: Just(managingPoints),
55 votingPoints: Just(votingPoints),
56 spawningPoints: Just(spawningPoints),
57 });
58 }, [contracts, wallet, _setControlledPointsCache]);
60 return {
61 getControlledPoints,
62 syncControlledPoints,
63 };