watch for future contract logs

This commit is contained in:
Jonathan Rainville 2018-08-10 10:41:35 -04:00
parent 77a77eb408
commit 40ec534d23
5 changed files with 42 additions and 2 deletions

View File

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

View File

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

View File

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

View File

@ -119,6 +119,19 @@ export function *watchListenToProcessLogs() {
yield takeEvery(actions.WATCH_NEW_PROCESS_LOGS, listenToProcessLogs); 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 const sendMessage = doRequest.bind(null, messageSend, api.sendMessage);
export function *watchSendMessage() { export function *watchSendMessage() {
@ -153,6 +166,7 @@ export default function *root() {
fork(watchFetchProcessLogs), fork(watchFetchProcessLogs),
fork(watchFetchContractLogs), fork(watchFetchContractLogs),
fork(watchListenToProcessLogs), fork(watchListenToProcessLogs),
fork(watchListenToContractLogs),
fork(watchFetchBlock), fork(watchFetchBlock),
fork(watchFetchTransactions), fork(watchFetchTransactions),
fork(watchPostCommand), fork(watchPostCommand),

View File

@ -85,6 +85,7 @@ class ConsoleListener {
blockNumber = utils.hexToNumber(blockNumber); blockNumber = utils.hexToNumber(blockNumber);
this.logs.push(Object.assign({}, request, {name, functionName, paramString, gasUsed, 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}`); this.logger.info(`Blockchain>`.underline + ` ${name}.${functionName}(${paramString})`.bold + ` | ${transactionHash} | gas:${gasUsed} | blk:${blockNumber} | status:${status}`);
} else { } else {
@ -95,6 +96,17 @@ class ConsoleListener {
_registerAPI() { _registerAPI() {
const apiRoute = '/embark-api/contracts/logs'; 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( this.embark.registerAPICall(
'get', 'get',
apiRoute, apiRoute,