0

I have a non-constant function inside a contract:

contract InnerContract {
    event Event(uint256);

    function func(uint256 val) external returns (uint256) {
        Event(val);
        return val;
    }
}

It is invoked from a function inside another contract:

import './InnerContract.sol';

contract OuterContract {
    InnerContract pInnerContract;

    function func(uint256 val) external returns (uint256) {
        return pInnerContract.func(val) + 1;
    }
}

I am trying to test it in truffle via the following script:

contract('Test', () => {
    let hInnerContract;
    let hOuterContract;

    before(async () => {
        hInnerContract = await artifacts.require("InnerContract.sol").new();
        hOuterContract = await artifacts.require("OuterContract.sol").new();
    });

    describe('test:', async () => {
        let input = 1;
        it(`expected output = ${input + 1}`, async () => {
            try {
                let hash = await hOuterContract.func(input);
                let output = Number(hash.receipt.logs[0].data);
                assert(output == input + 1, `actual output = ${output}`);
            }
            catch (error) {
                assert(false, error.message);
            }
        });
    });
});

However, the problem is with hash.receipt.logs[0].data.

It contains the value inside the event generated in the InnerContract function.

I've figured that I should generate an event also in the OuterContract function:

contract OuterContract {
    event Event(uint256);
    InnerContract pInnerContract;

    function func(uint256 val) external returns (uint256) {
        uint256 val2 = pInnerContract.func(val) + 1;
        Event(val2);
        return val2;
    }
}

But when I do this, I get the following exception in truffle:

Uncaught TypeError: Cannot read property 'constructor' of undefined
at C:\Users\...\cli.bundled.js:316118:19
at Array.forEach (<anonymous>)
at C:\Users\...\cli.bundled.js:316114:32
at Array.map (<anonymous>)
at Object.decodeLogs (C:\Users\...\cli.bundled.js:316067:19)
at Object.callback (C:\Users\...\cli.bundled.js:316195:35)
at C:\Users\...\cli.bundled.js:37985:25
at C:\Users\...\cli.bundled.js:328869:9
at C:\Users\...\cli.bundled.js:324536:9
at XMLHttpRequest.request.onreadystatechange (C:\Users\...\cli.bundled.js:327565:7)

BTW, before this exception occurs, truffle indicates that both events have indeed been emitted:

Events emitted during test:
---------------------------

Event(: 1)
Event(: 2)

---------------------------

Can you please explain what I am doing wrong here?

Thank you!!!

2 Answers 2

1

It seems that the Uncaught TypeError: Cannot read property 'constructor' of undefined exception can be resolved by "naming" the anonymous variable inside each one of the events.

In other words, changing every occurrence of this:

event Event(uint256);

To something like this:

event Event(uint256 x);

For some reason, the "anonymous event" works only as long as a single event is emitted.

0

If I understood you correctly, you have an Internal Contract with a function name func simply returns a value and an External contract that overrides the same function. The func of outer contract calls the func of Inner contract and adds 1 to the returned value.

Deploy the Inner Contract

contract InnerContract {
    function func(uint256 val) external returns (uint256) {
        return val;
    }
}

Let the address of Inner contract be: 0xdc04977a2078c8ffdf086d618d1f961b6c546222.

Deploy the Outer Contract with following code:

pragma solidity ^0.4.18;
contract InnerContract {
    function func(uint256 val) external returns (uint256) {
    }
}

contract OuterContract is InnerContract{
    InnerContract pInnerContract = InnerContract(0xdc04977a2078c8ffdf086d618d1f961b6c546222);
   function func(uint256 val) external returns (uint256) {
        return  pInnerContract.func(val) + 1;
    }
}

Now when you cal func_outer with value 1. The return value is 2.

3
  • "overrides the same function" - this is wrong, I am not overriding anything here. It's simply a (non-constant) function which calls a non-constant function. And I am unable to test the outer function with truffle test, due to some weird exception in truffle. Commented Jan 29, 2018 at 13:46
  • Yeah, that was my bad. Please check updates. Commented Jan 29, 2018 at 13:48
  • I don't have a problem calling the outer function. I have a problem invoking it from the JS script (a part of my question which you seem to have completely ignored), and retrieving its returned value - something which can be done only via events (another part of my question which you have not referred to). Commented Jan 29, 2018 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.