1

With the price of ethereum going up my contract is getting too expensive for what i am using it for. What i need is a contract where a string is stored and retrieved whenever needed. My current code is

contract demo {
   string public name = "some32bitString";
   function returnValue() constant returns (string){
      return name; 
   } 
}

Is there a workaround? or some other way to make this less expensive? Currently on etherscan the tx fee is 0.00693.

3

Deployment cost for that contract is ~148K GAS.

The following is a cheaper version that costs ~42K GAS:

pragma solidity ^0.4.16;

contract Contract {

    function returnValue() constant returns (bytes4) {
        return "abcd";
    }
}

It can become cheaper again if you let to the ÐApp the decoding phase of the characters. This is a version that costs ~30K GAS:

pragma solidity ^0.4.16;

contract Contract {

    function returnValue() constant returns (uint32) {
        return 0xFFFFFFFF;
    }
}
0

What is expensive? To deploy the contract (put it on the blockchain) or to call the function?

You can access the function result without any transaction, as the string is stored publicly in the blockchain. However, if you call it from another contract, there may be a cost.

If you put a lot of such contracts on the blockchain, it will probably be less expensive to have only once contract with several strings, because there must be some flat cost for deploying a contract.

  • Hello, Thanks for the edits. Its expensive to deploy it. can i have a contract and then just add strings to it? like have an array of strings? and if so i guess i can add as many as i like? – PetrosM Sep 6 '17 at 9:35
  • Yes but you will have to pay fees each time you update it. I guess that (n * deployment fees) > (deployment fees + n*(update fees)) when n is big enough, but I am not so sure about it. Anyway memory is not cheap in Ethereum. – Distic Sep 6 '17 at 9:43
  • Maybe it's me but, at current rate, your deployment costs just a bit more then 2 $ (1 ETH = 337$ * 0.00693 = 2.33 $). Is it expensive? It's a smart contract that will last forever in the blockchain, it seems to me pretty fair... – Giuseppe Bertone Sep 6 '17 at 9:44
  • @GiuseppeBertone The question is not if it is expensive or not, but how to make it cheaper. If you deploy one million of such contracts, then it becomes important to decrease the price as much as possible. And I'd say it is a good practice, as it decreases also the difficulty to check the blockchain. – Distic Sep 6 '17 at 9:56
  • Understood, I'll make it cheaper :) Still, I really don't understand why a contract like that should be useful, so it seems to me there's something wrong in the application design more then a problem with the optimization of GAS spent. – Giuseppe Bertone Sep 6 '17 at 11:26

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.