0

Edit

The original question below seems to work as expected. The issue happens with the call return data is empty. When the response is nothing, the code below fails.

The following works, but is there a better way to do it?

bytes[] memory val;
for(uint i = 0; i < item.length; i++) {
    (, bytes memory response) = addr.call.value(_value[i])(_data);
    if (response.length != 0) {
        val[i] = response;
    }
}

I am unable to figure out how to store an array of bytes. I understand that bytes is an array of byte1s, but I want to be able to store multiple bytes in an array? So ideally, I would be able to define bytes[], where each member of that array is a new bytes.

Some example code (which is not working) is:

bytes[] memory val;
for(uint i = 0; i < _value.length; i++) {
    (, bytes memory response) = addr.call.value(_value[i])(_data);
    val[i] = response;
}

This is reverting on val[i] = response;. Does anyone know why?

  • Don't you feel that there's something a bit odd (to say the least) with val[i] = response? The LHS and RHS are not even of the same type (let alone the fact that you're trying to set each entry in the output array to be equal to the input array). – goodvibration Oct 24 '19 at 4:09
  • I am adding a quick update and would love your opinion. – python_crypto_questions Oct 24 '19 at 4:14
  • Also, it looks like you should be using _value.length, not item.length (what are these two variables anyway?). – goodvibration Oct 24 '19 at 4:14
  • "reverting" or "not compiling"? – goodvibration Oct 24 '19 at 4:15
  • If "reverting", then please share your execution (or testing) code. What transaction were you trying to execute? Also, there are several undefined symbols in your code, please provide a minimal working example. – goodvibration Oct 24 '19 at 4:17
1

You should by the least add another internal for loop in your code.

Something like this:

bytes[] memory val = new bytes[](_value.length);
for (uint i = 0; i < _value.length; i++) {
    (bool success, bytes memory response) = addr.call.value(_value[i])(_data);
    require(success, "call failed");
    val[i] = new bytes(response.length);
    for (uint j = 0; j < response.length; j++) {
        val[i][j] = response[j];
    }
}
| improve this answer | |

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.