on 75e1000 help sign in
bridge/src/lib/usePermissionsForPoint.js
85 lines 2.4 kB history raw
1import { azimuth } from 'azimuth-js';
3import { usePointCache } from 'store/pointCache';
5import { eqAddr, ETH_ZERO_ADDR } from './wallet';
7const NULL_PERMISSIONS = {
8 isPlanet: false,
9 isStar: false,
10 isGalaxy: false,
11 //
12 isOwner: false,
13 isActiveOwner: false,
14 isTransferProxy: false,
15 isManagementProxy: false,
16 //
17 isManagementProxySet: false,
18 isSpawnProxySet: false,
19 isVotingProxySet: false,
20 isTransferProxySet: false,
21 //
22 canManage: false,
23 canTransfer: false,
24 canSpawn: false,
25 canVote: false,
26};
28/**
29 * @param {string} address
30 * @param {number} point
31 */
32export default function usePermissionsForPoint(address, point) {
33 const { getDetails } = usePointCache();
35 return getDetails(point).matchWith({
36 Nothing: () => NULL_PERMISSIONS,
37 Just: ({ value: details }) => {
38 const pointSize = azimuth.getPointSize(point);
39 const isPlanet = pointSize === azimuth.PointSize.Planet;
40 const isStar = pointSize === azimuth.PointSize.Star;
41 const isGalaxy = pointSize === azimuth.PointSize.Galaxy;
43 const isOwner = eqAddr(address, details.owner);
44 const isActiveOwner = isOwner && details.active;
45 const isManagementProxy = eqAddr(address, details.managementProxy);
46 const isSpawnProxy = eqAddr(address, details.spawnProxy);
47 const isVotingProxy = eqAddr(address, details.votingProxy);
48 const isTransferProxy = eqAddr(address, details.transferProxy);
50 const isManagementProxySet = !eqAddr(
51 ETH_ZERO_ADDR,
52 details.managementProxy
53 );
54 const isSpawnProxySet = !eqAddr(ETH_ZERO_ADDR, details.spawnProxy);
55 const isVotingProxySet = !eqAddr(ETH_ZERO_ADDR, details.votingProxy);
56 const isTransferProxySet = !eqAddr(ETH_ZERO_ADDR, details.transferProxy);
58 const canManage = isOwner || isManagementProxy;
59 const canTransfer = isOwner || isTransferProxy;
60 const canSpawn = (isStar || isGalaxy) && (isOwner || isSpawnProxy);
61 const canVote = isGalaxy && (isOwner || isVotingProxy);
63 return {
64 isPlanet,
65 isStar,
66 isGalaxy,
67 //
68 isOwner,
69 isActiveOwner,
70 isTransferProxy,
71 isManagementProxy,
72 //
73 isManagementProxySet,
74 isSpawnProxySet,
75 isVotingProxySet,
76 isTransferProxySet,
77 //
78 canManage,
79 canTransfer,
80 canSpawn,
81 canVote,
82 };
83 },
84 });