Changed inTransaction, removed decodeLogs.

This commit is contained in:
Nicolás Venturo
2018-11-22 16:17:25 -03:00
parent 5e95ef2afc
commit 0cdbbd553a
5 changed files with 47 additions and 31 deletions

View File

@ -1,12 +0,0 @@
const SolidityEvent = require('web3/lib/web3/event.js');
function decodeLogs (logs, events, address) {
return logs.map(log => {
const event = new SolidityEvent(null, events[log.topics[0]], address);
return event.decode(log);
});
}
module.exports = {
decodeLogs,
};

View File

@ -1,4 +1,5 @@
const { decodeLogs } = require('./decodeLogs');
const SolidityEvent = require('web3/lib/web3/event.js');
const _ = require('lodash');
const BigNumber = web3.BigNumber;
const should = require('chai')
@ -18,14 +19,13 @@ function inLogs (logs, eventName, eventArgs = {}) {
return event;
}
async function inTransaction (tx, eventName, eventArgs = {}) {
const { logs } = await tx;
return inLogs(logs, eventName, eventArgs);
async function inConstruction (contract, eventName, eventArgs = {}) {
return inTransaction(contract.transactionHash, contract.constructor, eventName, eventArgs);
}
async function inConstruction (contract, eventName, eventArgs = {}) {
const receipt = await web3.eth.getTransactionReceipt(contract.transactionHash);
const logs = decodeLogs(receipt.logs, contract.constructor.events, contract.address);
async function inTransaction (txHash, emitter, eventName, eventArgs = {}) {
const receipt = await web3.eth.getTransactionReceipt(txHash);
const logs = decodeLogs(receipt.logs, emitter.events);
return inLogs(logs, eventName, eventArgs);
}
@ -44,8 +44,17 @@ function isBigNumber (object) {
(object.constructor && object.constructor.name === 'BigNumber');
}
function decodeLogs (logs, events) {
return _.flatten(logs.map(log =>
log.topics.filter(topic => topic in events).map(topic => {
const event = new SolidityEvent(null, events[topic], 0);
return event.decode(log);
})
));
}
module.exports = {
inLogs,
inTransaction,
inConstruction,
inTransaction,
};

View File

@ -7,7 +7,7 @@ const should = require('chai')
.use(require('chai-as-promised'))
.should();
describe('expectEvent', function () {
describe.only('expectEvent', function () {
beforeEach(async function () {
this.constructionValues = {
uint: 42,
@ -23,15 +23,15 @@ describe('expectEvent', function () {
});
describe('inConstructor', function () {
context('long uint value', function () {
context('short uint value', function () {
it('accepts emitted events with correct number', async function () {
await expectEvent.inConstruction(this.emitter, 'LongUint',
await expectEvent.inConstruction(this.emitter, 'ShortUint',
{ value: this.constructionValues.uint }
);
});
it('throws if an incorrect value is passed', async function () {
return expectEvent.inConstruction(this.emitter, 'LongUint',
return expectEvent.inConstruction(this.emitter, 'ShortUint',
{ value: 23 }
).should.be.rejected;
});
@ -290,9 +290,26 @@ describe('expectEvent', function () {
});
describe('inTransaction', function () {
it('processes the logs inside a transaction', async function () {
const tx = await this.emitter.emitShortUint(5);
expectEvent.inTransaction(tx, 'ShortInt', { value: 5 });
describe('when emitting from called contract', function () {
context('short uint value', function () {
beforeEach(async function () {
this.value = 42;
const receipt = await this.emitter.emitShortUint(this.value);
this.txHash = receipt.tx;
});
it('accepts emitted events with correct number', async function () {
await expectEvent.inTransaction(this.txHash, EventEmitter, 'ShortUint', { value: this.value });
});
it('throws if an unemitted event is requested', function () {
return expectEvent.inTransaction(this.txHash, EventEmitter, 'UnemittedEvent', { value: this.value }).should.be.rejected;
});
it('throws if an incorrect value is passed', function () {
return expectEvent.inTransaction(this.txHash, EventEmitter, 'ShortUint', { value: 23 }).should.be.rejected;
});
});
});
});
});