on 58fbcd2 help sign in
bridge/src/views/IssueChild.js
179 lines 4.9 kB history raw
1import React, { useCallback, useMemo, useState } from 'react';
2import cn from 'classnames';
3import { Grid, Text } from 'indigo-react';
4import * as azimuth from 'azimuth-js';
5import * as ob from 'urbit-ob';
7import { useNetwork } from 'store/network';
8import { usePointCache } from 'store/pointCache';
9import { usePointCursor } from 'store/pointCursor';
11import * as need from 'lib/need';
12import useEthereumTransaction from 'lib/useEthereumTransaction';
13import { GAS_LIMITS } from 'lib/constants';
14import patp2dec from 'lib/patp2dec';
15import { getSpawnCandidate } from 'lib/child';
16import { useLocalRouter } from 'lib/LocalRouter';
17import useConstant from 'lib/useConstant';
19import ViewHeader from 'components/ViewHeader';
20import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
21import View from 'components/View';
22import { PointInput, AddressInput } from 'form/Inputs';
23import {
24 composeValidator,
25 buildPointValidator,
26 buildAddressValidator,
27} from 'form/validators';
28import BridgeForm from 'form/BridgeForm';
29import FormError from 'form/FormError';
31function useIssueChild() {
32 const { contracts } = useNetwork();
33 const { syncKnownPoint } = usePointCache();
35 const _contracts = need.contracts(contracts);
37 const [spawnedPoint, setSpawnedPoint] = useState();
39 return useEthereumTransaction(
40 useCallback(
41 (spawnedPoint, owner) => {
42 setSpawnedPoint(spawnedPoint);
43 return azimuth.ecliptic.spawn(_contracts, spawnedPoint, owner);
44 },
45 [_contracts]
46 ),
47 useCallback(() => syncKnownPoint(spawnedPoint), [
48 spawnedPoint,
49 syncKnownPoint,
50 ]),
51 GAS_LIMITS.DEFAULT
52 );
55export default function IssueChild() {
56 const { pop } = useLocalRouter();
57 const { contracts } = useNetwork();
58 const { pointCursor } = usePointCursor();
60 const _contracts = need.contracts(contracts);
61 const _point = parseInt(need.point(pointCursor), 10);
63 const availablePointsPromise = useConstant(() =>
64 azimuth.azimuth
65 .getUnspawnedChildren(_contracts, _point)
66 .then(points => new Set(points))
67 );
69 const candidates = useMemo(() => {
70 const getCandidate = () => ob.patp(getSpawnCandidate(_point));
72 return [getCandidate(), getCandidate(), getCandidate(), getCandidate()];
73 }, [_point]);
75 const {
76 isDefaultState,
77 construct,
78 unconstruct,
79 completed,
80 inputsLocked,
81 bind,
82 } = useIssueChild();
84 const validatePoint = useCallback(
85 async name => {
86 const point = patp2dec(name);
87 const hasPoint = (await availablePointsPromise).has(point);
89 if (!hasPoint) {
90 return 'This point cannot be spawned.';
91 }
92 },
93 [availablePointsPromise]
94 );
96 const validate = useMemo(
97 () =>
98 composeValidator({
99 point: buildPointValidator(4, [validatePoint]),
100 owner: buildAddressValidator(),
101 }),
102 [validatePoint]
103 );
105 const onValues = useCallback(
106 ({ valid, values }) => {
107 if (valid) {
108 construct(patp2dec(values.point), values.owner);
109 } else {
110 unconstruct();
111 }
112 },
113 [construct, unconstruct]
114 );
116 return (
117 <View pop={pop} inset>
118 <Grid>
119 <Grid.Item full as={ViewHeader}>
120 Issue Child Point
121 </Grid.Item>
123 {isDefaultState && (
124 <Grid.Item full as={Text}>
125 Perhaps one of {candidates.slice(0, 3).join(', ')}, or{' '}
126 {candidates[candidates.length - 1]}?
127 </Grid.Item>
128 )}
130 <BridgeForm validate={validate} onValues={onValues}>
131 {({ handleSubmit, values }) => (
132 <>
133 {completed && (
134 <Grid.Item
135 full
136 as={Text}
137 className={cn('f5', {
138 green3: completed,
139 })}>
140 {values.point} has been spawned and can be claimed by{' '}
141 {values.owner}.
142 </Grid.Item>
143 )}
145 {!completed && (
146 <>
147 <Grid.Item
148 full
149 as={PointInput}
150 name="point"
151 disabled={inputsLocked}
152 className="mt4"
153 />
154 <Grid.Item
155 full
156 as={AddressInput}
157 className="mb4"
158 name="owner"
159 label="Ethereum Address"
160 disabled={inputsLocked}
161 />
162 </>
163 )}
165 <Grid.Item full as={FormError} />
167 <Grid.Item
168 full
169 as={InlineEthereumTransaction}
170 {...bind}
171 onReturn={() => pop()}
172 />
173 </>
174 )}
175 </BridgeForm>
176 </Grid>
177 </View>
178 );