From 411d9ba419381f2906c4ccd2c78e9d55788de0c5 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 20 Jun 2018 18:23:48 +0200 Subject: [PATCH 1/3] Add max value in TxForm (fix #38) --- .../light-react/src/Send/TxForm/TxForm.js | 35 ++++++++++++- .../TokensList/TokenBalance/TokenBalance.js | 34 ++----------- packages/light-react/src/stores/sendStore.js | 4 +- .../light-react/src/stores/tokensStore.js | 1 + packages/light-react/src/utils/withBalance.js | 50 +++++++++++++++++++ packages/light-ui/src/TokenCard/TokenCard.js | 3 +- 6 files changed, 90 insertions(+), 37 deletions(-) create mode 100644 packages/light-react/src/utils/withBalance.js diff --git a/packages/light-react/src/Send/TxForm/TxForm.js b/packages/light-react/src/Send/TxForm/TxForm.js index 932dfb36..41c5e222 100644 --- a/packages/light-react/src/Send/TxForm/TxForm.js +++ b/packages/light-react/src/Send/TxForm/TxForm.js @@ -5,21 +5,40 @@ import React, { Component } from 'react'; import { FormField, Header } from 'light-ui'; +import { fromWei, toWei } from '@parity/api/lib/util/wei'; import { inject, observer } from 'mobx-react'; import { Link } from 'react-router-dom'; 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 @inject('sendStore') +@withBalance(({ sendStore: { token } }) => token) @observer class Send extends Component { componentDidMount () { this.props.sendStore.estimateGas(); } + getMaxAmount = () => { + const { + balance, + sendStore: { estimated, tx } + } = this.props; + + // TODO this in sendStore as @computed? + return balance && estimated + ? +fromWei( + toWei(balance).minus( + estimated.multipliedBy(toWei(tx.gasPrice, 'shannon')) + ) + ) + : 0.01; + }; + handleChangeAmount = ({ target: { value } }) => this.props.sendStore.setTxAmount(value); @@ -35,6 +54,8 @@ class Send extends Component { } }; + handleMax = () => this.props.sendStore.setTxAmount(this.getMaxAmount()); + handleSubmit = e => { e.preventDefault(); const { history, sendStore } = this.props; @@ -65,6 +86,7 @@ class Send extends Component {
@@ -97,9 +126,11 @@ class Send extends Component {