1import React, { useCallback, useMemo, useState } from 'react';
2import cn from 'classnames';
3import { Grid, Text, Flex, Button } from 'indigo-react';
4import * as azimuth from 'azimuth-js';
6import './SetProxy.scss';
8import { useNetwork } from 'store/network';
9import { usePointCache } from 'store/pointCache';
10import { usePointCursor } from 'store/pointCursor';
11import { useRollerStore } from 'store/roller';
16 proxyTypeToHumanDescription,
18import * as need from 'lib/need';
19import { useLocalRouter } from 'lib/LocalRouter';
20import { eqAddr, isZeroAddress } from 'lib/utils/address';
21import { capitalize } from 'lib/capitalize';
22import useEthereumTransaction from 'lib/useEthereumTransaction';
23import useRoller from 'lib/useRoller';
25import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
26import { ETH_ZERO_ADDR, GAS_LIMITS } from 'lib/constants';
27import { AddressInput } from 'form/Inputs';
30 buildCheckboxValidator,
31 buildAddressValidator,
32} from 'form/validators';
33import BridgeForm from 'form/BridgeForm';
34import FormError from 'form/FormError';
36const getL2ProxyName = proxy =>
37 proxy === PROXY_TYPE.MANAGEMENT
39 : proxy === PROXY_TYPE.SPAWN
41 : proxy === PROXY_TYPE.TRANSFER
45const proxyFromDetails = (details, contracts, proxyType) => {
47 case PROXY_TYPE.MANAGEMENT:
48 return details.managementProxy;
49 case PROXY_TYPE.SPAWN:
50 return details.spawnProxy;
51 case PROXY_TYPE.TRANSFER:
52 return details.transferProxy;
53 case PROXY_TYPE.VOTING:
54 if (eqAddr(details.votingProxy, contracts.delegatedSending.address)) {
55 return `${details.votingProxy} (invites contract)`;
57 return details.votingProxy;
60 throw new Error(`Unknown proxyType: ${proxyType}`);
64function useSetProxy(proxyType) {
65 const { contracts } = useNetwork();
66 const { pointCursor } = usePointCursor();
67 const { syncDetails } = usePointCache();
69 const _contracts = need.contracts(contracts);
70 const _point = need.point(pointCursor);
72 const { construct, ...rest } = useEthereumTransaction(
75 const txArgs = [_contracts, _point, address];
78 case PROXY_TYPE.MANAGEMENT:
79 return azimuth.ecliptic.setManagementProxy(...txArgs);
80 case PROXY_TYPE.SPAWN:
81 return azimuth.ecliptic.setSpawnProxy(...txArgs);
82 case PROXY_TYPE.TRANSFER:
83 return azimuth.ecliptic.setTransferProxy(...txArgs);
84 case PROXY_TYPE.VOTING:
85 return azimuth.ecliptic.setVotingProxy(...txArgs);
87 throw new Error(`Unknown proxyType ${proxyType}`);
90 [_contracts, _point, proxyType]
92 useCallback(() => syncDetails(_point), [_point, syncDetails]),
97 const unset = useCallback(() => {
98 construct(ETH_ZERO_ADDR);
108export default function SetProxy() {
109 const { data, pop } = useLocalRouter();
110 const { getDetails } = usePointCache();
111 const { pointCursor } = usePointCursor();
112 const { contracts } = useNetwork();
113 const { currentL2, currentL2Spawn } = useRollerStore();
114 const { setProxyAddress, getPendingTransactions } = useRoller();
115 const [newAddress, setNewAddress] = useState('');
117 const _point = need.point(pointCursor);
118 const _details = need.details(getDetails(_point));
119 const _contracts = need.contracts(contracts);
121 const properProxyType = capitalize(proxyTypeToHuman(data.proxyType));
124 data.proxyType === 'MANAGEMENT' && (currentL2Spawn || !currentL2)
126 : data.proxyType === 'MANAGEMENT' && currentL2 && !currentL2Spawn
128 : data.proxyType === 'SPAWN' && currentL2
139 } = useSetProxy(data.proxyType);
141 const setProxy = useCallback(async () => {
143 console.log(newAddress);
144 if (newAddress === '') return;
146 await setProxyAddress(getL2ProxyName(data.proxyType), newAddress);
147 getPendingTransactions(_point);
152 // setLoading(false);
156 getPendingTransactions,
163 const validateForm = useCallback((values, errors) => {
165 if (!values.unset && errors.address) {
170 const validate = useMemo(
174 unset: buildCheckboxValidator(),
175 address: buildAddressValidator(),
182 const onValues = useCallback(
183 ({ valid, values, form }) => {
187 form.change('address', '');
190 construct(values.address);
191 setNewAddress(values.address);
197 [construct, unconstruct, unset, setNewAddress]
200 const initialValues = useMemo(() => ({ unset: false }), []);
202 const proxyAddress = proxyFromDetails(_details, _contracts, data.proxyType);
203 const isProxySet = !isZeroAddress(proxyAddress);
205 const header = useMemo(() => {
206 const pfix = completed ? 'New' : 'Edit';
207 return `${pfix} ${properProxyType} Key`;
208 }, [completed, properProxyType]);
212 <Grid.Item full className="f5 mb2">
218 <Grid.Item full as={Text} className="mb4 f5 gray4">
219 {proxyTypeToHumanDescription(data.proxyType)}
224 className={cn('f6', {
235 initialValues={initialValues}>
236 {({ handleSubmit, values }) => (
238 <Grid.Item full as={Flex} row justify="between" align="center">
242 className={cn('mono mv3 gray4', {
243 black: !completed && isProxySet,
244 gray4: !completed && !isProxySet,
247 {isProxySet ? proxyAddress : 'Unset'}
252 <Grid.Item full className="mb4" />
259 label={`New ${properProxyType} Address`}
260 disabled={inputsLocked || values.unset}
264 <Grid.Item full as={FormError} />
270 className="set-proxy"
273 disabled={values.unset}
280 as={InlineEthereumTransaction}
282 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