on 261ea40 help sign in
bridge/src/lib/usePermissionsForPoint.js
108 lines 3.0 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 './wallet';
10const NULL_PERMISSIONS = {
11 isPlanet: false,
12 isStar: false,
13 isGalaxy: false,
14 //
15 isOwner: false,
16 isActiveOwner: false,
17 isManagementProxy: false,
18 isSpawnProxy: false,
19 isVotingProxy: false,
20 isTransferProxy: false,
21 //
22 isManagementProxySet: false,
23 isSpawnProxySet: false,
24 isVotingProxySet: false,
25 isTransferProxySet: false,
26 //
27 canManage: false,
28 canTransfer: false,
29 canSpawn: false,
30 canVote: false,
31 //
32 spawnIsDelegatedSending: false,
33};
35/**
36 * @param {string} address
37 * @param {number} point
38 */
39export default function usePermissionsForPoint(address, point) {
40 const { getDetails } = usePointCache();
41 const { contracts } = useNetwork();
43 return getDetails(point).matchWith({
44 Nothing: () => NULL_PERMISSIONS,
45 Just: ({ value: details }) => {
46 const _contracts = need.contracts(contracts);
48 const pointSize = azimuth.getPointSize(point);
49 const isPlanet = pointSize === azimuth.PointSize.Planet;
50 const isStar = pointSize === azimuth.PointSize.Star;
51 const isGalaxy = pointSize === azimuth.PointSize.Galaxy;
52 const isParent = isStar || isGalaxy;
54 const isActive = details.active;
56 const isOwner = eqAddr(address, details.owner);
57 const isActiveOwner = isOwner && isActive;
58 const isManagementProxy = eqAddr(address, details.managementProxy);
59 const isSpawnProxy = eqAddr(address, details.spawnProxy);
60 const isVotingProxy = eqAddr(address, details.votingProxy);
61 const isTransferProxy = eqAddr(address, details.transferProxy);
63 const isManagementProxySet = !isZeroAddress(details.managementProxy);
64 const isSpawnProxySet = !isZeroAddress(details.spawnProxy);
65 const isVotingProxySet = !isZeroAddress(details.votingProxy);
66 const isTransferProxySet = !isZeroAddress(details.transferProxy);
68 const canManage = isOwner || isManagementProxy;
69 const canTransfer = isOwner || isTransferProxy;
70 const canSpawn =
71 isParent && (isOwner || isSpawnProxy) && details.keyRevisionNumber > 0;
72 const canVote = isGalaxy && isActive && (isOwner || isVotingProxy);
74 const spawnIsDelegatedSending = eqAddr(
75 _contracts.delegatedSending.address,
76 details.spawnProxy
77 );
79 return {
80 isPlanet,
81 isStar,
82 isGalaxy,
83 isParent,
84 //
85 isActive,
86 //
87 isOwner,
88 isActiveOwner,
89 isManagementProxy,
90 isSpawnProxy,
91 isVotingProxy,
92 isTransferProxy,
93 //
94 isManagementProxySet,
95 isSpawnProxySet,
96 isVotingProxySet,
97 isTransferProxySet,
98 //
99 canManage,
100 canTransfer,
101 canSpawn,
102 canVote,
103 //
104 spawnIsDelegatedSending,
105 };
106 },
107 });