I'm trying to make a lottery.

pragma solidity ^0.4.23;

contract Lottery {

struct Ticket {
    uint id;
    address name;
    uint winCount;

uint public winFirstPercent = 60;
uint public prizeMoney = 0;
address public winner;
uint public priz;

uint public lastWinNumber = 0;

uint public ticketCount = 10;
uint public ticketPrice = 1 ether;
// Testnet
address public walletMoney = 0xca35b7d915458ef540ade6068dfe2f44e8fa733c; 

mapping (uint => Ticket) public tickets;

event buyTicket (uint ticketNum, address adrr);
event winAddress (uint ticketWin, address winAddress, uint priz, uint prizeMoney);

constructor () public {

function clearTickets() public {
    for (uint i = 0; i < ticketCount; i++){
        tickets[i] = Ticket(i,0,0);

function buyTickets (uint _ticketNum) public payable returns (bool success) {
    require ((_ticketNum > 0) && (_ticketNum < ticketCount));
    require (tickets[_ticketNum].name == 0);
    require (msg.value == ticketPrice);
    prizeMoney += msg.value;
    tickets[_ticketNum] = Ticket(_ticketNum, msg.sender, 0);
    emit buyTicket(_ticketNum, msg.sender);
    return true;

function playGame () public {
    //lastWinNumber = uint(block.blockhash(block.number-1))%ticketCount;
    lastWinNumber = 1;
    if (tickets[lastWinNumber].name != 0) {
        winner = tickets[lastWinNumber].name;
        priz = prizeMoney*winFirstPercent/100;
    emit winAddress(lastWinNumber, winner, priz, prizeMoney);

Buying tickets works correctly!

But when I start to run the function playGame (), I get an error

transact to Lottery.playGame errored: VM error: revert.
revert  The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value.  
Debug the transaction to get more information.

P.S. I specially for the tests of the application left the winning number a fixed ticket - 1

Tests do in remix


I believe the issue is that the contract doesn't have a sufficient balance to pay out the winner.

In buyTickets, all the ether that is sent in is transferred away but also added to prizeMoney:

prizeMoney += msg.value;

Then in playGame, an attempt is made to transfer out some ether to the winner:

priz = prizeMoney*winFirstPercent/100;

but this fails because the contract holds no ether.

  • Thank you! The thing really was that on the contract itself there was no money to pay in the ETH. – I. Simonov Aug 16 '18 at 13:54

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.