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!!!