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

feat(event): add assertion for indexed dynamic size parameters #413

Merged
merged 5 commits into from
Dec 21, 2020
Merged
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
10 changes: 9 additions & 1 deletion waffle-chai/src/matchers/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ export function supportEmit(Assertion: Chai.AssertionStatic) {
new Assertion(actualArgs[index][j]).equal(expectedArgs[index][j]);
}
} else {
new Assertion((actualArgs[index])).equal((expectedArgs[index]));
if (actualArgs[index].hash !== undefined && actualArgs[index]._isIndexed === true) {
const expectedArgBytes = utils.isHexString(expectedArgs[index])
? utils.arrayify(expectedArgs[index]) : utils.toUtf8Bytes(expectedArgs[index]);
new Assertion(actualArgs[index].hash).to.be.oneOf(
[expectedArgs[index], utils.keccak256(expectedArgBytes)]
);
} else {
new Assertion(actualArgs[index]).equal(expectedArgs[index]);
}
}
}
};
Expand Down
17 changes: 14 additions & 3 deletions waffle-chai/test/contracts/Events.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export const EVENTS_SOURCE = `
pragma solidity ^0.6.0;
pragma solidity ^0.6.0;

contract Events {
event One(uint value, string msg, bytes32 encoded);
event Two(uint indexed value, string msg);
event Index(string indexed msgHashed, string msg, bytes bmsg, bytes indexed bmsgHash, bytes32 indexed encoded);
event Arrays(uint256[3] value, bytes32[2] encoded);

function emitOne() public {
Expand All @@ -16,8 +17,16 @@ export const EVENTS_SOURCE = `
emit One(1, "DifferentKindOfOne", 0x0000000000000000000000000000000000000000000000000000000000000001);
}

