This is a contract I made when the latest version of solidity was 0.4.0, it was working then, but now it shows error in Remix, so I updated it to 0.5.0. Now it shows errors in the transfer function and keccak256 function.
The errors are:
Typeerror: member balance not found or not visible argument-dependent lookup in contractLottery. Use "address(this).balance" to access this address member.players[index].transfer(this.balance);
TypeError: Wrong argument count for function call: 3 arguments given but
expected 1. This function requires a single bytes argument. Use
abi.encodePacked(...) to obtain the pre-0.5.0 behaviour or abi.encode(...)
to use ABI encoding.
return uint(keccak256(block.difficulty,now,players));
CONTRACT
pragma solidity ^0.5.0;
contract Lottery {
address public manager;
address[] public players;
constructor() public {
manager = msg.sender;
}
function enter() public payable {
require(msg.value > .01 ether);
players.push(msg.sender);
}
function random() private view returns(uint) {
return uint(keccak256(block.difficulty, now, players));
}
function winner() public restricted {
uint index = random() % players.length;
players[index].transfer(this.balance);
players = new address[](0);
}
function allplayers() public {
return players;
}
modifier restricted() {
require(msg.sender == manager);
_;
}
}
random()
it's highly predictable, you could generate a function that calculatesrandom() % players.length
and callwinner()
only when he is the winner