Ethereum coinflip dapp discussion


#1

Welcome to the thread about the Coinflip Dapp. Here you can discuss everything about the code and potential problems that you might stumble upon.


#2

Whats the difference between this.balance and balance. Both seem to work. Is this only necessary when you have multiple contracts in one file?


#3

The this keyword is used to expressly reference a property of the current running instance of an object (Contract in the case of Solidity). Since all Solidity contracts inherit from Address this.balance references the the inherited balance property of the current contract instance. So why not use balance without the this? Mainly readability and clarity. this.balance is clearly different from sender.balance. It also ensures that if a programmer has added a local var balance (why would they? why is there air? :wink: ) Then you can differentiate between calling the contract’s balance using this.balance versus using the local variable balance.


#4

Hello

What if the callback Function of instance.getLastFlip() returns an error? Could it be, that the condition if(result) is false and we see a “You lost!” eventhough there was an error?


#5

Yes that could be the case. That’s a shortcut, indeed. That’s a great improvement you can make to the contract.


#6

If anyone wants a little challenge after coinflip exercise you can do some more steps:

  1. You don’t show button submit until you don’t get balance or balance is 0.
  2. When you bet check balance if the reward would be bigger then balance you write message to player that casino don’t have enough money.
  3. When player bet you hide button submit and write waiting for result or something.
  4. After you get result show button.

#7

I am stuck on this one.

here is my contract:

The transactions with zero value are from the dapp, not really worried about that yet(although I did get “you won” message once), the 100 and 340 wei are using the interact tab in superblocks. Either way, when I go to the getLastFlip from the interact screen I get (NO DATA) for both addresses that I have tried. This has to be getting set or I could not have won(maybe it is getting overwritten?)

I triple checked the contract against the video and the repository and I can not see where it isdifferent

pragma solidity ^0.4.17;

contract Coinflip {
  address owner;
  mapping(address => bool) lastFlip;

  function Coinflip() public{
    owner = msg.sender;
  }

  function getBalance() constant public returns (uint){
    return this.balance;
  }

  function getLastFlip(address player) constant public returns (bool){
    return lastFlip[player];
  }

  function flip() payable public{
    uint time = block.timestamp;
    uint bet = msg.value;

    if(time % 2 == 0){
      msg.sender.transfer(bet*2);
      lastFlip[msg.sender] = true;
    }
    else{
      lastFlip[msg.sender] = false;
    }
  }

  function deposit() payable public {

  }
}

#8

OK, I went to remix and tested, the contract is fine, and I can see the last flip when I test. I deployed from there and went back to superblocks and pointed the instance to remix deployed instance of the contract. I was able to get that to work, and from remix I can see the lastFlip for my address(true, I won!!!). I did get the js issues worked out but it still seems strange that I could not get a last flip when sending from the interact tab from superblocks, maybe it is a superblocks issue? If anyone else is sees the same issue, let me know.


#9

Hi rbondi,
Did you enter your account’s address in the _player(address) field before clicking on getLastFlip?


#10

Hi guys, that seems weird indeed. I just tried the code myself and get the same result. It seems like a mapping that points to false always returns “NO DATA”. It works fine if the value it points to is true. I tested the following code and it behaves just like that. Seems to be a bug within superblocks. I’ve contacted their CTO to get him to resolve it asap. However, I think it’s only a bug when running it on the VM. If you are running it on the testnet it should be fine.

pragma solidity ^0.4.17;

contract Coinflip {
  mapping(address => bool) test;

  function Coinflip() public{
    test[this] = false;
  }

  function getTest() constant public returns(bool){
      return test[this];
  }
}

#11

I created a pull request for this issue.


#12

This issue should be fixed now


#13

My program won’t work. I tried copying from GitHub with no success. The budget won’t load and I submit makes no visible change.
Here is my app.js code:

(function (Contract) {
    var web3;
    var instance;

    function init(cb) {
        web3 = new Web3(
            (window.web3 && window.web3.currentProvider) ||
            new Web3.providers.HttpProvider(Contract.endpoint));

        var contract_interface = web3.eth.contract(Contract.abi);
        instance = contract_interface.at(Contract.address);
        cb();
    }

    function getBalance() {
        instance.getBalance(function (error, result) {
            if(error){
              alert(error);
            }
            else{
              $("balance").html(result.toString());
            }
        });
    }

    function flip(){
      let val = parseInt($("#bet").val());
      instance.flip.sendTransaction({from: "0xa48f2e0be8ab5a04a5eb1f86ead1923f03a207fd", gas:100000, val}, function(error, result){
        if(error){
          alert(error);
        }
        else{
            alert("SUCCESS");
        }
      })
    }

    $(document).ready(function () {
          init(function () {
              getBalance();
          });
          $("#submit").click(function(){
            flip();
          })
      });
})(Contracts['CoinFlip']);

Here is my app.html code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <script type="text/javascript" src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
        <script type="text/javascript" src="https://unpkg.com/web3@0.20.5/dist/web3.min.js"></script>
        <!-- JAVASCRIPT -->
        <!-- STYLE -->
    </head>
    <body>
        <h1>Hello World DApp</h1>
        <p>Welcome to the coinflip contract</p>
        <h2>Balance: <span id="balance"></span></h2>
        <input id="bet" type="number"/>
        <button id="submit">Submit</button>
    </body>
</html>

Here is my contract:

pragma solidity ^0.4.17;

contract CoinFlip {
    address owner;


    function CoinFlip() public {
        owner = msg.sender;
    }
    function getBalance() constant public returns (uint){

        return this.balance;
    }

    function flip() payable public {
        uint time = block.timestamp;
        uint bet = msg.value;

        if(time % 2 == 0){
            msg.sender.transfer(bet+2);
        }
    }

}

Image of screen:

Edit: I tried it again in Chromium and got this error:

Constructor arguments given are not valid. Too many/few or wrong types.

Any idea what it means?


#14

Hi,
I have some trouble understanding callback functions. For example in our flip() function we have the following line:

waitForReceipt(txHash, function(receipt) { … }

Where does the parameter “receipt” come from? What is it’s value? It has not been declared anywhere.


#15

Did you change the amount of constructor arguments in superblocks? In the contract settings you should decrease the amount of constructor arguments to 0 and then save.


#16

I can understand that it is confusing at first. The “function(receipt) { … }” part is a callback function, passed the the waitForReceipt function. That function is passed in as an argument to the waitForReceipt function with the name cb. You can see it in the declaration of the waitForReceipt function.

function waitForReceipt(txHash, cb){...}

Inside that function you will see that we call the cb function like this

cb(receipt);

That is where the parameter receipt comes from. Let me know if you understand it now or if I should explain further.


#17

Hi,

Did you already find the solution? I have the same issue and the contructor arguments are set to zero.

Thanks


#18

Could you share your code and maybe a screenshot of your contract settings in superblocks? I don’t have any idea what the issue could be at the moment.


#19

I think I found what went wrong.
in the Github app.js file row 21

 $("balance").html(result.toString()); -> balance is missing #

if I change to below it work, ok?

$("#balance").html(result.toString());


#20

Thank you! Good spotting. I have updated the github code.