0

I am writing a simple smart contract for storing an array of bytes32, pushing to it, and retrieving it:

contract SimpleStorage {
    bytes32[] public items;

    event ItemAdded(bytes32 contents);

    function addItem(bytes32 contents) public returns(bool) {
        items.push(contents);
        emit ItemAdded(contents);
        return true;
    }

    function getAllItems() public constant returns(bytes32[]) {
        return items;
    }
}

This compiles fine, but when I try to write a Solidity test for this contract I am unable to store the result of getAllItems.

Line in question:

bytes32[] result = simpleStorage.getAllItems();

The error I get is: Type inaccessible dynamic type is not implicitly convertible to expected type bytes32[] storage pointer.

From this Github issue I see that it is known (and planned to be fixed in Ethereum 2.0) that we simply cannot at this time call functions that return dynamic arrays from another contract. We can, however, use these functions from the ABI.

Does this mean that it is essentially impossible to write Solidity tests for these types of functions at this time (since the unit tests are themselves contracts)? Has anyone else run into this issue and found a way around it?

I know I can just stick to writing JS tests but I am curious if there is a way to test these types of contracts using Solidity.

2

Which version of solidity compiler are you using ? I can't reproduce your error.

However with last version of solc (0.4.24) you have to add the memory keyword as by default a bytes32[] is stored in storage and you cannot assign a dynamic size type to storage.

bytes32[] memory result = simpleStorage.getAllItems();
  • Thanks! Turns out I hadn't upgraded the solc version that Truffle was using. – Chris Staikos May 30 '18 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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