import { ZeroEx } from '0x.js'; import { colors, EtherscanLinkSuffixes } from '@0xproject/react-shared'; import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import Paper from 'material-ui/Paper'; import * as moment from 'moment'; import * as React from 'react'; import * as ReactTooltip from 'react-tooltip'; import { EtherScanIcon } from 'ts/components/ui/etherscan_icon'; import { Party } from 'ts/components/ui/party'; import { Fill, Token, TokenByAddress } from 'ts/types'; const PRECISION = 5; const IDENTICON_DIAMETER = 40; interface TradeHistoryItemProps { fill: Fill; tokenByAddress: TokenByAddress; userAddress: string; networkId: number; } interface TradeHistoryItemState {} export class TradeHistoryItem extends React.Component { public render() { const fill = this.props.fill; const tokens = _.values(this.props.tokenByAddress); const takerToken = _.find(tokens, token => { return token.address === fill.takerToken; }); const makerToken = _.find(tokens, token => { return token.address === fill.makerToken; }); // For now we don't show history items for orders using custom ERC20 // tokens the client does not know how to display. // TODO: Try to retrieve the name/symbol of an unknown token in order to display it // Be sure to remove similar logic in trade_history.tsx if (_.isUndefined(takerToken) || _.isUndefined(makerToken)) { return null; } const amountColStyle: React.CSSProperties = { fontWeight: 100, display: 'inline-block', }; const amountColClassNames = 'col col-12 lg-col-4 md-col-4 lg-py2 md-py2 sm-py1 lg-pr2 md-pr2 \ lg-right-align md-right-align sm-center'; return (
{this._renderDate()}
{this._renderAmounts(makerToken, takerToken)}
); } private _renderAmounts(makerToken: Token, takerToken: Token) { const fill = this.props.fill; const filledTakerTokenAmountInUnits = ZeroEx.toUnitAmount(fill.filledTakerTokenAmount, takerToken.decimals); const filledMakerTokenAmountInUnits = ZeroEx.toUnitAmount(fill.filledMakerTokenAmount, takerToken.decimals); let exchangeRate = filledTakerTokenAmountInUnits.div(filledMakerTokenAmountInUnits); const fillMakerTokenAmount = ZeroEx.toBaseUnitAmount(filledMakerTokenAmountInUnits, makerToken.decimals); let receiveAmount; let receiveToken; let givenAmount; let givenToken; if (this.props.userAddress === fill.maker && this.props.userAddress === fill.taker) { receiveAmount = new BigNumber(0); givenAmount = new BigNumber(0); receiveToken = makerToken; givenToken = takerToken; } else if (this.props.userAddress === fill.maker) { receiveAmount = fill.filledTakerTokenAmount; givenAmount = fillMakerTokenAmount; receiveToken = takerToken; givenToken = makerToken; exchangeRate = new BigNumber(1).div(exchangeRate); } else if (this.props.userAddress === fill.taker) { receiveAmount = fillMakerTokenAmount; givenAmount = fill.filledTakerTokenAmount; receiveToken = makerToken; givenToken = takerToken; } else { // This condition should never be hit throw new Error("Found Fill that wasn't performed by this user"); } return (
+ {this._renderAmount(receiveAmount, receiveToken.symbol, receiveToken.decimals)}
- {this._renderAmount(givenAmount, givenToken.symbol, givenToken.decimals)}
{exchangeRate.toFixed(PRECISION)} {givenToken.symbol}/{receiveToken.symbol}
); } private _renderDate() { const blockMoment = moment.unix(this.props.fill.blockTimestamp); if (!blockMoment.isValid()) { return null; } const dayOfMonth = blockMoment.format('D'); const monthAbreviation = blockMoment.format('MMM'); const formattedBlockDate = blockMoment.format('H:mmA - MMMM D, YYYY'); const dateTooltipId = `${this.props.fill.transactionHash}-date`; return (
{monthAbreviation}
{dayOfMonth}
{formattedBlockDate}
); } private _renderAmount(amount: BigNumber, symbol: string, decimals: number) { const unitAmount = ZeroEx.toUnitAmount(amount, decimals); return ( {unitAmount.toFixed(PRECISION)} {symbol} ); } }