Programming Project - Phase 1

Hey everyone, @dan-i @filip @thecil

I hope all is well with you! I have recently gotten stuck on the project on the Coin Flipp Dapp Project.

I have started to receive an error when trying to interact with my dapp through the localhost server and Ganache. I am getting some error that seems to be pertaining to Ganache. Here is the following error message that I am receiving. Everything was working fine until this point.

MetaMask - RPC Error: Error: [ethjs-query] while formatting outputs from RPC '{"value":{"code":-32603,"data":{"message":"VM Exception while processing transaction: revert","code":-32000,"data":{"0x561ecfe8301c8c7c2724531363728afeaf555613467b10462f470cf869d16d36":{"error":"revert","program_counter":273,"return":"0x"},"stack":"RuntimeError: VM Exception while processing transaction: revert\n at Function.RuntimeError.fromResults (/tmp/.mount_ganachwb0RIt/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\n at BlockchainDouble.processBlock (/tmp/.mount_ganachwb0RIt/resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:627:24)\n at runMicrotasks (<anonymous>)\n at processTicksAndRejections (internal/process/task_queues.js:93:5)","name":"RuntimeError"}}}}' {code: -32603, message: "Error: [ethjs-query] while formatting outputs from…/task_queues.js:93:5)","name":"RuntimeError"}}}}'"}

I’ve spend 2 days trying to figure this out and look this up. With my current knowledge I have been unable to figure it out. Would anyone happen to know what would be causing me to get this error?

Things I’ve already tried:

  1. Resetting my MM
  2. Reinstalling MM
  3. Resetting my computer
  4. Clearing Cache
  5. Switching from port number 7545 to 8545
  6. Deleting CoinFlip.json file and re-migrating
  7. Going over my code

I think there may have been another student with a similar issue as well as I noticed the same question as mine posted on Ethereum Stack Exchange from 6 days ago for a coin flip dapp. Here is my github as well: https://github.com/Paul-Munley/CoinFlipDapp

Thank you for the help!

I had a similar issues.

  1. For the Revert error : Surely there is a RevertError in the code (@dan-i) can check code and revert.
  2. Also, I had to open up a new workplace in Ganache and Miraculously everything worked Ok after that (do not forget to copy the new contract address in main.js)
    Thanks and Cheers and Happy Coding

Su.kal Crypto

2 Likes

Hey @Su.kal.Crypto

  1. Ya I was thinking it had to be something in my code but and having a difficult time diagnosing it. I know for sure the contract code should be ok.

  2. Unfortunately I already tried that and didn’t seem to have much luck. It’s good to know that I haven’t been the only one with this issue though.

Thank you for the response, I really appreciate it! :slightly_smiling_face: :+1:

I’ve now narrowed this down to I think this code as being the issue but can’t figure out what is wrong with this. It seems to be throwing the error in regards to the config being passed into the send. Any ideas?

function inputBetData() {
    let bet = $("#bet_input").val();

    let config = {
        value: web3.utils.toWei(bet, "ether")
    }
    console.log(config);

    contractInstance.methods.setBet(bet).send(config)
    .on("transactionHash", function(hash){
        console.log(hash);
    })
    .on("confirmation", function(confirmationNr){
        console.log(confirmationNr);
    })
    .on("receipt", function(receipt){
        console.log(receipt);
    })
};

This code above will take the input for the bet amount and subtract it from the contract balance that was added to the page. It is meant to take in an ether amount from the user and send it to the contract.

Hi @Haysoose

I had to use .send({value: 0, from: contractInstance.address}). Is this the best way to handle that?

The payout should not be handled in the js file, it should be done from your contract directly.
Keep in mind that your contract must be 100% independent from the front end.

If you want to push money to the user in case he wins (not safe), you can use msg.sender.transfer().

if (userGuess == now %2) msg.sender.transfer etc…

I suggest you to keep track of the user balance and code a withdraw function instead.

Regards,
Dani

1 Like

Hi @Su.kal.Crypto

I wrote a basic faq about events, this will give you a general idea that you can of course improve: FAQ - How to listen for events

In the faq you will find a link to the web3 documentation, always check there as it contains examples and explanations that will surely help you :slight_smile:

Happy learning,
Dani

Hi @Paul_Mun

Ahh ok I see what you mean!! What is the difference between accessing a global variable through a getter function vs just setting the variable to public then accessing it thorough the getter function that is automatically created for it? Is one way more superior or more safer to use than the other?

A public variable can be read and used from other contracts, while a private variable can only be accessed if you code a getter function.
It really depends if you want that variable to be accessed by other contracts.

1 Like

Hi @Paul_Mun

