Programming Project - Phase 2

Hey everyone. I have a gaming related question that is slightly off topic for the project but not for an upgrade. What are deposit bonuses and extra rolls and how do they work? Seems self evident but I have not found a good explanation. Thanks!

thank’s gabba .

I also had some problems with python -m http.server . it wasn’t updated but it’s ok now.

1 Like

Hi Filip and everyone. Begginers questions:

  1. in the course lecture there was supposed to be a link for “how to listen to an event”. i think it is missing.
    2.I am trying to change a mapping variable called “await” from true to false after the call was received. Is it because that in order to do so i need also to catch the event? . see below:
mapping (address => bool) public await;

function __callback (bytes32 _queryId, string memory _result, bytes memory _proof) public {
  require (msg.sender== provable_cbAddress());

  await [msg.sender] = false;

  uint256 randomNumber = uint256(keccak256(abi.encodePacked(_result))) % 2; //was 100 originally
  latestNumber [playerAddress] = randomNumber;
  emit generatedRandomNumber (latestNumber [playerAddress]);
  }

  function update() private {
uint256 QUERY_EXECUTIO
`Preformatted text`N_DELAY = 0;
uint256 GAS_FOR_CALLBACK = 200000;

require (await[msg.sender]== false);
  
  bytes32 queryId = provable_newRandomDSQuery (QUERY_EXECUTION_DELAY, NUM_RANDOM_BYTES_REQUESTED, GAS_FOR_CALLBACK);
  senderQueriId[msg.sender]= queryId;

  await[msg.sender]=true;
  emit LogNewProvableQuery ("provable was sent, standing by for answer..");
  }

Hi @gabba @filip, it’s been a while, everybody is on lockdown now and I found myself trying to complete the ETH 201 course. I’m having troubles with listening to events from the oracle, it doesn’t work, the getPastEvents function for my Solidity event returns an empty list, could you please help me?
Here is the source code

Really nice challenge! :slight_smile:

Ok, I’ve pushed the code to a another branch: https://github.com/PedroMD/eth201-coinflip/tree/phase2-oracle

I have one question, though… Here:

// FIXME. not filterning by queryId...
            contractInstance.once('userWon', {
                filter: {
                    queryId: queryId
                },
                fromBlock: 0,
                toBlock: 'latest'
            }, function (error, event) {

Here it is on GitHub

It seems that the event is not really filtering by the queryId… If I open another window and play the same game concurrently (2 plays from the same account in a matter of 1 second), one of these windows will get the other event regardless, because they are not filtering by the corresponding queryId.

Any idea?!

Thanks!

Hey there, happy to help :slight_smile:

  1. Sorry about that. I’ve added it now. Here you can see how you do it in web3.js documentation: https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html#id36

Here you can see a rough implementation of it as well: https://ethereum.stackexchange.com/questions/35997/how-to-listen-to-events-using-web3-v1-0

  1. I’m not sure that I totally understand you question here. But yes, that’s good. You want to limit the player to only have one game going at the same time. Allowing a player to have 10s or 100s of pending games at the same time might be dangerous for your contract. So we limit the player by using a mapping like you have done. Are you having any issues with the code currently?
1 Like

Good job dude! I checked your code and I found one error. I’m proud to see that you tested the code and found the error. Now, how do we fix it.

It has to do with what’s called indexed event parameters. You can specify when you define the event in your contract if some of the parameters should be indexed or not. Indexed means that they can be filtered on. So if you want to be able to filter on queryId, you need to put queryId as an indexed parameter when you define the event.

event userWon(bytes32 indexed queryId, address player, uint256 prize, bool won);

Good job! I think you should be looking at @PedroDiogo code that was posted right below for how to listen for events. You want to use the once() function. GetPastEvents only retrieves already old events I think.

Try the code that pedro posted above :slight_smile:

@filip , thank you very much! Really missed that bit when reading the docs. Got it now, so simple :slight_smile:
Will add this and also adopt structures, not just mapping (more elegant solution).

Looking forward for the security course! :wink:

1 Like

Hi Filip
yes, the problem is that the value of the “await[msg.sender]” in the callback function is not changing back to false…
Thanks for helping :slight_smile:

Hi @filip @PedroDiogo, I thought that the mistake was in the way I wrote my solidity contract or my main.js file, so I tried using @PedroDiogo’s, but I still don’t get any events. What else could I be missing out? The link to the github project: https://github.com/dominikclemente/coinflip-dapp

Hi @Guy
Your _callback function should be called by the provable api, you don’t need to catch the event to make it works.
You will have to catch the event generatedRandomNumber to get your result. If you are not seeing your ‘await’ variable set to false, their is 2 options:

  • Their is an issue with the provable api, check in etherscan at your address if you see an external interaction with your smart contract ?
  • Your function is reverting in the require , did you set provable_setProof(proofType_Ledger);
    in your constructor ?

Can you send the transaction hash of one of your failing transaction ?

Hi @Dominik_Clemente
I don’t see the indexed key word in your event
event userWon(bytes32 queryId, address player, uint256 prize, bool won);
Should be
event userWon(bytes32 indexed queryId, address player, uint256 prize, bool won);

Are you using ganache gui ? I had issues with ganache gui mine doesn’t send back events.
I make it works with ganache-cli you can install it with npm.

I lok at your code which seem correct.

1 Like

Do you need Ganache to interact with the smart contract if it is deployed on the ropsten test network? Don’t you only need Truffle?

Ho you also have the error when it’s deployed, i though it was just locally, i ll clone your code and test it

Thanks, I tried locally and it worked with the local development trick showcased by @filip. Ohhh, I was missing the --reset argument. I only recompiled, not redeployed, didn’t I?

My hand in.


2 Likes

Congratulations on finishing the project…

:hugs: :partying_face:

Ivo

Thank you! Next is Defi 101 and then smartcontract security. :slightly_smiling_face:

1 Like

Hello,

I had this same issue and was able to downgrade my node version and resolve infura connection error

I am trying to test out the random number generation code on the ropsten network, but I’m getting the following error even though I have deployed the contract. Please let me know your thoughts on this

Thank you,