diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 79c372d72..a3af97cbd 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -21,6 +21,7 @@ class BlockchainConnector { this.isWeb3Ready = false; this.wait = options.wait; this.contractsSubscriptions = []; + this.contractsEvents = []; self.events.setCommandHandler("blockchain:web3:isReady", (cb) => { cb(self.isWeb3Ready); @@ -369,6 +370,24 @@ class BlockchainConnector { }); } ); + + plugin.registerAPICall( + 'ws', + '/embark-api/blockchain/contracts/event', + (ws, _req) => { + this.events.on('blockchain:contracts:event', function(data) { + ws.send(JSON.stringify(data), () => {}); + }); + } + ); + + plugin.registerAPICall( + 'get', + '/embark-api/blockchain/contracts/events', + (_req, res) => { + res.send(JSON.stringify(this.contractsEvents)); + } + ); } getAccountsWithTransactionCount(callback) { @@ -620,7 +639,9 @@ class BlockchainConnector { const eventEmitter = contract.events.allEvents(); this.contractsSubscriptions.push(eventEmitter); eventEmitter.on('data', (data) => { - + const dataWithName = Object.assign(data, {name: contractObject.className}); + this.contractsEvents.push(dataWithName) + this.events.emit('blockchain:contract:event', dataWithName); }); }); callback();