1import React, { useCallback, useMemo } from 'react';
2import cn from 'classnames';
3import { Grid, Text, Flex, ToggleInput } from 'indigo-react';
4import * as azimuth from 'azimuth-js';
6import { useNetwork } from 'store/network';
7import { usePointCache } from 'store/pointCache';
8import { usePointCursor } from 'store/pointCursor';
13 proxyTypeToHumanDescription,
15import * as need from 'lib/need';
16import { useLocalRouter } from 'lib/LocalRouter';
17import { ETH_ZERO_ADDR, eqAddr, isZeroAddress } from 'lib/wallet';
18import capitalize from 'lib/capitalize';
19import useEthereumTransaction from 'lib/useEthereumTransaction';
21import ViewHeader from 'components/ViewHeader';
22import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
23import { GAS_LIMITS } from 'lib/constants';
24import { AddressInput } from 'form/Inputs';
27 buildCheckboxValidator,
28 buildAddressValidator,
29} from 'form/validators';
30import BridgeForm from 'form/BridgeForm';
31import FormError from 'form/FormError';
32import CopiableAddress from 'components/CopiableAddress';
34const proxyFromDetails = (details, contracts, proxyType) => {
36 case PROXY_TYPE.MANAGEMENT:
37 return details.managementProxy;
38 case PROXY_TYPE.SPAWN:
39 return details.spawnProxy;
40 case PROXY_TYPE.TRANSFER:
41 return details.transferProxy;
42 case PROXY_TYPE.VOTING:
43 if (eqAddr(details.votingProxy, contracts.delegatedSending.address)) {
44 return `${details.votingProxy} (invites contract)`;
46 return details.votingProxy;
49 throw new Error(`Unknown proxyType: ${proxyType}`);
53function useSetProxy(proxyType) {
54 const { contracts } = useNetwork();
55 const { pointCursor } = usePointCursor();
56 const { syncDetails } = usePointCache();
58 const _contracts = need.contracts(contracts);
59 const _point = need.point(pointCursor);
61 const { construct, ...rest } = useEthereumTransaction(
64 const txArgs = [_contracts, _point, address];
67 case PROXY_TYPE.MANAGEMENT:
68 return azimuth.ecliptic.setManagementProxy(...txArgs);
69 case PROXY_TYPE.SPAWN:
70 return azimuth.ecliptic.setSpawnProxy(...txArgs);
71 case PROXY_TYPE.TRANSFER:
72 return azimuth.ecliptic.setTransferProxy(...txArgs);
73 case PROXY_TYPE.VOTING:
74 return azimuth.ecliptic.setVotingProxy(...txArgs);
76 throw new Error(`Unknown proxyType ${proxyType}`);
79 [_contracts, _point, proxyType]
81 useCallback(() => syncDetails(_point), [_point, syncDetails]),
86 const unset = useCallback(() => {
87 construct(ETH_ZERO_ADDR);
97export default function AdminSetProxy() {
98 const { data, pop } = useLocalRouter();
99 const { getDetails } = usePointCache();
100 const { pointCursor } = usePointCursor();
101 const { contracts } = useNetwork();
103 const _point = need.point(pointCursor);
104 const _details = need.details(getDetails(_point));
105 const _contracts = need.contracts(contracts);
107 const properProxyType = capitalize(proxyTypeToHuman(data.proxyType));
116 } = useSetProxy(data.proxyType);
118 const validateForm = useCallback((values, errors) => {
119 if (!values.unset && errors.address) {
124 const validate = useMemo(
128 unset: buildCheckboxValidator(),
129 address: buildAddressValidator(),
136 const onValues = useCallback(
137 ({ valid, values, form }) => {
141 form.change('address', '');
143 construct(values.address);
149 [construct, unconstruct, unset]
152 const initialValues = useMemo(() => ({ unset: false }), []);
154 const proxyAddress = proxyFromDetails(_details, _contracts, data.proxyType);
155 const isProxySet = !isZeroAddress(proxyAddress);
157 const proxyAddressLabel = `${
158 completed ? 'New' : 'Current'
159 } ${properProxyType} Address`;
163 <Grid.Item full as={ViewHeader}>
164 {properProxyType} Address
167 <Grid.Item full as={Text} className="mb4 f5">
168 {proxyTypeToHumanDescription(data.proxyType)}
174 className={cn('f6 mb1', {
183 initialValues={initialValues}>
184 {({ handleSubmit, values }) => (
186 <Grid.Item full as={Flex} row justify="between" align="center">
190 className={cn('mono', {
191 black: !completed && isProxySet,
192 gray4: !completed && !isProxySet,
196 <CopiableAddress>{proxyAddress}</CopiableAddress>
201 {!completed && isProxySet && (
206 inverseLabel="Specify"
207 disabled={inputsLocked}
213 <Grid.Item full className="mb4" />
220 label={`New ${properProxyType} Address`}
221 disabled={inputsLocked || values.unset}
225 <Grid.Item full as={FormError} />
229 as={InlineEthereumTransaction}
231 onReturn={() => pop()}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list