I agree with @Su.kal.Crypto, I think that your code is hitting a revert.
Your function random() has a require:

function random(uint256 choice) public payable returns(uint){
	    require(choice == 0 || choice == 1);

Are you sending 0 or 1 as parameter?

Also please give us more context, when does this error pops up?
Which function are you calling?
You can also add a revert error and check if that is returned, for example you could do something like this:

    require(choice == 0 || choice == 1, "invalid choice");

Hey @dan-i thank you for the response! The 0 and 1 I just had as a representation of the heads or tails choice for my radio buttons.

It happens specifically when I’m running the addBet button after you input how much you’d like to bet for the coin flip game. It seems to specifically be happening here in my JS file right around the .send() spot. I think it has to do with what I have set up for config to convert the input from ether to wei as that’s what I figured would make the most sense for someone wanting to bet a certain amount.

function inputBetData() {
    let bet = $("#bet_input").val();

    let config = {
        value: web3.utils.toWei(bet, "ether")
    }
    console.log(config);

    contractInstance.methods.setBet(bet).send(config)
    .on("transactionHash", function(hash){
        console.log(hash);
    })
    .on("confirmation", function(confirmationNr){
        console.log(confirmationNr);
    })
    .on("receipt", function(receipt){
        console.log(receipt);
    })
};

I was able to mess with it a bit and did get it to work, but it then takes the input only as wei and does not convert it to ether. Would there be a better way for me to do this if it does seem to be an error with the jquery not taking my config variable to convert the input to wei?

Can you console.log(config.value) and post the result?

Mhmm, when I console.log(config.value) it comes out to:
{value: "3000000000000000000"}

That’s the correct value (3 ether).
You said that but it then takes the input only as wei and does not convert it to ether.
Why are you thinking that? Do you get a wrong value when you send these ether to your contract?

When I press the addBet button is when I get that revert error and it seems to for some reason reference that part of my JS file right before I get that error. But the console log shows that the amount is correct. That’s what’s confusing about it, is that it’s showing that I’m right but I’m getting that strange Metamask Error. In the screenshot error breakdown I have towards the bottom of the error it shows:

inputBetData @ main.js:30 (which is from my code)
dispatch @ jquery-3.4.1.min.js:2 (It seems like this is not from my code. Not sure what this is)
v.handle @ jquery-3.4.1.min.js:2 (This seems like it’s not from my code. Not sure what this is)

Hi @Paul_Mun

You are sending ether to a non payable function therefore it fails.
SetBet is not payable.

function setBet(uint256 amount) public {

Regards,
Dani

1 Like

Hi @thecil, @filip,

I reached “Project 1” in my study plan. I reached out to my student counselor Mikolaj for some project ideas. I chose to “Make a Collectable”. Here are the details of my project. Can someone please review and let me know if I can move onto the next topics in my study plan?

I created an ERC-721 NFT. It’s called “My Blockchain Job NFT” :smiley: I will send it to myself when I finally get some paid work as a blockchain developer :slight_smile:

Github: https://github.com/kostka-tech/my-blockchain-job-nft

OpenSea: https://testnets.opensea.io/assets/0xed3d15B312723d305448855C681A594316fb902A/0

Looking forward to your review,

Thanks!
Brian

Hey @dan-i thank you so much for the help on this! This is exactly what it was! I was getting myself confused because when I was testing it on remix it wasn’t requiring it to be payable.

That was because in remix that part that I had in my JavaScript file was not being used and I was just testing the contract in Wei. So I totally missed that it would need to be payable when testing locally because of the {value: web3.utils.toWei} in that JavaScript file.

Thanks again, even though it took me a while to figure out it was a good learning experience. :blush::+1:

1 Like

https://drive.google.com/file/d/1dxeNxSuYmH90VW_rcUM_fYRpaAEwaAcy/view?usp=sharing
Phase 1 complete. I am very anxious to continue learning, so I will put more time into designing the frontend in the future and repost.

Hey @dan-i

I had in fact gone through this FAQ and in my opinion i wrote my code as per this FAQ only, but it ain’t still working ?
I mean there are errors displayed in the console, but the console.log of the event is not showing in the console too, thats why sent you the Github repository link. Could you please be kind to check and revert where am i Going wrong ?
https://github.com/Suveett/coinFlip-Dapp.git
Thanks and Regards

Su.Kal Crypto

Hi @Su.kal.Crypto

I checked your repository but I do not see code related to event handling.
Your last push was 4 days ago, are you sure you have pushed your latest modifications? :slight_smile:

Hi @dan-i, see my project above, maybe you can help? Or point me to someone who can…

Thanks!
Brian