Ethereum coinflip dapp discussion

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

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 ?

image

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?

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’]);

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:

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

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

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

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.

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 …

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:

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…

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

Yes :slight_smile: Thank you …

1 Like

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:

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.

Hello Everyone,

I have been searching a faucet for 1 week, but im not be able to get Ether for my Rinkeby Wallet,

Can anyone send me some? Thanks

0x782379b55519147DE4288F71604AabC2f80D3Aa3

Thank you in advance!

Hi,

When I try to deploy: “External account not found. Metamask unlocked?”,

I usually work with Metamask with others providers, this only fail with SuperBlocks!,

Thank you,

Here is a faucet that has ether: https://faucet.metamask.io/

Are you sure you have Metamask unlocked?

Hello Filip,

Yeah, Its working properly now.

Ohh, The first time that Metamask is installed with this feature selected by default:

Privacy Mode

Websites must request access to view your account informatio

Thnank

1 Like