Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Fix for Issue #3: Pass full ABI to formTxObject method #4

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ The magic:

```
// all 3 arguments are required
truffleEvent.formTxObject('ContractName', eventIndex, txScope);
truffleEvent.formTxObject(contractABI, eventIndex, txScope);

// example based on 2 contracts above
var barScope = truffleEvent.formTxObject('Bar', 1, fooScope);
var barScope = truffleEvent.formTxObject(barInstance.abi, 1, fooScope);
```

Test example:
Expand All @@ -90,11 +90,16 @@ Test example:
var truffleAssert = require('truffle-assertions');
var truffleEvent = require('truffle-events');

const Foo = artifacts.require("Foo");
const Bar = artifacts.require("Bar");

// code omitted for brevity

it("Foo#doSomethingExtra", async function(){
it("Foo#doSomethingExtra", async function() {
f = await Foo.deployed();
b = await Bar.deployed();
var fooScope = await f.doSomethingExtra(b.address);
var barScope = truffleEvent.formTxObject('Bar', 1, fooScope);
var barScope = truffleEvent.formTxObject(Bar.abi, 1, fooScope);

truffleAssert.eventEmitted(fooScope, 'LogNumber', (ev) => {
return ev.number == 100;
Expand Down
43 changes: 21 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@ const fs = require('fs');
const abiDecoder = require('abi-decoder');

module.exports = {
getAbi: function(contractName) {
let c = JSON.parse(fs.readFileSync(`./build/contracts/${contractName}.json`, `utf8`));
return c.abi;
},
// eventIndex is zero-based
formTxObject: function(eventContract, eventIndex, transaction) {
let abi = this.getAbi(eventContract);
// wrap in an array because 'decodeLogs' expects an array
let log = [transaction.receipt.logs[eventIndex]];
abiDecoder.addABI(abi);

let _decoded = abiDecoder.decodeLogs(log);
let decodedLog = _decoded[0];

formTxObject: function(eventContractAbi, eventIndex, transaction) {
let txObject = {};
let tempArgs = {};
txObject.tx = transaction.tx;
txObject.logs = [{"event":decodedLog.name}];

for (var i = 0; i < decodedLog.events.length; i++) {
let key = decodedLog.events[i].name;
let val = decodedLog.events[i].value;
tempArgs[`${key}`] = val;
}
abiDecoder.addABI(eventContractAbi);

txObject.logs[0].args = tempArgs;
let receipt = transaction.receipt;
let log = receipt.logs[eventIndex];
let rawLog = receipt.rawLogs[eventIndex];

if (rawLog != undefined) {
let _decoded = abiDecoder.decodeLogs([rawLog]);
let decodedLog = _decoded[0];
for (var i = 0; i < decodedLog.events.length; i++) {
let key = decodedLog.events[i].name;
let val = decodedLog.events[i].value;
tempArgs[`${key}`] = val;
}
txObject.logs = [{"event":decodedLog.name}];
txObject.logs[0].args = tempArgs;
} else if (log != undefined) {
txObject.logs = [{"event":log.event}];
};

txObject.tx = transaction.tx;
return txObject;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "truffle-events",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"main": "index.js",
"author": "Zulhilmi Zainudin <[email protected]>",
Expand Down