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.isWeb3Ready = false;
this.wait = options.wait;
this.contractsSubscriptions = [];
self.events.setCommandHandler("blockchain:web3:isReady", (cb) => {
cb(self.isWeb3Ready);
@ -29,6 +30,8 @@ class BlockchainConnector {
cb(self);
});
embark.registerActionForEvent("contracts:deploy:afterAll", this.subscribeToContractEvents.bind(this));
if (!this.web3) {
this.initWeb3();
} 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'];

View File

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