1import { Just, Nothing } from 'folktale/maybe'
2import React from 'react'
3import { azimuth, ecliptic } 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 { canDecodePatp, sendSignedTransaction, fromWei } from '../lib/txn'
22 addressFromSecp256k1Public
25class IssueChild extends React.Component {
29 const issuingPoint = props.pointCursor.matchWith({
30 Just: (pt) => parseInt(pt.value, 10),
32 throw BRIDGE_ERROR.MISSING_POINT
36 const getCandidate = () => ob.patp(getSpawnCandidate(issuingPoint))
48 issuingPoint: issuingPoint,
50 isAvailable: Nothing(), // use Nothing to allow attempt when offline
54 showGasDetails: false,
59 suggestions: suggestions,
62 this.handlePointInput = this.handlePointInput.bind(this)
63 this.handleAddressInput = this.handleAddressInput.bind(this)
64 this.handleConfirmAvailability = this.handleConfirmAvailability.bind(this)
65 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
66 this.handleSubmit = this.handleSubmit.bind(this)
67 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
68 this.handleSetTxn = this.handleSetTxn.bind(this)
69 this.handleSetStx = this.handleSetStx.bind(this)
70 this.handleSetNonce = this.handleSetNonce.bind(this)
71 this.handleSetChainId = this.handleSetChainId.bind(this)
72 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
73 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
74 this.toggleGasDetails = this.toggleGasDetails.bind(this)
79 showGasDetails: !this.state.showGasDetails
84 const { props } = this
86 const addr = props.wallet.matchWith({
87 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
89 throw BRIDGE_ERROR.MISSING_WALLET
93 props.web3.matchWith({
96 const validWeb3 = w3.value
98 const getTxMetadata = [
99 validWeb3.eth.getTransactionCount(addr),
100 validWeb3.eth.net.getId(),
101 validWeb3.eth.getGasPrice()
104 Promise.all(getTxMetadata).then(r => {
108 gasPrice: fromWei(r[2], 'gwei'),
111 this.setState({...txMetadata})
121 handleAddressInput = (receivingAddress) => {
122 this.setState({ receivingAddress })
123 this.handleClearTxn()
128 handlePointInput(desiredPoint) {
129 if (desiredPoint.length < 15) {
132 isAvailable: Nothing()
134 this.handleClearTxn()
140 handleConfirmAvailability() {
141 this.confirmPointAvailability().then(r => {
151 handleCreateUnsignedTxn() {
152 const txn = this.createUnsignedTxn()
153 this.setState({ txn })
157 handleSetUserApproval(){
159 this.setState({ userApproval: !state.userApproval })
165 this.setState({ txn })
179 handleSetNonce(nonce){
180 this.setState({ nonce })
181 this.handleClearStx()
186 handleSetChainId(chainId){
187 this.setState({ chainId })
188 this.handleClearStx()
193 handleSetGasPrice(gasPrice){
194 this.setState({ gasPrice })
195 this.handleClearStx()
200 handleSetGasLimit(gasLimit){
201 this.setState({ gasLimit })
202 this.handleClearStx()
226 handleClearTransaction() {
237 const { props, state } = this
238 sendSignedTransaction(props.web3.value, state.stx)
240 props.setTxnHashCursor(sent)
242 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
245 this.setState({ txError: err.map(val => val.merge()) })
249 async confirmPointAvailability() {
250 const { contracts } = this.props
251 const { desiredPoint } = this.state
253 const validContracts = contracts.matchWith({
254 Just: cs => cs.value,
256 throw BRIDGE_ERROR.MISSING_CONTRACTS
260 if (canDecodePatp(desiredPoint) === false) {
264 const point = ob.patp2dec(desiredPoint)
265 const pointSize = azimuth.getPointSize(point)
267 const prefix = azimuth.getPrefix(point)
268 const prefixSize = azimuth.getPointSize(prefix)
270 // NB (jtobin): this prevents galaxies from attempting to spawn planets
272 if (pointSize !== prefixSize + 1) {
276 const owner = await azimuth.getOwner(validContracts, point)
278 if (eqAddr(owner, ETH_ZERO_ADDR)) {
285 createUnsignedTxn() {
286 const { state, props } = this
288 if (isValidAddress(state.receivingAddress) === false) return Nothing()
289 if (state.isAvailable === false) return Nothing()
290 if (canDecodePatp(state.desiredPoint) === false) return Nothing()
292 const validContracts = props.contracts.matchWith({
293 Just: (cs) => cs.value,
295 throw BRIDGE_ERROR.MISSING_CONTRACTS
299 const pointDec = ob.patp2dec(state.desiredPoint)
301 const txn = ecliptic.spawn(
304 state.receivingAddress
313 const a = this.state.isAvailable
314 if (Nothing.hasInstance(a)) return 'blue'
315 if (a.value === false) return 'yellow'
316 if (a.value === true) return 'green'
321 buttonTriStateText() {
322 const a = this.state.isAvailable
323 if (Nothing.hasInstance(a)) return 'Confirm Availablility'
324 if (a.value === false) return 'Point is Not Available'
325 if (a.value === true) return 'Available'
328 validatePoint = patp => {
329 const point = this.point
333 vpatp = ob.isValidPatp(patp)
339 vchild = ob.sein(patp) === ob.patp(point)
343 return vpatp && vchild
348 const { props, state } = this
350 const validAddress = isValidAddress(state.receivingAddress)
351 const validPoint = canDecodePatp(state.desiredPoint)
353 const canGenerate = props.web3.matchWith({
355 return validAddress === true &&
359 return validAddress === true &&
360 validPoint === true &&
361 state.isAvailable.value === true
365 const canSign = Just.hasInstance(state.txn)
366 const canApprove = Just.hasInstance(state.stx)
368 Just.hasInstance(state.stx)
369 && state.userApproval === true
372 props.networkType === NETWORK_NAMES.ROPSTEN ||
373 props.networkType === NETWORK_NAMES.MAINNET
376 props.networkType === NETWORK_NAMES.ROPSTEN
377 ? "ropsten.etherscan.io"
384 { 'Issue a Child From ' } <code>{ `${ob.patp(state.issuingPoint)}` }</code>
390 `Please enter the point you would like to issue, and specify the
391 receiving Ethereum address. If you need to create an address, you
392 can also use Wallet Generator.`
398 `Your point can only issue children with particular names. Some
399 valid suggestions for `
403 { ob.patp(state.issuingPoint) }
410 { state.suggestions[0] }
414 { state.suggestions[1] }
418 { state.suggestions[2] }
425 prop-format='innerLabel'
426 className={'mono mt-8'}
427 placeholder={ `e.g. ${state.suggestions[3]}` }
428 value={ state.desiredPoint }
429 onChange={ this.handlePointInput }>
430 <InnerLabel>{ 'Point to Issue' }</InnerLabel>
432 className={'tr-0 mt-05 mr-0 abs'}
433 patp={state.desiredPoint}
436 validator={() => this.validatePoint(state.desiredPoint)} />
440 className='text-mono mt-8'
442 prop-format='innerLabel'
443 placeholder={ `e.g. 0x84295d5e054d8cff5a22428b195f5a1615bd644f` }
444 value={ state.receivingAddress }
445 disabled={ Nothing.hasInstance(state.isAvailable) || !state.isAvailable.value }
446 onChange={ v => this.handleAddressInput(v) }>
447 <InnerLabel>{ 'Receiving Address' }</InnerLabel>
448 <ShowBlockie className={'mt-1'} address={state.receivingAddress} />
454 prop-disabled={!isValidAddress(state.receivingAddress) || !esvisible}
456 href={`https://${esdomain}/address/${state.receivingAddress}`}>
457 {'View on Etherscan ↗'}
461 props.web3.matchWith({
462 Nothing: _ => <div />,
466 prop-color={this.buttonTriState()}
468 disabled={!validPoint}
469 onClick={this.handleConfirmAvailability}>
470 {this.buttonTriStateText()}
475 <StatelessTransaction
478 contracts={props.contracts}
479 wallet={props.wallet}
480 walletType={props.walletType}
481 walletHdPath={props.walletHdPath}
482 networkType={props.networkType}
488 gasPrice={state.gasPrice}
489 showGasDetails={state.showGasDetails}
490 toggleGasDetails={this.toggleGasDetails}
491 chainId={state.chainId}
492 gasLimit={state.gasLimit}
494 userApproval={state.userApproval}
495 canGenerate={ canGenerate }
497 canApprove={ canApprove }
500 createUnsignedTxn={this.handleCreateUnsignedTxn}
501 setUserApproval={this.handleSetUserApproval}
502 setTxn={this.handleSetTxn}
503 setStx={this.handleSetStx}
504 setNonce={this.handleSetNonce}
505 setChainId={this.handleSetChainId}
506 setGasPrice={this.handleSetGasPrice}
507 setGasLimit={this.handleSetGasLimit}
508 handleSubmit={this.handleSubmit} />
511 Nothing.hasInstance(state.txError)
513 : <Warning className={'mt-8'}>
514 <H3 style={{marginTop: 0, paddingTop: 0}}>
516 'There was an error sending your transaction.'
519 { state.txError.value }
529export default IssueChild
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