From 6ce78dfce30e50cee5d102000c510b0fbc15ecea Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 9 Oct 2018 14:38:51 +0100 Subject: [PATCH] Add endpoint --- lib/modules/blockchain_connector/index.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 79c372d7..a3af97cb 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();