Ethereum coinflip dapp discussion

Nevermind, I figured it out! :stuck_out_tongue: It had to do with the interference of Web3 extensions that I had installed on my chrome browser.

Could you post your code here?

It’s pretty much what you posted in GitHub.

If it’s only pretty much the same I still need to see the code in order to figure out if there is an error in it.

Sounds good. Thanks btw for helping out.

Coinflip.sol

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 address(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 {

  }
}

app.html

<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript" src="https://unpkg.com/[email protected]/dist/jquery.js"></script>
    <script type="text/javascript" src="https://unpkg.com/[email protected]/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>
    <h3 id="result"></h3>
</body>

app.css

>     body {
>         background-color: midnightblue;
>         color: darksalmon;
>         font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
>         text-align: center;
>     }

app.js
(function (Contract) {
var web3_instance;
var instance;
var accounts;

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

    accounts = web3.eth.accounts;

    var contract_interface = web3_instance.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 waitForReceipt(txHash, cb){
  web3_instance.eth.getTransactionReceipt(txHash, function(err, receipt){
    if(error){
      alert(err);
    }
    else if(receipt !== null){
      cb(receipt);
    }
    else{
      window.setTimeout(function(){
        waitForReceipt(txHash, cb);
      }, 5000);
    }
  })
}

function getResult(){
  instance.getLastFlip(accounts[0], function(error, result){
    if(result){
      $("#result").html("You won!");
    }
    else{
      $("#result").html("You lost!");
    }
  });
}

function flip(){
  let val = parseInt($("#bet").val());
  instance.flip.sendTransaction({from: accounts[0], gas:100000, value: val}, function(error, txHash){
    if(error){
      alert(error);
    }
    else{
        waitForReceipt(txHash, function(receipt){
          if(receipt.status === "0x1"){
            getResult();
            getBalance();
          }
          else{
            alert("receipt status fail");
          }
        });
    }
  })
}

$(document).ready(function () {
      init(function () {
          getBalance();
      });
      $("#submit").click(function(){
        flip();
      })
  });

})(Contracts[‘Coinflip’]);

Hello everyone, although it’s not directly related to the CoinFlip dapp, I have a question that the example doesn’t explain. How do I use the “sendTranscation” method for Solidity functions that need to receive inputs? And, more specifically, more than one input? Thanks in advance.

Thank you. I will be trying to debug this as soon as I have time. I’m sorry that it has taken some time. I’m doing my best :slight_smile:

1 Like

If we take the coinflip function as an example, you would add two arguments like this

instance.flip.sendTransaction(arg1, arg2, {from: accounts[0], gas:100000, value: val}, function(error, txHash)

I tried this already, so the problem is another one. Could I email you with the specific code i’m trying to work on?

I don’t get any alerts - no alert about fail and no alert with the receipt even though my code is the same as in filip’s github, what can I do?

Send me a PM here on toshitimes and we can connect :slight_smile:

I’m looking into it. Will get back. Sorry for the slow response. I have been abroad.

Sorry for the delay, I have been traveling. I found the error. It was a very simple one, haha.

At the very bottom of app.js you write

})(Contracts[‘Coinflip’]);

The quotes around Coinflip is of the wrong type. You need to use ’ instead of ‘. The compiler can’t interpret the later one. So the last line of app.js should be

})(Contracts['Coinflip']);

1 Like

Wow, of course it would be that small.
Thanks!! Will try it now!

1 Like

: https://github.com/filipmartinsson/Solidity/tree/master/Dapps/Coinflip%20dapp%20part5;
Hi. Fillip. Using the link and your video tutorials, the final part in my superblocks - the deposit function is missing. The rest like getLastFlip, getBalance and flip is present while doing the interact. Where did I go wrong?
Thanks for your help.
.

What does your solidity code look like? Where are you missing the deposit function, in the interact tab?

Hi, Filip. It’s okay now. I checked with your summary found some difference and missing",;".
Thanks::clap::+1:

1 Like

I try to make the dogcontract into DAPP, but some problem happened. I run the addDog function, metamask popup, but I can’t add dog. Please help me

dogContract.sol

function addDog(string _name, uint _age) payable public {
uint id = dogList.push(Dogs(_name, _age));
ownerToDog[msg.sender] = id;
}

app.js

function addDog(){
var name = $("#dogName").val();
var age = $("#dogAge").val();

    instance.addDog.sendTransaction(name, age, {from: accounts[0], gas:100000, value:100}, function(error, txHash){
        if(error){
            alert(error);
        }
        else{
            waitForReceipt(txHash, function(receipt){
                if(receipt.status === "0x1"){
                    alert("Dog is added successfully.");
                }
                else{
                    alert("Dog is failed to add.");
                }
            });
        }

    })
}

I see one error on the line

uint id = dogList.push(Dogs(_name, _age));

Dogs should be Dog. At least if the rest of your code is the same as mine. Meaning the struct is called Dog.

Hi Filip!

I had the same problem: Everythig works, except for the “you won” and “you lost” part.
It was not working in my own intepretation of the code so I copied the whole code of yours (from all the files, .html, .js and .sol)
It still shows the same, everything works, except for the “you won”/“you lost” part - with the code from gitHub
Any ideas?

1 Like