Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter handling for contract event logs fixed in EventFilterEncoder #2458

Merged
merged 6 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/web3-eth-contract/src/AbstractContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ export default class AbstractContract extends AbstractWeb3Module {
* @callback callback callback(error, result)
* @returns {Promise<Array>}
*/
async getPastEvents(eventName, options, callback) {
getPastEvents(eventName, options, callback) {
let method;

if (eventName !== 'allEvents') {
if (!this.abiModel.hasEvent(eventName)) {
throw new Error(`Event with name "${eventName}" does not exists.`);
return Promise.reject(new Error(`Event with name "${eventName}" does not exists.`));
}

method = this.methodFactory.createPastEventLogsMethod(this.abiModel.getEvent(eventName));
Expand All @@ -173,9 +173,7 @@ export default class AbstractContract extends AbstractWeb3Module {
method.parameters = [options];
method.callback = callback;

const response = await method.execute(this);

return response;
return method.execute(this);
}

/**
Expand Down
15 changes: 9 additions & 6 deletions packages/web3-eth-contract/src/encoders/EventFilterEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,28 @@ export default class EventFilterEncoder {
* @returns {Array}
*/
encode(abiItemModel, filter) {
const indexedInputs = abiItemModel.getIndexedInputs();
let topics = [];

indexedInputs.forEach((indexedInput) => {
if (filter[indexedInput.name]) {
let filterItem = filter[indexedInput.name];
abiItemModel.getIndexedInputs().forEach((input) => {
if (filter[input.name]) {
let filterItem = filter[input.name];

if (isArray(filterItem)) {
filterItem = filterItem.map((item) => {
return this.abiCoder.encodeParameter(indexedInput.type, item);
return this.abiCoder.encodeParameter(input.type, item);
});

topics.push(filterItem);

return;
}

topics.push(this.abiCoder.encodeParameter(indexedInput.type, filterItem));
topics.push(this.abiCoder.encodeParameter(input.type, filterItem));

return;
}

topics.push(null);
});

return topics;
Expand Down
8 changes: 4 additions & 4 deletions packages/web3-eth-contract/src/mappers/EventOptionsMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export default class EventOptionsMapper {
options.toBlock = this.formatters.inputBlockNumberFormatter(options.toBlock);
}

if (!abiItemModel.anonymous) {
options.topics.unshift(abiItemModel.signature);
}

if (typeof options.filter !== 'undefined') {
options.topics = options.topics.concat(this.eventFilterEncoder.encode(abiItemModel, options.filter));
}

if (!abiItemModel.anonymous) {
options.topics.unshift(abiItemModel.signature);
}

if (!options.address) {
options.address = contract.address;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class AllPastEventLogsMethod extends GetPastLogsMethod {
/**
* @param {Utils} utils
* @param {Object} formatters
* @param {AllEventsLogDecoder} eventLogDecoder
* @param {AllEventsLogDecoder} allEventsLogDecoder
* @param {AbiModel} abiModel
* @param {AllEventsOptionsMapper} allEventsOptionsMapper
*
Expand All @@ -50,6 +50,10 @@ export default class AllPastEventLogsMethod extends GetPastLogsMethod {
super.beforeExecution(moduleInstance);

this.parameters[0] = this.allEventsOptionsMapper.map(this.abiModel, moduleInstance, this.parameters[0]);

if (this.parameters[0].filter) {
delete this.parameters[0].filter;
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/web3-eth-contract/src/methods/PastEventLogsMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export default class PastEventLogsMethod extends GetPastLogsMethod {
beforeExecution(moduleInstance) {
super.beforeExecution(moduleInstance);

// TODO: Clean up the event options and topics handling instead of deleting the property here.
this.parameters[0] = this.eventOptionsMapper.map(this.abiItemModel, moduleInstance, this.parameters[0]);

if (this.parameters[0].filter) {
delete this.parameters[0].filter;
}
}

/**
Expand Down