2

I'm getting the warning

Gas requirement of function helloWorld.getGreeting() high: infinite. If the gas requirement of a function is higher than the block gas limit, it cannot be executed. Please avoid loops in your functions or actions that modify large areas of storage (this includes clearing or copying arrays in storage)

in the following contract

pragma solidity ^0.4.22;

contract helloWorld {

    string public greeting; 

    string[] public greetings;

    constructor() public {
        greeting = 'hello World';
        greetings.length= greetings.length + 1;
    }

    function setGreeting(string _newGreeting) public {
        greeting = _newGreeting;
        greetings.push(_newGreeting);
    }

    function getGreeting() public view returns (string) {
        return greeting;
    }
}

I would like to be able to have the contract with none of these warnings, any help would be great !

thanks in advance !

1

The issue is that you are using dynamic arrays (i.e, string). Remix cannot know "a priori" the length of these variables, therefore this function has the potential of requiring a lot of gas.

  • what would u recommend using a fixed array ? – conwise17 May 13 '18 at 12:30
  • do you know of any documentation that would help understand more of this warning in particular ? thanks in advance – conwise17 May 13 '18 at 12:48
  • Yes, I think using a fixed array will solve it. – Jaime May 13 '18 at 13:49
  • I just did it quicky in remix, changing all your arrays, for instance, to bytes32 silence all the warnings about gas. – Jaime May 13 '18 at 13:53
  • 1
    If this solved your question, please accept the answer – Jaime May 14 '18 at 9:33

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.