on e006bf2 help sign in
bridge/src/views/CreateGalaxy.js
150 lines 3.9 kB history raw
1import React, { useCallback, useMemo, useState } from 'react';
2import cn from 'classnames';
3import { Grid, Text, Input } from 'indigo-react';
4import * as azimuth from 'azimuth-js';
6import { useNetwork } from 'store/network';
7import { usePointCache } from 'store/pointCache';
9import * as need from 'lib/need';
10import useEthereumTransaction from 'lib/useEthereumTransaction';
11import { GAS_LIMITS } from 'lib/constants';
12import patp2dec from 'lib/patp2dec';
13import { isZeroAddress } from 'lib/wallet';
14import { useLocalRouter } from 'lib/LocalRouter';
16import ViewHeader from 'components/ViewHeader';
17import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
18import View from 'components/View';
19import BridgeForm from 'form/BridgeForm';
20import { PointInput } from 'form/Inputs';
21import {
22 composeValidator,
23 buildPointValidator,
24 buildAddressValidator,
25} from 'form/validators';
26import FormError from 'form/FormError';
28function useCreateGalaxy() {
29 const { contracts } = useNetwork();
30 const { syncKnownPoint } = usePointCache();
32 const _contracts = need.contracts(contracts);
34 const [galaxy, setGalaxy] = useState();
36 return useEthereumTransaction(
37 useCallback(
38 (galaxy, owner) => {
39 setGalaxy(galaxy);
40 return azimuth.ecliptic.createGalaxy(_contracts, galaxy, owner);
41 },
42 [_contracts]
43 ),
44 useCallback(() => syncKnownPoint(galaxy), [galaxy, syncKnownPoint]),
45 GAS_LIMITS.DEFAULT
46 );
49export default function CreateGalaxy() {
50 const { pop } = useLocalRouter();
51 const { contracts } = useNetwork();
52 const _contracts = need.contracts(contracts);
54 const {
55 construct,
56 unconstruct,
57 completed,
58 inputsLocked,
59 bind,
60 } = useCreateGalaxy();
62 const validateGalaxy = useCallback(
63 async galaxyName => {
64 const currentOwner = await azimuth.azimuth.getOwner(
65 _contracts,
66 patp2dec(galaxyName)
67 );
69 const isAvailable = isZeroAddress(currentOwner);
70 if (!isAvailable) {
71 return 'This galaxy is already spawned and owned.';
72 }
73 },
74 [_contracts]
75 );
77 const validate = useMemo(
78 () =>
79 composeValidator({
80 galaxyName: buildPointValidator(1, [validateGalaxy]),
81 owner: buildAddressValidator(),
82 }),
83 [validateGalaxy]
84 );
86 const onValues = useCallback(
87 ({ valid, values }) => {
88 if (valid) {
89 construct(patp2dec(values.galaxyName), values.owner);
90 } else {
91 unconstruct();
92 }
93 },
94 [construct, unconstruct]
95 );
97 return (
98 <View pop={pop} inset>
99 <Grid>
100 <Grid.Item full as={ViewHeader}>
101 Create a Galaxy
102 </Grid.Item>
104 <BridgeForm validate={validate} onValues={onValues}>
105 {({ handleSubmit, values }) => (
106 <>
107 {completed && (
108 <Grid.Item
109 full
110 as={Text}
111 className={cn('f5', {
112 green3: completed,
113 })}>
114 {values.galaxyName} has been created and can be claimed by{' '}
115 {values.owner}.
116 </Grid.Item>
117 )}
119 <Grid.Item
120 full
121 as={PointInput}
122 className="mt4"
123 name="galaxyName"
124 label="Galaxy Name"
125 disabled={inputsLocked}
126 />
127 <Grid.Item
128 full
129 as={Input}
130 className="mb4"
131 name="owner"
132 label="Ethereum Address"
133 disabled={inputsLocked}
134 />
136 <Grid.Item full as={FormError} />
138 <Grid.Item
139 full
140 as={InlineEthereumTransaction}
141 {...bind}
142 onReturn={() => pop()}
143 />
144 </>
145 )}
146 </BridgeForm>
147 </Grid>
148 </View>
149 );