1import React, { useCallback, useEffect, 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/rollerStore';
16 proxyTypeToHumanDescription,
19import * as need from 'lib/need';
20import { useLocalRouter } from 'lib/LocalRouter';
21import { eqAddr, isZeroAddress } from 'lib/utils/address';
22import { capitalize } from 'lib/capitalize';
23import useEthereumTransaction from 'lib/useEthereumTransaction';
24import useRoller from 'lib/useRoller';
26import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
27import { ETH_ZERO_ADDR, GAS_LIMITS } from 'lib/constants';
28import { AddressInput } from 'form/Inputs';
31 buildCheckboxValidator,
32 buildAddressValidator,
33} from 'form/validators';
34import BridgeForm from 'form/BridgeForm';
35import FormError from 'form/FormError';
36import Window from 'components/L2/Window/Window';
37import HeaderPane from 'components/L2/Window/HeaderPane';
38import BodyPane from 'components/L2/Window/BodyPane';
39import { Row } from '@tlon/indigo-react';
41import './SetProxy.scss';
43const getL2ProxyName = proxy =>
44 proxy === PROXY_TYPE.MANAGEMENT
46 : proxy === PROXY_TYPE.SPAWN
48 : proxy === PROXY_TYPE.TRANSFER
52const proxyFromDetails = (details, contracts, proxyType) => {
54 case PROXY_TYPE.MANAGEMENT:
55 return details.managementProxy;
56 case PROXY_TYPE.SPAWN:
57 return details.spawnProxy;
58 case PROXY_TYPE.TRANSFER:
59 return details.transferProxy;
60 case PROXY_TYPE.VOTING:
61 if (eqAddr(details.votingProxy, contracts.delegatedSending.address)) {
62 return `${details.votingProxy} (invites contract)`;
64 return details.votingProxy;
67 throw new Error(`Unknown proxyType: ${proxyType}`);
71const getButtonText = proxyType => {
73 case PROXY_TYPE.MANAGEMENT:
74 return 'Set Management Proxy';
75 case PROXY_TYPE.SPAWN:
76 return 'Set Spawn Proxy';
77 case PROXY_TYPE.TRANSFER:
78 return 'Set Transfer Proxy';
80 return 'Set Management Proxy';
84function useSetProxy(proxyType) {
85 const { contracts } = useNetwork();
86 const { pointCursor } = usePointCursor();
87 const { syncDetails } = usePointCache();
89 const _contracts = need.contracts(contracts);
90 const _point = need.point(pointCursor);
92 const { construct, ...rest } = useEthereumTransaction(
95 const txArgs = [_contracts, _point, address];
98 case PROXY_TYPE.MANAGEMENT:
99 return azimuth.ecliptic.setManagementProxy(...txArgs);
100 case PROXY_TYPE.SPAWN:
101 return azimuth.ecliptic.setSpawnProxy(...txArgs);
102 case PROXY_TYPE.TRANSFER:
103 return azimuth.ecliptic.setTransferProxy(...txArgs);
104 case PROXY_TYPE.VOTING:
105 return azimuth.ecliptic.setVotingProxy(...txArgs);
107 throw new Error(`Unknown proxyType ${proxyType}`);
110 [_contracts, _point, proxyType]
112 useCallback(() => syncDetails(_point), [_point, syncDetails]),
117 const unset = useCallback(() => {
118 construct(ETH_ZERO_ADDR);
128export default function SetProxy() {
129 const { data, pop } = useLocalRouter();
130 const { contracts } = useNetwork();
131 const { point, setLoading } = useRollerStore();
134 getPendingTransactions,
137 const [newAddress, setNewAddress] = useState('');
139 const _contracts = need.contracts(contracts);
141 const humanProxyType = proxyTypeToHuman(data.proxyType);
142 const properProxyType = capitalize(humanProxyType);
144 const { isL2, isL2Spawn } = point;
147 (data.proxyType === 'MANAGEMENT' && isL2) ||
148 (data.proxyType === 'SPAWN' && isL2Spawn) ||
149 (data.proxyType === 'TRANSFER' && isL2);
159 } = useSetProxy(data.proxyType);
161 const setProxy = useCallback(async () => {
163 if (newAddress === '') return;
165 await setProxyAddress(getL2ProxyName(data.proxyType), newAddress);
169 message: `${point.patp}'s ${humanProxyType} proxy has been set!`,
172 getPendingTransactions(point.value);
181 getPendingTransactions,
195 message: `${point.patp}'s ${humanProxyType} proxy has been set!`,
197 id: `${newAddress}-${point.value}`,
199 type: proxyTypeToL1TxnType(data.proxyType),
201 time: new Date().getTime(),
205 }, [completed]); // eslint-disable-line react-hooks/exhaustive-deps
207 const validateForm = useCallback((values, errors) => {
208 if (!values.unset && errors.address) {
213 const validate = useMemo(
217 unset: buildCheckboxValidator(),
218 address: buildAddressValidator(),
225 const onValues = useCallback(
226 ({ valid, values, form }) => {
230 form.change('address', '');
233 construct(values.address);
234 setNewAddress(values.address);
240 [construct, unconstruct, unset, setNewAddress]
243 const initialValues = useMemo(() => ({ unset: false }), []);
245 const proxyAddress = proxyFromDetails(point, _contracts, data.proxyType);
246 const isProxySet = !isZeroAddress(proxyAddress);
248 const header = useMemo(() => {
249 const pfix = completed ? 'New' : 'Edit';
250 return `${pfix} ${properProxyType} Key`;
251 }, [completed, properProxyType]);
254 <Window className="set-proxy">
256 <Row className="header-row">
261 <Grid className="w-full">
264 <Grid.Item full as={Text} className="mb4 f5">
265 {proxyTypeToHumanDescription(data.proxyType)}
270 className={cn('f5', {
281 initialValues={initialValues}>
282 {({ handleSubmit, values }) => (
284 <Grid.Item full as={Flex} row justify="between" align="center">
288 style={{ overflowWrap: 'anywhere' }}
289 className={cn('mono mv3 gray4', {
290 black: !completed && isProxySet,
291 gray4: !completed && !isProxySet,
294 {isProxySet ? proxyAddress : 'Not set'}
299 <Grid.Item full className="mb4" />
306 label={`New ${properProxyType} Address`}
307 disabled={inputsLocked || values.unset}
311 <Grid.Item full as={FormError} />
317 className="set-proxy"
320 disabled={values.unset}
322 {getButtonText(data.proxyType)}
327 as={InlineEthereumTransaction}
329 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