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
58 suggestions: suggestions,
61 this.handlePointInput = this.handlePointInput.bind(this)
62 this.handleAddressInput = this.handleAddressInput.bind(this)
63 this.handleConfirmAvailability = this.handleConfirmAvailability.bind(this)
64 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
65 this.handleSubmit = this.handleSubmit.bind(this)
66 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
67 this.handleSetTxn = this.handleSetTxn.bind(this)
68 this.handleSetStx = this.handleSetStx.bind(this)
69 this.handleSetNonce = this.handleSetNonce.bind(this)
70 this.handleSetChainId = this.handleSetChainId.bind(this)
71 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
72 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
76 const { props } = this
78 const addr = props.wallet.matchWith({
79 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
81 throw BRIDGE_ERROR.MISSING_WALLET
85 props.web3.matchWith({
88 const validWeb3 = w3.value
90 const getTxMetadata = [
91 validWeb3.eth.getTransactionCount(addr),
92 validWeb3.eth.net.getId(),
93 validWeb3.eth.getGasPrice()
96 Promise.all(getTxMetadata).then(r => {
100 gasPrice: fromWei(r[2], 'gwei'),
103 this.setState({...txMetadata})
113 handleAddressInput = (receivingAddress) => {
114 this.setState({ receivingAddress })
115 this.handleClearTxn()
120 handlePointInput(desiredPoint) {
121 if (desiredPoint.length < 15) {
124 isAvailable: Nothing()
126 this.handleClearTxn()
132 handleConfirmAvailability() {
133 this.confirmPointAvailability().then(r => {
143 handleCreateUnsignedTxn() {
144 const txn = this.createUnsignedTxn()
145 this.setState({ txn })
149 handleSetUserApproval(){
151 this.setState({ userApproval: !state.userApproval })
157 this.setState({ txn })
171 handleSetNonce(nonce){
172 this.setState({ nonce })
173 this.handleClearStx()
178 handleSetChainId(chainId){
179 this.setState({ chainId })
180 this.handleClearStx()
185 handleSetGasPrice(gasPrice){
186 this.setState({ gasPrice })
187 this.handleClearStx()
192 handleSetGasLimit(gasLimit){
193 this.setState({ gasLimit })
194 this.handleClearStx()
218 handleClearTransaction() {
229 const { props, state } = this
230 sendSignedTransaction(props.web3.value, state.stx)
232 props.setTxnHashCursor(sent)
234 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
237 this.setState({ txError: err.map(val => val.merge()) })
241 async confirmPointAvailability() {
242 const { contracts } = this.props
243 const { desiredPoint } = this.state
245 const validContracts = contracts.matchWith({
246 Just: cs => cs.value,
248 throw BRIDGE_ERROR.MISSING_CONTRACTS
252 if (canDecodePatp(desiredPoint) === false) {
256 const point = ob.patp2dec(desiredPoint)
257 const pointSize = azimuth.getPointSize(point)
259 const prefix = azimuth.getPrefix(point)
260 const prefixSize = azimuth.getPointSize(prefix)
262 // NB (jtobin): this prevents galaxies from attempting to spawn planets
264 if (pointSize !== prefixSize + 1) {
268 const owner = await azimuth.getOwner(validContracts, point)
270 if (eqAddr(owner, ETH_ZERO_ADDR)) {
277 createUnsignedTxn() {
278 const { state, props } = this
280 if (isValidAddress(state.receivingAddress) === false) return Nothing()
281 if (state.isAvailable === false) return Nothing()
282 if (canDecodePatp(state.desiredPoint) === false) return Nothing()
284 const validContracts = props.contracts.matchWith({
285 Just: (cs) => cs.value,
287 throw BRIDGE_ERROR.MISSING_CONTRACTS
291 const pointDec = ob.patp2dec(state.desiredPoint)
293 const txn = ecliptic.spawn(
296 state.receivingAddress
305 const a = this.state.isAvailable
306 if (Nothing.hasInstance(a)) return 'blue'
307 if (a.value === false) return 'yellow'
308 if (a.value === true) return 'green'
313 buttonTriStateText() {
314 const a = this.state.isAvailable
315 if (Nothing.hasInstance(a)) return 'Confirm Availablility'
316 if (a.value === false) return 'Point is Not Available'
317 if (a.value === true) return 'Available'
320 validatePoint = patp => {
321 const point = this.point
325 vpatp = ob.isValidPatp(patp)
331 vchild = ob.sein(patp) === ob.patp(point)
335 return vpatp && vchild
340 const { props, state } = this
342 const validAddress = isValidAddress(state.receivingAddress)
343 const validPoint = canDecodePatp(state.desiredPoint)
345 const canGenerate = props.web3.matchWith({
347 return validAddress === true &&
351 return validAddress === true &&
352 validPoint === true &&
353 state.isAvailable.value === true
357 const canSign = Just.hasInstance(state.txn)
358 const canApprove = Just.hasInstance(state.stx)
360 Just.hasInstance(state.stx)
361 && state.userApproval === true
364 props.networkType === NETWORK_NAMES.ROPSTEN ||
365 props.networkType === NETWORK_NAMES.MAINNET
368 props.networkType === NETWORK_NAMES.ROPSTEN
369 ? "ropsten.etherscan.io"
376 { 'Issue a Child From ' } <code>{ `${ob.patp(state.issuingPoint)}` }</code>
382 `Please enter the point you would like to issue, and specify the
383 receiving Ethereum address. If you need to create an address, you
384 can also use Wallet Generator.`
390 `Your point can only issue children with particular names. Some
391 valid suggestions for `
395 { ob.patp(state.issuingPoint) }
402 { state.suggestions[0] }
406 { state.suggestions[1] }
410 { state.suggestions[2] }
417 prop-format='innerLabel'
418 className={'mono mt-8'}
419 placeholder={ `e.g. ${state.suggestions[3]}` }
420 value={ state.desiredPoint }
421 onChange={ this.handlePointInput }>
422 <InnerLabel>{ 'Point to Issue' }</InnerLabel>
424 className={'tr-0 mt-05 mr-0 abs'}
425 patp={state.desiredPoint}
428 validator={() => this.validatePoint(state.desiredPoint)} />
432 className='text-mono mt-8'
434 prop-format='innerLabel'
435 placeholder={ `e.g. 0x84295d5e054d8cff5a22428b195f5a1615bd644f` }
436 value={ state.receivingAddress }
437 disabled={ !state.available }
438 onChange={ v => this.handleAddressInput(v) }>
439 <InnerLabel>{ 'Receiving Address' }</InnerLabel>
440 <ShowBlockie className={'mt-1'} address={state.receivingAddress} />
446 prop-disabled={!isValidAddress(state.receivingAddress) || !esvisible}
448 href={`https://${esdomain}/address/${state.receivingAddress}`}>
449 {'View on Etherscan ↗'}
453 props.web3.matchWith({
454 Nothing: _ => <div />,
458 prop-color={this.buttonTriState()}
460 disabled={!validPoint}
461 onClick={this.handleConfirmAvailability}>
462 {this.buttonTriStateText()}
467 <StatelessTransaction
470 contracts={props.contracts}
471 wallet={props.wallet}
472 walletType={props.walletType}
473 walletHdPath={props.walletHdPath}
474 networkType={props.networkType}
480 gasPrice={state.gasPrice}
481 chainId={state.chainId}
482 gasLimit={state.gasLimit}
484 userApproval={state.userApproval}
485 canGenerate={ canGenerate }
487 canApprove={ canApprove }
490 createUnsignedTxn={this.handleCreateUnsignedTxn}
491 setUserApproval={this.handleSetUserApproval}
492 setTxn={this.handleSetTxn}
493 setStx={this.handleSetStx}
494 setNonce={this.handleSetNonce}
495 setChainId={this.handleSetChainId}
496 setGasPrice={this.handleSetGasPrice}
497 setGasLimit={this.handleSetGasLimit}
498 handleSubmit={this.handleSubmit} />
501 Nothing.hasInstance(state.txError)
503 : <Warning className={'mt-8'}>
504 <H3 style={{marginTop: 0, paddingTop: 0}}>
506 'There was an error sending your transaction.'
509 { state.txError.value }
519export 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