[Ethereum] How to get event logs in transaction receipt, when I trigger event defined in other contract

eventsreceiptssolidity

I'm trying to trigger event defined in another contract and get the event logs in transaction receipt but it's not working this way –

Base Contract —

pragma solidity ^0.4.0;

contract DataStore  {

    event Borrow(uint indexed bookId, address indexed borrower, uint timestamp);

    function borrowEvent(uint id, address borrower) {
        Borrow(id, msg.sender, now);
    }
 }

My Contract —

pragma solidity ^0.4.0;
import "./DataStore.sol";

contract Organisation {

    function borrowBook(address bookStore, uint id) payable {
        DataStore(bookStore).borrowEvent(id, msg.sender);
    }
}

Test Case –

it.only("should generate Borrow event log", async function() {
    let res = await org.borrowBook(orgAdr, 1, {from: accounts[1], value: web3.toWei(0.1)});
    let log = res.logs[0].args;
    assert.equal(log.bookId.valueOf(), 1);
    assert.equal(log.borrower, accounts[1]);
    assert.isAtMost(log.timestamp.valueOf(), Math.floor(Date.now() / 1000));
    assert.isAbove(log.timestamp.valueOf(), Math.floor(Date.now() / 1000) - 300);
});

In test case res.logs[0] is coming as undefined, which seems correct to me since the event is triggered via parent contract. How can I get that event logs in borrowBook function transaction receipt?

Best Answer

You're asking for logs generated by a smart contract called DataStore. Make sure, in your test case that you're asking for logs of that address. I can't read Javascript, but it looks to me like you're looking for logs from the Organization contract not the DataStore contract. Even though Organization initiated the log, DataStore wrote it. Try listening for logs from DataStore as well as Organization.

Bump

Related Topic