on cf73cee help sign in
bridge/src/views/IssueChild.js
515 lines 13 kB history raw
1import Maybe from 'folktale/maybe'
2import React from 'react'
3import * as azimuth from 'azimuth-js'
4import * as ob from 'urbit-ob'
6import { Row, Col, H1, H3, P, Anchor, Warning } from '../components/Base'
7import { Button, ShowBlockie, ValidatedSigil } from '../components/Base'
8import { PointInput, AddressInput, InnerLabel } from '../components/Base'
10import StatelessTransaction from '../components/StatelessTransaction'
12import { ROUTE_NAMES } from '../lib/router'
13import { NETWORK_NAMES } from '../lib/network'
14import { BRIDGE_ERROR } from '../lib/error'
15import { getSpawnCandidate } from '../lib/child'
16import {
17 canDecodePatp,
18 sendSignedTransaction,
19 fromWei,
20 } from '../lib/txn'
22import {
23 ETH_ZERO_ADDR,
24 isValidAddress,
25 eqAddr,
26 addressFromSecp256k1Public
27} from '../lib/wallet'
29class IssueChild extends React.Component {
30 constructor(props) {
31 super(props)
34 const issuingPoint = props.pointCursor.matchWith({
35 Just: (pt) => parseInt(pt.value, 10),
36 Nothing: () => {
37 throw BRIDGE_ERROR.MISSING_POINT
38 }
39 })
41 const getCandidate = () => ob.patp(getSpawnCandidate(issuingPoint))
43 const suggestions = [
44 getCandidate(),
45 getCandidate(),
46 getCandidate(),
47 getCandidate()
48 ]
50 this.state = {
51 txError: Maybe.Nothing(),
52 receivingAddress: '',
53 issuingPoint: issuingPoint,
54 desiredPoint: '',
55 isAvailable: Maybe.Nothing(), // allow attempt when offline
56 userApproval: false,
57 nonce: '',
58 gasPrice: '5',
59 chainId: '',
60 gasLimit: '600000',
61 txn: Maybe.Nothing(),
62 stx: Maybe.Nothing(),
63 suggestions: suggestions,
64 }
66 this.handlePointInput = this.handlePointInput.bind(this)
67 this.handleAddressInput = this.handleAddressInput.bind(this)
68 this.handleConfirmAvailability = this.handleConfirmAvailability.bind(this)
69 // Transaction
70 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
71 this.handleSubmit = this.handleSubmit.bind(this)
72 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
73 this.handleSetTxn = this.handleSetTxn.bind(this)
74 this.handleSetStx = this.handleSetStx.bind(this)
75 this.handleSetNonce = this.handleSetNonce.bind(this)
76 this.handleSetChainId = this.handleSetChainId.bind(this)
77 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
78 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
79 }
81 componentDidMount() {
82 const { props } = this
84 const addr = props.wallet.matchWith({
85 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
86 Nothing: () => {
87 throw BRIDGE_ERROR.MISSING_WALLET
88 }
89 })
91 props.web3.matchWith({
92 Nothing: () => {},
93 Just: (w3) => {
94 const validWeb3 = w3.value
96 const getTxMetadata = [
97 validWeb3.eth.getTransactionCount(addr),
98 validWeb3.eth.net.getId(),
99 validWeb3.eth.getGasPrice()
100 ];
102 Promise.all(getTxMetadata).then(r => {
103 const txMetadata = {
104 nonce: r[0],
105 chainId: r[1],
106 gasPrice: fromWei(r[2], 'gwei'),
107 };
109 this.setState({...txMetadata})
111 })
112 }
113 });
115 }
119 handleAddressInput = (receivingAddress) => {
120 this.setState({ receivingAddress })
121 this.handleClearTxn()
122 }
126 handlePointInput(desiredPoint) {
127 if (desiredPoint.length < 15) {
128 this.setState({
129 desiredPoint,
130 isAvailable: Maybe.Nothing()
131 })
132 this.handleClearTxn()
133 }
134 }
138 handleConfirmAvailability() {
139 this.confirmPointAvailability().then(r => {
140 this.setState({
141 isAvailable: r,
142 })
143 })
145 }
149 handleCreateUnsignedTxn() {
150 const txn = this.createUnsignedTxn()
151 this.setState({ txn })
152 }
155 handleSetUserApproval(){
156 const {state} = this
157 this.setState({ userApproval: !state.userApproval })
158 }
162 handleSetTxn(txn){
163 this.setState({ txn })
164 }
168 handleSetStx(stx){
169 this.setState({
170 stx,
171 userApproval: false,
172 })
173 }
177 handleSetNonce(nonce){
178 this.setState({ nonce })
179 this.handleClearStx()
180 }
184 handleSetChainId(chainId){
185 this.setState({ chainId })
186 this.handleClearStx()
187 }
191 handleSetGasPrice(gasPrice){
192 this.setState({ gasPrice })
193 this.handleClearStx()
194 }
198 handleSetGasLimit(gasLimit){
199 this.setState({ gasLimit })
200 this.handleClearStx()
201 }
205 handleClearStx() {
206 this.setState({
207 userApproval: false,
208 stx: Maybe.Nothing(),
209 })
210 }
214 handleClearTxn() {
215 this.setState({
216 userApproval: false,
217 txn: Maybe.Nothing(),
218 stx: Maybe.Nothing(),
219 })
220 }
224 handleClearTransaction() {
225 this.setState({
226 userApproval: false,
227 txn: Maybe.Nothing(),
228 stx: Maybe.Nothing(),
229 })
230 }
234 handleSubmit(){
235 const { props, state } = this
236 sendSignedTransaction(props.web3.value, state.stx)
237 .then(sent => {
238 props.setTxnHashCursor(sent)
239 props.popRoute()
240 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
241 })
242 .catch(err => {
243 this.setState({ txError: err.map(val => val.merge()) })
244 })
245 }
249 async confirmPointAvailability() {
250 const { state, props } = this
252 if (canDecodePatp(state.desiredPoint) === false) return Maybe.Just(false)
254 const validContracts = props.contracts.matchWith({
255 Just: (cs) => cs.value,
256 Nothing: () => {
257 throw BRIDGE_ERROR.MISSING_CONTRACTS
258 }
259 })
261 const pointDec = ob.patp2dec(state.desiredPoint)
263 const owner = await azimuth.azimuth.getOwner(
264 validContracts,
265 pointDec
266 )
268 if (eqAddr(owner, ETH_ZERO_ADDR)) return Maybe.Just(true)
270 return Maybe.Just(false)
271 }
275 createUnsignedTxn() {
276 const { state, props } = this
278 if (isValidAddress(state.receivingAddress) === false) return Maybe.Nothing()
279 if (state.isAvailable === false) return Maybe.Nothing()
280 if (canDecodePatp(state.desiredPoint) === false) return Maybe.Nothing()
282 const validContracts = props.contracts.matchWith({
283 Just: (cs) => cs.value,
284 Nothing: () => {
285 throw BRIDGE_ERROR.MISSING_CONTRACTS
286 }
287 })
289 const pointDec = ob.patp2dec(state.desiredPoint)
291 const txn = azimuth.ecliptic.spawn(
292 validContracts,
293 pointDec,
294 state.receivingAddress
295 )
297 return Maybe.Just(txn)
298 }
302 buttonTriState() {
303 const a = this.state.isAvailable
304 if (Maybe.Nothing.hasInstance(a)) return 'blue'
305 if (a.value === false) return 'yellow'
306 if (a.value === true) return 'green'
307 }
311 buttonTriStateText() {
312 const a = this.state.isAvailable
313 if (Maybe.Nothing.hasInstance(a)) return 'Confirm Availablility'
314 if (a.value === false) return 'Point is Not Available'
315 if (a.value === true) return 'Available'
316 }
318 validatePoint = patp => {
319 const point = this.point
321 let vpatp = false
322 try {
323 vpatp = ob.isValidPatp(patp)
324 } catch(_) {
325 }
327 let vchild = false
328 try {
329 vchild = ob.sein(patp) === ob.patp(point)
330 } catch(_) {
331 }
333 return vpatp && vchild
334 }
337 render() {
338 const { props, state } = this
340 const validAddress = isValidAddress(state.receivingAddress)
341 const validPoint = canDecodePatp(state.desiredPoint)
343 const canGenerate = props.web3.matchWith({
344 Nothing: () => {
345 return validAddress === true &&
346 validPoint === true
347 },
348 Just: () => {
349 return validAddress === true &&
350 validPoint === true &&
351 state.isAvailable.value === true
352 }
353 })
355 const canSign = !Maybe.Nothing.hasInstance(state.txn)
356 const canApprove = !Maybe.Nothing.hasInstance(state.stx)
357 const canSend = !Maybe.Nothing.hasInstance(state.stx) && state.userApproval === true
359 const esvisible =
360 props.networkType === NETWORK_NAMES.ROPSTEN ||
361 props.networkType === NETWORK_NAMES.MAINNET
363 const esdomain =
364 props.networkType === NETWORK_NAMES.ROPSTEN
365 ? "ropsten.etherscan.io"
366 : "etherscan.io"
368 return (
369 <Row>
370 <Col>
371 <H1>
372 { 'Issue a Child From ' } <code>{ `${ob.patp(state.issuingPoint)}` }</code>
373 </H1>
376 <P>
377 {
378 `Please enter the point you would like to issue, and specify the
379 receiving Ethereum address. If you need to create an address, you
380 can also use Wallet Generator.`
381 }
382 </P>
384 <P>
385 {
386 `Your point can only issue children with particular names. Some
387 valid suggestions for `
388 }
389 {
390 <code>
391 { ob.patp(state.issuingPoint) }
392 </code>
393 }
394 {
395 ' are '
396 }
397 <code>
398 { state.suggestions[0] }
399 </code>
400 { ', ' }
401 <code>
402 { state.suggestions[1] }
403 </code>
404 { ', and ' }
405 <code>
406 { state.suggestions[2] }
407 </code>
408 { '.' }
409 </P>
411 <PointInput
412 prop-size='lg'
413 prop-format='innerLabel'
414 className={'mono mt-8'}
415 placeholder={ `e.g. ${state.suggestions[3]}` }
416 value={ state.desiredPoint }
417 onChange={ this.handlePointInput }>
418 <InnerLabel>{ 'Point to Issue' }</InnerLabel>
419 <ValidatedSigil
420 className={'tr-0 mt-05 mr-0 abs'}
421 patp={state.desiredPoint}
422 size={76}
423 margin={8}
424 validator={() => this.validatePoint(state.desiredPoint)} />
425 </PointInput>
427 <AddressInput
428 className='text-mono mt-8'
429 prop-size='lg'
430 prop-format='innerLabel'
431 placeholder={ `e.g. 0x84295d5e054d8cff5a22428b195f5a1615bd644f` }
432 value={ state.receivingAddress }
433 disabled={ !state.available }
434 onChange={ v => this.handleAddressInput(v) }>
435 <InnerLabel>{ 'Receiving Address' }</InnerLabel>
436 <ShowBlockie className={'mt-1'} address={state.receivingAddress} />
437 </AddressInput>
439 <Anchor
440 className={'mt-1'}
441 prop-size={'sm'}
442 prop-disabled={!isValidAddress(state.receivingAddress) || !esvisible}
443 target={'_blank'}
444 href={`https://${esdomain}/address/${state.receivingAddress}`}>
445 {'View on Etherscan ↗'}
446 </Anchor>
448 {
449 props.web3.matchWith({
450 Nothing: _ => <div />,
451 Just: _ =>
452 <Button
453 prop-size='lg wide'
454 prop-color={this.buttonTriState()}
455 className={'mt-8'}
456 disabled={!validPoint}
457 onClick={this.handleConfirmAvailability}>
458 {this.buttonTriStateText()}
459 </Button>
460 })
461 }
463 <StatelessTransaction
464 // Upper scope
465 web3={props.web3}
466 contracts={props.contracts}
467 wallet={props.wallet}
468 walletType={props.walletType}
469 walletHdPath={props.walletHdPath}
470 networkType={props.networkType}
471 // Tx
472 txn={state.txn}
473 stx={state.stx}
474 // Tx details
475 nonce={state.nonce}
476 gasPrice={state.gasPrice}
477 chainId={state.chainId}
478 gasLimit={state.gasLimit}
479 // Checks
480 userApproval={state.userApproval}
481 canGenerate={ canGenerate }
482 canSign={ canSign }
483 canApprove={ canApprove }
484 canSend={ canSend }
485 // Methods
486 createUnsignedTxn={this.handleCreateUnsignedTxn}
487 setUserApproval={this.handleSetUserApproval}
488 setTxn={this.handleSetTxn}
489 setStx={this.handleSetStx}
490 setNonce={this.handleSetNonce}
491 setChainId={this.handleSetChainId}
492 setGasPrice={this.handleSetGasPrice}
493 setGasLimit={this.handleSetGasLimit}
494 handleSubmit={this.handleSubmit} />
496 {
497 Maybe.Nothing.hasInstance(state.txError)
498 ? ''
499 : <Warning className={'mt-8'}>
500 <H3 style={{marginTop: 0, paddingTop: 0}}>
501 {
502 'There was an error sending your transaction.'
503 }
504 </H3>
505 { state.txError.value }
506 </Warning>
507 }
509 </Col>
510 </Row>
511 )
512 }
515export default IssueChild