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. Commented Feb 1, 2023 at 14:46
  • the question is quickly answered then, lol Commented Feb 1, 2023 at 15:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.