0

I would like to know what exactly this require condition do in this smart contract:

function placeBet(uint betMask, uint modulo, uint commitLastBlock, uint commit, uint8 v, bytes32 r, bytes32 s) external payable {
        // Check that the bet is in 'clean' state.
        Bet storage bet = bets[commit];
        require (bet.gambler == address(0), "Bet should be in a 'clean' state.");

        // Validate input data ranges.
        uint amount = msg.value;
        require (modulo > 1 && modulo <= MAX_MODULO, "Modulo should be within range.");
        require (amount >= MIN_BET && amount <= MAX_AMOUNT, "Amount should be within range.");
        require (betMask > 0 && betMask < MAX_BET_MASK, "Mask should be within range.");

        // Check that commit is valid - it has not expired and its signature is valid.
        require (block.number <= commitLastBlock, "Commit has expired.");
        require (verifySignature(commitLastBlock, commit, v, r, s), "ECDSA signature is not valid.");
        uint rollUnder;
        uint mask;

        if (modulo <= MAX_MASK_MODULO) {

            rollUnder = ((betMask * POPCNT_MULT) & POPCNT_MASK) % POPCNT_MODULO;
            mask = betMask;
        } else {
            // Larger modulos specify the right edge of half-open interval of
            // winning bet outcomes.
            require (betMask > 0 && betMask <= modulo, "High modulo range, betMask larger than modulo.");
            rollUnder = betMask;
        }

        // Winning amount and jackpot increase.
        uint possibleWinAmount;
        uint jackpotFee;

        (possibleWinAmount, jackpotFee) = getDiceWinAmount(amount, modulo, rollUnder);

        // Enforce max profit limit.
        require (possibleWinAmount <= amount + maxProfit, "maxProfit limit violation.");

        // Lock funds.
        lockedInBets += uint128(possibleWinAmount);
        jackpotSize += uint128(jackpotFee);

        // Check whether contract has enough funds to process this bet.
        require (jackpotSize + lockedInBets <= address(this).balance, "Cannot afford to lose this bet.");

        // Store bet parameters on blockchain.
        bet.amount = amount;
        bet.modulo = uint8(modulo);
        bet.rollUnder = uint8(rollUnder);
        bet.placeBlockNumber = uint40(block.number);
        bet.mask = uint40(mask);
        bet.gambler = msg.sender;

        //Record bet in logs.
       emit OnCommit(commit);
       emit BetPlaced(commit, bet.gambler, bet.amount, bet.mask, bet.modulo);
    }

I would like to know the meaning of the below require condition:

require (bet.gambler == address(0), "Bet should be in a 'clean' state.");

These are my doubts: How is it possible to obtain the bet.gambler from the blockchain, before the event emitted?

Kindly advice, Thanks

1 Answer 1

2

When you introduce a variable in a contract without setting a value to it, its value is set to a default value. In the case of an address type the default value is address(0). Therefore what that require checks is that there is no explicit address set for that gambler; probably it means that the gambler hasn't been set at all.

Setting a variable value has nothing to do with events - events never set any variables. The bet variable is retrieved from a (probably) storage variable called bets. That variable's state is preserved in the blockchain so it stays the same for each transaction (until some transaction modifies that state). Therefore the function can access that variable's preset value and check whether the gambler is set.

1
  • so Setting a variable value has nothing to do with events - events never set any variables. I understood it now, :-) Thanks..
    – Aldo
    Commented Apr 21, 2020 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.