-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathEvents.ts
More file actions
57 lines (51 loc) · 1.6 KB
/
Events.ts
File metadata and controls
57 lines (51 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export async function getEventsFromContract(contract: any, blockIdx: number) {
// console.log(`blockIdx: ${blockIdx}`);
return contract.getPastEvents("allEvents", {
fromBlock: blockIdx,
toBlock: blockIdx
});
}
// This works differently from truffleAssert.eventEmitted in that it also is able to
// get events emitted in `deep contracts` (i.e. events not emitted in the contract
// the function got called in).
export async function assertEventsEmitted(
contract: any,
eventName: string,
numExpected: number,
filter?: any
) {
const blockNumber = await web3.eth.getBlockNumber();
const allEvents: any = await getEventsFromContract(contract, blockNumber);
const events = allEvents.filter((event: any) => event.event === eventName);
// const util = require("util");
// console.log(`events: ${util.inspect(events)}`);
const items = events.map((eventObj: any) => {
const args = eventObj.args ? eventObj.args : eventObj.returnValues;
if (filter !== undefined) {
assert(
filter(args),
"Event " +
eventName +
" values unexpected: \n" +
JSON.stringify(args, null, 4)
);
}
return args;
});
assert.equal(
items.length,
numExpected,
"Unexpected number of " + eventName + " events: "
);
return items;
}
export async function assertEventEmitted(
contract: any,
event: string,
filter?: any
) {
return (await this.assertEventsEmitted(contract, event, 1, filter))[0];
}
export async function assertNoEventEmitted(contract: any, event: string) {
await this.assertEventsEmitted(contract, event, 0, undefined);
}