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';
41const getL2ProxyName = proxy =>
42 proxy === PROXY_TYPE.MANAGEMENT
44 : proxy === PROXY_TYPE.SPAWN
46 : proxy === PROXY_TYPE.TRANSFER
50const proxyFromDetails = (details, contracts, proxyType) => {
52 case PROXY_TYPE.MANAGEMENT:
53 return details.managementProxy;
54 case PROXY_TYPE.SPAWN:
55 return details.spawnProxy;
56 case PROXY_TYPE.TRANSFER:
57 return details.transferProxy;
58 case PROXY_TYPE.VOTING:
59 if (eqAddr(details.votingProxy, contracts.delegatedSending.address)) {
60 return `${details.votingProxy} (invites contract)`;
62 return details.votingProxy;
65 throw new Error(`Unknown proxyType: ${proxyType}`);
69const getButtonText = proxyType => {
71 case PROXY_TYPE.MANAGEMENT:
72 return 'Set Management Proxy';
73 case PROXY_TYPE.SPAWN:
74 return 'Set Spawn Proxy';
75 case PROXY_TYPE.TRANSFER:
76 return 'Set Transfer Proxy';
78 return 'Set Management Proxy';
82function useSetProxy(proxyType) {
83 const { contracts } = useNetwork();
84 const { pointCursor } = usePointCursor();
85 const { syncDetails } = usePointCache();
87 const _contracts = need.contracts(contracts);
88 const _point = need.point(pointCursor);
90 const { construct, ...rest } = useEthereumTransaction(
93 const txArgs = [_contracts, _point, address];
96 case PROXY_TYPE.MANAGEMENT:
97 return azimuth.ecliptic.setManagementProxy(...txArgs);
98 case PROXY_TYPE.SPAWN:
99 return azimuth.ecliptic.setSpawnProxy(...txArgs);
100 case PROXY_TYPE.TRANSFER:
101 return azimuth.ecliptic.setTransferProxy(...txArgs);
102 case PROXY_TYPE.VOTING:
103 return azimuth.ecliptic.setVotingProxy(...txArgs);
105 throw new Error(`Unknown proxyType ${proxyType}`);
108 [_contracts, _point, proxyType]
110 useCallback(() => syncDetails(_point), [_point, syncDetails]),
115 const unset = useCallback(() => {
116 construct(ETH_ZERO_ADDR);
126export default function SetProxy() {
127 const { data, pop } = useLocalRouter();
128 const { contracts } = useNetwork();
129 const { point, setLoading } = useRollerStore();
132 getPendingTransactions,
135 const [newAddress, setNewAddress] = useState('');
137 const _contracts = need.contracts(contracts);
139 const humanProxyType = proxyTypeToHuman(data.proxyType);
140 const properProxyType = capitalize(humanProxyType);
142 const { isL2, isL2Spawn } = point;
145 (data.proxyType === 'MANAGEMENT' && isL2) ||
146 (data.proxyType === 'SPAWN' && isL2Spawn) ||
147 (data.proxyType === 'TRANSFER' && isL2);
157 } = useSetProxy(data.proxyType);
159 const setProxy = useCallback(async () => {
161 if (newAddress === '') return;
163 await setProxyAddress(getL2ProxyName(data.proxyType), newAddress);
167 message: `${point.patp}'s ${humanProxyType} proxy has been set!`,
170 getPendingTransactions(point.value);
179 getPendingTransactions,
193 message: `${point.patp}'s ${humanProxyType} proxy has been set!`,
195 id: `${newAddress}-${point.value}`,
197 type: proxyTypeToL1TxnType(data.proxyType),
202 }, [completed]); // eslint-disable-line react-hooks/exhaustive-deps
204 const validateForm = useCallback((values, errors) => {
205 if (!values.unset && errors.address) {
210 const validate = useMemo(
214 unset: buildCheckboxValidator(),
215 address: buildAddressValidator(),
222 const onValues = useCallback(
223 ({ valid, values, form }) => {
227 form.change('address', '');
230 construct(values.address);
231 setNewAddress(values.address);
237 [construct, unconstruct, unset, setNewAddress]
240 const initialValues = useMemo(() => ({ unset: false }), []);
242 const proxyAddress = proxyFromDetails(point, _contracts, data.proxyType);
243 const isProxySet = !isZeroAddress(proxyAddress);
245 const header = useMemo(() => {
246 const pfix = completed ? 'New' : 'Edit';
247 return `${pfix} ${properProxyType} Key`;
248 }, [completed, properProxyType]);
253 <Row className="header-row">
258 <Grid className="w-full">
261 <Grid.Item full as={Text} className="mb4 f5 gray4">
262 {proxyTypeToHumanDescription(data.proxyType)}
267 className={cn('f6', {
278 initialValues={initialValues}>
279 {({ handleSubmit, values }) => (
281 <Grid.Item full as={Flex} row justify="between" align="center">
285 className={cn('mono mv3 gray4', {
286 black: !completed && isProxySet,
287 gray4: !completed && !isProxySet,
290 {isProxySet ? proxyAddress : 'Unset'}
295 <Grid.Item full className="mb4" />
302 label={`New ${properProxyType} Address`}
303 disabled={inputsLocked || values.unset}
307 <Grid.Item full as={FormError} />
313 className="set-proxy"
316 disabled={values.unset}
318 {getButtonText(data.proxyType)}
323 as={InlineEthereumTransaction}
325 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