on master help sign in
proxies: add getProxy functions
commitdd02fcdb54bcb0486e4120406cd061ce007fb9b6 authoryosoyubik <yosoyubik@gmail.com> dateThu 30 Sep 2021, 09:53 (5 years ago) parentcd5c22a
2 files changed+49−56
src/lib/useRoller.ts+8−31 src/lib/utils/proxy.ts+41−25
src/lib/useRoller.ts+8−31
@@ -23,10 +23,10 @@ import {
2323import { usePointCache } from 'store/pointCache';
2424import { getOutgoingPoints } from 'views/Points';
2525import {
26− isManagementProxy,
27− isOwnerProxy,
28− isTransferProxy,
29− isSpawnProxy,
26+ getSpawnProxy,
27+ getManagerProxy,
28+ getAddressProxy,
29+ getTransferProxy,
3030} from './utils/proxy';
3131import { isPlanet } from './utils/point';
3232
@@ -165,11 +165,7 @@ export default function useRoller() {
165165 }[] = [];
166166 const requests: Promise<string>[] = [];
167167
168− const proxy = isOwnerProxy(starInfo.ownership!, _wallet.address)
169− ? 'own'
170− : isSpawnProxy(starInfo.ownership!, _wallet.address)
171− ? 'spawn'
172− : undefined;
168+ const proxy = getSpawnProxy(starInfo.ownership!, _wallet.address);
173169
174170 if (proxy === undefined)
175171 throw new Error("Error: Address doesn't match proxy");
@@ -429,12 +425,7 @@ export default function useRoller() {
429425 }
430426
431427 const pointDetails = await api.getPoint(_point);
432− // const proxy = getProxy(pointDetails.ownership!, _wallet.address);
433− const proxy = isOwnerProxy(pointDetails.ownership!, _wallet.address)
434− ? 'own'
435− : isManagementProxy(pointDetails.ownership!, _wallet.address)
436− ? 'manage'
437− : undefined;
428+ const proxy = getManagerProxy(pointDetails.ownership!, _wallet.address);
438429
439430 if (proxy === undefined)
440431 throw new Error("Error: Address doesn't match proxy");
@@ -564,17 +555,7 @@ export default function useRoller() {
564555
565556 const pointDetails = await api.getPoint(_point);
566557 const ownership = pointDetails.ownership!;
567− const proxy = isOwnerProxy(ownership, _wallet.address)
568− ? 'own'
569− : proxyType === 'manage' &&
570− isManagementProxy(ownership, _wallet.address)
571− ? 'manage'
572− : proxyType === 'spawn' && isSpawnProxy(ownership, _wallet.address)
573− ? 'spawn'
574− : proxyType === 'transfer' &&
575− isTransferProxy(ownership, _wallet.address)
576− ? 'transfer'
577− : undefined;
558+ const proxy = getAddressProxy(ownership, _wallet.address, proxyType);
578559
579560 if (proxy === undefined)
580561 throw new Error("Error: Address doesn't match expected proxy");
@@ -609,11 +590,7 @@ export default function useRoller() {
609590 }
610591
611592 const pointDetails = await api.getPoint(_point);
612− const proxy = isOwnerProxy(pointDetails.ownership!, _wallet.address)
613− ? 'own'
614− : isTransferProxy(pointDetails.ownership!, _wallet.address)
615− ? 'transfer'
616− : undefined;
593+ const proxy = getTransferProxy(pointDetails.ownership!, _wallet.address);
617594
618595 if (proxy === undefined)
619596 throw new Error("Error: Address doesn't match proxy");
src/lib/utils/proxy.ts+41−25
@@ -1,33 +1,49 @@
1−import { Ownership } from '@urbit/roller-api';
1+import { Ownership, Proxy } from '@urbit/roller-api';
22
3−// TODO: test when all proxies are configured to the same address
4−export const getProxy = (
5− owner: Ownership,
6− address: string
7−): string | undefined =>
8− owner.owner?.address === address
9− ? 'own'
10− : owner.managementProxy?.address === address
11− ? 'manage'
12− : owner.spawnProxy?.address === address
13− ? 'spawn'
14− : owner.votingProxy?.address === address
15− ? 'vote'
16− : owner.transferProxy?.address === address
17− ? 'transfer'
18− : undefined;
19−
20−export const isManagementProxy = (owner: Ownership, address: string) =>
3+const isManagementProxy = (owner: Ownership, address: string) =>
214 owner.managementProxy?.address === address;
225
23−export const isTransferProxy = (owner: Ownership, address: string) =>
6+const isTransferProxy = (owner: Ownership, address: string) =>
247 owner.transferProxy?.address === address;
258
26−export const isVotingProxy = (owner: Ownership, address: string) =>
27− owner.votingProxy?.address === address;
28−
29−export const isSpawnProxy = (owner: Ownership, address: string) =>
9+const isSpawnProxy = (owner: Ownership, address: string) =>
3010 owner.spawnProxy?.address === address;
3111
32−export const isOwnerProxy = (owner: Ownership, address: string) =>
12+const isOwnerProxy = (owner: Ownership, address: string) =>
3313 owner.owner?.address === address;
14+
15+export const getSpawnProxy = (owner: Ownership, address: string) =>
16+ isOwnerProxy(owner, address)
17+ ? 'own'
18+ : isSpawnProxy(owner, address)
19+ ? 'spawn'
20+ : undefined;
21+
22+export const getManagerProxy = (owner: Ownership, address: string) =>
23+ isOwnerProxy(owner, address)
24+ ? 'own'
25+ : isManagementProxy(owner, address)
26+ ? 'manage'
27+ : undefined;
28+
29+export const getAddressProxy = (
30+ owner: Ownership,
31+ address: string,
32+ proxyType: Proxy
33+) =>
34+ isOwnerProxy(owner, address)
35+ ? 'own'
36+ : proxyType === 'manage' && isManagementProxy(owner, address)
37+ ? 'manage'
38+ : proxyType === 'spawn' && isSpawnProxy(owner, address)
39+ ? 'spawn'
40+ : proxyType === 'transfer' && isTransferProxy(owner, address)
41+ ? 'transfer'
42+ : undefined;
43+
44+export const getTransferProxy = (owner: Ownership, address: string) =>
45+ isOwnerProxy(owner, address)
46+ ? 'own'
47+ : isTransferProxy(owner, address)
48+ ? 'transfer'
49+ : undefined;