function emitIndex() public {
emit Index("Three",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a newline after (

"Three",
bytes("Three"),
bytes("Three"),
0x00cFBbaF7DDB3a1476767101c12a0162e241fbAD2a0162e2410cFBbaF7162123);
}

function emitTwo() public {
emit Two(2, "Two");
emit Two(2, "Two");
}

function emitBoth() public {
Expand Down Expand Up @@ -48,14 +57,16 @@ export const EVENTS_SOURCE = `
export const EVENTS_ABI = [
'event One(uint value, string msg, bytes32 encoded)',
'event Two(uint indexed value, string msg)',
'event Index(string indexed msgHashed, string msg, bytes bmsg, bytes indexed bmsgHash, bytes32 indexed encoded)',
'event Arrays(uint256[3] value, bytes32[2] encoded)',
'function emitOne() public',
'function emitOneMultipleTimes() public',
'function emitTwo() public',
'function emitIndex() public',
'function emitBoth() public',
'function emitArrays() public',
'function doNotEmit() pure public'
];

// eslint-disable-next-line max-len
export const EVENTS_BYTECODE = '608060405234801561001057600080fd5b50610586806100206000396000f3fe608060405234801561001057600080fd5b50600436106100625760003560e01c806334c10115146100675780633f0e64ba14610071578063a35a3a0d1461007b578063b2f9983814610085578063d5eacee01461008f578063db6cdf6814610099575b600080fd5b61006f6100a3565b005b61007961010f565b005b6100836101ac565b005b61008d6102b2565b005b610097610466565b005b6100a161054e565b005b60027f726d8d77432fef0b8999b8e1f5ed6d11c42c0a861c61228b03e767ad3c43d0df6040518080602001828103825260038152602001807f54776f000000000000000000000000000000000000000000000000000000000081525060200191505060405180910390a2565b7f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60017ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360405180838152602001806020018360001b8152602001828103825260038152602001807f4f6e650000000000000000000000000000000000000000000000000000000000815250602001935050505060405180910390a1565b7f35cf379c46b4950eedc35bc96d30e9fe7480e2422431c50ea5c4b211ee6b1b8d60405180606001604052806001815260200160028152602001600381525060405180604001604052807ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360001b81526020017ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212460001b8152506040518083600360200280838360005b8381101561026f578082015181840152602081019050610254565b5050505090500182600260200280838360005b8381101561029d578082015181840152602081019050610282565b505050509050019250505060405180910390a1565b7f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60017ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360405180838152602001806020018360001b8152602001828103825260038152602001807f4f6e650000000000000000000000000000000000000000000000000000000000815250602001935050505060405180910390a17f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60017ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360405180838152602001806020018360001b8152602001828103825260038152602001807f4f6e650000000000000000000000000000000000000000000000000000000000815250602001935050505060405180910390a17f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60018060405180838152602001806020018360001b8152602001828103825260128152602001807f446966666572656e744b696e644f664f6e650000000000000000000000000000815250602001935050505060405180910390a1565b7f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60018060405180838152602001806020018360001b8152602001828103825260038152602001807f4f6e650000000000000000000000000000000000000000000000000000000000815250602001935050505060405180910390a160027f726d8d77432fef0b8999b8e1f5ed6d11c42c0a861c61228b03e767ad3c43d0df6040518080602001828103825260038152602001807f54776f000000000000000000000000000000000000000000000000000000000081525060200191505060405180910390a2565b56fea2646970667358221220e505630e9a894ec6127d6e69224f74fa696a208c9a5fa71ef29f18bd2303fb9e64736f6c63430006000033';
export const EVENTS_BYTECODE = '608060405234801561001057600080fd5b506107a6806100206000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c8063a35a3a0d1161005b578063a35a3a0d146100a0578063b2f99838146100aa578063d5eacee0146100b4578063db6cdf68146100be5761007d565b806334c10115146100825780633f0e64ba1461008c57806377f3094b14610096575b600080fd5b61008a6100c8565b005b610094610134565b005b61009e6101d1565b005b6100a86103cc565b005b6100b26104d2565b005b6100bc610686565b005b6100c661076e565b005b60027f726d8d77432fef0b8999b8e1f5ed6d11c42c0a861c61228b03e767ad3c43d0df6040518080602001828103825260038152602001807f54776f000000000000000000000000000000000000000000000000000000000081525060200191505060405180910390a2565b7f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60017ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360405180838152602001806020018360001b8152602001828103825260038152602001807f4f6e650000000000000000000000000000000000000000000000000000000000815250602001935050505060405180910390a1565b7ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360001b6040518060400160405280600581526020017f54687265650000000000000000000000000000000000000000000000000000008152506040518082805190602001908083835b6020831061025d578051825260208201915060208101905060208303925061023a565b6001836020036101000a038019825116818451168082178552505050505050905001915050604051809103902060405180807f5468726565000000000000000000000000000000000000000000000000000000815250600501905060405180910390207f2c0160d31a12563decf7b38f40fd0fab153ae9c20f87643b7fb747e3f0e4c93f6040518060400160405280600581526020017f5468726565000000000000000000000000000000000000000000000000000000815250604051808060200180602001838103835260058152602001807f5468726565000000000000000000000000000000000000000000000000000000815250602001838103825284818151815260200191508051906020019080838360005b8381101561038f578082015181840152602081019050610374565b50505050905090810190601f1680156103bc5780820380516001836020036101000a031916815260200191505b50935050505060405180910390a4565b7f35cf379c46b4950eedc35bc96d30e9fe7480e2422431c50ea5c4b211ee6b1b8d60405180606001604052806001815260200160028152602001600381525060405180604001604052807ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360001b81526020017ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212460001b8152506040518083600360200280838360005b8381101561048f578082015181840152602081019050610474565b5050505090500182600260200280838360005b838110156104bd5780820151818401526020810190506104a2565b505050509050019250505060405180910390a1565b7f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60017ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360405180838152602001806020018360001b8152602001828103825260038152602001807f4f6e650000000000000000000000000000000000000000000000000000000000815250602001935050505060405180910390a17f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60017ecfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf716212360405180838152602001806020018360001b8152602001828103825260038152602001807f4f6e650000000000000000000000000000000000000000000000000000000000815250602001935050505060405180910390a17f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60018060405180838152602001806020018360001b8152602001828103825260128152602001807f446966666572656e744b696e644f664f6e650000000000000000000000000000815250602001935050505060405180910390a1565b7f824e7918d5bcff68837d677d05258e17ec7b1bd7b488aa5e3bd2d5cbefa9e04c60018060405180838152602001806020018360001b8152602001828103825260038152602001807f4f6e650000000000000000000000000000000000000000000000000000000000815250602001935050505060405180910390a160027f726d8d77432fef0b8999b8e1f5ed6d11c42c0a861c61228b03e767ad3c43d0df6040518080602001828103825260038152602001807f54776f000000000000000000000000000000000000000000000000000000000081525060200191505060405180910390a2565b56fea26469706673582212207600ca79558af769230836d4dfab9c3d063f7ba532de1571d0c454b22127de5864736f6c634300060c0033';
29 changes: 27 additions & 2 deletions waffle-chai/test/matchers/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect, AssertionError} from 'chai';
import {BigNumber, Contract, ContractFactory} from 'ethers';
import {BigNumber, Contract, ContractFactory, ethers} from 'ethers';
import {MockProvider} from '@ethereum-waffle/provider';
import {EVENTS_ABI, EVENTS_BYTECODE} from '../contracts/Events';

Expand Down Expand Up @@ -27,7 +27,9 @@ describe('INTEGRATION: Events', () => {
});

it('Emit two: success', async () => {
await expect(events.emitTwo()).to.emit(events, 'Two');
await expect(events.emitTwo())
.to.emit(events, 'Two')
.withArgs(2, 'Two');
});

it('Emit two: fail', async () => {
Expand All @@ -39,6 +41,29 @@ describe('INTEGRATION: Events', () => {
);
});

it('Emit index: success', async () => {
const bytes = ethers.utils.hexlify(ethers.utils.toUtf8Bytes('Three'));
const hash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes('Three'));
await expect(events.emitIndex())
.to.emit(events, 'Index')
.withArgs(
hash,
'Three',
bytes,
hash,
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
);
await expect(events.emitIndex())
.to.emit(events, 'Index')
.withArgs(
'Three',
'Three',
bytes,
bytes,
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
);
});

it('Do not emit one: fail', async () => {
await expect(
expect(events.emitOne()).to.not.emit(events, 'One')
Expand Down