solidity – How to Get Rid of Infinite/HIGH Gas Limit Warning of a Function – Expert Tips

gassolidity

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 !

Best Answer

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.

Related Topic