Programming Project - Phase 2

Hey @Ula

I am running your code and seems working for me, please DM me with your wallet address, I need to do some verifications.

Cheers,
Dani

2 Likes

hey Erno, loving the project! just stumbled across and liking you using all the cool things react has to offer :smiley: UX is pretty cool too

2 Likes

Github link : https://github.com/Mislav96/Coin-Flip-Dapp

1 Like

Hi @cryptocrom, thank you for all of your advices, it really helped:) error in console stopped showing up and I think I haven’t had anything to do with that :thinking:

var query_Id;
contractInstance.events.LogQueryId
    function(error, event){console.log(event);})
    .on('data', function(event){
            query_Id = event.returnValues.queryId;
    });

this part definitely worked for me, also there was no need to put event listeners on the top, but seems like they don’t work when placed in the called method, does anyone know why?

contractInstance.methods.flip(coinside).send(config)
    .on("transactionHash", function(hash){
        console.log(hash);
        $("#action").text("Wait for your transaction to be confirmed");
    })
    .on("confirmation", function(confirmationNr){
        $("#action").text("Transaction confirmed! Wait for result of your game");
        console.log("transaction confirmed"); // no event listener will work here

My thinking was that LogQueryId event listener placed in the flip() function in js will be ā€œattachedā€ to this particular user who called this fuction, am I wrong? I also tried to place it inside .on("receipt", function(receipt){...} option, but it doesnt show up in the console…Is it better to filter by user address?
Here is my main.js file for better picture:

var web3 = new Web3(Web3.givenProvider);
var contractInstance;
$(document).ready(function() {
    window.ethereum.enable().then(function(accounts){
        contractInstance = new web3.eth.Contract(abi, "0x3bFA3EA63836bfaB3dd24fA07d8CBf6A912AbA55", {from: accounts[0]});
        console.log(contractInstance);
    });

    $("#flip").click(flipAcoin);
    $("#withdraw").click(withdraw);
    $("#showbalance").click(balance);
    $("#withdrawbalance").click(withdrawbalance);
    
});

function flipAcoin(){
    var coinside = $("input[name=coinside]:checked").val();
    var betvalue = $("#bet_input").val();
    var config = {
        value: Web3.utils.toWei(betvalue, "ether")
    }
    var query_Id;
    console.log("the coinside is " + coinside);
    console.log("the bet amount is " + betvalue);
    
    if (betvalue<0.1) alert("minimum bet is 0.1 ether");
    

    contractInstance.methods.flip(coinside).send(config)
    .on("transactionHash", function(hash){
        console.log(hash);
        $("#action").text("Wait for your transaction to be confirmed");
    })
    .on("confirmation", function(confirmationNr){
        if (confirmationNr == 0) {
            $("#action").text("Transaction confirmed! Wait for result of your game");
        };
        console.log("transaction confirmed");  
    })
    .on("receipt", function(receipt){
        receipt.events.LogQueryId(function(error, result){
            query_Id = result.returnValues.queryId;
            console.log(query_Id);//this event doesn't work, any ideas 
//why events inside the method dont work?
        })
    })
    
    contractInstance.once('coinFlipResult'
        , {filter: {queryId: query_Id}
        , fromBlock: 0
        , toBlock: 'latest'
     }, function(error, event){
        console.log(event);
        if (event.returnValues.win == true) {
            let prize = web3.utils.fromWei(event.returnValues.prize, "ether")
            $("#action").text("Congratulations! You won! Your prize is: " + prize);
        }
        else {
            $("#action").text("Ahhh... You lost a bet");
        }
    })  
}

Awesome, thanks for the feedback and glad at least some of it helped - I just hope I am not like the blind leading the blind haha…

I have no idea why my event listeners only work at the top then - so frustrating.

Anyways, my understanding of event listeners is that they subscribe to a particular event of the contract you set them to listen for and any event that matches the criteria (the filters, if you set any) will show up as requested.

Sending a transaction to your contract is a separate instance to an event firing - even if the event emitter is within the function of that transaction in your smart contract - they are separate occurrences, so I’m guessing this is partly to do with why it doesn’t work and also because I believe they are asynchronous, the function would just listen - hear nothing and move on…but if you have the event listener on its own, it will be always listening and can carry out whatever instructions it has when ever the event criteria are met.

You’re not really wrong but the front end doesn’t really care who the user is unless you tell it directly - looking bigger picture, you would probably run into trouble if lets say 5 people are playing your DApp all at the same time. During testing with a single user and bet, you will not have any issues, but lets say you do have 5 people playing and they all put their transaction in at nearly the same time. They will all be using the same front end code, each person will have slightly different variables based on their bet but when it comes to the waiting for results, they will all be waiting for the event LogQueryId to fire. So while you are correct that the LogQueryId event listener is attached to them in the smart contract, the front end can’t tell the difference between users and this is where you could run into trouble. You have to tell it what to look for, for example filtering for the queryId or the user address in the event listener so that it only picks up on events that are pertinent to that user or their callback id and then fires the code included accordingly. Otherwise you and I might both be waiting for results and then when your results come in, my front end may fire and tell me that I’ve won, when in fact the smart contract is telling a different story. I know that most of us probably aren’t going to deploy this as a DApp for real use, but I think it is useful pretending that you will and then trying to address any issues that you think might pop up during use on a larger scale. This has helped my understanding greatly as it helps me to think about issues I might have later, not just addressing issues I have as I go. Anyways, thanks heaps for the feedback - I thought I’d given you rubbish advice after Dani said she had it all working for her. Glad you’re having some luck with it - good luck.

Hey guys, when I try to deploy my contract to the Ropsten network, I get this error below:

Anyone familiar with this?

what was the other faucet urls? I get this on error on faucet.metamask.io:
{ā€œerrorā€:"[ethjs-query] while formatting outputs from RPC ā€˜{ā€œvalueā€:{ā€œcodeā€:-32000,ā€œmessageā€:ā€œreplacement transaction underpricedā€}}’"}

I’ve got no clue at all but found a few github threads regarding this issue and it seems like maybe changing the gas price ā€˜may’ do the trick…a couple of the threads are listed below, all I did was copy and paste your error message into my search engine and have a read…


From this link - I saw this post a fair way down…
I had the same issue with Infura on the Ropsten network. I wasn’t providing a gasPrice when I got the ā€œtransaction underpricedā€ error; only gas. Looks like I had to provide all transaction parameters except nonce like below if not managed by geth and if signing the transaction myself. Transaction went through fine right after.

{ā€œjsonrpcā€:ā€œ2.0ā€,ā€œidā€:3,ā€œerrorā€:{ā€œcodeā€:-32000,ā€œmessageā€:ā€œtransaction underpricedā€}}

web3.eth.sendTransaction({ from: from, to: to, value: value, gasPrice: "20000000000", gas: "21000" }, function(error, result){ if(error) { } else { } })

{ā€œjsonrpcā€:ā€œ2.0ā€,ā€œidā€:3,ā€œresultā€:ā€œ0xe47688ed9bc2b9d4f631b0a94b7a40dd83ab884ec1493e3ba9b741da67b3a83bā€}


From this link I found this
You can usually get a transaction through by bidding a high enough gas price. You can get advice on good gas prices to bid here: http://ethgasstation.info/

I found this post in this thread above - I have not used this but FrankB seems to have had success with it.

Good luck

answered in DM because the code was too long to be posted here.

1 Like

Hey @eaglejr

Please downgrade your NODE JS by following this FAQ: FAQ - How to downgrade Node.Js

Keep me posted.

Dani

Hey @bjorkmanders

Those are most likely errors on their side. What you can do is reset Metamask but nothing more than that.
In these cases I suggest to try other faucets, and come back to that one later on.

Happy coding,
Dani

1 Like

Finished with phase 2.

Project gitHub

1 Like

I think I’m on the cusp of being able to code the website now, but the main purpose of the contract isn’t working!

Github Link

https://github.com/Uy4n/Coinflip_vOracle

In the Coinflip_vOracle.sol file, the function flipCoin returns the following error when tested on Remix:

transact to Coinflip_vOracle.flipCoin errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.

I’m not really sure what the problem is, since it was working weeks ago, while the rest of the contract wasn’t ready.

Can anyone solve the problem? @dan-i

Another question: I deployed a previous version of the contract on Ropsten through Remix’s Injected web3. How do I interact with this deployed version of the contract?

Deployed Contract on Ropsten

https://ropsten.etherscan.io/tx/0xb4f8c8ffc69248a7249de566a62e5940af6b3bf6902245ed8f483e0e270b9e19

Thanks @dan-i

It looks like I’m still getting the same error, though.

I got stuck with the stupidest problem. My ganache is constantly lugging and shuts down when I try to deploy the Coinflip project with all of those ProvableAPI.sol contracts. Does anyone experience the same problem? Is there a way to minimize that contract?

https://youtu.be/xn5AVdQ_lhY

@dan-i Ok so I changed the compiler version and it looks like it fixed the error I was getting, but now I’m facing a new error when I open the dapp in a browser.jqueryerror

It looks like it’s referring to this line of code in the main.js file

Hey @eaglejr

@dani69654 Ok so I changed the compiler version and it looks like it fixed the error I was getting, but now I’m facing a new error when I open the dapp in a browser.

You get this error because you are not running a local server.

Remember to run python simple server and test you dapp in your localhost :slight_smile:

  • Navigate in your dapp directory using your terminal;
  • Once inside the directory type python -m SimpleHTTPServer 8000;
  • Now open your browser and type localhost:8000

Happy learning,
Dani

Hey @rostyslavdzhohola

The youtube video posted is private, therefore I cannot watch it.

Also if you are using ProvableApi means that you have to deploy in Ropsten and not in your local Ganache.

Keep me posted,
Dani

1 Like

I’ve changed permissions.
As for ProvableApi, I am using it with that dummy contract proposed in the video to first develop a smartcontract instead of redeploying it each time I need to make changes.

2nd Phase Project

Solidity Project

Angular Web App Project (UI for the contract)

Screen Shot

1 Like