1import React, { useCallback, useEffect, useState } from 'react';
2import cn from 'classnames';
3import { Grid, Text, Input, Flex, LinkButton } 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 } 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 [isUnsetting, setUnsetting] = useState(false);
56 const buildUnsignedTx = useCallback(
58 const txArgs = [_contracts, _point, address];
61 case PROXY_TYPE.MANAGEMENT:
62 return azimuth.ecliptic.setManagementProxy(...txArgs);
63 case PROXY_TYPE.SPAWN:
64 return azimuth.ecliptic.setSpawnProxy(...txArgs);
65 case PROXY_TYPE.TRANSFER:
66 return azimuth.ecliptic.setTransferProxy(...txArgs);
67 case PROXY_TYPE.VOTING:
68 return azimuth.ecliptic.setVotingProxy(...txArgs);
70 throw new Error(`Unknown proxyType ${proxyType}`);
73 [_contracts, _point, proxyType]
77 construct: _construct,
81 } = useEthereumTransaction(GAS_LIMITS.SET_PROXY);
83 // sync point details after success
86 syncOwnedPoint(_point);
88 }, [_point, confirmed, syncOwnedPoint]);
90 // construct the unsigned transaction when we have a valid address
91 const construct = useCallback(
92 address => _construct(buildUnsignedTx(address)),
93 [_construct, buildUnsignedTx]
97 const unset = useCallback(() => {
99 _construct(buildUnsignedTx(ETH_ZERO_ADDR));
100 }, [_construct, buildUnsignedTx]);
107 inputsLocked: inputsLocked || isUnsetting,
112export default function AdminSetProxy() {
113 const { data, pop } = useLocalRouter();
114 const { getDetails } = usePointCache();
115 const { pointCursor } = usePointCursor();
116 const { contracts } = useNetwork();
118 const _point = need.point(pointCursor);
119 const _details = need.details(getDetails(_point));
120 const _contracts = need.contracts(contracts);
122 const properProxyType = capitalize(proxyTypeToHuman(data.proxyType));
124 const { construct, unset, inputsLocked, confirmed, bind } = useSetProxy(
127 const [addressInput, { pass, data: address }] = useAddressInput({
129 label: `New ${properProxyType} Address`,
130 disabled: inputsLocked,
137 }, [pass, address, construct]);
139 const proxyAddress = proxyFromDetails(_details, _contracts, data.proxyType);
140 const isProxySet = !eqAddr(ETH_ZERO_ADDR, proxyAddress);
142 const proxyAddressLabel = `${
143 confirmed ? 'New' : 'Current'
144 } ${properProxyType} Address`;
148 <Grid.Item full as={MiniBackButton} onClick={() => pop()} />
150 <Grid.Item full as={ViewHeader}>
151 {properProxyType} Address
154 <Grid.Item full as={Text} className="mb4 f5">
155 {proxyTypeToHumanDescription(data.proxyType)}
161 className={cn('f6 mb1', {
167 <Grid.Item full as={Flex} row justify="between" align="center">
171 className={cn('mono', {
172 black: !confirmed && isProxySet,
173 gray4: !confirmed && !isProxySet,
176 {isProxySet ? proxyAddress : 'Unset'}
178 {!confirmed && isProxySet && (
179 <Flex.Item as={LinkButton} onClick={unset} disabled={inputsLocked}>
186 <Grid.Item full className="mb4" />
188 <Grid.Item full as={Input} {...addressInput} className="mv4" />
193 as={InlineEthereumTransaction}
195 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