2017-06-26 22:27:55 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import translate from 'translations';
|
|
|
|
import { UnlockHeader } from 'components/ui';
|
|
|
|
import {
|
2017-07-02 05:49:06 +00:00
|
|
|
Donate,
|
|
|
|
DataField,
|
|
|
|
CustomMessage,
|
|
|
|
GasField,
|
|
|
|
AmountField,
|
2017-08-23 06:57:18 +00:00
|
|
|
AddressField,
|
|
|
|
ConfirmationModal
|
2017-06-26 22:27:55 +00:00
|
|
|
} from './components';
|
2017-07-13 21:02:39 +00:00
|
|
|
import { BalanceSidebar } from 'components';
|
2017-06-26 22:27:55 +00:00
|
|
|
import pickBy from 'lodash/pickBy';
|
2017-06-29 23:03:11 +00:00
|
|
|
import type { State as AppState } from 'reducers';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import BaseWallet from 'libs/wallet/base';
|
2017-06-26 22:27:55 +00:00
|
|
|
// import type { Transaction } from './types';
|
|
|
|
import customMessages from './messages';
|
2017-07-04 00:20:47 +00:00
|
|
|
import { donationAddressMap } from 'config/data';
|
2017-08-08 03:45:08 +00:00
|
|
|
import { isValidETHAddress } from 'libs/validators';
|
2017-08-31 04:00:31 +00:00
|
|
|
import { toUnit } from 'libs/units';
|
2017-08-11 21:54:10 +00:00
|
|
|
import {
|
|
|
|
getNodeLib,
|
|
|
|
getNetworkConfig,
|
|
|
|
getGasPriceGwei
|
|
|
|
} from 'selectors/config';
|
2017-08-08 03:45:08 +00:00
|
|
|
import { getTokens } from 'selectors/wallet';
|
2017-08-11 21:54:10 +00:00
|
|
|
import type { Token, NetworkConfig } from 'config/data';
|
2017-08-08 03:45:08 +00:00
|
|
|
import Big from 'bignumber.js';
|
|
|
|
import { valueToHex } from 'libs/values';
|
|
|
|
import ERC20 from 'libs/erc20';
|
2017-07-15 23:05:57 +00:00
|
|
|
import type { TokenBalance } from 'selectors/wallet';
|
2017-08-31 04:00:31 +00:00
|
|
|
import {
|
|
|
|
getTokenBalances,
|
|
|
|
getTxFromBroadcastStatusTransactions
|
|
|
|
} from 'selectors/wallet';
|
2017-08-11 21:54:10 +00:00
|
|
|
import type { RPCNode } from 'libs/nodes';
|
2017-08-31 04:00:31 +00:00
|
|
|
import { broadcastTx } from 'actions/wallet';
|
|
|
|
import type { BroadcastTxRequestedAction } from 'actions/wallet';
|
|
|
|
import type { BroadcastStatusTransaction } from 'libs/transaction';
|
2017-08-11 21:54:10 +00:00
|
|
|
import type {
|
|
|
|
TransactionWithoutGas,
|
|
|
|
BroadcastTransaction
|
|
|
|
} from 'libs/transaction';
|
|
|
|
import type { UNIT } from 'libs/units';
|
2017-08-23 06:57:18 +00:00
|
|
|
import { toWei, toTokenUnit } from 'libs/units';
|
2017-08-08 03:45:08 +00:00
|
|
|
import { formatGasLimit } from 'utils/formatters';
|
2017-08-11 21:54:10 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
|
|
|
import type { ShowNotificationAction } from 'actions/notifications';
|
2017-08-23 06:57:18 +00:00
|
|
|
import type { NodeConfig } from 'config/data';
|
|
|
|
import { getNodeConfig } from 'selectors/config';
|
2017-08-31 04:00:31 +00:00
|
|
|
import { generateTransaction, getBalanceMinusGasCosts } from 'libs/transaction';
|
2017-06-26 22:27:55 +00:00
|
|
|
|
|
|
|
type State = {
|
2017-07-02 05:49:06 +00:00
|
|
|
hasQueryString: boolean,
|
|
|
|
readOnly: boolean,
|
|
|
|
to: string,
|
2017-08-31 04:00:31 +00:00
|
|
|
// amount value
|
2017-07-02 05:49:06 +00:00
|
|
|
value: string,
|
2017-08-11 21:54:10 +00:00
|
|
|
// $FlowFixMe - Comes from getParam not validating unit
|
|
|
|
unit: UNIT,
|
2017-08-23 06:57:18 +00:00
|
|
|
token: ?Token,
|
2017-07-02 05:49:06 +00:00
|
|
|
gasLimit: string,
|
|
|
|
data: string,
|
2017-08-11 21:54:10 +00:00
|
|
|
gasChanged: boolean,
|
2017-08-23 06:57:18 +00:00
|
|
|
transaction: ?BroadcastTransaction,
|
2017-08-31 04:00:31 +00:00
|
|
|
showTxConfirm: boolean,
|
|
|
|
generateDisabled: boolean
|
2017-06-26 22:27:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function getParam(query: { [string]: string }, key: string) {
|
2017-07-02 05:49:06 +00:00
|
|
|
const keys = Object.keys(query);
|
|
|
|
const index = keys.findIndex(k => k.toLowerCase() === key.toLowerCase());
|
|
|
|
if (index === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
return query[keys[index]];
|
2017-06-26 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO query string
|
|
|
|
// TODO how to handle DATA?
|
|
|
|
|
2017-07-15 23:05:57 +00:00
|
|
|
type Props = {
|
|
|
|
location: {
|
|
|
|
query: {
|
|
|
|
[string]: string
|
|
|
|
}
|
|
|
|
},
|
|
|
|
wallet: BaseWallet,
|
|
|
|
balance: Big,
|
2017-08-23 06:57:18 +00:00
|
|
|
node: NodeConfig,
|
2017-08-11 21:54:10 +00:00
|
|
|
nodeLib: RPCNode,
|
|
|
|
network: NetworkConfig,
|
2017-08-08 03:45:08 +00:00
|
|
|
tokens: Token[],
|
2017-08-11 21:54:10 +00:00
|
|
|
tokenBalances: TokenBalance[],
|
|
|
|
gasPrice: number,
|
2017-08-31 04:00:31 +00:00
|
|
|
broadcastTx: (signedTx: string) => BroadcastTxRequestedAction,
|
2017-08-11 21:54:10 +00:00
|
|
|
showNotification: (
|
|
|
|
level: string,
|
|
|
|
msg: string,
|
|
|
|
duration?: number
|
2017-08-31 04:00:31 +00:00
|
|
|
) => ShowNotificationAction,
|
|
|
|
transactions: Array<BroadcastStatusTransaction>
|
|
|
|
};
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
hasQueryString: false,
|
|
|
|
readOnly: false,
|
|
|
|
to: '',
|
|
|
|
value: '',
|
|
|
|
unit: 'ether',
|
|
|
|
token: null,
|
|
|
|
gasLimit: '21000',
|
|
|
|
data: '',
|
|
|
|
gasChanged: false,
|
|
|
|
showTxConfirm: false,
|
|
|
|
transaction: null,
|
|
|
|
generateDisabled: true
|
2017-07-15 23:05:57 +00:00
|
|
|
};
|
|
|
|
|
2017-06-26 22:27:55 +00:00
|
|
|
export class SendTransaction extends React.Component {
|
2017-07-15 23:05:57 +00:00
|
|
|
props: Props;
|
2017-08-31 04:00:31 +00:00
|
|
|
state: State = initialState;
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
componentDidMount() {
|
|
|
|
const queryPresets = pickBy(this.parseQuery());
|
|
|
|
if (Object.keys(queryPresets).length) {
|
|
|
|
this.setState({ ...queryPresets, hasQueryString: true });
|
2017-06-26 22:27:55 +00:00
|
|
|
}
|
2017-07-02 05:49:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 03:45:08 +00:00
|
|
|
componentDidUpdate(_prevProps: Props, prevState: State) {
|
2017-08-31 04:00:31 +00:00
|
|
|
// TODO listen to gas price changes here
|
|
|
|
// TODO debounce the call
|
2017-08-08 03:45:08 +00:00
|
|
|
if (
|
2017-08-31 04:00:31 +00:00
|
|
|
// if gas has not changed
|
2017-08-08 03:45:08 +00:00
|
|
|
!this.state.gasChanged &&
|
2017-08-31 04:00:31 +00:00
|
|
|
// if we have valid tx
|
2017-08-08 03:45:08 +00:00
|
|
|
this.isValid() &&
|
2017-08-31 04:00:31 +00:00
|
|
|
// if any relevant fields changed
|
2017-08-08 03:45:08 +00:00
|
|
|
(this.state.to !== prevState.to ||
|
|
|
|
this.state.value !== prevState.value ||
|
|
|
|
this.state.unit !== prevState.unit ||
|
|
|
|
this.state.data !== prevState.data)
|
|
|
|
) {
|
2017-08-31 04:00:31 +00:00
|
|
|
if (!isNaN(parseInt(this.state.value))) {
|
|
|
|
this.estimateGas();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.state.generateDisabled !== !this.isValid()) {
|
|
|
|
this.setState({ generateDisabled: !this.isValid() });
|
|
|
|
}
|
|
|
|
|
|
|
|
const componentStateTransaction = this.state.transaction;
|
|
|
|
if (componentStateTransaction) {
|
|
|
|
// lives in redux state
|
|
|
|
const currentTxAsBroadcastTransaction = getTxFromBroadcastStatusTransactions(
|
|
|
|
this.props.transactions,
|
|
|
|
componentStateTransaction.signedTx
|
|
|
|
);
|
|
|
|
// if there is a matching tx in redux state
|
|
|
|
if (currentTxAsBroadcastTransaction) {
|
|
|
|
// if the broad-casted transaction attempt is successful, clear the form
|
|
|
|
if (currentTxAsBroadcastTransaction.successfullyBroadcast) {
|
|
|
|
this.resetTransaction();
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
render() {
|
2017-07-04 03:21:19 +00:00
|
|
|
const unlocked = !!this.props.wallet;
|
2017-07-16 21:02:13 +00:00
|
|
|
const {
|
|
|
|
to,
|
|
|
|
value,
|
|
|
|
unit,
|
|
|
|
gasLimit,
|
|
|
|
data,
|
|
|
|
readOnly,
|
2017-08-11 21:54:10 +00:00
|
|
|
hasQueryString,
|
2017-08-23 06:57:18 +00:00
|
|
|
showTxConfirm,
|
2017-08-11 21:54:10 +00:00
|
|
|
transaction
|
2017-07-16 21:02:13 +00:00
|
|
|
} = this.state;
|
2017-07-02 05:49:06 +00:00
|
|
|
const customMessage = customMessages.find(m => m.to === to);
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
return (
|
|
|
|
<section className="container" style={{ minHeight: '50%' }}>
|
|
|
|
<div className="tab-content">
|
2017-07-06 00:36:11 +00:00
|
|
|
<main className="tab-pane active">
|
2017-07-02 05:49:06 +00:00
|
|
|
{hasQueryString &&
|
|
|
|
<div className="alert alert-info">
|
|
|
|
<p>
|
|
|
|
{translate('WARN_Send_Link')}
|
|
|
|
</p>
|
|
|
|
</div>}
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
<UnlockHeader title={'NAV_SendEther'} />
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
{unlocked &&
|
|
|
|
<article className="row">
|
2017-08-31 04:00:31 +00:00
|
|
|
{/* <!-- Sidebar --> */}
|
2017-07-02 05:49:06 +00:00
|
|
|
<section className="col-sm-4">
|
|
|
|
<div style={{ maxWidth: 350 }}>
|
2017-07-13 21:02:39 +00:00
|
|
|
<BalanceSidebar />
|
2017-07-02 05:49:06 +00:00
|
|
|
<hr />
|
|
|
|
<Donate onDonate={this.onNewTx} />
|
|
|
|
</div>
|
|
|
|
</section>
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
<section className="col-sm-8">
|
|
|
|
<div className="row form-group">
|
|
|
|
<h4 className="col-xs-12">
|
|
|
|
{translate('SEND_trans')}
|
|
|
|
</h4>
|
|
|
|
</div>
|
|
|
|
<AddressField
|
2017-07-04 00:20:47 +00:00
|
|
|
placeholder={donationAddressMap.ETH}
|
2017-07-02 05:49:06 +00:00
|
|
|
value={this.state.to}
|
|
|
|
onChange={readOnly ? null : this.onAddressChange}
|
|
|
|
/>
|
|
|
|
<AmountField
|
|
|
|
value={value}
|
|
|
|
unit={unit}
|
2017-07-15 23:05:57 +00:00
|
|
|
tokens={this.props.tokenBalances
|
|
|
|
.filter(token => !token.balance.eq(0))
|
|
|
|
.map(token => token.symbol)
|
|
|
|
.sort()}
|
2017-07-02 05:49:06 +00:00
|
|
|
onChange={readOnly ? void 0 : this.onAmountChange}
|
|
|
|
/>
|
2017-07-16 21:02:13 +00:00
|
|
|
<GasField
|
|
|
|
value={gasLimit}
|
|
|
|
onChange={readOnly ? void 0 : this.onGasChange}
|
|
|
|
/>
|
2017-07-02 05:49:06 +00:00
|
|
|
{unit === 'ether' &&
|
2017-07-16 21:02:13 +00:00
|
|
|
<DataField
|
|
|
|
value={data}
|
|
|
|
onChange={readOnly ? void 0 : this.onDataChange}
|
|
|
|
/>}
|
2017-07-02 05:49:06 +00:00
|
|
|
<CustomMessage message={customMessage} />
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
<div className="row form-group">
|
|
|
|
<div className="col-xs-12 clearfix">
|
2017-08-31 04:00:31 +00:00
|
|
|
<button
|
|
|
|
disabled={this.state.generateDisabled}
|
2017-07-16 21:02:13 +00:00
|
|
|
className="btn btn-info btn-block"
|
|
|
|
onClick={this.generateTx}
|
|
|
|
>
|
2017-07-02 05:49:06 +00:00
|
|
|
{translate('SEND_generate')}
|
2017-08-31 04:00:31 +00:00
|
|
|
</button>
|
2017-07-02 05:49:06 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
{transaction &&
|
|
|
|
<div>
|
|
|
|
<div className="row form-group">
|
|
|
|
<div className="col-sm-6">
|
|
|
|
<label>
|
|
|
|
{translate('SEND_raw')}
|
|
|
|
</label>
|
|
|
|
<textarea
|
|
|
|
className="form-control"
|
|
|
|
value={transaction.rawTx}
|
|
|
|
rows="4"
|
|
|
|
readOnly
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-sm-6">
|
|
|
|
<label>
|
|
|
|
{translate('SEND_signed')}
|
|
|
|
</label>
|
|
|
|
<textarea
|
|
|
|
className="form-control"
|
|
|
|
value={transaction.signedTx}
|
|
|
|
rows="4"
|
|
|
|
readOnly
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
<div className="form-group">
|
2017-08-31 04:00:31 +00:00
|
|
|
<button
|
2017-08-23 06:57:18 +00:00
|
|
|
className="btn btn-primary btn-block col-sm-11"
|
|
|
|
onClick={this.openTxModal}
|
|
|
|
>
|
|
|
|
{translate('SEND_trans')}
|
2017-08-31 04:00:31 +00:00
|
|
|
</button>
|
2017-08-23 06:57:18 +00:00
|
|
|
</div>
|
|
|
|
</div>}
|
2017-07-02 05:49:06 +00:00
|
|
|
</section>
|
|
|
|
</article>}
|
|
|
|
</main>
|
|
|
|
</div>
|
2017-08-23 06:57:18 +00:00
|
|
|
{transaction &&
|
|
|
|
showTxConfirm &&
|
|
|
|
<ConfirmationModal
|
|
|
|
wallet={this.props.wallet}
|
|
|
|
node={this.props.node}
|
2017-08-31 04:00:31 +00:00
|
|
|
signedTx={transaction.signedTx}
|
|
|
|
onClose={this.hideConfirmTx}
|
2017-08-23 06:57:18 +00:00
|
|
|
onConfirm={this.confirmTx}
|
|
|
|
/>}
|
2017-07-02 05:49:06 +00:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
parseQuery() {
|
|
|
|
const query = this.props.location.query;
|
|
|
|
const to = getParam(query, 'to');
|
|
|
|
const data = getParam(query, 'data');
|
|
|
|
// FIXME validate token against presets
|
|
|
|
const unit = getParam(query, 'tokenSymbol');
|
|
|
|
const value = getParam(query, 'value');
|
|
|
|
let gasLimit = getParam(query, 'gas');
|
|
|
|
if (gasLimit === null) {
|
|
|
|
gasLimit = getParam(query, 'limit');
|
2017-06-26 22:27:55 +00:00
|
|
|
}
|
2017-08-31 04:00:31 +00:00
|
|
|
const readOnly = getParam(query, 'readOnly') != null;
|
2017-07-02 05:49:06 +00:00
|
|
|
return { to, data, value, unit, gasLimit, readOnly };
|
|
|
|
}
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-08-08 03:45:08 +00:00
|
|
|
isValid() {
|
2017-08-31 04:00:31 +00:00
|
|
|
const { to, value, gasLimit } = this.state;
|
2017-08-08 03:45:08 +00:00
|
|
|
return (
|
|
|
|
isValidETHAddress(to) &&
|
|
|
|
value &&
|
|
|
|
Number(value) > 0 &&
|
|
|
|
!isNaN(Number(value)) &&
|
2017-08-31 04:00:31 +00:00
|
|
|
isFinite(Number(value)) &&
|
|
|
|
!isNaN(parseInt(gasLimit)) &&
|
|
|
|
isFinite(parseInt(gasLimit))
|
2017-08-08 03:45:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
async getTransactionInfoFromState(): Promise<TransactionWithoutGas> {
|
2017-08-11 21:54:10 +00:00
|
|
|
const { wallet } = this.props;
|
2017-08-23 06:57:18 +00:00
|
|
|
const { token } = this.state;
|
2017-08-11 21:54:10 +00:00
|
|
|
|
2017-08-08 03:45:08 +00:00
|
|
|
if (this.state.unit === 'ether') {
|
|
|
|
return {
|
|
|
|
to: this.state.to,
|
2017-08-11 21:54:10 +00:00
|
|
|
from: await wallet.getAddress(),
|
2017-08-23 06:57:18 +00:00
|
|
|
value: valueToHex(this.state.value),
|
|
|
|
data: this.state.data
|
2017-08-08 03:45:08 +00:00
|
|
|
};
|
2017-08-23 06:57:18 +00:00
|
|
|
} else {
|
|
|
|
if (!token) {
|
|
|
|
throw new Error('No matching token');
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
return {
|
|
|
|
to: token.address,
|
|
|
|
from: await wallet.getAddress(),
|
|
|
|
value: '0x0',
|
|
|
|
data: ERC20.transfer(
|
|
|
|
this.state.to,
|
|
|
|
toTokenUnit(new Big(this.state.value), token)
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 21:54:10 +00:00
|
|
|
async estimateGas() {
|
2017-08-31 04:00:31 +00:00
|
|
|
try {
|
|
|
|
const transaction = await this.getTransactionInfoFromState();
|
|
|
|
// Grab a reference to state. If it has changed by the time the estimateGas
|
|
|
|
// call comes back, we don't want to replace the gasLimit in state.
|
|
|
|
const state = this.state;
|
|
|
|
const gasLimit = await this.props.nodeLib.estimateGas(transaction);
|
2017-08-08 03:45:08 +00:00
|
|
|
if (this.state === state) {
|
|
|
|
this.setState({ gasLimit: formatGasLimit(gasLimit, state.unit) });
|
2017-08-31 04:00:31 +00:00
|
|
|
} else {
|
|
|
|
this.estimateGas();
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
2017-08-31 04:00:31 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.props.showNotification('danger', error.message, 5000);
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
// FIXME use mkTx instead or something that could take care of default gas/data and whatnot,
|
|
|
|
onNewTx = (
|
|
|
|
address: string,
|
|
|
|
amount: string,
|
|
|
|
unit: string,
|
|
|
|
data: string = '',
|
|
|
|
gasLimit: string = '21000'
|
|
|
|
) => {
|
|
|
|
this.setState({
|
|
|
|
to: address,
|
|
|
|
value: amount,
|
|
|
|
unit,
|
|
|
|
data,
|
|
|
|
gasLimit,
|
|
|
|
gasChanged: false
|
|
|
|
});
|
|
|
|
};
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
onAddressChange = (value: string) => {
|
|
|
|
this.setState({
|
|
|
|
to: value
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onDataChange = (value: string) => {
|
|
|
|
if (this.state.unit !== 'ether') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
...this.state,
|
|
|
|
data: value
|
|
|
|
});
|
|
|
|
};
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
onGasChange = (value: string) => {
|
|
|
|
this.setState({ gasLimit: value, gasChanged: true });
|
|
|
|
};
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
onAmountChange = (value: string, unit: string) => {
|
2017-07-13 21:02:39 +00:00
|
|
|
if (value === 'everything') {
|
2017-07-15 23:05:57 +00:00
|
|
|
if (unit === 'ether') {
|
2017-08-31 04:00:31 +00:00
|
|
|
const { balance, gasPrice } = this.props;
|
|
|
|
const { gasLimit } = this.state;
|
|
|
|
const weiBalance = toWei(balance, 'ether');
|
|
|
|
value = getBalanceMinusGasCosts(
|
|
|
|
new Big(gasLimit),
|
|
|
|
gasPrice,
|
|
|
|
weiBalance
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const tokenBalance = this.props.tokenBalances.find(
|
|
|
|
tokenBalance => tokenBalance.symbol === unit
|
|
|
|
);
|
|
|
|
if (!tokenBalance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
value = tokenBalance.balance.toString();
|
2017-07-15 23:05:57 +00:00
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|
2017-08-23 06:57:18 +00:00
|
|
|
let token = this.props.tokens.find(x => x.symbol === unit);
|
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
this.setState({
|
|
|
|
value,
|
2017-08-23 06:57:18 +00:00
|
|
|
unit,
|
|
|
|
token
|
2017-07-02 05:49:06 +00:00
|
|
|
});
|
|
|
|
};
|
2017-08-11 21:54:10 +00:00
|
|
|
|
|
|
|
generateTx = async () => {
|
|
|
|
const { nodeLib, wallet } = this.props;
|
2017-08-23 06:57:18 +00:00
|
|
|
const { token } = this.state;
|
|
|
|
const stateTxInfo = await this.getTransactionInfoFromState();
|
2017-08-11 21:54:10 +00:00
|
|
|
|
|
|
|
try {
|
2017-08-23 06:57:18 +00:00
|
|
|
const transaction = await generateTransaction(
|
|
|
|
nodeLib,
|
2017-08-11 21:54:10 +00:00
|
|
|
{
|
2017-08-23 06:57:18 +00:00
|
|
|
...stateTxInfo,
|
2017-08-11 21:54:10 +00:00
|
|
|
gasLimit: this.state.gasLimit,
|
|
|
|
gasPrice: this.props.gasPrice,
|
|
|
|
chainId: this.props.network.chainId
|
|
|
|
},
|
2017-08-23 06:57:18 +00:00
|
|
|
wallet,
|
|
|
|
token
|
2017-08-11 21:54:10 +00:00
|
|
|
);
|
|
|
|
this.setState({ transaction });
|
|
|
|
} catch (err) {
|
|
|
|
this.props.showNotification('danger', err.message, 5000);
|
|
|
|
}
|
|
|
|
};
|
2017-08-23 06:57:18 +00:00
|
|
|
|
|
|
|
openTxModal = () => {
|
|
|
|
if (this.state.transaction) {
|
|
|
|
this.setState({ showTxConfirm: true });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-31 04:00:31 +00:00
|
|
|
hideConfirmTx = () => {
|
2017-08-23 06:57:18 +00:00
|
|
|
this.setState({ showTxConfirm: false });
|
|
|
|
};
|
|
|
|
|
2017-08-31 04:00:31 +00:00
|
|
|
resetTransaction = () => {
|
|
|
|
this.setState({
|
|
|
|
to: '',
|
|
|
|
value: '',
|
|
|
|
transaction: null
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
confirmTx = (signedTx: string) => {
|
|
|
|
this.props.broadcastTx(signedTx);
|
2017-08-23 06:57:18 +00:00
|
|
|
};
|
2017-06-26 22:27:55 +00:00
|
|
|
}
|
2017-06-29 23:03:11 +00:00
|
|
|
|
|
|
|
function mapStateToProps(state: AppState) {
|
2017-07-04 03:21:19 +00:00
|
|
|
return {
|
2017-07-13 21:02:39 +00:00
|
|
|
wallet: state.wallet.inst,
|
2017-07-15 23:05:57 +00:00
|
|
|
balance: state.wallet.balance,
|
2017-08-11 21:54:10 +00:00
|
|
|
tokenBalances: getTokenBalances(state),
|
2017-08-23 06:57:18 +00:00
|
|
|
node: getNodeConfig(state),
|
2017-08-11 21:54:10 +00:00
|
|
|
nodeLib: getNodeLib(state),
|
|
|
|
network: getNetworkConfig(state),
|
2017-08-08 03:45:08 +00:00
|
|
|
tokens: getTokens(state),
|
2017-08-31 04:00:31 +00:00
|
|
|
gasPrice: toWei(new Big(getGasPriceGwei(state)), 'gwei'),
|
|
|
|
transactions: state.wallet.transactions
|
2017-07-04 03:21:19 +00:00
|
|
|
};
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 04:00:31 +00:00
|
|
|
export default connect(mapStateToProps, { showNotification, broadcastTx })(
|
|
|
|
SendTransaction
|
|
|
|
);
|