diff --git a/embark-ui/src/actions/index.js b/embark-ui/src/actions/index.js index 9fe03506..2836ce33 100644 --- a/embark-ui/src/actions/index.js +++ b/embark-ui/src/actions/index.js @@ -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 diff --git a/embark-ui/src/api/index.js b/embark-ui/src/api/index.js index a4c6b3f9..09acbfbb 100644 --- a/embark-ui/src/api/index.js +++ b/embark-ui/src/api/index.js @@ -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`); } diff --git a/embark-ui/src/containers/ContractLoggerContainer.js b/embark-ui/src/containers/ContractLoggerContainer.js index 1c56b6f2..ecf37479 100644 --- a/embark-ui/src/containers/ContractLoggerContainer.js +++ b/embark-ui/src/containers/ContractLoggerContainer.js @@ -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)); diff --git a/embark-ui/src/sagas/index.js b/embark-ui/src/sagas/index.js index 0044eebb..7f2fcea8 100644 --- a/embark-ui/src/sagas/index.js +++ b/embark-ui/src/sagas/index.js @@ -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), diff --git a/lib/modules/console_listener/index.js b/lib/modules/console_listener/index.js index 308b45c3..5e763125 100644 --- a/lib/modules/console_listener/index.js +++ b/lib/modules/console_listener/index.js @@ -85,6 +85,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 { @@ -95,6 +96,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,