Skip to content

Commit

Permalink
#199 Improves ethers.js test with more logging and better assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
EnchanterIO committed Sep 3, 2019
1 parent e795b6b commit 6175f3e
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions truffle/test/07_ethers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ const HelloBlockchainWorld = artifacts.require("HelloBlockchainWorld");
describe('Ethers', () => {
let ROOT_ACCOUNT = extractEnvAccountAndPwd(process.env.NETWORK).from;
let helloWorldInstance;
let incrementHelloCountTx;

it("should emit an event in a SC", async () => {
helloWorldInstance = await HelloBlockchainWorld.deployed();

const estimatedGas = await helloWorldInstance.incrementHelloCount.estimateGas();
const tx = await helloWorldInstance.incrementHelloCount({
incrementHelloCountTx = await helloWorldInstance.incrementHelloCount({
from: ROOT_ACCOUNT,
gasLimit: estimatedGas,
});

const emitedEvent = tx.receipt.logs[0];
const emitedEvent = incrementHelloCountTx.receipt.logs[0];

assert.equal(tx.receipt.status, "0x1", "successful TX status expected");
assert.equal(incrementHelloCountTx.receipt.status, "0x1", "successful TX status expected");
assert.equal(emitedEvent.event, "HelloCountIncremented");
assert.equal(emitedEvent.blockHash, tx.receipt.blockHash);
assert.equal(emitedEvent.blockHash, incrementHelloCountTx.receipt.blockHash);
});

it("should be able to query emitted event logs", async () => {
Expand All @@ -29,12 +30,19 @@ describe('Ethers', () => {

const jsonRpcProvider = new providers.JsonRpcProvider(host);

const latestBlock = await jsonRpcProvider.getBlockNumber();

const events = await jsonRpcProvider.getLogs({
fromBlock: 0,
address: helloWorldInstance.address
fromBlock: latestBlock - 100,
address: helloWorldInstance.address,
topics: [incrementHelloCountTx.receipt.rawLogs[0].topics[0]],
});

console.log(events);

assert.equal(events[0].blockNumber, incrementHelloCountTx.receipt.blockNumber);
assert.equal(events[0].address, helloWorldInstance.address);
assert.equal(events[0].topics.length, 1);
assert.equal(events[0].topics[0], incrementHelloCountTx.receipt.rawLogs[0].topics[0]);
});
});

0 comments on commit 6175f3e

Please sign in to comment.