on master help sign in
bridge/src/lib/usePermissionsForPoint.js
107 lines 3.1 kB history raw
1import { azimuth } from 'azimuth-js';
3import { usePointCache } from 'store/pointCache';
4import { useNetwork } from 'store/network';
6import * as need from 'lib/need';
8import { eqAddr, isZeroAddress } from './utils/address';
10const NULL_PERMISSIONS = {
11 isActive: false,
12 //
13 isOwner: false,
14 isActiveOwner: false,
15 isManagementProxy: false,
16 isSpawnProxy: false,
17 isVotingProxy: false,
18 isTransferProxy: false,
19 //
20 isManagementProxySet: false,
21 isSpawnProxySet: false,
22 isVotingProxySet: false,
23 isTransferProxySet: false,
24 //
25 canManage: false,
26 canTransfer: false,
27 canSpawn: false,
28 canVote: false,
29 //
30 spawnIsDelegatedSending: false,
31};
33/**
34 * @param {string} address
35 * @param {number} point
36 */
37export default function usePermissionsForPoint(address, point) {
38 const { getDetails } = usePointCache();
39 const { contracts } = useNetwork();
41 const pointSize = azimuth.getPointSize(point);
42 const isPlanet = pointSize === azimuth.PointSize.Planet;
43 const isStar = pointSize === azimuth.PointSize.Star;
44 const isGalaxy = pointSize === azimuth.PointSize.Galaxy;
45 const isParent = isStar || isGalaxy;
46 const staticPermissions = { isPlanet, isStar, isGalaxy, isParent };
48 return {
49 ...staticPermissions,
50 ...getDetails(point).matchWith({
51 Nothing: () => NULL_PERMISSIONS,
52 Just: ({ value: details }) => {
53 const _contracts = need.contracts(contracts);
55 const isActive = details.active;
57 const isOwner = eqAddr(address, details.owner);
58 const isActiveOwner = isOwner && isActive;
59 const isManagementProxy = eqAddr(address, details.managementProxy);
60 const isSpawnProxy = eqAddr(address, details.spawnProxy);
61 const isVotingProxy = eqAddr(address, details.votingProxy);
62 const isTransferProxy = eqAddr(address, details.transferProxy);
64 const isManagementProxySet = !isZeroAddress(details.managementProxy);
65 const isSpawnProxySet = !isZeroAddress(details.spawnProxy);
66 const isVotingProxySet = !isZeroAddress(details.votingProxy);
67 const isTransferProxySet = !isZeroAddress(details.transferProxy);
69 const canManage = isOwner || isManagementProxy;
70 const canTransfer = isOwner || isTransferProxy;
71 const canSpawn =
72 isParent &&
73 (isOwner || isSpawnProxy) &&
74 details.keyRevisionNumber > 0;
75 const canVote = isGalaxy && isActive && (isOwner || isVotingProxy);
77 const spawnIsDelegatedSending = eqAddr(
78 _contracts.delegatedSending.address,
79 details.spawnProxy
80 );
82 return {
83 isActive,
84 //
85 isOwner,
86 isActiveOwner,
87 isManagementProxy,
88 isSpawnProxy,
89 isVotingProxy,
90 isTransferProxy,
91 //
92 isManagementProxySet,
93 isSpawnProxySet,
94 isVotingProxySet,
95 isTransferProxySet,
96 //
97 canManage,
98 canTransfer,
99 canSpawn,
100 canVote,
101 //
102 spawnIsDelegatedSending,
103 };
104 },
105 }),
106 };