2020-01-10 16:55:27 +01:00
|
|
|
import React, { Component } from 'react'
|
2018-04-27 21:03:00 +02:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-18 21:18:25 +02:00
|
|
|
import { debounce } from 'lodash'
|
2018-04-27 21:03:00 +02:00
|
|
|
import {
|
|
|
|
getAmountErrorObject,
|
2018-06-29 19:19:40 +02:00
|
|
|
getGasFeeErrorObject,
|
2018-06-18 18:37:01 +02:00
|
|
|
getToAddressForGasUpdate,
|
2018-04-27 21:03:00 +02:00
|
|
|
doesAmountErrorRequireUpdate,
|
|
|
|
} from './send.utils'
|
2019-07-31 21:56:44 +02:00
|
|
|
import { getToWarningObject, getToErrorObject } from './send-content/add-recipient/add-recipient'
|
2019-03-22 00:03:30 +01:00
|
|
|
import SendHeader from './send-header'
|
2019-07-31 21:56:44 +02:00
|
|
|
import AddRecipient from './send-content/add-recipient'
|
2019-03-22 00:03:30 +01:00
|
|
|
import SendContent from './send-content'
|
|
|
|
import SendFooter from './send-footer'
|
2019-07-31 21:56:44 +02:00
|
|
|
import EnsInput from './send-content/add-recipient/ens-input'
|
|
|
|
|
2020-01-10 16:55:27 +01:00
|
|
|
export default class SendTransactionScreen extends Component {
|
2018-04-27 21:03:00 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
2020-01-10 15:39:44 +01:00
|
|
|
addressBook: PropTypes.arrayOf(PropTypes.object),
|
2018-04-27 21:03:00 +02:00
|
|
|
amount: PropTypes.string,
|
2018-05-22 17:16:53 +02:00
|
|
|
blockGasLimit: PropTypes.string,
|
2018-04-27 21:03:00 +02:00
|
|
|
conversionRate: PropTypes.number,
|
|
|
|
editingTransactionId: PropTypes.string,
|
2019-12-06 16:32:03 +01:00
|
|
|
fetchBasicGasEstimates: PropTypes.func.isRequired,
|
2018-04-27 21:03:00 +02:00
|
|
|
from: PropTypes.object,
|
|
|
|
gasLimit: PropTypes.string,
|
|
|
|
gasPrice: PropTypes.string,
|
|
|
|
gasTotal: PropTypes.string,
|
2020-01-10 15:39:44 +01:00
|
|
|
hasHexData: PropTypes.bool,
|
2018-04-27 21:03:00 +02:00
|
|
|
history: PropTypes.object,
|
|
|
|
network: PropTypes.string,
|
|
|
|
primaryCurrency: PropTypes.string,
|
2019-12-06 16:32:03 +01:00
|
|
|
resetSendState: PropTypes.func.isRequired,
|
2018-04-27 21:03:00 +02:00
|
|
|
selectedAddress: PropTypes.string,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: PropTypes.object,
|
2019-12-06 16:32:03 +01:00
|
|
|
showHexData: PropTypes.bool,
|
|
|
|
to: PropTypes.string,
|
|
|
|
toNickname: PropTypes.string,
|
2019-07-31 21:56:44 +02:00
|
|
|
tokens: PropTypes.array,
|
2018-04-27 21:03:00 +02:00
|
|
|
tokenBalance: PropTypes.string,
|
|
|
|
tokenContract: PropTypes.object,
|
2019-12-06 16:32:03 +01:00
|
|
|
updateAndSetGasLimit: PropTypes.func.isRequired,
|
|
|
|
updateSendEnsResolution: PropTypes.func.isRequired,
|
|
|
|
updateSendEnsResolutionError: PropTypes.func.isRequired,
|
|
|
|
updateSendErrors: PropTypes.func.isRequired,
|
|
|
|
updateSendTo: PropTypes.func.isRequired,
|
|
|
|
updateSendTokenBalance: PropTypes.func.isRequired,
|
|
|
|
updateToNicknameIfNecessary: PropTypes.func.isRequired,
|
|
|
|
scanQrCode: PropTypes.func.isRequired,
|
|
|
|
qrCodeDetected: PropTypes.func.isRequired,
|
2018-07-25 02:32:20 +02:00
|
|
|
qrCodeData: PropTypes.object,
|
2018-11-20 01:06:34 +01:00
|
|
|
}
|
2018-04-27 21:03:00 +02:00
|
|
|
|
2018-07-02 04:09:28 +02:00
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2019-07-31 21:56:44 +02:00
|
|
|
metricsEvent: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
query: '',
|
|
|
|
toError: null,
|
|
|
|
toWarning: null,
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
this.dValidate = debounce(this.validate, 1000)
|
2018-11-20 01:06:34 +01:00
|
|
|
}
|
2018-07-02 04:09:28 +02:00
|
|
|
|
2018-04-27 21:03:00 +02:00
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
const {
|
|
|
|
amount,
|
|
|
|
conversionRate,
|
|
|
|
from: { address, balance },
|
|
|
|
gasTotal,
|
|
|
|
network,
|
|
|
|
primaryCurrency,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2018-04-27 21:03:00 +02:00
|
|
|
tokenBalance,
|
|
|
|
updateSendErrors,
|
2020-01-10 15:39:44 +01:00
|
|
|
updateSendTo,
|
2018-04-27 21:03:00 +02:00
|
|
|
updateSendTokenBalance,
|
|
|
|
tokenContract,
|
2019-07-31 21:56:44 +02:00
|
|
|
to,
|
|
|
|
toNickname,
|
|
|
|
addressBook,
|
|
|
|
updateToNicknameIfNecessary,
|
2020-01-10 15:39:44 +01:00
|
|
|
qrCodeData,
|
|
|
|
qrCodeDetected,
|
2018-04-27 21:03:00 +02:00
|
|
|
} = this.props
|
|
|
|
|
2020-01-10 15:39:44 +01:00
|
|
|
let updateGas = false
|
2018-04-27 21:03:00 +02:00
|
|
|
const {
|
|
|
|
from: { balance: prevBalance },
|
|
|
|
gasTotal: prevGasTotal,
|
|
|
|
tokenBalance: prevTokenBalance,
|
|
|
|
network: prevNetwork,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: prevSendToken,
|
2020-01-10 15:39:44 +01:00
|
|
|
to: prevTo,
|
2018-04-27 21:03:00 +02:00
|
|
|
} = prevProps
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const uninitialized = [prevBalance, prevGasTotal].every((n) => n === null)
|
2018-05-24 21:57:33 +02:00
|
|
|
|
2018-05-30 18:40:30 +02:00
|
|
|
const amountErrorRequiresUpdate = doesAmountErrorRequireUpdate({
|
|
|
|
balance,
|
|
|
|
gasTotal,
|
|
|
|
prevBalance,
|
|
|
|
prevGasTotal,
|
|
|
|
prevTokenBalance,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2018-05-30 18:40:30 +02:00
|
|
|
tokenBalance,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (amountErrorRequiresUpdate) {
|
|
|
|
const amountErrorObject = getAmountErrorObject({
|
|
|
|
amount,
|
2018-04-27 21:03:00 +02:00
|
|
|
balance,
|
2018-05-30 18:40:30 +02:00
|
|
|
conversionRate,
|
2018-04-27 21:03:00 +02:00
|
|
|
gasTotal,
|
2018-05-30 18:40:30 +02:00
|
|
|
primaryCurrency,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2018-04-27 21:03:00 +02:00
|
|
|
tokenBalance,
|
|
|
|
})
|
2020-05-29 19:46:10 +02:00
|
|
|
const gasFeeErrorObject = sendToken
|
2018-06-29 19:19:40 +02:00
|
|
|
? getGasFeeErrorObject({
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
gasTotal,
|
|
|
|
primaryCurrency,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2018-06-29 19:19:40 +02:00
|
|
|
})
|
|
|
|
: { gasFee: null }
|
|
|
|
updateSendErrors(Object.assign(amountErrorObject, gasFeeErrorObject))
|
2018-05-30 18:40:30 +02:00
|
|
|
}
|
2018-04-27 21:03:00 +02:00
|
|
|
|
2018-05-30 18:40:30 +02:00
|
|
|
if (!uninitialized) {
|
2018-04-27 21:03:00 +02:00
|
|
|
|
|
|
|
if (network !== prevNetwork && network !== 'loading') {
|
|
|
|
updateSendTokenBalance({
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2018-04-27 21:03:00 +02:00
|
|
|
tokenContract,
|
|
|
|
address,
|
|
|
|
})
|
2019-07-31 21:56:44 +02:00
|
|
|
updateToNicknameIfNecessary(to, toNickname, addressBook)
|
2020-01-10 15:39:44 +01:00
|
|
|
updateGas = true
|
2018-04-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-17 21:15:13 +02:00
|
|
|
|
2020-05-29 19:46:10 +02:00
|
|
|
const prevTokenAddress = prevSendToken && prevSendToken.address
|
|
|
|
const sendTokenAddress = sendToken && sendToken.address
|
2019-04-17 21:15:13 +02:00
|
|
|
|
2020-05-29 19:46:10 +02:00
|
|
|
if (sendTokenAddress && prevTokenAddress !== sendTokenAddress) {
|
2019-04-17 21:15:13 +02:00
|
|
|
this.updateSendToken()
|
2020-01-10 15:39:44 +01:00
|
|
|
updateGas = true
|
|
|
|
}
|
|
|
|
|
|
|
|
let scannedAddress
|
|
|
|
if (qrCodeData) {
|
|
|
|
if (qrCodeData.type === 'address') {
|
|
|
|
scannedAddress = qrCodeData.values.address.toLowerCase()
|
|
|
|
const currentAddress = prevTo && prevTo.toLowerCase()
|
|
|
|
if (currentAddress !== scannedAddress) {
|
|
|
|
updateSendTo(scannedAddress)
|
|
|
|
updateGas = true
|
|
|
|
// Clean up QR code data after handling
|
|
|
|
qrCodeDetected(null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateGas) {
|
|
|
|
if (scannedAddress) {
|
|
|
|
this.updateGas({ to: scannedAddress })
|
|
|
|
} else {
|
|
|
|
this.updateGas()
|
|
|
|
}
|
2019-04-17 21:15:13 +02:00
|
|
|
}
|
2018-04-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:28:27 +02:00
|
|
|
componentDidMount () {
|
2018-10-09 18:35:54 +02:00
|
|
|
this.props.fetchBasicGasEstimates()
|
2019-07-31 21:56:44 +02:00
|
|
|
.then(() => {
|
|
|
|
this.updateGas()
|
|
|
|
})
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 16:40:07 +01:00
|
|
|
UNSAFE_componentWillMount () {
|
2019-04-17 21:15:13 +02:00
|
|
|
this.updateSendToken()
|
2018-07-31 04:57:05 +02:00
|
|
|
|
|
|
|
// Show QR Scanner modal if ?scan=true
|
|
|
|
if (window.location.search === '?scan=true') {
|
|
|
|
this.props.scanQrCode()
|
2018-08-01 00:30:40 +02:00
|
|
|
|
|
|
|
// Clear the queryString param after showing the modal
|
2020-04-15 19:23:27 +02:00
|
|
|
const cleanUrl = window.location.href.split('?')[0]
|
|
|
|
window.history.pushState({}, null, `${cleanUrl}`)
|
2018-08-01 00:30:40 +02:00
|
|
|
window.location.hash = '#send'
|
2018-07-31 04:57:05 +02:00
|
|
|
}
|
2018-04-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2018-06-29 19:19:40 +02:00
|
|
|
componentWillUnmount () {
|
|
|
|
this.props.resetSendState()
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
onRecipientInputChange = (query) => {
|
2019-07-31 21:56:44 +02:00
|
|
|
if (query) {
|
|
|
|
this.dValidate(query)
|
|
|
|
} else {
|
|
|
|
this.validate(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
query,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
validate (query) {
|
|
|
|
const {
|
|
|
|
hasHexData,
|
|
|
|
tokens,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2019-07-31 21:56:44 +02:00
|
|
|
network,
|
|
|
|
} = this.props
|
|
|
|
|
|
|
|
if (!query) {
|
2020-08-12 21:06:57 +02:00
|
|
|
this.setState({ toError: '', toWarning: '' })
|
|
|
|
return
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 20:31:57 +02:00
|
|
|
const toErrorObject = getToErrorObject(query, hasHexData, network)
|
2020-05-29 19:46:10 +02:00
|
|
|
const toWarningObject = getToWarningObject(query, tokens, sendToken)
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
toError: toErrorObject.to,
|
|
|
|
toWarning: toWarningObject.to,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-17 21:15:13 +02:00
|
|
|
updateSendToken () {
|
|
|
|
const {
|
|
|
|
from: { address },
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2019-04-17 21:15:13 +02:00
|
|
|
tokenContract,
|
|
|
|
updateSendTokenBalance,
|
|
|
|
} = this.props
|
|
|
|
|
|
|
|
updateSendTokenBalance({
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2019-04-17 21:15:13 +02:00
|
|
|
tokenContract,
|
|
|
|
address,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-31 21:56:44 +02:00
|
|
|
updateGas ({ to: updatedToAddress, amount: value, data } = {}) {
|
|
|
|
const {
|
|
|
|
amount,
|
|
|
|
blockGasLimit,
|
|
|
|
editingTransactionId,
|
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
|
|
|
selectedAddress,
|
2020-06-10 20:34:16 +02:00
|
|
|
sendToken,
|
2019-07-31 21:56:44 +02:00
|
|
|
to: currentToAddress,
|
|
|
|
updateAndSetGasLimit,
|
|
|
|
} = this.props
|
|
|
|
|
|
|
|
updateAndSetGasLimit({
|
|
|
|
blockGasLimit,
|
|
|
|
editingTransactionId,
|
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
|
|
|
selectedAddress,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2019-07-31 21:56:44 +02:00
|
|
|
to: getToAddressForGasUpdate(updatedToAddress, currentToAddress),
|
|
|
|
value: value || amount,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:03:00 +02:00
|
|
|
render () {
|
2019-07-31 21:56:44 +02:00
|
|
|
const { history, to } = this.props
|
|
|
|
let content
|
|
|
|
|
|
|
|
if (to) {
|
|
|
|
content = this.renderSendContent()
|
|
|
|
} else {
|
|
|
|
content = this.renderAddRecipient()
|
|
|
|
}
|
2018-04-27 21:03:00 +02:00
|
|
|
|
|
|
|
return (
|
2018-05-01 04:42:57 +02:00
|
|
|
<div className="page-container">
|
2019-07-31 21:56:44 +02:00
|
|
|
<SendHeader history={history} />
|
|
|
|
{ this.renderInput() }
|
|
|
|
{ content }
|
2018-05-01 04:42:57 +02:00
|
|
|
</div>
|
2018-04-27 21:03:00 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-31 21:56:44 +02:00
|
|
|
renderInput () {
|
|
|
|
return (
|
|
|
|
<EnsInput
|
|
|
|
className="send__to-row"
|
2020-02-15 21:34:12 +01:00
|
|
|
scanQrCode={(_) => {
|
2019-07-31 21:56:44 +02:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Transactions',
|
|
|
|
action: 'Edit Screen',
|
|
|
|
name: 'Used QR scanner',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
this.props.scanQrCode()
|
|
|
|
}}
|
|
|
|
onChange={this.onRecipientInputChange}
|
2019-08-03 01:00:50 +02:00
|
|
|
onValidAddressTyped={(address) => this.props.updateSendTo(address, '')}
|
2020-02-15 21:34:12 +01:00
|
|
|
onPaste={(text) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
this.props.updateSendTo(text) && this.updateGas()
|
|
|
|
}}
|
2019-07-31 21:56:44 +02:00
|
|
|
onReset={() => this.props.updateSendTo('', '')}
|
|
|
|
updateEnsResolution={this.props.updateSendEnsResolution}
|
|
|
|
updateEnsResolutionError={this.props.updateSendEnsResolutionError}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAddRecipient () {
|
2020-08-28 17:27:07 +02:00
|
|
|
const { toError } = this.state
|
2019-07-31 21:56:44 +02:00
|
|
|
return (
|
|
|
|
<AddRecipient
|
|
|
|
updateGas={({ to, amount, data } = {}) => this.updateGas({ to, amount, data })}
|
|
|
|
query={this.state.query}
|
|
|
|
toError={toError}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderSendContent () {
|
2019-12-08 04:10:47 +01:00
|
|
|
const { history, showHexData } = this.props
|
2020-08-28 17:27:07 +02:00
|
|
|
const { toWarning } = this.state
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
<SendContent
|
|
|
|
key="send-content"
|
|
|
|
updateGas={({ to, amount, data } = {}) => this.updateGas({ to, amount, data })}
|
|
|
|
showHexData={showHexData}
|
2020-08-28 17:27:07 +02:00
|
|
|
warning={toWarning}
|
2019-07-31 21:56:44 +02:00
|
|
|
/>,
|
|
|
|
<SendFooter key="send-footer" history={history} />,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:03:00 +02:00
|
|
|
}
|