Ethereum coinflip dapp discussion

#124

If I run the dapp in superblocks I don’t get the balance “0”. What am I doing wrong?
(I tried to copy/paste the code from github, same result.
If I look into the console I get an error (see below).

EDIT: i got it working today

This is my app.js

(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: "0x95c2332b26bb22153a689ae619d81a6c59e0a804", gas:100000, value: val}, function(error, result){
        if(error){
          alert(error);
        }
        else{
            alert("SUCCESS");
        }
      })
    }

    $(document).ready(function () {
          init(function () {
              getBalance();
          });
          $("#submit").click(function(){
            flip();
          })
      });
})(Contracts['Coinflip']);
Using Superblocks web3 provider for endpoint: http://superblocks-browser
jquery.js:3818 jQuery.Deferred exception: Cannot read property 'abi' of undefined TypeError: Cannot read property 'abi' of undefined
    at init (about:srcdoc:36:61)
    at HTMLDocument.<anonymous> (about:srcdoc:65:11)
    at mightThrow (https://unpkg.com/jquery@3.3.1/dist/jquery.js:3534:29)
    at process (https://unpkg.com/jquery@3.3.1/dist/jquery.js:3602:12) undefined
jQuery.Deferred.exceptionHook @ jquery.js:3818
process @ jquery.js:3606
setTimeout (async)
(anonymous) @ jquery.js:3640
fire @ jquery.js:3268
fireWith @ jquery.js:3398
fire @ jquery.js:3406
fire @ jquery.js:3268
fireWith @ jquery.js:3398
ready @ jquery.js:3878
completed @ jquery.js:3888
jquery.js:3827 Uncaught TypeError: Cannot read property 'abi' of undefined
    at init (VM534 about:srcdoc:36)
    at HTMLDocument.<anonymous> (VM534 about:srcdoc:65)
    at mightThrow (VM519 jquery.js:3534)
    at process (VM519 jquery.js:3602)
init @ VM596 about:srcdoc:36
(anonymous) @ VM596 about:srcdoc:65
mightThrow @ jquery.js:3534
process @ jquery.js:3602
setTimeout (async)
jQuery.readyException @ jquery.js:3826
(anonymous) @ jquery.js:3846
mightThrow @ jquery.js:3534
process @ jquery.js:3602
setTimeout (async)
(anonymous) @ jquery.js:3640
fire @ jquery.js:3268
fireWith @ jquery.js:3398
fire @ jquery.js:3406
fire @ jquery.js:3268
fireWith @ jquery.js:3398
process @ jquery.js:3622
setTimeout (async)
(anonymous) @ jquery.js:3640
fire @ jquery.js:3268
fireWith @ jquery.js:3398
fire @ jquery.js:3406
fire @ jquery.js:3268
fireWith @ jquery.js:3398
ready @ jquery.js:3878
completed @ jquery.js:3888
0 Likes

#125

I have just 1 question about this part. When I deployed my contract on the testnet i get the following error, but everything seems to work fine. What is this error ?

0 Likes

#126

Do you know how to check what you are passing into the constructor in superblocks? What do they look like and do they match up with the code?

0 Likes

#127

Help needed. For part 3 lesson, unable to “Receipt Status Failed”. I tried to debug, but in vain.

This is my codes.

pragma solidity ^0.4.17;

contract FlipCoin {
address owner;

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

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

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

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

} 

}

///////////////////////////////////////////////////////////////////////

(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 waitForReceipt(txHash, cb){
    web3.eth.getTransactionRecipt(txHash, function(error, receipt){
        if(error){
            alert(error);
        }
        else if(receipt !== null){
            cb(receipt);
        }
        else{
            window.setTimeout(function(){
                waitForReceipt(txHash, cb);
            }, 5000);           // every 5000 milliseconds will query for transaction receipt from the Ethereum Blockchain Network
        }
    })
}

function flip() {
    let val = parseInt($("#bet").val());
    instance.flip.sendTransaction({from:"0x95c2332b26bb22153a689ae619d81a6c59e0a804", gas:100000, value: val}, function(error, txHash){
        if(error){
                alert(error);
        }
        else{
            waitForReceipt(txHash, function(receipt){
                if(receipt.status === "0x1"){
                        alert(JSON.stringify(receipt));
                }
                else{
                        alert("Receipt status failed");
                }
            });
        }
    })
}


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

})(Contracts[‘FlipCoin’]);

0 Likes

#128

hey @vincentchia
You wrote web3.eth.getTransactionRecipt instead of web3.eth.getTransactionReceipt
I think you error come from this typo.
You can use the console to catch this kind of error :wink:

0 Likes

#129

Super. Thanks a lot. How to get the console ? Pardon me for asking. New to all these. Appeciated.

0 Likes

#130

If you are using chrome, right click and press inspect or inspect page.

0 Likes

#132

Hi , Im having trouble with calling the function (which is public) in my smart contract with instance. in .js file, when I type instance, the function I want to call isnt in the options that I can choose , what can be the problem ? thx

0 Likes

#133

That shouldn’t matter, as long as the function is correctly defined. It’s just an issue with the editor. Just type the correct name.

0 Likes

#134

Aha, I just seems weird, like in your contract and js. file when I type instance. I get the function name but in my I dont… I broke down the steps in which the whole app works to fully understand it

  1. first I`m just trying to get back the txHash when I click the submit button, but my instance.getHash.sendTransaction({from: address[0], gas:100000, value: }, function(error, txHash){ ;
    has no value passed trough to the getHash function , or do I have to pass in a value ?
    The function in the SC is like this :
    function getHash() public payable {
    }
  2. When the hash arrives I go to the waitForRecipt function to get the receipt , the call back function and if the receipt is ok , I get a message …
    The problem is when I click the button I assume it doesnt get passed to the function getHash and I was thinking maybe the thing with the instance is the problem …
0 Likes

#135

Off-course I know that if the function your calling hasnt the argument of an value to be passed in to it and to do something with that value, you can`t pass a value, so do I just leave that value blank in the .sendTransaction for the sake of just retrieving the txHash of the transaction ? just wanted to clarify my question :slight_smile:

0 Likes

#136

Ok I figured it out, I had an argument in the function I was passing the value (and it doesnt need it), because i tough thats the way you can access the val, but its accessed by msg.value … Anyways Im trying to make a simple multiplication function in which the value you type in the input gets multiplied and the result is send back and displayed on the web page…

0 Likes

#137

Is everything working now? Otherwise please share the code so I can have a look.

0 Likes

#138

Yes :slight_smile: Thank you …

1 Like

#139

Hey Philip, well I have a potentially stupid question :smiley: :smiley: What stops one from making a corrupt coinflip dapp and putting it on a blockhain , corrupt in a way that one writes the coinflip random function so it is in his favor like 60/40, so 60% of the time the flip is in his favor and he receives the users ether ? ok im out, thx :slight_smile:

0 Likes

#140

Your customers would probably want to see your code. And if they have the code they can hash it and verify so that it’s that exact code that is running on the blockchain.

0 Likes