Newer
Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
import React, { Component } from 'react';
import debounce from 'lodash/debounce';
import { FormField, Header } from 'fether-ui';
import { fromWei, toWei } from '@parity/api/lib/util/wei';
import { inject, observer } from 'mobx-react';
import { isAddress } from '@parity/api/lib/util/address';
import ReactTooltip from 'react-tooltip';
import TokenBalance from '../../Tokens/TokensList/TokenBalance';
import withBalance from '../../utils/withBalance';
const MAX_GAS_PRICE = 40; // In Gwei
const MIN_GAS_PRICE = 3; // Safelow gas price from GasStation, in Gwei
@withBalance(({ sendStore: { token } }) => token)
@observer
class Send extends Component {
state = {
amount: '', // In Ether or in token
gasPrice: 4, // in Gwei
to: '',
...this.props.sendStore.tx
};
componentDidUpdate () {
const { amount, gasPrice, to } = this.state;
this.props.sendStore.setTx({ amount, gasPrice, to });
this.estimateGas();
}
estimateGas = debounce(() => {
this.props.sendStore.estimateGas();
}, 1000);
static getDerivedStateFromProps (nextProps, prevState) {
sendStore: { estimated }
} = nextProps;
return {
maxAmount:
balance && estimated
? +fromWei(
toWei(balance).minus(
estimated.mul(toWei(prevState.gasPrice, 'shannon'))
)
)
: 0.01
};
}
handleChangeAmount = ({ target: { value } }) =>
handleChangeGasPrice = ({ target: { value } }) =>
handleMax = () => this.setState({ amount: this.state.maxAmount });
handleSubmit = e => {
e.preventDefault();
// Post a request to the transaction. There is a next step to sign this
// request.
history.push('/send/signer');
};
/**
* Get form errors.
*
* TODO Use a React form library to do this?
*/
const { amount, maxAmount, to } = this.state;
if (!amount || isNaN(amount)) {
return 'Please enter a valid amount';
}
if (amount < 0) {
return 'Please enter a positive amount ';
}
if (amount > maxAmount) {
return "You don't have enough balance";
}
if (!isAddress(to)) {
return 'Please enter a valid Ethereum address';
}
return null;
};
const { amount, gasPrice, maxAmount, to } = this.state;
<Link to='/tokens' className='icon -close'>
Close
</Link>
}
title={<h1>Send {token.name}</h1>}
/>
<div className='window_content'>
<div className='box -padded'>
drawers={[
<form
className='send-form'
key='txForm'
onSubmit={this.handleSubmit}
>
<FormField
input={
<div>
<input
className='form_field_amount'
onChange={this.handleChangeAmount}
required
<button
className='button -utility'
onClick={this.handleMax}
type='button'
>
Max
</button>
</nav>
</div>
}
label='Amount'
/>
<FormField
input={
className='-sm'
onChange={this.handleChangeTo}
/>
}
label='To'
/>
<FormField
className='-range'
input={
<div>
<input
max={MAX_GAS_PRICE}
min={MIN_GAS_PRICE}
onChange={this.handleChangeGasPrice}
required
/>
<nav className='range-nav'>
<span className='range-nav_label'>Cheap</span>
<span className='range-nav_value'>
</span>
<span className='range-nav_label'>Fast</span>
</nav>
</div>
}
label='Gas'
/>
<span data-tip={error || ''}>
<button disabled={error} className='button'>
]}
onClick={null}
token={token}
/>
<ReactTooltip
effect='solid'
event='mouseover'
eventOff='mouseout'
place='top'
/>
</div>
);
}
}
export default Send;