Home:ALL Converter>Lottery contract has infinite gas issue

Lottery contract has infinite gas issue

Ask Time:2021-09-22T05:29:33         Author:Stephan Sandtner

Json Formatter

I'm getting this error "Gas costs: Gas requirement of function Lottery.getPlayers is 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)" Also I want to add a way to deposit extra tokens from the same wallet as the contract is deployed. ``` pragma solidity ^0.6.0;

contract Lottery {
    address public manager;
    address[] public  players;
    constructor() public {
        manager = msg.sender;
    }
    modifier restricted() {
        require(msg.sender == manager,"only manager has access");
        _;
    }
    function balanceInPool()public view returns(uint){
        return address(this).balance;
    }
    function enter() public payable {
        require(msg.value > .01 ether,"the amount should be more than 0.1 ether");
        players.push(msg.sender);
   }
    function random() private view returns (uint) {
        return uint(keccak256(abi.encodePacked(block.difficulty,now,players)));
    }
    function pickWinner() public restricted {
        uint index = random() % players.length;
        address payable winner = payable(players[index]);
        players = new address[](0);
        winner.transfer(address(this).balance);
     }

    function getPlayers()public view returns(address[] memory){
    return players;
    }

        
}
```

Author:Stephan Sandtner,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275760/lottery-contract-has-infinite-gas-issue
yy