0

i have a function in my smart contract which needs a string to work, and for this i have two options :

OPTION 1 :

Store my string in a state variable :

contract A {
    string public immutable myString = "my string in storage"

    function myFunction() {
       // do something with myString
    }
}

OPTION 2 :

Pass as a parameter the string value every time i call the function :

contract A {
    
    function myFunction(string memory _myString) {
       // do something with _myString
    }
}

My question : What is the most gas efficient option knowing that :

  • the string doesn't fit in 32 bytes
  • the string value must be immutable
2
  • 1
    if you pass the string as tx.Data() parameter your users are going to pay 16 gas for every byte. If you store the string using SSTORE and then read it every time by SLOAD your users will spend 800 gas to read every 32 bytes of the string.
    – Nulik
    Feb 1 at 14:46
  • the question is quickly answered then, lol
    – noro meb
    Feb 1 at 15:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.