-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2332 from ethereum/issue/2316
"allEvents" for contract.getPastEvents(...)
- Loading branch information
Showing
7 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/web3-eth-contract/src/methods/AllPastEventLogsMethod.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* @file AllPastEventLogsMethod.js | ||
* @author Samuel Furter <samuel@ethereum.org> | ||
* @date 2018 | ||
*/ | ||
|
||
import {GetPastLogsMethod} from 'web3-core-method'; | ||
|
||
export default class AllPastEventLogsMethod extends GetPastLogsMethod { | ||
/** | ||
* @param {Utils} utils | ||
* @param {Object} formatters | ||
* @param {AllEventsLogDecoder} eventLogDecoder | ||
* @param {AbiModel} abiModel | ||
* | ||
* @constructor | ||
*/ | ||
constructor(utils, formatters, allEventsLogDecoder, abiModel) { | ||
super(utils, formatters); | ||
this.abiModel = abiModel; | ||
this.allEventsLogDecoder = allEventsLogDecoder; | ||
} | ||
|
||
/** | ||
* This method will be executed after the RPC request. | ||
* | ||
* @method afterExecution | ||
* | ||
* @param {Array} response | ||
* | ||
* @returns {Array} | ||
*/ | ||
afterExecution(response) { | ||
const formattedLogs = super.afterExecution(response); | ||
|
||
return formattedLogs.map((logItem) => { | ||
return this.allEventsLogDecoder.decode(this.abiModel, logItem); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/web3-eth-contract/tests/src/methods/AllPastEventLogsMethodTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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 AbiModel from '../../../src/models/AbiModel'; | ||
import AllPastEventLogsMethod from '../../../src/methods/AllPastEventLogsMethod'; | ||
|
||
// Mocks | ||
jest.mock('Utils'); | ||
jest.mock('formatters'); | ||
jest.mock('../../../src/decoders/AllEventsLogDecoder'); | ||
jest.mock('../../../src/models/AbiModel'); | ||
|
||
/** | ||
* AllPastEventLogsMethod test | ||
*/ | ||
describe('AllPastEventLogsMethodTest', () => { | ||
let allPastEventLogsMethod, allEventsLogDecoderMock, abiModelMock; | ||
|
||
beforeEach(() => { | ||
new AllEventsLogDecoder(); | ||
allEventsLogDecoderMock = AllEventsLogDecoder.mock.instances[0]; | ||
|
||
new AbiModel(); | ||
abiModelMock = AbiModel.mock.instances[0]; | ||
|
||
allPastEventLogsMethod = new AllPastEventLogsMethod(Utils, formatters, allEventsLogDecoderMock, abiModelMock); | ||
}); | ||
|
||
it('constructor check', () => { | ||
expect(allPastEventLogsMethod.utils).toEqual(Utils); | ||
|
||
expect(allPastEventLogsMethod.formatters).toEqual(formatters); | ||
|
||
expect(allPastEventLogsMethod.allEventsLogDecoder).toEqual(allEventsLogDecoderMock); | ||
|
||
expect(allPastEventLogsMethod.abiModel).toEqual(abiModelMock); | ||
|
||
expect(allPastEventLogsMethod).toBeInstanceOf(GetPastLogsMethod); | ||
}); | ||
|
||
it('calls afterExecution and returns the expected result', () => { | ||
const response = [false, false, false]; | ||
|
||
formatters.outputLogFormatter.mockReturnValue(true); | ||
|
||
allEventsLogDecoderMock.decode.mockReturnValue('decoded'); | ||
|
||
const mappedResponse = allPastEventLogsMethod.afterExecution(response); | ||
|
||
expect(mappedResponse).toEqual(['decoded', 'decoded', 'decoded']); | ||
|
||
expect(formatters.outputLogFormatter).toHaveBeenCalledTimes(3); | ||
|
||
expect(allEventsLogDecoderMock.decode).toHaveBeenCalledTimes(3); | ||
|
||
expect(allEventsLogDecoderMock.decode).toHaveBeenCalledWith(abiModelMock, true); | ||
}); | ||
}); |