on a77fa97 help sign in
bridge/src/lib/usePermissionsForPoint.js
104 lines 2.9 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 isTransferProxy: false,
18 isManagementProxy: 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 return getDetails(point).matchWith({
42 Nothing: () => NULL_PERMISSIONS,
43 Just: ({ value: details }) => {
44 const _contracts = need.contracts(contracts);
46 const pointSize = azimuth.getPointSize(point);
47 const isPlanet = pointSize === azimuth.PointSize.Planet;
48 const isStar = pointSize === azimuth.PointSize.Star;
49 const isGalaxy = pointSize === azimuth.PointSize.Galaxy;
50 const isParent = isStar || isGalaxy;
52 const isActive = details.active;
54 const isOwner = eqAddr(address, details.owner);
55 const isActiveOwner = isOwner && isActive;
56 const isManagementProxy = eqAddr(address, details.managementProxy);
57 const isSpawnProxy = eqAddr(address, details.spawnProxy);
58 const isVotingProxy = eqAddr(address, details.votingProxy);
59 const isTransferProxy = eqAddr(address, details.transferProxy);
61 const isManagementProxySet = !isZeroAddress(details.managementProxy);
62 const isSpawnProxySet = !isZeroAddress(details.spawnProxy);
63 const isVotingProxySet = !isZeroAddress(details.votingProxy);
64 const isTransferProxySet = !isZeroAddress(details.transferProxy);
66 const canManage = isOwner || isManagementProxy;
67 const canTransfer = isOwner || isTransferProxy;
68 const canSpawn =
69 isParent && (isOwner || isSpawnProxy) && details.keyRevisionNumber > 0;
70 const canVote = isGalaxy && isActive && (isOwner || isVotingProxy);
72 const spawnIsDelegatedSending = eqAddr(
73 _contracts.delegatedSending.address,
74 details.spawnProxy
75 );
77 return {
78 isPlanet,
79 isStar,
80 isGalaxy,
81 isParent,
82 //
83 isActive,
84 //
85 isOwner,
86 isActiveOwner,
87 isTransferProxy,
88 isManagementProxy,
89 //
90 isManagementProxySet,
91 isSpawnProxySet,
92 isVotingProxySet,
93 isTransferProxySet,
94 //
95 canManage,
96 canTransfer,
97 canSpawn,
98 canVote,
99 //
100 spawnIsDelegatedSending,
101 };
102 },
103 });