0

I am pretty new at solidity and I am trying to create a roulette where you can place bets on a color or number and then resolve the bets and receive the amount of currency you won. Currently I am doing it on remix with test ether accounts and I am having problems transferring the winnings for a bet from the contract to the current users address. Any ideas? Thank you in advance.

Also I have noticed when trying to return winnings for just a single bet at a time it doesn't work only for the first win when I launch the app and after that the transfer works.

The error I get when I try:

transact to roulette.spinAll errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

pragma solidity ^0.5.0;

contract roulette {
    
    
    enum BetType { Color, Number }
    
    struct Bet {
        address user;
        uint amount;
        uint block;
        int choice;
        BetType betType;
    }
    
    uint public counter = 0;
    int public result;
    
    mapping(uint => Bet) public bets;
    mapping(int => int) public colors;
    
    uint public constant NUM_POCKETS = 38;
    
    uint8[18] public RED_NUMBERS = [
        1, 3, 5, 7, 9, 12,
        14, 16, 18, 19, 21, 23,
        25, 27, 30, 32, 34, 36
        ];
        
    uint8[18] public BLACK_NUMBERS = [
        2, 4, 6, 8, 10, 11,
        13, 15, 17, 20, 22, 24,
        26, 28, 29, 31, 33, 35
        ];
        
    function Start() public {
        for(uint i = 0; i < 18; i++) {
            colors[RED_NUMBERS[i]] = 1;
        }

    }
        
    function makeBet(BetType _betType, int _choice) payable public {
        if(_betType == BetType.Color)
            require(_choice == 0 || _choice == 1);
        else require(_choice >= -1 && _choice <=36);
        counter++;
        bets[counter] = Bet(msg.sender, msg.value, block.number, _choice, _betType);
    }
    
    function spinAll() public {
        bytes32 random = keccak256(abi.encodePacked(now, msg.sender));
        result = int(uint(random) % NUM_POCKETS) - 1;
        for(uint i = 0; i < counter; i++) {
            resolveBets(i);
        }
        counter = 0;
    }
    
    function resolveBets(uint id) payable public {
        Bet storage bet = bets[id];
        require(msg.sender == bet.user);
        if(bet.betType == BetType.Color) {
            if(bet.choice == colors[result] && result > 0)
                msg.sender.transfer(bet.amount * 2);       //    < --- This does not work!!!
                
        }
        else if(bet.betType == BetType.Number)
        {
            if(result == bet.choice)
                msg.sender.transfer(bet.amount * 35);
        }
        
        delete bets[id];
    }
}
1
  • My guess is that the contract doesn't have enough Ethers to pay the double amount. So you test it with making a bet with X Ethers, then the contract has X Ethers in it, you resolve the bet and try to pay out 2X Ethers and it fails. – Lauri Peltonen May 16 at 18:27
0

I think you should try payable(msg.sender).transfer(bet.amount * 35); payable keyword should be mentioned before msg.sender

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.