on 4b0ca21 help sign in
bridge/src/views/AcceptLockup.js
156 lines 4.1 kB history raw
1import React, { useCallback, useMemo } from 'react';
2import cn from 'classnames';
3import { Grid, Text } from 'indigo-react';
4import * as azimuth from 'azimuth-js';
5import { memoize } from 'lodash';
7import { useNetwork } from 'store/network';
8import { useWallet } from 'store/wallet';
9import { useHistory } from 'store/history';
11import * as need from 'lib/need';
12import useEthereumTransaction from 'lib/useEthereumTransaction';
13import { GAS_LIMITS } from 'lib/constants';
14import { useLocalRouter } from 'lib/LocalRouter';
15import { useFlowCommand } from 'lib/flowCommand';
16import { isValidAddress, eqAddr } from 'lib/utils/address';
18import ViewHeader from 'components/ViewHeader';
19import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
20import View from 'components/View';
22import BridgeForm from 'form/BridgeForm';
23import { AddressInput } from 'form/Inputs';
24import { composeValidator } from 'form/validators';
26function useAcceptLockup() {
27 const { contracts } = useNetwork();
28 const { push, names } = useHistory();
30 const flow = useFlowCommand();
32 const _contracts = need.contracts(contracts);
34 const transaction = useEthereumTransaction(
35 useCallback(
36 from => {
37 if (flow.lock === 'conditional') {
38 return azimuth.conditionalSR.transferCommitment(_contracts, from);
39 } else {
40 return azimuth.linearSR.transferBatch(_contracts, from);
41 }
42 },
43 [_contracts, flow]
44 ),
45 useCallback(() => push(names.STAR_RELEASE), [push, names]),
46 GAS_LIMITS.TRANSFER_LOCKUP
47 );
49 return {
50 ...transaction,
51 };
54export default function AcceptLockup() {
55 const { pop } = useLocalRouter();
56 const { contracts } = useNetwork();
57 const { wallet } = useWallet();
58 const flow = useFlowCommand();
60 const _contracts = need.contracts(contracts);
61 const _address = need.addressFromWallet(wallet);
63 const {
64 completed,
65 bind,
66 inputsLocked,
67 construct,
68 unconstruct,
69 } = useAcceptLockup();
71 const initialValues = useMemo(() => {
72 if (!flow.from) return {};
73 return { from: flow.from };
74 }, [flow]);
76 const getApproved = useMemo(() => {
77 return memoize(async from => {
78 console.log('------ looking up');
79 const lok = await (flow.lock === 'conditional'
80 ? azimuth.conditionalSR.getCommitment(_contracts, from)
81 : azimuth.linearSR.getBatch(_contracts, from));
82 return lok.approvedTransferTo;
83 });
84 }, [flow, _contracts]);
86 const validate = useMemo(
87 () =>
88 composeValidator({
89 from: async from => {
90 if (!isValidAddress(from)) {
91 return 'This is not a valid Ethereum address.';
92 }
93 const approved = await getApproved(from);
94 if (!eqAddr(_address, approved)) {
95 return 'Not permitted to take lockup from this address.';
96 }
97 },
98 }),
99 [_address, getApproved]
100 );
102 const onValues = useCallback(
103 ({ valid, values, form }) => {
104 if (valid) {
105 construct(values.from);
106 } else {
107 unconstruct();
108 }
109 },
110 [construct, unconstruct]
111 );
113 return (
114 <View pop={pop} inset>
115 <Grid>
116 <Grid.Item full as={ViewHeader}>
117 Accept Star Lockup
118 </Grid.Item>
120 <Grid.Item
121 full
122 as={Text}
123 className={cn('f5 wrap', {
124 green3: completed,
125 })}>
126 {completed
127 ? `You have accepted the star lockup.`
128 : `Accept the incoming transfer of a star lockup.`}
129 </Grid.Item>
131 <BridgeForm
132 validate={validate}
133 initialValues={initialValues}
134 onValues={onValues}>
135 {() => (
136 <Grid.Item
137 full
138 as={AddressInput}
139 name="from"
140 label="Previous owner of the star lockup"
141 disabled={inputsLocked}
142 className="mv4"
143 />
144 )}
145 </BridgeForm>
147 <Grid.Item
148 full
149 as={InlineEthereumTransaction}
150 {...bind}
151 onReturn={() => pop()}
152 />
153 </Grid>
154 </View>
155 );