Listen to contract events

This commit is contained in:
Anthony Laibe 2018-10-09 14:19:58 +01:00 committed by Pascal Precht
parent 5fa118ae2e
commit 4c27d34e0c
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
2 changed files with 25 additions and 2 deletions

View File

@ -20,6 +20,7 @@ class BlockchainConnector {
this.web3Endpoint = ''; this.web3Endpoint = '';
this.isWeb3Ready = false; this.isWeb3Ready = false;
this.wait = options.wait; this.wait = options.wait;
this.contractsSubscriptions = [];
self.events.setCommandHandler("blockchain:web3:isReady", (cb) => { self.events.setCommandHandler("blockchain:web3:isReady", (cb) => {
cb(self.isWeb3Ready); cb(self.isWeb3Ready);
@ -29,6 +30,8 @@ class BlockchainConnector {
cb(self); cb(self);
}); });
embark.registerActionForEvent("contracts:deploy:afterAll", this.subscribeToContractEvents.bind(this));
if (!this.web3) { if (!this.web3) {
this.initWeb3(); this.initWeb3();
} else { } else {
@ -601,6 +604,28 @@ class BlockchainConnector {
}); });
}); });
} }
subscribeToContractEvents(callback) {
this.contractsSubscriptions.forEach((eventEmitter) => {
eventEmitter.unsubscribe();
});
this.contractsSubscriptions = [];
this.events.request("contracts:list", (_err, contractsList) => {
contractsList.forEach(contractObject => {
if (!contractObject.address){
return;
}
const contract = this.ContractObject({abi: contractObject.abiDefinition, address: contractObject.address});
const eventEmitter = contract.events.allEvents();
this.contractsSubscriptions.push(eventEmitter);
eventEmitter.on('data', (data) => {
});
});
callback();
});
}
} }
BlockchainConnector.ACCEPTED_TYPES = ['rpc', 'ws', 'vm']; BlockchainConnector.ACCEPTED_TYPES = ['rpc', 'ws', 'vm'];

View File

@ -16,7 +16,6 @@ const parseRequest = function (reqBody) {
} catch (e) { } catch (e) {
return; // Request is not a json. Do nothing return; // Request is not a json. Do nothing
} }
console.error("Request ", jsonO)
if (jsonO.method === "eth_sendTransaction") { if (jsonO.method === "eth_sendTransaction") {
commList[jsonO.id] = { commList[jsonO.id] = {
type: 'contract-log', type: 'contract-log',
@ -38,7 +37,6 @@ const parseResponse = function (ipc, resBody) {
} catch (e) { } catch (e) {
return; // Response is not a json. Do nothing return; // Response is not a json. Do nothing
} }
console.error("Response", jsonO)
if (commList[jsonO.id]) { if (commList[jsonO.id]) {
commList[jsonO.id].transactionHash = jsonO.result; commList[jsonO.id].transactionHash = jsonO.result;
transactions[jsonO.result] = {commListId: jsonO.id}; transactions[jsonO.result] = {commListId: jsonO.id};