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

Problem: decodeLogs doesn't support contracts with multiple events wi… #200

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/expectEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,29 @@ function decodeLogs (logs, emitter, eventName) {
throw new Error('Unknown contract object');
}

let eventABI = abi.filter(x => x.type === 'event' && x.name === eventName);
if (eventABI.length === 0) {
const eventABIs = abi.filter(x => x.type === 'event' && x.name === eventName);
if (eventABIs.length === 0) {
throw new Error(`No ABI entry for event '${eventName}'`);
} else if (eventABI.length > 1) {
throw new Error(`Multiple ABI entries for event '${eventName}', only uniquely named events are supported`);
}

eventABI = eventABI[0];

// The first topic will equal the hash of the event signature
const eventSignature = `${eventName}(${eventABI.inputs.map(input => input.type).join(',')})`;
const eventTopic = web3.utils.sha3(eventSignature);
const eventSignatures = eventABIs.map(
eventABI => `${eventName}(${eventABI.inputs.map(input => input.type).join(',')})`,
);
const eventTopics = eventSignatures.map(eventSignature => web3.utils.sha3(eventSignature));

// Only decode events of type 'EventName'
return logs
.filter(log => log.topics.length > 0 && log.topics[0] === eventTopic && (!address || log.address === address))
.map(log => web3.eth.abi.decodeLog(eventABI.inputs, log.data, log.topics.slice(1)))
.filter(
log => log.topics.length > 0 &&
eventTopics.find(eventTopic => log.topics[0] === eventTopic) &&
(!address || log.address === address),
)
.map(log => web3.eth.abi.decodeLog(
eventABIs[eventTopics.findIndex(eventTopic => log.topics[0] === eventTopic)].inputs,
log.data,
log.topics.slice(1)),
)
.map(decoded => ({ event: eventName, args: decoded }));
}

Expand Down Expand Up @@ -191,11 +197,11 @@ expectEvent.notEmitted.inTransaction = notInTransaction;
expectEvent.not = {};
expectEvent.not.inConstruction = deprecate(
notInConstruction,
'expectEvent.not is deprecated. Use expectEvent.notEmitted instead.'
'expectEvent.not is deprecated. Use expectEvent.notEmitted instead.',
);
expectEvent.not.inTransaction = deprecate(
notInTransaction,
'expectEvent.not is deprecated. Use expectEvent.notEmitted instead.'
'expectEvent.not is deprecated. Use expectEvent.notEmitted instead.',
);

module.exports = expectEvent;
20 changes: 9 additions & 11 deletions test/src/expectEvent.truffle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,14 @@ contract('expectEvent (truffle contracts)', function ([deployer]) {

context('with directly called contract', function () {
it('accepts emitted events with correct indexed parameter and emitter object', async function () {
expectEvent.inTransaction(this.txHash, this.emitter, 'IndexedUint', {
await expectEvent.inTransaction(this.txHash, this.emitter, 'IndexedUint', {
indexedValue: this.indexedValue,
normalValue: this.normalValue,
});
});

it('accepts emitted indexed events with correct indexed parameter and emitter class', async function () {
expectEvent.inTransaction(this.txHash, EventEmitter, 'IndexedUint', {
await expectEvent.inTransaction(this.txHash, EventEmitter, 'IndexedUint', {
indexedValue: this.indexedValue,
normalValue: this.normalValue,
});
Expand All @@ -579,14 +579,14 @@ contract('expectEvent (truffle contracts)', function ([deployer]) {

context('with indirectly called contract', function () {
it('accepts events emitted from other contracts with emitter object', async function () {
expectEvent.inTransaction(this.txHash, this.secondEmitter, 'IndexedUint', {
await expectEvent.inTransaction(this.txHash, this.secondEmitter, 'IndexedUint', {
indexedValue: this.indexedValue2,
normalValue: this.normalValue2,
});
});

it('accepts emitted indexed events with emitter class', async function () {
expectEvent.inTransaction(this.txHash, IndirectEventEmitter, 'IndexedUint', {
await expectEvent.inTransaction(this.txHash, IndirectEventEmitter, 'IndexedUint', {
indexedValue: this.indexedValue2,
normalValue: this.normalValue2,
});
Expand Down Expand Up @@ -685,18 +685,16 @@ contract('expectEvent (truffle contracts)', function ([deployer]) {
});
});

describe('with non-unique event names', function () {
it('throws', async function () {
const { transactionHash } = await this.emitter.emitRepeated('0x');
await assertFailure(expectEvent.inTransaction(transactionHash, EventEmitter, 'Repeated'));
});
it('accepts events with non-unique event names', async function () {
const { tx } = await this.emitter.emitRepeated('0x01');
await expectEvent.inTransaction(tx, EventEmitter, 'Repeated', { value: '0x01' });
});

describe('with non-existing event names', function () {
it('throws', async function () {
// Which function we call is not important for this test
const { transactionHash } = await this.emitter.emitArgumentless();
await assertFailure(expectEvent.inTransaction(transactionHash, EventEmitter, 'Nonexistant'));
const { tx } = await this.emitter.emitArgumentless();
await assertFailure(expectEvent.inTransaction(tx, EventEmitter, 'Nonexistant'));
});
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/src/expectEvent.web3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,14 @@ contract('expectEvent (web3 contracts) ', function ([deployer]) {

context('with directly called contract', function () {
it('accepts emitted events with correct indexed parameter and emitter object', async function () {
expectEvent.inTransaction(this.txHash, this.emitter, 'IndexedUint', {
await expectEvent.inTransaction(this.txHash, this.emitter, 'IndexedUint', {
indexedValue: this.indexedValue,
normalValue: this.normalValue,
});
});

it('accepts emitted indexed events with correct indexed parameter and emitter class', async function () {
expectEvent.inTransaction(this.txHash, EventEmitter, 'IndexedUint', {
await expectEvent.inTransaction(this.txHash, EventEmitter, 'IndexedUint', {
indexedValue: this.indexedValue,
normalValue: this.normalValue,
});
Expand All @@ -525,13 +525,13 @@ contract('expectEvent (web3 contracts) ', function ([deployer]) {

context('with indirectly called contract', function () {
it('accepts events emitted from other contracts', async function () {
expectEvent.inTransaction(this.txHash, this.secondEmitter, 'IndexedUint', {
await expectEvent.inTransaction(this.txHash, this.secondEmitter, 'IndexedUint', {
indexedValue: this.indexedValue2,
normalValue: this.normalValue2,
});
});
it('accepts emitted indexed events with correct indexed parameter and emitter class', async function () {
expectEvent.inTransaction(this.txHash, IndirectEventEmitter, 'IndexedUint', {
await expectEvent.inTransaction(this.txHash, IndirectEventEmitter, 'IndexedUint', {
indexedValue: this.indexedValue2,
normalValue: this.normalValue2,
});
Expand Down Expand Up @@ -632,8 +632,8 @@ contract('expectEvent (web3 contracts) ', function ([deployer]) {

describe('with non-unique event names', function () {
it('throws', async function () {
const { transactionHash } = await this.emitter.methods.emitRepeated('0x').send();
await assertFailure(expectEvent.inTransaction(transactionHash, EventEmitter, 'Repeated'));
const { transactionHash } = await this.emitter.methods.emitRepeated('0x01').send();
await expectEvent.inTransaction(transactionHash, EventEmitter, 'Repeated', { value: '0x01' });
});
});

Expand Down