Programming Project - Phase 2

Finally finished my first coinflip Dapp in Solidity en JavaScript. Took me a long time to finish it. I came across a lot of hurdles but manage to solve most of them except a bug with the event triggers.

See below for my coinflip Dapp.

Github repository: https://github.com/TopgunJHW/CoinFlip-Dapp
Video Dapp in action: https://github.com/TopgunJHW/CoinFlip-Dapp/blob/master/demo/Dapp%20coinflip%20-%20demo.mp4

I still have a bug in the front-end regarding triggering of multiple event listeners by 1 event. I could not solve it and hope someone can help me.

See the bug in the videolink below. I have 2 bets waiting for the provable oracle. At timestamp 1.24 the first event comes in and triggers both event listeners of both my bets. The console in video shows that both events has been triggered. The event where the query ID is not the same should not trigger.

https://github.com/TopgunJHW/CoinFlip-Dapp/blob/master/demo/CoinFlip%20Dapp%20-%20Event%20trigger%20bug.mp4

Hi friends, I’m revisiting this after completing the project a couple of months ago. When trying to run this on the truffle test network, I’m not seeing the various output that should pop up during compilation. Am I running this correctly? For example, I’m not seeing the contract address show up - am I missing something?
image

Hey @oneworldcoder

Try this import 'https://github.com/provable-things/ethereum-api/blob/master/provableAPI_0.5.sol';
Let me know, if it does not work I will look into it :slight_smile:

Cheers,
Dani

Hey @Jian_Hao_Wei

You can set a filter for the event and also use different kind of event listeners (for example once).
There are really many of them, take a look at the docs : https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#once

Happy learning,
Dani

Hey @lohba

truffle migrate --reset --network ropsten.
You need to reset your migration to deploy fresh new contracts.

Cheers,
Dani

Hey @dan-i,

I did look at the website and used the once event trigger with filtering on the queryID.
The problem occurs when I have two bets in play. When the event of the first bet comes in. It triggers the first one correctly but it also triggers the event of my second bet. I even console log the queryID’s and the second bet should not have been triggered, see screenshot and my code snippet below.

function bet(){
      var choice = document.getElementById("choice").textContent;
      var betAmount = $("#betAmount").val();
      var config = {value: web3.utils.toWei(betAmount, "ether")};

      contractInstance.methods.makeBet(choice).send(config)
      .on("transactionHash", function(hash){     //.on is an event listener listening to specific event
        console.log(hash);
      })
      .on("confirmation", function(confirmationNr){
        console.log(confirmationNr);
      })
      .on("receipt", function(receipt){
        console.log(receipt);

        var queryID = receipt.events.logNewProvableQuery.returnValues.queryID;
        insertTableRow('table_bets_body', queryID, [blockNumber, choice, 'pending', 'pending']);

        contractInstance.once('betResult', {filter: {queryID: queryID}}, function(err, event){
          console.log("Event trigger Success");
          console.log("QueryID event equal to queryID input: ", queryID == event.returnValues.queryID);
          ...
            });
          });

      })
      // .then(function(re

Perfect, thank you very much. I have a follow up question. When making edits in my main.js file, for some reason I’m not seeing the changes being reflected. I’ve updated the abi.js file and have truffle migrated but this is not showing up, what am I doing wrong here?

Hello friends
I have a problem with Infura. I’m really not sure what is going on.( maybe timeouts). I tried with even the most basic project hello world and i got timeouts errors. the project id ,infura key and seed are ok


Hey @Jian_Hao_Wei

Add the starting block to your event listener:

filter: {queryID: queryID},
fromBlock: 'latest'

Let me know

1 Like

Sometimes the python server is slow at updating things.
You should see the changes by waiting a bit more.

You can verify the issue by creating a new infura account and use a new key.
Let us know!

I’m still not seeing the changes reflected - I noticed that when I compile my contract, I’m not seeing Coinflip.sol and Migrations.sol being compiled. Could this be the reason?
image

I’m still not seeing the changes reflected - I noticed that when I compile my contract, I’m not seeing Coinflip.sol and Migrations.sol being compiled. Could this be the reason?
image

I cannot see the command you are running from the screenshot, but you should truffle migrate --reset.

What you reported before however was about your main.js.
Double check if the issue is solved, otherwise describe the issue and how you reproduce it.

Cheers,
Dani

Thanks dani, I ran truffle migrate --network ropsten and got what I attached. I subsequently ran truffle migrate --reset and still not seeing the changes I made in the main.js reflected. If easier, my repo can be found here: https://github.com/lohba/Coin-Flip-Dapp-v2

Can someone please tell me why I’m getting this error in PowerShell when I try to install hdwallet-provider?

Hello @dan-i

I created another account, another id, and simplified my contact to the easiest, and had the same error.
the best result i had is this


The warning is telling you to use the new hd wallet provider, so you can go ahead an install it as in the console.
npm i @truffle/hdwallet-provider

Regards,
Dani

1 Like

I tried to deploy a contract to Ropsten and I was able to proceed.
Lets update Truffle first:

npm uninstall -g truffle
npm install -g truffle

Your node version is the same as I have so I would leave it like this.

Give it a try

Please describe what’s missing, and how to reproduce the issue.

Hey @dan-i ,

I added your suggestion to my code, but i still get the same bug.

I tried to figure out what the problem was and tried the getPastEvents function in the console to retrieve the last event using the filter options. See code snippet below. The result is shown in screenshot below. It seems that the filter does not work for me and it returns all the past events instead of the last one.

Is this also related to the problem i mentioned in my previous post?

contractInstance.getPastEvents(
     'betResult', {
          filter: {queryID: '0xc2ea405aebff28beeb5f7b215c7b72b64fb83b586f4c47ca66a47e15155d38e3'},
          fromBlock: 0},
          function(err, event){
            console.log("Event trigger Success");
            console.log(event);
})

Cheers,
Jian