Skip to content

Commit

Permalink
Merge pull request #2338 from ethereum/issue/getPastEvents
Browse files Browse the repository at this point in the history
getPastEvents JSON-RPC methods updated
  • Loading branch information
nivida authored Feb 7, 2019
2 parents fffc0bd + 6ee7371 commit a770407
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 24 deletions.
32 changes: 16 additions & 16 deletions packages/web3-eth-contract/src/AbstractContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,25 @@ export default class AbstractContract extends AbstractWeb3Module {
* @callback callback callback(error, result)
* @returns {Promise<Array>}
*/
getPastEvents(eventName, options, callback) {
return new Promise(async (resolve, reject) => {
let method;

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

method = this.methodFactory.createPastEventLogsMethod(this.abiModel.getEvent(eventName));
} else {
method = this.methodFactory.createAllPastEventLogsMethod(this.abiModel);
async getPastEvents(eventName, options, callback) {
let method;

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

method.parameters = [options];
method.callback = callback;
method = this.methodFactory.createPastEventLogsMethod(this.abiModel.getEvent(eventName));
} else {
method = this.methodFactory.createAllPastEventLogsMethod(this.abiModel);
}

return resolve(await method.execute(this));
});
method.parameters = [options];
method.callback = callback;

const response = await method.execute(this);

return response;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/web3-eth-contract/src/factories/MethodFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ export default class MethodFactory {
this.utils,
this.formatters,
this.contractModuleFactory.createEventLogDecoder(),
abiItem
abiItem,
this.contractModuleFactory.createEventOptionsMapper()
);
}

Expand All @@ -115,7 +116,8 @@ export default class MethodFactory {
this.utils,
this.formatters,
this.contractModuleFactory.createAllEventsLogDecoder(),
abiModel
abiModel,
this.contractModuleFactory.createAllEventsOptionsMapper()
);
}

