Ethereum coinflip dapp discussion

OH i see the error now, it was an issue with my HTML. The ID for balance was wrong. Sorry about that! Thanks anyways!

@filip, I’ve been getting the error below not only on the Coinflip dApp, but almost every dApp I create. Searching for answers on the internet has been very vague, mostly referring to the gas limit on the transaction. But even after I set it to 210000, I still get the error. Please help?

inpage.js:1 MetaMask - RPC Error: Error: [ethjs-rpc] rpc error with payload {“id”:3336971970746,“jsonrpc”:“2.0”,“params”:[“0xf86c81808504a817c800830334509483060a4d8d805588e93972b042277124e572e93f8084cde4efa9822d46a076269871c709744961fe9ff82640e6e422e9a6457e97b5a2424c9c4bf2134331a062166eab667c8baabcdfae3fe5e926fd24355a5270a801f1acd62b5636e82ed8”],“method”:“eth_sendRawTransaction”} [object Object] {code: -32603, message: “Error: [ethjs-rpc] rpc error with payload {“id”:33…method”:“eth_sendRawTransaction”} [object Object]", stack: “Error: Error: [ethjs-rpc] rpc error with payload {…method”:“eth_sendRawTransaction”} [object Object]"}

And also to add to this, in another dApp that I created, I sometimes get that error when I use accounts 1&2 in Metamask, but when I switched to accounts 2&3 etc, I do not get the error.

Hi @SatoshiSingh
This is a metamask issue, it happens sometime when the injected account doesn’t update, logout from metamask and refreshing the page often solve the issue.

1 Like

I have problems since superblocks dont recognize Metamask.
I am using Chrome

I already managed to detect Metamask but now it gives me this error " Contract does not exist. When running the in-browser blockchain it gets wiped on every refresh."
And in all the options I have configured testnet and external

Hi @Thaddeus19

Superblocks was used in the old ethereum course switch to the new one, we are not using it anymore

1 Like

Thanks @gabba for the info, I will start to see the new course

1 Like

Hi folks,

I am having a bit of a prblem. I have the solidity parts working well (I think!). The web part doesn’t seem to be to happy though.

At first it says it didn’;t have abi defined, even though I included it in the html file, in the same way as last time. I copied it directly into the main.js file and that seemed to make it a little happier, but when I try and call my play-game function I get errors that you can see in the console here. (You can also see the main.js function that calls the play-game function in the solidity file.

If anyone can help that would be great!

Hi @Kiki
You have 2 metamsk notification did you authorize metamask interacting with your smart contract ?
What is this number variable is it from you code, or is it from metamask ?
Can you link a github repository i ll test it

1 Like

OK, so It turns out that it was brave shields. Also, I solved the transaction fee missing problem. It was to do with meta mask connection to the contract. I connected metamask to a different blockchain, back to ganache and changed the account. That seems to get it to reconnect.

I have one last thing I am stuck on now, I can’t pass the win/lose state of my playGame function back to main.js because return values can’t be sent from a solidity function to a js script (or at least that’s what I read). So instead I am trying to raise an event.

I have set the solidity script to set up the even using:

image

Then call the event using:
image

Then listen for the event in main.js using:

But the console in the app shows the error:
image

Does anyone know how I can fix it? @gabba @filip

Hi @Kiki

Sorry for the late reply @ivga80 told me about it , i moved this topic inside the eth 201 course as it was still in the Blockchain Programing course.
We had create sub topics 2 weeks ago and i am only following topics related to the course. But we didn’t migrate this one … sorry :sweat_smile:

You have this error because “events” are not functions. Events are stored in the transaction receipt not in your contract storage.
It means that you can’t access them from your contract you have to watch every transaction receipt with a javascript function to check if the event is emit in the transaction receipt.

In your case you can do this

          await contract.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
            async (err, events) => {
              this.setState(state => {
                 // Here as events is an array you get the last element of the array
                //  And you check the return value
                console.log(events[events.length-1].returnValues.win);
              });
            }
          )

getPastEvents takes an array of events, so in your case i just use “returnWin”. Then the first and last block this event will have to look for.

Because this function will look at the smart contract tx receipt and check if there is an event emit inside.
So i used latest and latest to only check the last event emitted. But you can look at 0 to latest
fromBlock takes a block number but you can use latest to check the last block.

you can look at this example for more information
https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html#getpastevents

i prefer to use once

https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html#once

PS: If you have an error saying you can’t call await in a function which is not asynchronous just add the keyword
async
before function

1 Like

Hi @gabba I read your response and I am still confused, probably more so.

I am aware of events systems and have used them in other projects before, though not in solidity nor java script. I know that they are not functions.

I am assuming that the code that you added needs calling after the playgame function has been called, which has emitted the event.

my playgame function looks like this:

Open for code
async function playTheGame(){

  //play the gameplay
  var config = {
    value: web3.utils.toWei(gameStake, "ether")
  }

  console.log("\nPlaying game...\n");
  contractInstance.methods.playGame().send(config)
  .on("transactionHash", function (hash){
    console.log("hash of confirmed transaction was :" + hash);
    console.log("Win state was "+winState);
  })
  //update the outputs
  await contractInstance.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
    async (err, events) => {
      this.setState(state => {
         // Here as events is an array you get the last element of the array
        //  And you check the return value
        console.log(events[events.length-1].returnValues.win);
      });
    }
  )

}

And throws the following error:
image

I’m not sure what the this.setState call you included is, why it’s there or what it is supposed to be doing.

Hi @Kiki
Sorry i am using this in my reactjs project ^^ no need to do setState.
Did you look at the different exmaple of events in the docs ?
This one should work

  await contractInstance.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
    async (err, events) => {
         // Here as events is an array you get the last element of the array
        //  And you check the return value
        console.log(events[events.length-1].returnValues.win);
    }
  )

Hi @gabba

That doesn’t seem to work. I don’t get any errors now, but it still returns as undefined.

Expand code
// adds data from form
async function playTheGame(){

  //play the gameplay
  var config = {
    value: web3.utils.toWei(gameStake, "ether")
  }

  console.log("\nPlaying game...\n");
  contractInstance.methods.playGame().send(config)
  .on("transactionHash", async function (hash){
      console.log("hash of confirmed transaction was :" + hash);

    //update the outputs
    await contractInstance.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
      async (err, events) => {
      console.log("result of win event was" +events[events.length-1].returnValues.win);
    });
 });

}

image

:frowning::frowning::frowning:

Ok @Kiki can you print the content of events[events.length-1] ?

    await contractInstance.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
      async (err, events) => {
      console.log(events[events.length-1]);
    });

Are you testing on the testnet or are you still testing locally with truffle ?
Can you check if the event is emitted in remix ? Can you see it in the log ?

Edit: can you also share a github repo i ll look at your code

1 Like

Thanks @gabba :slight_smile:

The github is here: https://github.com/kirasummers/flip

The output of the console.log of events that you requested is image

I am just trying to get it working in truffle before I move onto trying it on the testnet. I was trying to resolve it before I moved on, but I also see it is needed for part 2. I am trying to breakdown the various challenges that part 2 presents; events, oracles and mapping of user info and balances.

Thanks Gabba.

@Kiki i can t see what is in the returnValues but it seems there is something :slight_smile:

1 Like

It has this:

I can’t see a name for the event other than perhaps name: “1”, but it says it has length 0.

So you are trying it locally ? Are you using ganache Gui or ganache-cli ? I had issue in the past with ganache Gui it drives me crazy the events were not sent back. If it is the case can you try with ganache-cli you can install it with npm

1 Like