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';
35import Window from 'components/L2/Window/Window';
36import HeaderPane from 'components/L2/Window/HeaderPane';
37import BodyPane from 'components/L2/Window/BodyPane';
38import { Row } from '@tlon/indigo-react';
40const getL2ProxyName = proxy =>
41 proxy === PROXY_TYPE.MANAGEMENT
43 : proxy === PROXY_TYPE.SPAWN
45 : proxy === PROXY_TYPE.TRANSFER
49const proxyFromDetails = (details, contracts, proxyType) => {
51 case PROXY_TYPE.MANAGEMENT:
52 return details.managementProxy;
53 case PROXY_TYPE.SPAWN:
54 return details.spawnProxy;
55 case PROXY_TYPE.TRANSFER:
56 return details.transferProxy;
57 case PROXY_TYPE.VOTING:
58 if (eqAddr(details.votingProxy, contracts.delegatedSending.address)) {
59 return `${details.votingProxy} (invites contract)`;
61 return details.votingProxy;
64 throw new Error(`Unknown proxyType: ${proxyType}`);
68function useSetProxy(proxyType) {
69 const { contracts } = useNetwork();
70 const { pointCursor } = usePointCursor();
71 const { syncDetails } = usePointCache();
73 const _contracts = need.contracts(contracts);
74 const _point = need.point(pointCursor);
76 const { construct, ...rest } = useEthereumTransaction(
79 const txArgs = [_contracts, _point, address];
82 case PROXY_TYPE.MANAGEMENT:
83 return azimuth.ecliptic.setManagementProxy(...txArgs);
84 case PROXY_TYPE.SPAWN:
85 return azimuth.ecliptic.setSpawnProxy(...txArgs);
86 case PROXY_TYPE.TRANSFER:
87 return azimuth.ecliptic.setTransferProxy(...txArgs);
88 case PROXY_TYPE.VOTING:
89 return azimuth.ecliptic.setVotingProxy(...txArgs);
91 throw new Error(`Unknown proxyType ${proxyType}`);
94 [_contracts, _point, proxyType]
96 useCallback(() => syncDetails(_point), [_point, syncDetails]),
101 const unset = useCallback(() => {
102 construct(ETH_ZERO_ADDR);
112export default function SetProxy() {
113 const { data, pop } = useLocalRouter();
114 const { getDetails } = usePointCache();
115 const { pointCursor } = usePointCursor();
116 const { contracts } = useNetwork();
117 const { currentL2, currentL2Spawn } = useRollerStore();
118 const { setProxyAddress, getPendingTransactions } = useRoller();
119 const [newAddress, setNewAddress] = useState('');
121 const _point = need.point(pointCursor);
122 const _details = need.details(getDetails(_point));
123 const _contracts = need.contracts(contracts);
125 const properProxyType = capitalize(proxyTypeToHuman(data.proxyType));
128 data.proxyType === 'MANAGEMENT' && (currentL2Spawn || !currentL2)
130 : data.proxyType === 'MANAGEMENT' && currentL2 && !currentL2Spawn
132 : data.proxyType === 'SPAWN' && currentL2
143 } = useSetProxy(data.proxyType);
145 const setProxy = useCallback(async () => {
147 console.log(newAddress);
148 if (newAddress === '') return;
150 await setProxyAddress(getL2ProxyName(data.proxyType), newAddress);
151 getPendingTransactions(_point);
156 // setLoading(false);
160 getPendingTransactions,
167 const validateForm = useCallback((values, errors) => {
168 if (!values.unset && errors.address) {
173 const validate = useMemo(
177 unset: buildCheckboxValidator(),
178 address: buildAddressValidator(),
185 const onValues = useCallback(
186 ({ valid, values, form }) => {
190 form.change('address', '');
193 construct(values.address);
194 setNewAddress(values.address);
200 [construct, unconstruct, unset, setNewAddress]
203 const initialValues = useMemo(() => ({ unset: false }), []);
205 const proxyAddress = proxyFromDetails(_details, _contracts, data.proxyType);
206 const isProxySet = !isZeroAddress(proxyAddress);
208 const header = useMemo(() => {
209 const pfix = completed ? 'New' : 'Edit';
210 return `${pfix} ${properProxyType} Key`;
211 }, [completed, properProxyType]);
216 <Row className="header-row">
221 <Grid style={{ width: '100%' }}>
224 <Grid.Item full as={Text} className="mb4 f5 gray4">
225 {proxyTypeToHumanDescription(data.proxyType)}
230 className={cn('f6', {
241 initialValues={initialValues}>
242 {({ handleSubmit, values }) => (
244 <Grid.Item full as={Flex} row justify="between" align="center">
248 className={cn('mono mv3 gray4', {
249 black: !completed && isProxySet,
250 gray4: !completed && !isProxySet,
253 {isProxySet ? proxyAddress : 'Unset'}
258 <Grid.Item full className="mb4" />
265 label={`New ${properProxyType} Address`}
266 disabled={inputsLocked || values.unset}
270 <Grid.Item full as={FormError} />
276 className="set-proxy"
279 disabled={values.unset}
286 as={InlineEthereumTransaction}
288 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