@@ -1,25 +1,28 @@
1−import React, { useCallback } from 'react';
1+import React, { useCallback, useEffect, useState } from 'react';
2+import cn from 'classnames';
2−import { Grid, P, Text, Input, Flex, LinkButton } from 'indigo-react';
3+import { Grid, Text, Input, Flex, LinkButton } from 'indigo-react';
3import * as azimuth from 'azimuth-js';
4import * as azimuth from 'azimuth-js';
5import { useNetwork } from 'store/network';
6import { useNetwork } from 'store/network';
6−
7+import { usePointCache } from 'store/pointCache';
7−import * as need from 'lib/need';
8−import { useLocalRouter } from 'lib/LocalRouter';
9−import { ETH_ZERO_ADDR, eqAddr } from 'lib/wallet';
11−import ViewHeader from 'components/ViewHeader';
12import { usePointCursor } from 'store/pointCursor';
8import { usePointCursor } from 'store/pointCursor';
14 PROXY_TYPE,
11 PROXY_TYPE,
15 proxyTypeToHuman,
12 proxyTypeToHuman,
16 proxyTypeToHumanDescription,
13 proxyTypeToHumanDescription,
17} from 'lib/proxy';
14} from 'lib/proxy';
18−import MiniBackButton from 'components/MiniBackButton';
15+import * as need from 'lib/need';
16+import { useLocalRouter } from 'lib/LocalRouter';
17+import { ETH_ZERO_ADDR, eqAddr } from 'lib/wallet';
19import capitalize from 'lib/capitalize';
18import capitalize from 'lib/capitalize';
20−import { usePointCache } from 'store/pointCache';
21−import { GenerateButton } from 'components/Buttons';
22import { useAddressInput } from 'lib/useInputs';
19import { useAddressInput } from 'lib/useInputs';
20+import useEthereumTransaction from 'lib/useEthereumTransaction';
22+import ViewHeader from 'components/ViewHeader';
23+import MiniBackButton from 'components/MiniBackButton';
24+import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
25+import { GAS_LIMITS } from 'lib/constants';
24const proxyFromDetails = (details, proxyType) => {
27const proxyFromDetails = (details, proxyType) => {
25 switch (proxyType) {
28 switch (proxyType) {
@@ -36,16 +39,18 @@ const proxyFromDetails = (details, proxyType) => {
39−function useSetProxy(proxyType) {
42+function useSetProxy(proxyType, address) {
40− // TODO: abstract eth login into higher level useTransaction()
41 const { contracts } = useNetwork();
43 const { contracts } = useNetwork();
42 const { pointCursor } = usePointCursor();
44 const { pointCursor } = usePointCursor();
45+ const { syncOwnedPoint } = usePointCache();
47+ const _contracts = need.contracts(contracts);
48+ const _point = need.point(pointCursor);
50+ const [isUnsetting, setUnsetting] = useState(false);
44 const buildUnsignedTx = useCallback(
52 const buildUnsignedTx = useCallback(
45 address => {
53 address => {
46− const _contracts = need.contracts(contracts);
47− const _point = need.point(pointCursor);
49 const txArgs = [_contracts, _point, address];
54 const txArgs = [_contracts, _point, address];
51 switch (proxyType) {
56 switch (proxyType) {
@@ -56,30 +61,55 @@ function useSetProxy(proxyType) {
56 case PROXY_TYPE.TRANSFER:
61 case PROXY_TYPE.TRANSFER:
57 return azimuth.ecliptic.setTransferProxy(...txArgs);
62 return azimuth.ecliptic.setTransferProxy(...txArgs);
58 case PROXY_TYPE.VOTING:
63 case PROXY_TYPE.VOTING:
59− return azimuth.ecliptic.setTransferProxy(...txArgs);
64+ return azimuth.ecliptic.setVotingProxy(...txArgs);
61 throw new Error(`Unknown proxyType ${proxyType}`);
66 throw new Error(`Unknown proxyType ${proxyType}`);
64− [proxyType, contracts, pointCursor]
69+ [_contracts, _point, proxyType]
75+ construct: _construct,
86+ } = useEthereumTransaction(GAS_LIMITS.SET_PROXY);
88+ // sync point details after success
91+ syncOwnedPoint(_point);
93+ }, [_point, confirmed, syncOwnedPoint]);
95+ // construct the unsigned transaction when we have a valid address
67− const generate = useCallback(
96+ const construct = useCallback(
69− const utx = buildUnsignedTx(address);
97+ address => _construct(buildUnsignedTx(address)),
72− [buildUnsignedTx]
98+ [_construct, buildUnsignedTx]
75− const unset = useCallback(() => generate(ETH_ZERO_ADDR), [generate]);
102+ const unset = useCallback(() => {
76−
103+ setUnsetting(true);
77− const locked = false;
104+ _construct(buildUnsignedTx(ETH_ZERO_ADDR));
105+ }, [_construct, buildUnsignedTx]);
80− locked,
108+ construct,
111+ inputsLocked: inputsLocked || isUnsetting,
@@ -92,14 +122,18 @@ export default function AdminSetProxy() {
93 const properProxyType = capitalize(proxyTypeToHuman(data.proxyType));
123 const properProxyType = capitalize(proxyTypeToHuman(data.proxyType));
95− const { generate, unset, locked } = useSetProxy(data.proxyType);
125+ const { construct, unset, inputsLocked, bind } = useSetProxy(data.proxyType);
96 const [addressInput, { pass, data: address }] = useAddressInput({
126 const [addressInput, { pass, data: address }] = useAddressInput({
97 name: 'address',
127 name: 'address',
98 label: `New ${properProxyType} Address`,
128 label: `New ${properProxyType} Address`,
99− disabled: locked,
129+ disabled: inputsLocked,
102− const _generate = useCallback(() => generate(address), [address, generate]);
132+ useEffect(() => {
136+ }, [pass, address, construct]);
104 const proxyAddress = proxyFromDetails(details, data.proxyType);
138 const proxyAddress = proxyFromDetails(details, data.proxyType);
105 const isProxySet = !eqAddr(ETH_ZERO_ADDR, proxyAddress);
139 const isProxySet = !eqAddr(ETH_ZERO_ADDR, proxyAddress);
@@ -117,11 +151,17 @@ export default function AdminSetProxy() {
117 Current {properProxyType} Address
151 Current {properProxyType} Address
118 </Grid.Item>
152 </Grid.Item>
119 <Grid.Item full as={Flex} row justify="between" align="center">
153 <Grid.Item full as={Flex} row justify="between" align="center">
120− <Flex.Item flex as={Text} className="mono">
157+ className={cn({
158+ 'mono black': isProxySet,
121 {isProxySet ? proxyAddress : 'Unset'}
161 {isProxySet ? proxyAddress : 'Unset'}
122 </Flex.Item>
162 </Flex.Item>
123 {isProxySet && (
163 {isProxySet && (
124− <Flex.Item as={LinkButton} onClick={unset}>
164+ <Flex.Item as={LinkButton} onClick={unset} disabled={inputsLocked}>
126 </Flex.Item>
166 </Flex.Item>
@@ -129,9 +169,7 @@ export default function AdminSetProxy() {
130 <Grid.Item full as={Input} {...addressInput} className="mv4" />
170 <Grid.Item full as={Input} {...addressInput} className="mv4" />
132− <Grid.Item full as={GenerateButton} onClick={_generate} disabled={!pass}>
172+ <Grid.Item full as={InlineEthereumTransaction} {...bind} />
133− Generate & Sign Transaction