// @flow import React from 'react'; import translate from 'translations'; import { UnlockHeader } from 'components/ui'; import { Donate, DataField, CustomMessage, GasField, AmountField, AddressField, ConfirmationModal } from './components'; import { BalanceSidebar } from 'components'; import pickBy from 'lodash/pickBy'; import type { State as AppState } from 'reducers'; import { connect } from 'react-redux'; import BaseWallet from 'libs/wallet/base'; // import type { Transaction } from './types'; import customMessages from './messages'; import { donationAddressMap } from 'config/data'; import { isValidETHAddress } from 'libs/validators'; import { getNodeLib, getNetworkConfig, getGasPriceGwei } from 'selectors/config'; import { getTokens } from 'selectors/wallet'; import type { Token, NetworkConfig } from 'config/data'; import Big from 'bignumber.js'; import { valueToHex } from 'libs/values'; import ERC20 from 'libs/erc20'; import type { TokenBalance } from 'selectors/wallet'; import { getTokenBalances } from 'selectors/wallet'; import type { RPCNode } from 'libs/nodes'; import type { TransactionWithoutGas, BroadcastTransaction } from 'libs/transaction'; import type { UNIT } from 'libs/units'; import { toWei, toTokenUnit } from 'libs/units'; import { formatGasLimit } from 'utils/formatters'; import { showNotification } from 'actions/notifications'; import type { ShowNotificationAction } from 'actions/notifications'; import type { NodeConfig } from 'config/data'; import { getNodeConfig } from 'selectors/config'; import { generateTransaction } from 'libs/transaction'; type State = { hasQueryString: boolean, readOnly: boolean, to: string, value: string, // $FlowFixMe - Comes from getParam not validating unit unit: UNIT, token: ?Token, gasLimit: string, data: string, gasChanged: boolean, transaction: ?BroadcastTransaction, showTxConfirm: boolean }; function getParam(query: { [string]: string }, key: string) { const keys = Object.keys(query); const index = keys.findIndex(k => k.toLowerCase() === key.toLowerCase()); if (index === -1) { return null; } return query[keys[index]]; } // TODO query string // TODO how to handle DATA? type Props = { location: { query: { [string]: string } }, wallet: BaseWallet, balance: Big, node: NodeConfig, nodeLib: RPCNode, network: NetworkConfig, tokens: Token[], tokenBalances: TokenBalance[], gasPrice: number, showNotification: ( level: string, msg: string, duration?: number ) => ShowNotificationAction }; export class SendTransaction extends React.Component { props: Props; state: State = { hasQueryString: false, readOnly: false, // FIXME use correct defaults to: '', value: '', unit: 'ether', token: null, gasLimit: '21000', data: '', gasChanged: false, showTxConfirm: false, transaction: null }; componentDidMount() { const queryPresets = pickBy(this.parseQuery()); if (Object.keys(queryPresets).length) { this.setState({ ...queryPresets, hasQueryString: true }); } } componentDidUpdate(_prevProps: Props, prevState: State) { // if gas is not changed // and we have valid tx // and relevant fields changed // estimate gas // TODO we might want to listen to gas price changes here // TODO debunce the call if ( !this.state.gasChanged && this.isValid() && (this.state.to !== prevState.to || this.state.value !== prevState.value || this.state.unit !== prevState.unit || this.state.data !== prevState.data) ) { this.estimateGas(); } } render() { const unlocked = !!this.props.wallet; const hasEnoughBalance = false; const { to, value, unit, gasLimit, data, readOnly, hasQueryString, showTxConfirm, transaction } = this.state; const customMessage = customMessages.find(m => m.to === to); return (
{hasQueryString &&

{translate('WARN_Send_Link')}

} {unlocked &&
{'' /* */}

{readOnly && !hasEnoughBalance &&
Warning! You do not have enough funds to complete this swap.
Please add more funds or access a different wallet.
}

{translate('SEND_trans')}

!token.balance.eq(0)) .map(token => token.symbol) .sort()} onChange={readOnly ? void 0 : this.onAmountChange} /> {unit === 'ether' && } {transaction &&