1

I'm writing a smart contract and inevitably need to have arrays of bytes as input and output of functions. In remix arrays work just fine but with web3j and web3js there are several issues with arrays which make it impossible to have array as inputs and outputs.

So I think I need some sort of JSON.stringify() and JSON.parse() implementation in solidity to convert array of bytes to string and vice versa to get strings in input, work with arrays inside EVM and return strings in output.

Is there any clean and cost-effective implementation of such functions?
Thanks.

1 Answer 1

0

with web3js there are several issues with arrays which make it impossible to have array as inputs and outputs

No, this is definitely possible.

If you're on web3 v0.x, then:

  • Use web3.fromAscii before you send the string to the smart contract
  • Use web3.toAscii after you read the string from the smart contract

If you're on web3 v1.x, then:

7
  • Thank you for response. These methods convert string to hex and vice versa. The issue is with arrays not strings. Strings works just fine. Jan 13, 2019 at 8:02
  • @saman.shahmohamadi: You're gonna have to share you code, and explain what problems exactly you're having in sending byte arrays as input, or receiving them as output. Jan 13, 2019 at 8:13
  • The issue is about array of bytes not byte arrays. First of all, solidity itself does not support arrays in inputs and outputs and you have to use experimental ABIEncoderV2. By the way this is another issue, now I'm looking for a serializer method in solidity. Jan 13, 2019 at 8:19
  • @saman.shahmohamadi: English grammar: array of bytes = byte array. Jan 13, 2019 at 8:21
  • It's about solidity not English. If you know solidity, you should know that we have bytes which is an array of multiple byte elements (in Java it called byte array). And we can make an array which contains that bytes elements. So in solidity byte array is called bytes and array of bytes is whole different thing. If you know the case you can test it on remix and you will see that you have to import ABIEncoderV2. Jan 13, 2019 at 8:27

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.