1import React, { useCallback, useEffect } from 'react';
2import cn from 'classnames';
3import { Grid, Text, Input, 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 } from 'lib/wallet';
18import capitalize from 'lib/capitalize';
19import { useAddressInput, useCheckboxInput } from 'lib/useInputs';
20import useEthereumTransaction from 'lib/useEthereumTransaction';
22import ViewHeader from 'components/ViewHeader';
23import MiniBackButton from 'components/MiniBackButton';
24import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
25import { GAS_LIMITS } from 'lib/constants';
27const proxyFromDetails = (details, contracts, proxyType) => {
29 case PROXY_TYPE.MANAGEMENT:
30 return details.managementProxy;
31 case PROXY_TYPE.SPAWN:
32 return details.spawnProxy;
33 case PROXY_TYPE.TRANSFER:
34 return details.transferProxy;
35 case PROXY_TYPE.VOTING:
36 if (eqAddr(details.votingProxy, contracts.delegatedSending.address)) {
37 return `${details.votingProxy} (invites contract)`;
39 return details.votingProxy;
42 throw new Error(`Unknown proxyType: ${proxyType}`);
46function useSetProxy(proxyType) {
47 const { contracts } = useNetwork();
48 const { pointCursor } = usePointCursor();
49 const { syncOwnedPoint } = usePointCache();
51 const _contracts = need.contracts(contracts);
52 const _point = need.point(pointCursor);
54 const buildUnsignedTx = useCallback(
56 const txArgs = [_contracts, _point, address];
59 case PROXY_TYPE.MANAGEMENT:
60 return azimuth.ecliptic.setManagementProxy(...txArgs);
61 case PROXY_TYPE.SPAWN:
62 return azimuth.ecliptic.setSpawnProxy(...txArgs);
63 case PROXY_TYPE.TRANSFER:
64 return azimuth.ecliptic.setTransferProxy(...txArgs);
65 case PROXY_TYPE.VOTING:
66 return azimuth.ecliptic.setVotingProxy(...txArgs);
68 throw new Error(`Unknown proxyType ${proxyType}`);
71 [_contracts, _point, proxyType]
75 construct: _construct,
80 } = useEthereumTransaction(GAS_LIMITS.SET_PROXY);
82 // sync point details after success
85 syncOwnedPoint(_point);
87 }, [_point, confirmed, syncOwnedPoint]);
89 // construct the unsigned transaction when we have a valid address
90 const construct = useCallback(
91 address => _construct(buildUnsignedTx(address)),
92 [_construct, buildUnsignedTx]
96 const unset = useCallback(() => {
97 _construct(buildUnsignedTx(ETH_ZERO_ADDR));
98 }, [_construct, buildUnsignedTx]);
105 inputsLocked: inputsLocked,
110export default function AdminSetProxy() {
111 const { data, pop } = useLocalRouter();
112 const { getDetails } = usePointCache();
113 const { pointCursor } = usePointCursor();
114 const { contracts } = useNetwork();
116 const _point = need.point(pointCursor);
117 const _details = need.details(getDetails(_point));
118 const _contracts = need.contracts(contracts);
120 const properProxyType = capitalize(proxyTypeToHuman(data.proxyType));
129 } = useSetProxy(data.proxyType);
131 const [unsetInput, { data: isUnsetting }] = useCheckboxInput({
134 inverseLabel: 'Specify',
139 { pass: validAddress, data: address },
140 { reset: resetAddress },
141 ] = useAddressInput({
143 label: `New ${properProxyType} Address`,
144 disabled: inputsLocked || isUnsetting,
151 } else if (validAddress) {
166 const proxyAddress = proxyFromDetails(_details, _contracts, data.proxyType);
167 const isProxySet = !eqAddr(ETH_ZERO_ADDR, proxyAddress);
169 const proxyAddressLabel = `${
170 confirmed ? 'New' : 'Current'
171 } ${properProxyType} Address`;
175 <Grid.Item full as={MiniBackButton} onClick={() => pop()} />
177 <Grid.Item full as={ViewHeader}>
178 {properProxyType} Address
181 <Grid.Item full as={Text} className="mb4 f5">
182 {proxyTypeToHumanDescription(data.proxyType)}
188 className={cn('f6 mb1', {
194 <Grid.Item full as={Flex} row justify="between" align="center">
198 className={cn('mono', {
199 black: !confirmed && isProxySet,
200 gray4: !confirmed && !isProxySet,
203 {isProxySet ? proxyAddress : 'Unset'}
205 {!confirmed && isProxySet && (
206 <Flex.Item as={ToggleInput} {...unsetInput} />
211 <Grid.Item full className="mb4" />
213 <Grid.Item full as={Input} {...addressInput} className="mv4" />
218 as={InlineEthereumTransaction}
220 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