0

I have one issue while deploying the below contract.could anyone help me to find the issue as unable to find issue.

pragma solidity >=0.4.22 <0.6.0;

contract Lottery {
    address payable[] public  players; // dynamic array with players addresses
    address payable public manager; // contract manager

    // contract constructor, runs once at contract deployment
    constructor() public {
        // the manager is account address that deploys the contract
        manager = msg.sender; 
    }

    // this fallback payable function will be automatically called when somebody
    //sends ether to our contract address
    function () payable external {
        require(msg.value >= 0.01 ether);
        players.push(msg.sender); // add the address of the account that                                     
                                  // sends ether to players array
    }

    function get_balance() public view returns(uint) {
        require(msg.sender == manager);
        return address(this).balance; // return contract balance
    }

    // returns a very big pseodo-random integer no.
    function random() public view returns(uint256) {
        return keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length)); <--error is this line
    }

    function selectWinner() public view returns(address) {
        require(msg.sender == manager);
        uint r = random();
        address payable winner;

        // a random index
        uint index = r % players.length;
        winner = players[index];

        // transfer contract balance to the winner address
        winner.transfer(address(this).balance);

        players = new address payable[](0); // resetting the players dynamic array
    }
 }
3

Just type cast your randomized result to uint256 uint256(keccak256(abi.encodePacked(block.difficulty,block.timestamp, players.length))) as keccack returns bytes32 output and you are returning uint256 from your function, also you are collecting output in r which you've declared uint maintain some datatype consistency in your code. Also you've declared selectWinner() as view, which it is not, as you are changing state and making a transfer.

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.