Expand Down
17 changes: 16 additions & 1 deletion packages/web3-eth-contract/src/methods/AllPastEventLogsMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,28 @@ export default class AllPastEventLogsMethod extends GetPastLogsMethod {
* @param {Object} formatters
* @param {AllEventsLogDecoder} eventLogDecoder
* @param {AbiModel} abiModel
* @param {AllEventsOptionsMapper} allEventsOptionsMapper
*
* @constructor
*/
constructor(utils, formatters, allEventsLogDecoder, abiModel) {
constructor(utils, formatters, allEventsLogDecoder, abiModel, allEventsOptionsMapper) {
super(utils, formatters);
this.abiModel = abiModel;
this.allEventsLogDecoder = allEventsLogDecoder;
this.allEventsOptionsMapper = allEventsOptionsMapper;
}

/**
* This method will be executed before the RPC request.
*
* @method beforeExecution
*
* @param {AbstractWeb3Module} moduleInstance - The package where the method is called from for example Eth.
*/
beforeExecution(moduleInstance) {
super.beforeExecution(moduleInstance);

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

/**
Expand Down
17 changes: 16 additions & 1 deletion packages/web3-eth-contract/src/methods/PastEventLogsMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,28 @@ export default class PastEventLogsMethod extends GetPastLogsMethod {
* @param {Object} formatters
* @param {EventLogDecoder} eventLogDecoder
* @param {AbiItemModel} abiItemModel
* @param {EventOptionsMapper} eventOptionsMapper
*
* @constructor
*/
constructor(utils, formatters, eventLogDecoder, abiItemModel) {
constructor(utils, formatters, eventLogDecoder, abiItemModel, eventOptionsMapper) {
super(utils, formatters);
this.abiItemModel = abiItemModel;
this.eventLogDecoder = eventLogDecoder;
this.eventOptionsMapper = eventOptionsMapper;
}

/**
* This method will be executed before the RPC request.
*
* @method beforeExecution
*
* @param {AbstractWeb3Module} moduleInstance - The package where the method is called from for example Eth.
*/
beforeExecution(moduleInstance) {
super.beforeExecution(moduleInstance);

this.parameters[0] = this.eventOptionsMapper.map(this.abiItemModel, moduleInstance, this.parameters[0]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import * as Utils from 'web3-utils';
import {formatters} from 'web3-core-helpers';
import {GetPastLogsMethod} from 'web3-core-method';
import AllEventsLogDecoder from '../../../src/decoders/AllEventsLogDecoder';
import AllEventsOptionsMapper from '../../../src/mappers/AllEventsOptionsMapper';
import AbiModel from '../../../src/models/AbiModel';
import AbstractContract from '../../../src/AbstractContract';
import AllPastEventLogsMethod from '../../../src/methods/AllPastEventLogsMethod';

// Mocks
jest.mock('Utils');
jest.mock('formatters');
jest.mock('../../../src/decoders/AllEventsLogDecoder');
jest.mock('../../../src/models/AbiModel');
jest.mock('../../../src/mappers/AllEventsOptionsMapper');
jest.mock('../../../src/AbstractContract');

/**
* AllPastEventLogsMethod test
*/
describe('AllPastEventLogsMethodTest', () => {
let allPastEventLogsMethod, allEventsLogDecoderMock, abiModelMock;
let allPastEventLogsMethod, allEventsLogDecoderMock, abiModelMock, allEventsOptionsMapperMock;

beforeEach(() => {
new AllEventsLogDecoder();
Expand All @@ -24,7 +28,16 @@ describe('AllPastEventLogsMethodTest', () => {
new AbiModel();
abiModelMock = AbiModel.mock.instances[0];

allPastEventLogsMethod = new AllPastEventLogsMethod(Utils, formatters, allEventsLogDecoderMock, abiModelMock);
new AllEventsOptionsMapper();
allEventsOptionsMapperMock = AllEventsOptionsMapper.mock.instances[0];

allPastEventLogsMethod = new AllPastEventLogsMethod(
Utils,
formatters,
allEventsLogDecoderMock,
abiModelMock,
allEventsOptionsMapperMock
);
});

it('constructor check', () => {
Expand All @@ -39,6 +52,22 @@ describe('AllPastEventLogsMethodTest', () => {
expect(allPastEventLogsMethod).toBeInstanceOf(GetPastLogsMethod);
});

it('calls beforeExecution and executes the expected methods', () => {
new AbstractContract();
const contractMock = AbstractContract.mock.instances[0];

allEventsOptionsMapperMock.map.mockReturnValueOnce({mapped: true});

formatters.inputLogFormatter.mockReturnValueOnce({options: true});

allPastEventLogsMethod.parameters = [{}];
allPastEventLogsMethod.beforeExecution(contractMock);

expect(allEventsOptionsMapperMock.map).toHaveBeenCalledWith(abiModelMock, contractMock, {options: true});

expect(formatters.inputLogFormatter).toHaveBeenCalledWith({});
});

it('calls afterExecution and returns the expected result', () => {
const response = [false, false, false];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ import {formatters} from 'web3-core-helpers';
import {GetPastLogsMethod} from 'web3-core-method';
import EventLogDecoder from '../../../src/decoders/EventLogDecoder';
import AbiItemModel from '../../../src/models/AbiItemModel';
import EventOptionsMapper from '../../../src/mappers/EventOptionsMapper';
import AbstractContract from '../../../src/AbstractContract';
import PastEventLogsMethod from '../../../src/methods/PastEventLogsMethod';

// Mocks
jest.mock('Utils');
jest.mock('formatters');
jest.mock('../../../src/decoders/EventLogDecoder');
jest.mock('../../../src/models/AbiItemModel');
jest.mock('../../../src/mappers/EventOptionsMapper');
jest.mock('../../../src/AbstractContract');

/**
* PastEventLogsMethod test
*/
describe('PastEventLogsMethodTest', () => {
let pastEventLogsMethod, eventLogDecoderMock, abiItemModelMock;
let pastEventLogsMethod, eventLogDecoderMock, abiItemModelMock, eventOptionsMapperMock;

beforeEach(() => {
new EventLogDecoder();
Expand All @@ -24,7 +28,16 @@ describe('PastEventLogsMethodTest', () => {
new AbiItemModel();
abiItemModelMock = AbiItemModel.mock.instances[0];

pastEventLogsMethod = new PastEventLogsMethod(Utils, formatters, eventLogDecoderMock, abiItemModelMock);
new EventOptionsMapper();
eventOptionsMapperMock = EventOptionsMapper.mock.instances[0];

pastEventLogsMethod = new PastEventLogsMethod(
Utils,
formatters,
eventLogDecoderMock,
abiItemModelMock,
eventOptionsMapperMock
);
});

it('constructor check', () => {
Expand All @@ -39,6 +52,22 @@ describe('PastEventLogsMethodTest', () => {
expect(pastEventLogsMethod).toBeInstanceOf(GetPastLogsMethod);
});

it('calls beforeExecution and executes the expected methods', () => {
new AbstractContract();
const contractMock = AbstractContract.mock.instances[0];

eventOptionsMapperMock.map.mockReturnValueOnce({mapped: true});

formatters.inputLogFormatter.mockReturnValueOnce({options: true});

pastEventLogsMethod.parameters = [{}];
pastEventLogsMethod.beforeExecution(contractMock);

expect(eventOptionsMapperMock.map).toHaveBeenCalledWith(abiItemModelMock, contractMock, {options: true});

expect(formatters.inputLogFormatter).toHaveBeenCalledWith({});
});

it('calls afterExecution and returns the expected result', () => {
const response = [false, false, false];

Expand Down

0 comments on commit a770407

Please sign in to comment.