on 6542dce help sign in
bridge/src/views/SetProxy.js
181 lines 5.0 kB history raw
1import Maybe from 'folktale/maybe';
2import React from 'react';
3import * as need from '../lib/need';
4import * as azimuth from 'azimuth-js';
5import {
6 H1,
7 P,
8 InnerLabel,
9 ShowBlockie,
10 Anchor,
11 HorizontalSelector,
12} from '../components/old/Base';
13import { AddressInput } from '../components/old/Base';
14import * as ob from 'urbit-ob';
16import { PROXY_TYPE, renderProxyType } from '../lib/proxy';
18import { NETWORK_TYPES } from '../lib/network';
20import StatelessTransaction from '../components/old/StatelessTransaction';
22import { isValidAddress } from '../lib/wallet';
23import { withNetwork } from '../store/network';
24import { compose } from '../lib/lib';
25import { withPointCursor } from '../store/pointCursor';
26import View from 'components/View';
28class _SetProxy extends React.Component {
29 constructor(props) {
30 super(props);
32 const issuingPoint = need.pointCursor(props.pointCursor);
34 this.state = {
35 proxyAddress: '',
36 issuingPoint: issuingPoint,
37 setProxy: true,
38 };
40 this.statelessRef = React.createRef();
41 this.handleSetUnset = this.handleSetUnset.bind(this);
42 this.handleAddressInput = this.handleAddressInput.bind(this);
43 this.createUnsignedTxn = this.createUnsignedTxn.bind(this);
44 }
46 handleSetUnset(selected) {
47 const set = selected === 'set';
48 this.setState({
49 setProxy: set,
50 proxyAddress: set ? '' : '0x0000000000000000000000000000000000000000',
51 });
52 }
54 handleAddressInput(proxyAddress) {
55 this.setState({ proxyAddress });
57 this.statelessRef.current.clearTxn();
58 }
60 createUnsignedTxn(proxyAddress) {
61 const { state, props } = this;
63 const validContracts = need.contracts(props.contracts);
64 const validPoint = need.pointCursor(props.pointCursor);
66 const txArgs = [validContracts, validPoint, state.proxyAddress];
68 if (props.proxyType === PROXY_TYPE.TRANSFER_PROXY) {
69 return Maybe.Just(azimuth.ecliptic.setTransferProxy(...txArgs));
70 }
72 if (props.proxyType === PROXY_TYPE.SPAWN_PROXY) {
73 return Maybe.Just(azimuth.ecliptic.setSpawnProxy(...txArgs));
74 }
76 if (props.proxyType === PROXY_TYPE.MANAGEMENT_PROXY) {
77 return Maybe.Just(azimuth.ecliptic.setManagementProxy(...txArgs));
78 }
80 return Maybe.Nothing();
81 }
83 render() {
84 const { props, state } = this;
86 const renderedProxyType = renderProxyType(props.proxyType);
87 const validAddress = isValidAddress(state.proxyAddress);
88 const canGenerate = validAddress === true;
90 const esvisible =
91 props.networkType === NETWORK_TYPES.ROPSTEN ||
92 props.networkType === NETWORK_TYPES.MAINNET;
94 const esdomain =
95 props.networkType === NETWORK_TYPES.ROPSTEN
96 ? 'ropsten.etherscan.io'
97 : 'etherscan.io';
99 const ucFirst = w => w.charAt(0).toUpperCase() + w.slice(1);
101 const setUnset = [
102 { title: 'Set', value: 'set' },
103 { title: 'Unset', value: 'unset' },
104 ];
106 const titleVerb = this.state.setProxy ? 'Set' : 'Unset';
108 let addressInput = null;
109 if (this.state.setProxy) {
110 addressInput = (
111 <>
112 <P className="mt-8">
113 {'Please provide an Ethereum address to act as the ' +
114 `${renderedProxyType} proxy. You can also use Wallet ` +
115 'Generator to generate a keypair.'}
116 </P>
118 <AddressInput
119 className="mono mt-8"
120 prop-size="lg"
121 prop-format="innerLabel"
122 placeholder={`e.g. 0x84295d5e054d8cff5a22428b195f5a1615bd644f`}
123 value={state.proxyAddress}
124 onChange={v => this.handleAddressInput(v)}>
125 <InnerLabel>{'Proxy Address'}</InnerLabel>
126 <ShowBlockie className={'mt-1'} address={state.proxyAddress} />
127 </AddressInput>
129 <Anchor
130 className={'mt-1'}
131 prop-size={'sm'}
132 prop-disabled={!isValidAddress(state.proxyAddress) || !esvisible}
133 target={'_blank'}
134 href={`https://${esdomain}/address/${state.proxyAddress}`}>
135 {'View on Etherscan ↗'}
136 </Anchor>
137 </>
138 );
139 }
141 return (
142 <View>
143 <H1>
144 {`${titleVerb} ${ucFirst(renderedProxyType)} Proxy For `}
145 <code>{`${ob.patp(state.issuingPoint)}`}</code>
146 </H1>
148 <HorizontalSelector
149 options={setUnset}
150 onChange={this.handleSetUnset}
151 className="mt-8"
152 />
154 {addressInput}
156 <StatelessTransaction
157 ref={this.statelessRef}
158 createUnsignedTxn={this.createUnsignedTxn}
159 canGenerate={canGenerate}
160 />
161 </View>
162 );
163 }
166const SetProxy = compose(
167 withNetwork,
168 withPointCursor
169)(_SetProxy);
171export const SetManagementProxy = props => (
172 <SetProxy {...props} proxyType={PROXY_TYPE.MANAGEMENT_PROXY} />
173);
175export const SetSpawnProxy = props => (
176 <SetProxy {...props} proxyType={PROXY_TYPE.SPAWN_PROXY} />
177);
179export const SetTransferProxy = props => (
180 <SetProxy {...props} proxyType={PROXY_TYPE.TRANSFER_PROXY} />
181);