// Copyright 2015-2017 Parity Technologies (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 React, { Component } from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import BigNumber from 'bignumber.js';
import { newError } from '@parity/shared/redux/actions';
import { setVisibleAccounts } from '@parity/shared/redux/providers/personalActions';
import { Actionbar, Button, Page, Portal } from '@parity/ui';
import { CancelIcon, DeleteIcon, EditIcon, PlayIcon, VisibleIcon } from '@parity/ui/Icons';
import Editor from '@parity/ui/Editor';
import EditMeta from '@parity/dapp-account/EditMeta';
import Header from '@parity/dapp-account/Header';
import Delete from '@parity/dapp-address/Delete';
import ExecuteContract from './ExecuteContract';
import Events from './Events';
import Queries from './Queries';
import styles from './contract.css';
class Contract extends Component {
static contextTypes = {
api: React.PropTypes.object.isRequired
};
static propTypes = {
setVisibleAccounts: PropTypes.func.isRequired,
accounts: PropTypes.object,
accountsInfo: PropTypes.object,
contracts: PropTypes.object,
netVersion: PropTypes.string.isRequired,
params: PropTypes.object
};
state = {
contract: null,
fromAddress: '',
showDeleteDialog: false,
showEditDialog: false,
showExecuteDialog: false,
showDetailsDialog: false,
subscriptionId: -1,
blockSubscriptionId: -1,
allEvents: [],
minedEvents: [],
pendingEvents: [],
queryValues: {},
loadingEvents: true
};
componentDidMount () {
const { api } = this.context;
this.attachContract(this.props);
this.setBaseAccount(this.props);
this.setVisibleAccounts();
api
.subscribe('eth_blockNumber', this.queryContract)
.then(blockSubscriptionId => this.setState({ blockSubscriptionId }));
}
componentWillReceiveProps (nextProps) {
const { accounts, contracts } = nextProps;
if (Object.keys(contracts).length !== Object.keys(this.props.contracts).length) {
this.attachContract(nextProps);
}
if (Object.keys(accounts).length !== Object.keys(this.props.accounts).length) {
this.setBaseAccount(nextProps);
}
const prevAddress = this.props.params.address;
const nextAddress = nextProps.params.address;
if (prevAddress !== nextAddress) {
this.setVisibleAccounts(nextProps);
}
}
componentWillUnmount () {
const { api } = this.context;
const { subscriptionId, blockSubscriptionId, contract } = this.state;
if (blockSubscriptionId >= 0) {
api.unsubscribe(blockSubscriptionId);
}
if (subscriptionId >= 0) {
contract.unsubscribe(subscriptionId);
}
this.props.setVisibleAccounts([]);
}
setVisibleAccounts (props = this.props) {
const { params, setVisibleAccounts } = props;
const addresses = [ params.address ];
setVisibleAccounts(addresses);
}
render () {
const { accountsInfo, contracts, netVersion, params } = this.props;
const { allEvents, contract, queryValues, loadingEvents } = this.state;
const account = contracts[params.address];
if (!account) {
return null;
}
return (