// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see .
import BigNumber from 'bignumber.js';
import React, { Component, PropTypes } from 'react';
import { Container, ContainerTitle } from '../../../ui';
import styles from '../contract.css';
export default class Events extends Component {
static contextTypes = {
api: PropTypes.object
}
static propTypes = {
events: PropTypes.array,
isTest: PropTypes.bool
}
state = {
transactions: {}
}
componentDidMount () {
this.componentWillReceiveProps(this.props);
}
componentWillReceiveProps (newProps) {
this.retrieveTransactions(newProps.events);
}
render () {
const { events, isTest } = this.props;
const { transactions } = this.state;
if (!events || !events.length) {
return null;
}
const rows = events.map((event) => {
const transaction = transactions[event.transactionHash] || {};
const classes = `${styles.event} ${styles[event.state]}`;
const url = `https://${isTest ? 'testnet.' : ''}etherscan.io/tx/${event.transactionHash}`;
const keys = Object.keys(event.params).map((key, index) => {
return
{ key }
;
});
const values = Object.values(event.params).map((value, index) => {
return (
{ this.renderValue(value) }
);
});
return (
{ event.state === 'pending' ? 'pending' : event.blockNumber.toFormat(0) } |
{ transaction.from }
{ event.transactionHash }
|
{ event.type } =>
{ keys }
|
{ values }
|
);
});
return (
);
}
renderValue (value) {
const { api } = this.context;
if (api.util.isInstanceOf(value, BigNumber)) {
return value.toFormat(0);
} else if (api.util.isArray(value)) {
return api.util.bytesToHex(value);
}
return value.toString();
}
retrieveTransactions (events) {
const { api } = this.context;
const { transactions } = this.state;
const hashes = {};
events.forEach((event) => {
if (!hashes[event.transactionHash] && !transactions[event.transactionHash]) {
hashes[event.transactionHash] = true;
}
});
Promise
.all(Object.keys(hashes).map((hash) => api.eth.getTransactionByHash(hash)))
.then((newTransactions) => {
this.setState({
transactions: newTransactions.reduce((store, transaction) => {
transactions[transaction.hash] = transaction;
return transactions;
}, transactions)
});
});
}
}