Programming Project - Phase 1

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

Hello everyone!

General question about the python3 server. I made some changes to my code, I did “truffle migrate --reset”, added the new contract address to “main.js.”, then updated “abi.js”. When I launch the local server and inspect the page, the old contract address still shows in the “setProvider” field (shown below). No matter how many times I migrate reset or restart Ganache, it does not change! Why does this happen ??? :sob: Could it be related to the red error message regarding web3.js not being injected anymore?

Hi @calvalini

Sometimes the python server could take some time to update, are you now able to see the new contract address?

Hi @dan-i !

Thanks for the speedy response! Unfortunately it still does not work. Not even the message in console.log updates when I change it. It seems most of main.js doesn’t work. Could it have to do with Metamask’s update from a couple days ago ? The warning messages seem to indicate it, but I am not sure how to fix it.

1 Like

Hey @calvalini

Can you please post your main.js code here and a new screenshot (taken now) of the contract address logged on the console?

Could it have to do with Metamask’s update from a couple days ago ?

Nope it should not be related to that.

It is not finished yet! Yet here it is.

var web3 = new Web3(Web3.givenProvider);
var contractInstance;


$(document).ready(function() {
    window.ethereum.enable().then(function(accounts){
      contractInstance = new web3.eth.Contract(abi, "0x4c99cD14BDc7B4a0DF2a28987d64cAc7C10BB7C0", {from:accounts[0]});
      console.log(contractInstance, "Test!");
    });
    $("#add_data_button").click(inputData);
    $("#get_data_button").click(fetchData);
});


function inputData(){
  var name = $("#name_input").val();
  var age = $("#age_input").val();
  var height = $("#height_input").val();

  var config = {
    value : web3.utils.toWei("1","ether")
  }

  contractInstance.methods.createPerson(name, age, height).send(config)
  .on("transactionHash", function(hash){
    console.log(hash);
  })
  .on("confirmation", function(confirmationNr){
    console.log(confirmationNr);
  })
  .on("receipt", function(receipt){
    console.log(receipt);
  })
}

function fetchData(){
  contractInstance.methods.getPerson().call().then(function(res){
    $("#name_output").text(res.name);
    $("#age_output").text(res.age);
    $("#height_output").text(res.height);
  })
}

Screenshot of address logged