I've a smartcontract compiled with pragma solidity ^0.4.0; version and one function receives a bytes parameters which contain a long string. In these version it doesnt need to set the parameters as "memory".

I've create a new contract with version pragma solidity >=0.4.25 <0.6.0; and i'm not able to create unless I mark the byte parameters as memory.

bytes memory dBlock

These have increase the gas cost for the transaction more them 3x. I've test with a bytes32 variable the gas cost is almost nothing.

However in these particular case I often receive a long string much longer than 32 chars. How I can pass a long string to a smartcontract and on same time save some gas in the transaction?

1 Answer 1


Setting your function to external (in case it being called only through txs or other external smart contracts) can save some gas in certain scenarios.

function myExternalFunction(bytes calldata mydata) external {
    // do some stuff

bytes will cost more than bytes32, because the length information itself is stored in a memory slot of 32 bytes. So in any case it is better to use bytes32, if you can ensure that you will never have to transfer more than 32 bytes. One more note, in previous solidity versions the "memory" keyword was implicitly contained (when used as a function parameter). So basically when you wrote function myfunc(bytes xyz) it was equal to function myfunc(bytes memory xyz)

  • Many thanks for your comment, I'll change visibility to external it will never be called internally so it save a lot of gas.
    – mastervv
    Commented Nov 25, 2019 at 14:46
  • 1
    You're welcome. One more tip, you can use optimizers to further reduce the cast cost. In this specific scenario there is not much space for optimizations anymore though. The solc compiler has an integrated optimizer and you can further use external optimizers which can detect and optimally optimize the code. Why don't you try it out yourself and check how much gas could be at least safed through additional optimizations (sadly it does only tell how much, not where and how): EVMuncher
    – sea212
    Commented Nov 25, 2019 at 15:48
  • 1
    Thanks again, I'm also remember to compress text data before sent to smartcontract. Also reduce a little bit of gas
    – mastervv
    Commented Nov 27, 2019 at 12:32

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.