import { colors } from '@0x/react-shared'; import { BigNumber } from '@0x/utils'; import * as _ from 'lodash'; import Dialog from 'material-ui/Dialog'; import FlatButton from 'material-ui/FlatButton'; import * as React from 'react'; import { Blockchain } from 'ts/blockchain'; import { TokenAmountInput } from 'ts/components/inputs/token_amount_input'; import { EthAmountInput } from 'ts/containers/inputs/eth_amount_input'; import { Side, Token } from 'ts/types'; interface EthWethConversionDialogProps { blockchain: Blockchain; userAddress: string; networkId: number; direction: Side; onComplete: (direction: Side, value: BigNumber) => void; onCancelled: () => void; isOpen: boolean; token: Token; etherBalanceInWei?: BigNumber; lastForceTokenStateRefetch: number; } interface EthWethConversionDialogState { value?: BigNumber; shouldShowIncompleteErrs: boolean; hasErrors: boolean; isEthTokenBalanceLoaded: boolean; ethTokenBalance: BigNumber; } export class EthWethConversionDialog extends React.Component< EthWethConversionDialogProps, EthWethConversionDialogState > { private _isUnmounted: boolean; constructor(props: EthWethConversionDialogProps) { super(props); this._isUnmounted = false; this.state = { shouldShowIncompleteErrs: false, hasErrors: false, isEthTokenBalanceLoaded: false, ethTokenBalance: new BigNumber(0), }; } public componentWillMount(): void { // tslint:disable-next-line:no-floating-promises this._fetchEthTokenBalanceAsync(); } public componentWillUnmount(): void { this._isUnmounted = true; } public render(): React.ReactNode { const convertDialogActions = [ , , ]; const title = this.props.direction === Side.Deposit ? 'Wrap ETH' : 'Unwrap WETH'; return !_.isUndefined(this.props.etherBalanceInWei) ? ( {this._renderConversionDialogBody()} ) : null; } private _renderConversionDialogBody(): React.ReactNode { const explanation = this.props.direction === Side.Deposit ? 'Convert your Ether into a tokenized, tradable form.' : "Convert your Wrapped Ether back into it's native form."; const isWrappedVersion = this.props.direction === Side.Receive; return (
{explanation}
{this._renderCurrency(isWrappedVersion)}
{this._renderCurrency(!isWrappedVersion)}
{this.props.direction === Side.Receive ? ( ) : ( )}
1 ETH = 1 WETH
{this.props.direction === Side.Receive && this.state.isEthTokenBalanceLoaded && (
Max
)}
); } private _renderCurrency(isWrappedVersion: boolean): React.ReactNode { const name = isWrappedVersion ? 'Wrapped Ether' : 'Ether'; const iconUrl = isWrappedVersion ? '/images/token_icons/ether_erc20.png' : '/images/ether.png'; const symbol = isWrappedVersion ? 'WETH' : 'ETH'; return (
{name}
({symbol})
); } private _onMaxClick(): void { this.setState({ value: this.state.ethTokenBalance, }); } private _onValueChange(isValid: boolean, amount?: BigNumber): void { this.setState({ value: amount, hasErrors: !isValid, }); } private _onConvertClick(): void { if (this.state.hasErrors) { this.setState({ shouldShowIncompleteErrs: true, }); } else { const value = this.state.value; this.setState({ value: undefined, }); this.props.onComplete(this.props.direction, value); } } private _onCancel(): void { this.setState({ value: undefined, }); this.props.onCancelled(); } private async _fetchEthTokenBalanceAsync(): Promise { const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress; const [balance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync( userAddressIfExists, this.props.token.address, ); if (!this._isUnmounted) { this.setState({ isEthTokenBalanceLoaded: true, ethTokenBalance: balance, }); } } }