watch for future contract logs

This commit is contained in:
Jonathan Rainville 2018-08-10 10:41:35 -04:00 committed by Pascal Precht
parent 1ffe9505c8
commit e0ca082a57
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
5 changed files with 42 additions and 2 deletions

View File

@ -127,6 +127,7 @@ export const messageListen = {
// Web Socket
export const WATCH_NEW_PROCESS_LOGS = 'WATCH_NEW_PROCESS_LOGS';
export const WATCH_NEW_CONTRACT_LOGS = 'WATCH_NEW_CONTRACT_LOGS';
export const INIT_BLOCK_HEADER = 'INIT_BLOCK_HEADER';
export function listenToProcessLogs(processName) {
@ -136,6 +137,12 @@ export function listenToProcessLogs(processName) {
};
}
export function listenToContractLogs() {
return {
type: WATCH_NEW_CONTRACT_LOGS
};
}
export function initBlockHeader(){
return {
type: INIT_BLOCK_HEADER

View File

@ -88,6 +88,10 @@ export function webSocketProcess(processName) {
return new WebSocket(constants.wsEndpoint + '/process-logs/' + processName);
}
export function webSocketContractLogs() {
return new WebSocket(constants.wsEndpoint + '/contracts/logs');
}
export function webSocketBlockHeader() {
return new WebSocket(`${constants.wsEndpoint}/blockchain/blockHeader`);
}

View File

@ -2,7 +2,7 @@ import React, {Component} from 'react';
import {connect} from 'react-redux';
import PropTypes from 'prop-types';
import {withRouter} from 'react-router-dom';
import {contractLogs as contractLogsAction} from '../actions';
import {contractLogs as contractLogsAction, listenToContractLogs} from '../actions';
import ContractLogger from '../components/ContractLogger';
import DataWrapper from "../components/DataWrapper";
@ -11,6 +11,7 @@ import {getContractLogsByContract} from "../reducers/selectors";
class ContractProfileContainer extends Component {
componentDidMount() {
if (this.props.contractLogs.length === 0) {
this.props.listenToContractLogs();
this.props.fetchContractLogs(this.props.match.params.contractName);
}
}
@ -33,12 +34,14 @@ function mapStateToProps(state, props) {
ContractProfileContainer.propTypes = {
contractLogs: PropTypes.array,
fetchContractLogs: PropTypes.func,
listenToContractLogs: PropTypes.func,
match: PropTypes.object
};
export default withRouter(connect(
mapStateToProps,
{
fetchContractLogs: contractLogsAction.request
fetchContractLogs: contractLogsAction.request,
listenToContractLogs: listenToContractLogs
}
)(ContractProfileContainer));

View File

@ -119,6 +119,19 @@ export function *watchListenToProcessLogs() {
yield takeEvery(actions.WATCH_NEW_PROCESS_LOGS, listenToProcessLogs);
}
export function *listenToContractLogs() {
const socket = api.webSocketContractLogs();
const channel = yield call(createChannel, socket);
while (true) {
const contractLog = yield take(channel);
yield put(contractLogs.success([contractLog]));
}
}
export function *watchListenToContractLogs() {
yield takeEvery(actions.WATCH_NEW_CONTRACT_LOGS, listenToContractLogs);
}
export const sendMessage = doRequest.bind(null, messageSend, api.sendMessage);
export function *watchSendMessage() {
@ -153,6 +166,7 @@ export default function *root() {
fork(watchFetchProcessLogs),
fork(watchFetchContractLogs),
fork(watchListenToProcessLogs),
fork(watchListenToContractLogs),
fork(watchFetchBlock),
fork(watchFetchTransactions),
fork(watchPostCommand),

View File

@ -95,6 +95,7 @@ class ConsoleListener {
blockNumber = utils.hexToNumber(blockNumber);
this.logs.push(Object.assign({}, request, {name, functionName, paramString, gasUsed, blockNumber}));
this.events.emit('contracts:log', this.logs[this.logs.length - 1]);
this.logger.info(`Blockchain>`.underline + ` ${name}.${functionName}(${paramString})`.bold + ` | ${transactionHash} | gas:${gasUsed} | blk:${blockNumber} | status:${status}`);
} else {
@ -105,6 +106,17 @@ class ConsoleListener {
_registerAPI() {
const apiRoute = '/embark-api/contracts/logs';
this.embark.registerAPICall(
'ws',
apiRoute,
(ws, _req) => {
this.events.on('contracts:log', function(log) {
ws.send(JSON.stringify(log), () => {
});
});
}
);
this.embark.registerAPICall(
'get',
apiRoute,