1
  1. First, I want someone to send a bet (money) to the contract.
  2. Then I want someone to match the original bet.

(There are many more safety checks in the contract. I just removed them to keep this as short as possible).

Let's say I have this:

pragma solidity >=0.5.10 <0.7.0;

contract test {
    
    constructor() public payable {
        require(msg.value > 0, "Must bet a positive amount");
    }
    
    function viewBet() public view returns(uint) {
        return address(this).balance;
    }
    
    function matchBet() external payable {
        require(msg.value == address(this).balance, "Must match original bet");
    }
}

I can deploy the contract.

I can view the pot (just to ensure everything is fine).

But then I try to send an equal amount with matchBet and it always says "Must match original bet".

Maybe I'm going crazy, but I swear this used to work.

I remember I implemented it after I asked this (from another account of mine): How can a Smart Contract pull an amount of Ether equal to its own balance from an External Account?

But now it's not working.

Why can't matchbet match the bet sent with the constructor?

1
  • Note that, in general, address(this).payable doesn't always have to come from a bet.
    – hrkrshnn
    Feb 22 at 17:04
1

This is because the ether passed to matchBet() was added to the previous amount of ether. Write a non-payable function like matchBet(uint betAmount) to compare.

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.