Programming Project - Phase 1

@mikekai
You can ask as many question you want :slight_smile:

The contract looks fine to me.

Just make sure your are entering amount as 2 ETH :slight_smile:

or else will have share Github link to check

2 Likes

@saoirse - sorry I just saw this. Hopefully you have figured it out. If not, in MetaMask, click on an account and select ā€œSettingsā€ scroll down and find and click on ā€œAdvancedā€, scroll and click on ā€œReset Accountā€

Regards,

xacatant

1 Like

This is the dapp I created for Phase 1. It turned out pretty OK I think. Unfortunately the metamask part didn’t get recorded along with it. It looks like I keep clicking on tails but I am actually confirming on metamask.

edit* not sure why but the gif didn’t show up. So I am leaving a link to ImgBB.

1 Like

hi, i seem to receive an error quite often from metamask:

the annoying this is is that sometimes my dapp does work as expected and then suddenly after a few times of betting it starts returning this error. any advice?

@doctormartin67
This error is Failed Gas estimation error :slight_smile:

1 Like

@mikekai
Amazing work! Keep it up :+1:

2 Likes

So I reorganized everything and now, it seems to me, it’s all working properly. In my first solution there was too much work done in the unsafe front-end in main.js. So in the new version everything that seemed sensitive to me is handled on the contract side, and I also used a mapping and a struct to handle customer information. The ā€œnowā€ function I also got to work (don’t ask me how—it just suddenly decided to work properly), but during migration test runs, the randomness that it produced was somewhat limited (probably due to insufficient speed in the advance of the block-time—that’s my guess), and I therefore added another count-variable and then produced the hash of the resulting concatenation. I also added an encoding string as before, but this time as a private variable in the contract. I have no idea how easily a private variable in a contract can be accessed, but as I mentioned before, to the best of my understanding of the properties of the hash function, the encoding string would render the pseudo-random sequence generated by the hash function nearly unbreakable if it could be kept in a safe place like an off-line ledger wallet. In any case, I decided to leave the encoding string in the hash-input in case that it adds a bit of additional safety.

The button for the withrawal of all funds in the contract that you can see in the picture shows up only if the person playing is the contract owner. The corresponding test is also run through the contract rather than main.js so that there is no information about the owner’s address available in the front-end code.

Here is the link to my files on gitHub in case that you want to take a look:

And here is a screen copy of my program in action:

The number line provides a little animation as the numbers cycle through color changes from white or black to red, and as the outcome of the game is indicated by the last number that turns red. The reason for having two input options—one for zero and one for one—is simply that users may want to have that freedom of choice. Mathematically, any two-way bet is always equivalent to a corresponding one-way bet, but as I said, it might be nicer for a user to be able to pick an outcome—zero or one.

Finally, let me say again, that I greatly appreciated the opportunity to work on this project. I feel that I really learned a lot, and I look forward to the second part using oracles. Thank you!

Thanks! I am excited for the oracle section but it looks scary. :grimacing: haha.

1 Like

I had a quick question. When migrating a contract to ganache do we add value to the contract via the migration.js file (im using my coin migration file not the intial)? Is it done like this?

const Coinflip = artifacts.require("Coinflip");

module.exports = function(deployer, network,accounts) {
  deployer.deploy(Coinflip).then(function(instance){

  	instance.send({from:accounts[0],value:web3.utils.toWei("5","ether")})

})
 

};
1 Like

Looks fine! :slight_smile:

Sample code

const Flip = artifacts.require("Flip");

module.exports = function(deployer, network, accounts) {
  deployer.deploy(Flip).then(function(instance){
        instance.fundContract({value: web3.utils.toWei('2', 'ether')})
  });
};
2 Likes

Thank you very much!

1 Like

Not a lot to show here, I recycled the HTML from the example and there’s basically two functions: One to flip the coin, another to load the contract. The contract also loads 10 ETH on startup and displays the balance at the bottom.

The only thing I couldn’t figure out how to do (not an expert on jquery tbh) is how to display the contract balance when loading the website. I’d really appreciate some help there. In the old course, Filip coded his example so that the contract balance is displayed when Metamask is connected but I just couldn’t figure out how to do this.

Moving on to the next phase!

@Juan_M_Villaverde
Thanks for reaching out!
It will be nice if you can share your code via GitHub link for helping you with displaying contract balance using jquery :slight_smile:

1 Like

Thanks Taha!

Here it is: https://github.com/MemphisIX/RandomDapp/blob/master/main.js

@Juan_M_Villaverde
I have done some changes to the Github main.js file.

basically, you will need to add the following lines in your ready function
and create an HTML element in index.html to input the data into

$(document).ready(function() {
//create variable for contract address balance
    var contract_balance = web3.eth.getBalance("your contract address");
    //create an element in HTML with id
    //add the id of the HTML element below
    $("#yourHTMLelementid").html(contract_balance);
1 Like

Perfect! Thanks Taha. I’ll put this good use. I’m ready to start doing web3 with the integrated oracle already, it’s deployed on Ropsten and working as intended after… Lots and lots of trials lol!

1 Like

Hey Taha! How can I call a getter function on loading the contract? reason I ask is the new version’s ā€œbalanceā€ doesn’t really match the balance on the testnet. Part of the contract balance is reserved for users who won bets so I really need to call the ā€œbalanceā€ variable and display that on the website.

When I try var contract_balance = contractInstance.methods.getBalance().call(); I just get an error saying contractInstance doesn’t exist.

Thanks!

Hi @Juan_M_Villaverde

You have multiples way of doing it, first do you have a method called getBalance in your contract ?
If yes you need to create your

var contract_instance

At the top of your main.js, then you initialize your contract


$(document).ready(function() {
    window.ethereum.enable().then(function(accounts){ // window.ethereum.enable brings up the Metamask prompt in the webpage.
      contractInstance=new web3.eth.Contract(abi, "0x62E2fF8FbCFa57149c129fd8d66741ABe3735529", {from: accounts[0]}); //This creates an instance of a contract, the argument abi is the functions and variables therein (see abi.js), the second argument is the contract address, as a string. Finally, the third specifies the sender for the contract.
      console.log(contractInstance);
    });
});

Then you can call this method in your code

var returnValue = contractInstance.methods.getBalance().call();

This function will call your contract and get the value you are returning.

If you want to know the real contract value you should do it the way @Taha described it

var contract_balance = web3.eth.getBalance("your contract address");

If you have a balance variable in your contract which is public you can also call it this way.

var balanceVar =  contractInstance.balance.call();

When your variable are declared as public the compiler is creating getter automatically for you.

I hope it helps, if you still have issues please add you solidity contract on github, because you had only pushed the fronted code. :wink:

2 Likes

1 Like

@Hristov
Welcome to the forum!

Amazing work, you can share your code for review as well :slight_smile:

2 Likes