This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
/
ChannelCreateForm.js
394 lines (349 loc) · 10.3 KB
/
ChannelCreateForm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import React from 'react'
import PropTypes from 'prop-types'
import { FormattedMessage, injectIntl, intlShape } from 'react-intl'
import { Keyframes } from 'react-spring'
import get from 'lodash.get'
import { Box, Flex } from 'rebass'
import { convert } from '@zap/utils/btc'
import {
Bar,
Button,
Form,
NodePubkeyInput,
Label,
Panel,
Span,
Text,
Toggle,
TransactionFeeInput,
} from 'components/UI'
import { CurrencyFieldGroup, CryptoValue } from 'containers/UI'
import Padlock from 'components/Icon/Padlock'
import ChannelBackButton from './ChannelBackButton'
import ChannelCreateSummary from './ChannelCreateSummary'
import messages from './messages'
import {
TRANSACTION_SPEED_SLOW,
TRANSACTION_SPEED_MEDIUM,
TRANSACTION_SPEED_FAST,
} from './constants'
/**
* Animation to handle showing/hiding the amount fields.
*/
const ShowHide = Keyframes.Spring({
show: async next => {
await next({ pointerEvents: 'auto' })
await next({ opacity: 1 })
},
hide: { opacity: 0, pointerEvents: 'none' },
})
const FormButtons = ({ isNextButtonDisabled, nextButtonText, onBack }) => (
<Flex alignItems="center" mb={3}>
<Box width={1 / 4}>
<ChannelBackButton mr="auto" onClick={onBack} />
</Box>
<Flex justifyContent="center" width={2 / 4}>
<Button isDisabled={isNextButtonDisabled} type="submit">
{nextButtonText}
</Button>
</Flex>
</Flex>
)
FormButtons.propTypes = {
isNextButtonDisabled: PropTypes.bool,
nextButtonText: PropTypes.node.isRequired,
onBack: PropTypes.func.isRequired,
}
const FormFooter = ({ walletBalance, currencyName }) => (
<Box>
<Text textAlign="center">
<FormattedMessage {...messages.open_channel_form_onchain_balance} />
{` `}
<CryptoValue value={walletBalance} />
{` `}
{currencyName}
</Text>
</Box>
)
FormFooter.propTypes = {
currencyName: PropTypes.string.isRequired,
walletBalance: PropTypes.number.isRequired,
}
class ChannelCreateForm extends React.Component {
state = {
step: 'form',
isSubmitComplete: false, // if we have succesfully initiated channel opening
}
static propTypes = {
activeWalletSettings: PropTypes.shape({
type: PropTypes.string.isRequired,
}).isRequired,
currency: PropTypes.string.isRequired,
currencyName: PropTypes.string.isRequired,
fetchTickers: PropTypes.func.isRequired,
intl: intlShape.isRequired,
isQueryingFees: PropTypes.bool,
onchainFees: PropTypes.shape({
fastestFee: PropTypes.number,
halfHourFee: PropTypes.number,
hourFee: PropTypes.number,
}),
onSubmit: PropTypes.func.isRequired,
openChannel: PropTypes.func.isRequired,
queryFees: PropTypes.func.isRequired,
searchQuery: PropTypes.string,
selectedNodeDisplayName: PropTypes.string,
updateContactFormSearchQuery: PropTypes.func.isRequired,
walletBalance: PropTypes.number.isRequired,
}
static defaultProps = {
isQueryingFees: false,
onchainFees: {},
}
componentDidMount() {
const { fetchTickers, queryFees } = this.props
fetchTickers()
queryFees()
}
/**
* Get the current per byte fee based on the form values.
*/
getFee = () => {
const formState = this.formApi.getState()
const { speed } = formState.values
return this.getFeeRate(speed)
}
getFeeRate = speed => {
const { onchainFees } = this.props
switch (speed) {
case TRANSACTION_SPEED_SLOW:
return get(onchainFees, 'hourFee', null)
case TRANSACTION_SPEED_MEDIUM:
return get(onchainFees, 'halfHourFee', null)
case TRANSACTION_SPEED_FAST:
return get(onchainFees, 'fastestFee', null)
}
}
handleSubmit = values => {
const { onSubmit } = this.props
const { step } = this.state
if (step === 'form') {
return this.setState({ step: 'summary' })
}
// set submitted flag to prevent multiple submissions
this.setState({
isSubmitComplete: true,
})
// Run our submit handler.
this.onSubmit(values)
// Then run any user supplied submit handler.
if (onSubmit) {
onSubmit(values)
}
}
onBack = () => {
const { step } = this.state
if (step === 'form') {
this.clearSearchQuery()
} else {
this.setState({ step: 'form' })
}
}
/**
* Open a channel using the supplied details.
*/
onSubmit = values => {
const { currency, openChannel } = this.props
const { amountCrypto, nodePubkey } = values
// Convert amount to satoshis.
const amountInSatoshis = convert(currency, 'sats', amountCrypto)
// Extract node details.
const [pubkey, host] = nodePubkey.split('@')
// Determine the fee rate to use.
const satPerByte = this.getFee()
// submit the channel to LND.
openChannel({
pubkey,
host,
localamt: amountInSatoshis,
satPerByte,
isPrivate: values.private,
})
}
/**
* Clear the current search query.
*/
clearSearchQuery = () => {
const { updateContactFormSearchQuery } = this.props
updateContactFormSearchQuery(null)
}
/**
* Custom validation for the amount input.
*/
validateAmount = value => {
if (!value) {
return
}
const { intl, currency, walletBalance } = this.props
const fee = this.getFee()
const amount = convert(currency, 'sats', value)
// FIXME: The fee here is a per byte fee, however what we realy need is the projected fee for the transaction.
// This is not currently available in lnd, but will be in it's upcoming fee estimation API.
const totalAmount = amount + fee
if (totalAmount > walletBalance) {
return intl.formatMessage({ ...messages.error_not_enough_funds })
}
}
/**
* Store the formApi on the component context to make it available at this.formApi.
*/
setFormApi = formApi => {
this.formApi = formApi
}
renderFormFields() {
const { intl, activeWalletSettings, isQueryingFees, searchQuery } = this.props
const formState = this.formApi.getState()
const { speed } = formState.values
const fee = this.getFee(speed)
return (
<Box>
<Box mb={3}>
<Text textAlign="justify">
<FormattedMessage {...messages.open_channel_form_description} />
</Text>
</Box>
<NodePubkeyInput
field="nodePubkey"
initialValue={searchQuery}
isDisabled={Boolean(searchQuery)}
isRequired
validateOnBlur
validateOnChange
/>
<Bar my={3} variant="light" />
<CurrencyFieldGroup
css={{ height: '88px' }}
formApi={this.formApi}
isRequired
validate={this.validateAmount}
validateOnBlur={formState.submits > 0}
validateOnChange={formState.submits > 0}
/>
<Bar my={3} variant="light" />
<TransactionFeeInput
fee={fee}
field="speed"
isQueryingFees={isQueryingFees}
label={intl.formatMessage({ ...messages.fee })}
required
/>
{activeWalletSettings.type !== 'local' && (
<>
<Bar my={3} variant="light" />
<Flex alignItems="center" justifyContent="space-between">
<Flex>
<Span color="gray" fontSize="s" mr={2}>
<Padlock />
</Span>
<Label htmlFor="private">
<FormattedMessage {...messages.private_label} />
</Label>
</Flex>
<Flex>
<Toggle field="private" />
</Flex>
</Flex>
</>
)}
</Box>
)
}
renderFormSummary() {
const { currency, selectedNodeDisplayName } = this.props
const formState = this.formApi.getState()
const { speed, amountCrypto, nodePubkey } = formState.values
const fee = this.getFee()
const amount = convert(currency, 'sats', amountCrypto)
return (
<ChannelCreateSummary
amount={amount}
fee={fee}
nodeDisplayName={selectedNodeDisplayName}
nodePubkey={nodePubkey}
speed={speed}
/>
)
}
render() {
const {
intl,
activeWalletSettings,
walletBalance,
currencyName,
isQueryingFees,
fetchTickers,
onchainFees,
onSubmit,
openChannel,
queryFees,
searchQuery,
selectedNodeDisplayName,
updateContactFormSearchQuery,
...rest
} = this.props
const { step, isSubmitComplete } = this.state
return (
<Form
css={{ height: '100%' }}
{...rest}
getApi={this.setFormApi}
onSubmit={this.handleSubmit}
>
{({ formState }) => {
const { amountCrypto } = formState.values
return (
<Panel>
<Panel.Body css={{ position: 'relative' }}>
<ShowHide state={step === 'form' ? 'show' : 'hide'}>
{styles => (
<Box css={{ position: 'absolute' }} style={styles}>
{this.renderFormFields()}
</Box>
)}
</ShowHide>
<ShowHide state={step === 'summary' ? 'show' : 'hide'}>
{styles => (
<Box style={styles}>{step === 'summary' && this.renderFormSummary()}</Box>
)}
</ShowHide>
</Panel.Body>
<Panel.Footer>
<FormButtons
isNextButtonDisabled={
(isSubmitComplete && step === 'summary') ||
(formState.submits > 0 && formState.invalid)
}
nextButtonText={
<FormattedMessage
{...messages[
step === 'form'
? 'open_channel_form_next_button_text'
: 'open_channel_summary_next_button_text'
]}
values={{
amount: `${amountCrypto} ${currencyName}`,
}}
/>
}
onBack={this.onBack}
/>
<FormFooter currencyName={currencyName} walletBalance={walletBalance} />
</Panel.Footer>
</Panel>
)
}}
</Form>
)
}
}
export default injectIntl(ChannelCreateForm)