on 616e621 help sign in
bridge/src/views/IssueChild.js
510 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, Form, InnerLabel } from '../components/Base'
10import StatelessTransaction from '../components/StatelessTransaction'
12import { ROUTE_NAMES } from '../lib/router'
13import { BRIDGE_ERROR } from '../lib/error'
14import { getSpawnCandidate } from '../lib/child'
15import {
16 canDecodePatp,
17 sendSignedTransaction,
18 fromWei,
19 } from '../lib/txn'
21import {
22 ETH_ZERO_ADDR,
23 isValidAddress,
24 eqAddr,
25 addressFromSecp256k1Public
26} from '../lib/wallet'
28class IssueChild extends React.Component {
29 constructor(props) {
30 super(props)
33 const issuingPoint = props.pointCursor.matchWith({
34 Just: (pt) => parseInt(pt.value, 10),
35 Nothing: () => {
36 throw BRIDGE_ERROR.MISSING_POINT
37 }
38 })
40 const getCandidate = () => ob.patp(getSpawnCandidate(issuingPoint))
42 const suggestions = [
43 getCandidate(),
44 getCandidate(),
45 getCandidate(),
46 getCandidate()
47 ]
49 this.state = {
50 txError: Maybe.Nothing(),
51 receivingAddress: '',
52 issuingPoint: issuingPoint,
53 desiredPoint: '',
54 isAvailable: Maybe.Nothing(), // allow attempt when offline
55 userApproval: false,
56 nonce: '',
57 gasPrice: '5',
58 chainId: '',
59 gasLimit: '600000',
60 txn: Maybe.Nothing(),
61 stx: Maybe.Nothing(),
62 suggestions: suggestions,
63 }
65 this.handlePointInput = this.handlePointInput.bind(this)
66 this.handleAddressInput = this.handleAddressInput.bind(this)
67 this.handleConfirmAvailability = this.handleConfirmAvailability.bind(this)
68 // Transaction
69 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
70 this.handleSubmit = this.handleSubmit.bind(this)
71 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
72 this.handleSetTxn = this.handleSetTxn.bind(this)
73 this.handleSetStx = this.handleSetStx.bind(this)
74 this.handleSetNonce = this.handleSetNonce.bind(this)
75 this.handleSetChainId = this.handleSetChainId.bind(this)
76 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
77 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
78 }
80 componentDidMount() {
81 const { props } = this
83 const addr = props.wallet.matchWith({
84 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
85 Nothing: () => {
86 throw BRIDGE_ERROR.MISSING_WALLET
87 }
88 })
90 props.web3.matchWith({
91 Nothing: () => {},
92 Just: (w3) => {
93 const validWeb3 = w3.value
95 const getTxMetadata = [
96 validWeb3.eth.getTransactionCount(addr),
97 validWeb3.eth.net.getId(),
98 validWeb3.eth.getGasPrice()
99 ];
101 Promise.all(getTxMetadata).then(r => {
102 const txMetadata = {
103 nonce: r[0],
104 chainId: r[1],
105 gasPrice: fromWei(r[2], 'gwei'),
106 };
108 this.setState({...txMetadata})
110 })
111 }
112 });
114 }
118 handleAddressInput = (receivingAddress) => {
119 this.setState({ receivingAddress })
120 this.handleClearTxn()
121 }
125 handlePointInput(desiredPoint) {
126 if (desiredPoint.length < 15) {
127 this.setState({
128 desiredPoint,
129 isAvailable: Maybe.Nothing()
130 })
131 this.handleClearTxn()
132 }
133 }
137 handleConfirmAvailability() {
138 this.confirmPointAvailability().then(r => {
139 this.setState({
140 isAvailable: r,
141 })
142 })
144 }
148 handleCreateUnsignedTxn() {
149 const txn = this.createUnsignedTxn()
150 this.setState({ txn })
151 }
154 handleSetUserApproval(){
155 const {state} = this
156 this.setState({ userApproval: !state.userApproval })
157 }
161 handleSetTxn(txn){
162 this.setState({ txn })
163 }
167 handleSetStx(stx){
168 this.setState({
169 stx,
170 userApproval: false,
171 })
172 }
176 handleSetNonce(nonce){
177 this.setState({ nonce })
178 this.handleClearStx()
179 }
183 handleSetChainId(chainId){
184 this.setState({ chainId })
185 this.handleClearStx()
186 }
190 handleSetGasPrice(gasPrice){
191 this.setState({ gasPrice })
192 this.handleClearStx()
193 }
197 handleSetGasLimit(gasLimit){
198 this.setState({ gasLimit })
199 this.handleClearStx()
200 }
204 handleClearStx() {
205 this.setState({
206 userApproval: false,
207 stx: Maybe.Nothing(),
208 })
209 }
213 handleClearTxn() {
214 this.setState({
215 userApproval: false,
216 txn: Maybe.Nothing(),
217 stx: Maybe.Nothing(),
218 })
219 }
223 handleClearTransaction() {
224 this.setState({
225 userApproval: false,
226 txn: Maybe.Nothing(),
227 stx: Maybe.Nothing(),
228 })
229 }
233 handleSubmit(){
234 const { props, state } = this
235 sendSignedTransaction(props.web3.value, state.stx)
236 .then(sent => {
237 props.setTxnCursor(sent)
238 props.popRoute()
239 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
240 })
241 .catch(err => {
242 // Note that value.value is due to wrapped Maybe.Just + Result.Error
243 this.setState({ txError: Maybe.Just(err.value.value) })
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
342 const validAddress = isValidAddress(state.receivingAddress)
343 const validPoint = canDecodePatp(state.desiredPoint)
345 const canGenerate = props.web3.matchWith({
346 Nothing: () => {
347 return validAddress === true &&
348 validPoint === true
349 },
350 Just: () => {
351 return validAddress === true &&
352 validPoint === true &&
353 state.isAvailable.value === true
354 }
355 })
358 const canSign = !Maybe.Nothing.hasInstance(state.txn)
359 const canApprove = !Maybe.Nothing.hasInstance(state.stx)
360 const canSend = !Maybe.Nothing.hasInstance(state.stx) && state.userApproval === true
362 return (
363 <Row>
364 <Col>
365 <H1>
366 { 'Issue a Child From ' } <code>{ `${ob.patp(state.issuingPoint)}` }</code>
367 </H1>
370 <P>
371 {
372 `Please enter the point you would like to issue, and specify the
373 receiving Ethereum address. If you need to create an address, you
374 can also use Wallet Generator.`
375 }
376 </P>
378 <P>
379 {
380 `Your point can only issue children with particular names. Some
381 valid suggestions for `
382 }
383 {
384 <code>
385 { ob.patp(state.issuingPoint) }
386 </code>
387 }
388 {
389 ' are '
390 }
391 <code>
392 { state.suggestions[0] }
393 </code>
394 { ', ' }
395 <code>
396 { state.suggestions[1] }
397 </code>
398 { ', and ' }
399 <code>
400 { state.suggestions[2] }
401 </code>
402 { '.' }
403 </P>
405 <Form>
406 <PointInput
407 prop-size='lg'
408 prop-format='innerLabel'
409 className={'mono mt-8'}
410 placeholder={ `e.g. ${state.suggestions[3]}` }
411 value={ state.desiredPoint }
412 onChange={ this.handlePointInput }>
413 <InnerLabel>{ 'Point to Issue' }</InnerLabel>
414 <ValidatedSigil
415 className={'tr-0 mt-05 mr-0 abs'}
416 patp={state.desiredPoint}
417 size={76}
418 margin={8}
419 validator={() => this.validatePoint(state.desiredPoint)} />
420 </PointInput>
422 <AddressInput
423 className='text-mono mt-8'
424 prop-size='lg'
425 prop-format='innerLabel'
426 placeholder={ `e.g. 0x84295d5e054d8cff5a22428b195f5a1615bd644f` }
427 value={ state.receivingAddress }
428 disabled={ !state.available }
429 onChange={ v => this.handleAddressInput(v) }>
430 <InnerLabel>{ 'Receiving Address' }</InnerLabel>
431 <ShowBlockie className={'mt-1'} address={state.receivingAddress} />
432 </AddressInput>
434 <Anchor
435 className={'mt-1'}
436 prop-size={'sm'}
437 prop-disabled={!isValidAddress(state.receivingAddress)}
438 target={'_blank'}
439 href={`https://etherscan.io/address/${state.receivingAddress}`}>
440 {'View on Etherscan ↗'}
441 </Anchor>
443 {
444 props.web3.matchWith({
445 Nothing: _ => <div />,
446 Just: _ =>
447 <Button
448 prop-size='lg wide'
449 prop-color={this.buttonTriState()}
450 className={'mt-8'}
451 disabled={!validPoint}
452 onClick={this.handleConfirmAvailability}>
453 {this.buttonTriStateText()}
454 </Button>
455 })
456 }
458 <StatelessTransaction
459 // Upper scope
460 web3={props.web3}
461 contracts={props.contracts}
462 wallet={props.wallet}
463 walletType={props.walletType}
464 walletHdPath={props.walletHdPath}
465 // Tx
466 txn={state.txn}
467 stx={state.stx}
468 // Tx details
469 nonce={state.nonce}
470 gasPrice={state.gasPrice}
471 chainId={state.chainId}
472 gasLimit={state.gasLimit}
473 // Checks
474 userApproval={state.userApproval}
475 canGenerate={ canGenerate }
476 canSign={ canSign }
477 canApprove={ canApprove }
478 canSend={ canSend }
479 // Methods
480 createUnsignedTxn={this.handleCreateUnsignedTxn}
481 setUserApproval={this.handleSetUserApproval}
482 setTxn={this.handleSetTxn}
483 setStx={this.handleSetStx}
484 setNonce={this.handleSetNonce}
485 setChainId={this.handleSetChainId}
486 setGasPrice={this.handleSetGasPrice}
487 setGasLimit={this.handleSetGasLimit}
488 handleSubmit={this.handleSubmit} />
490 </Form>
492 {
493 Maybe.Nothing.hasInstance(state.txError)
494 ? ''
495 : <Warning className={'mt-8'}>
496 <H3 style={{marginTop: 0, paddingTop: 0}}>
497 {
498 'There was an error sending your transaction.'
499 }
500 </H3>
501 { state.txError.value }
502 </Warning>
503 }
504 </Col>
505 </Row>
506 )
507 }
510export default IssueChild