2017-01-25 18:51:41 +01:00
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2016-12-09 13:44:35 +01:00
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BigNumber from 'bignumber.js' ;
import { observer } from 'mobx-react' ;
2016-12-11 17:43:51 +01:00
import React , { Component , PropTypes } from 'react' ;
2017-02-03 20:01:09 +01:00
import { FormattedMessage } from 'react-intl' ;
2016-12-09 13:44:35 +01:00
2017-02-03 20:01:09 +01:00
import { Input , InputDate , InputTime , RadioButtons } from '../Form' ;
2016-12-11 17:43:51 +01:00
import GasPriceSelector from '../GasPriceSelector' ;
2016-12-09 13:44:35 +01:00
2017-02-03 20:01:09 +01:00
import Store , { CONDITIONS } from './store' ;
2016-12-09 13:44:35 +01:00
import styles from './gasPriceEditor.css' ;
2017-02-03 20:01:09 +01:00
const CONDITION _VALUES = [
{
label : (
< FormattedMessage
id = 'txEditor.condition.none'
defaultMessage = 'No conditions'
/ >
) ,
key : CONDITIONS . NONE
} ,
{
label : (
< FormattedMessage
id = 'txEditor.condition.blocknumber'
defaultMessage = 'Send after BlockNumber'
/ >
) ,
key : CONDITIONS . BLOCK
} ,
{
label : (
< FormattedMessage
id = 'txEditor.condition.datetime'
defaultMessage = 'Send after Date & Time'
/ >
) ,
key : CONDITIONS . TIME
}
] ;
2016-12-09 13:44:35 +01:00
@ observer
export default class GasPriceEditor extends Component {
2016-12-09 15:43:24 +01:00
static contextTypes = {
api : PropTypes . object . isRequired
} ;
2016-12-09 13:44:35 +01:00
static propTypes = {
2016-12-11 17:43:51 +01:00
children : PropTypes . node ,
onChange : PropTypes . func ,
store : PropTypes . object . isRequired
2016-12-09 13:44:35 +01:00
}
static Store = Store ;
render ( ) {
2016-12-09 15:43:24 +01:00
const { api } = this . context ;
2016-12-11 17:43:51 +01:00
const { children , store } = this . props ;
2017-02-03 20:01:09 +01:00
const { conditionType , errorGas , errorPrice , errorTotal , estimated , gas , histogram , price , priceDefault , totalValue } = store ;
2016-12-09 13:44:35 +01:00
2016-12-09 15:43:24 +01:00
const eth = api . util . fromWei ( totalValue ) . toFormat ( ) ;
2016-12-09 13:44:35 +01:00
const gasLabel = ` gas (estimated: ${ new BigNumber ( estimated ) . toFormat ( ) } ) ` ;
const priceLabel = ` price (current: ${ new BigNumber ( priceDefault ) . toFormat ( ) } ) ` ;
return (
2016-12-11 17:43:51 +01:00
< div className = { styles . container } >
2017-02-03 20:01:09 +01:00
< RadioButtons
className = { styles . conditionRadio }
label = {
< FormattedMessage
id = 'txEditor.condition.label'
defaultMessage = 'Condition where transaction activates'
2017-01-18 13:05:01 +01:00
/ >
2017-02-03 20:01:09 +01:00
}
2017-05-12 12:06:16 +02:00
name = 'conditionType'
2017-02-03 20:01:09 +01:00
onChange = { this . onChangeConditionType }
value = { conditionType }
values = { CONDITION _VALUES }
/ >
{ this . renderConditions ( ) }
< div className = { styles . graphContainer } >
< div className = { styles . graphColumn } >
< GasPriceSelector
histogram = { histogram }
2016-12-11 17:43:51 +01:00
onChange = { this . onEditGasPrice }
2017-02-03 20:01:09 +01:00
price = { price }
2017-01-18 13:05:01 +01:00
/ >
2017-02-03 20:01:09 +01:00
< div className = { styles . gasPriceDesc } >
< FormattedMessage
id = 'txEditor.gas.info'
defaultMessage = 'You can choose the gas price based on the distribution of recent included transaction gas prices. The lower the gas price is, the cheaper the transaction will be. The higher the gas price is, the faster it should get mined by the network.'
/ >
< / d i v >
2016-12-09 13:44:35 +01:00
< / d i v >
2017-02-03 20:01:09 +01:00
< div className = { styles . editColumn } >
< div className = { styles . row } >
< Input
error = { errorGas }
hint = 'the amount of gas to use for the transaction'
label = { gasLabel }
min = { 1 }
onChange = { this . onEditGas }
type = 'number'
value = { gas }
/ >
< Input
error = { errorPrice }
hint = 'the price of gas to use for the transaction'
label = { priceLabel }
min = { 1 }
onChange = { this . onEditGasPrice }
type = 'number'
value = { price }
/ >
< / d i v >
< div className = { styles . row } >
< Input
disabled
error = { errorTotal }
hint = 'the total amount of the transaction'
label = 'total transaction amount'
value = { ` ${ eth } ETH ` }
/ >
< / d i v >
< div className = { styles . row } >
{ children }
< / d i v >
< / d i v >
< / d i v >
< / d i v >
) ;
}
renderConditions ( ) {
const { conditionType , condition , conditionBlockError } = this . props . store ;
if ( conditionType === CONDITIONS . NONE ) {
return null ;
}
if ( conditionType === CONDITIONS . BLOCK ) {
return (
< div className = { styles . conditionContainer } >
< div className = { styles . input } >
2016-12-09 15:43:24 +01:00
< Input
2017-02-03 20:01:09 +01:00
error = { conditionBlockError }
hint = {
< FormattedMessage
id = 'txEditor.condition.block.hint'
defaultMessage = 'The minimum block to send from'
/ >
}
label = {
< FormattedMessage
id = 'txEditor.condition.block.label'
defaultMessage = 'Transaction send block'
/ >
}
min = { 1 }
onChange = { this . onChangeConditionBlock }
type = 'number'
value = { condition . block }
2017-01-18 13:05:01 +01:00
/ >
2016-12-09 13:44:35 +01:00
< / d i v >
2017-02-03 20:01:09 +01:00
< / d i v >
) ;
}
return (
< div className = { styles . conditionContainer } >
< div className = { styles . input } >
< InputDate
hint = {
< FormattedMessage
id = 'txEditor.condition.date.hint'
defaultMessage = 'The minimum date to send from'
/ >
}
label = {
< FormattedMessage
id = 'txEditor.condition.date.label'
defaultMessage = 'Transaction send date'
/ >
}
onChange = { this . onChangeConditionDateTime }
value = { condition . time }
/ >
< / d i v >
< div className = { styles . input } >
< InputTime
hint = {
< FormattedMessage
id = 'txEditor.condition.time.hint'
defaultMessage = 'The minimum time to send from'
/ >
}
label = {
< FormattedMessage
id = 'txEditor.condition.time.label'
defaultMessage = 'Transaction send time'
/ >
}
onChange = { this . onChangeConditionDateTime }
value = { condition . time }
/ >
2016-12-09 13:44:35 +01:00
< / d i v >
< / d i v >
) ;
}
onEditGas = ( event , gas ) => {
const { store , onChange } = this . props ;
store . setGas ( gas ) ;
onChange && onChange ( 'gas' , gas ) ;
}
onEditGasPrice = ( event , price ) => {
const { store , onChange } = this . props ;
store . setPrice ( price ) ;
onChange && onChange ( 'gasPrice' , price ) ;
}
2017-02-03 20:01:09 +01:00
2017-05-12 12:06:16 +02:00
onChangeConditionType = ( event , conditionType ) => {
this . props . store . setConditionType ( conditionType ) ;
2017-02-03 20:01:09 +01:00
}
onChangeConditionBlock = ( event , blockNumber ) => {
this . props . store . setConditionBlockNumber ( blockNumber ) ;
}
onChangeConditionDateTime = ( event , datetime ) => {
this . props . store . setConditionDateTime ( datetime ) ;
}
2016-12-09 13:44:35 +01:00
}