Programming Project - Phase 1

That is correct :slight_smile:

1 Like

Here is my version:

Sorry for the shaky hands, I didn’t find a free screen recorder that doesn’t leave a watermark. :smile:

Here is the Github: https://github.com/MarcisB8/Coinflip

There is also a night mode which I forgot to capture. I used the design and some functionality from my Javascript project, as well as the Ownable and Destroyable functions from PeopleProject.
Some lines in the code are sneaked from @Paultier.

Two problems I have noticed so far:

  1. As I tried to show in the video, all my functions create two transactions in MetaMask and they both can be executed. I couldn’t figure out why.
  2. Offtopic - the numbers in the table were meant to be time of the bet. After recording and uploading I noticed that the format is messed up. Also, the new Date () should be called on every transaction, not just once when the page is loaded.

My contract doesn’t keep track of its own balance but queries the blockchain for the actual balance instead. Would it cause problems in real life?

1 Like

Hey @MarcisB well done :slight_smile:

The issue with the function placeBet() triggered twice is because you are importing main.js twice in your index.html.
Remove <script src="main.js"></script> <!-- Button functions --> is on the bottom of your index.html.

Regarding the date, use append.
$("#entries tbody").append($("<tr><td>"+ time +"</td><td>"+ win +"</td></tr>"));

Schermata 2020-09-16 alle 17.21.27

Also yes, it is perfectly ok to take the balance from the blockchain instead of using a variable.

Happy learning,
Dani

2 Likes

Here is my project:

I know I can extend to functionality, css and withdraw, but I want to do the oracle :slight_smile:

1 Like

I have issue with sending Ether to my Contract’s address:

EthQuery - RPC Error - Error: [ethjs-rpc] rpc error with payload {"id":6719510559920,"jsonrpc":"2.0","params":["0xf86e138572c917020082520894e9beb94d634f9b126898f8ca8dc23ecc4c298dc0888ac7230489e8000080822d45a08ebdd9f11f0c66207eba7544a3013fa5981e3c4bd18bf331af992f98126ffb38a0540b7745ceedad24fd4d381a8b433cfb7b96db57c145c98266728d3f0b819fe0"],"method":"eth_sendRawTransaction"} [object Object]

Shouldn’t I be able just send ether to any Ethereum address?

2 Likes

I have interesting issue with getting value from user and converting it to web3.utils type that I can use in metamask. Here is the code below:

function placeBet(){
  var bet = $("bet_input").val();
  //new BN(bet).toString();
  var bet_to_ether = { value: web3.utils.toBN(bet).toString()}

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

I bet there is a very simpe way to do it I just don’t kow yet about.

Hi,

I’m getting close, but experiencing some issues. The first one is that upon page loading, I get this error in the console:

main.js:6 Uncaught (in promise) ReferenceError: abi is not defined
    at main.js:6

Here is the link to my files:
https://drive.google.com/drive/folders/1qvklfND7iy5Ub0S3PasFUsE5u06cxX05?usp=sharing

Also, if there are any other major flaws you see as far as functionality, please let me know. I will continue to improve the look of the site, etc.

Thanks!

Randy

Hey @rostyslavdzhohola

What is your idea here?

 var bet_to_ether = { value: web3.utils.toBN(bet).toString()}

If your goal is to take the value from $("bet_input") and use it as input for your coinflip() function try just to: var bet_to_ether = web3.utils.toWei(bet,ether)
Documentation : https://web3js.readthedocs.io/en/v1.2.7/web3-utils.html

Also if $("bet_input") > bet_input is an html id, you should write $("#bet_input")

Happy learning,
Dani

2 Likes

Hello @Flaflaflohi

You are not importing abi.js in your index.html.
<script type="text/javascript" src="abi.js"></script>

Cheers,
Dani

1 Like

Thanks Dani-

Can you take another look and let me know why when I click the “flip” button, I am being taken to 404 page not found instead of calling the sendBet function?

Randy

Hey @Flaflaflohi ,

Please upload your project on GitHub and give me the link to your repo.
I will have a look and let you know :slight_smile:

Enjoy your weekend,
Dani

1 Like

Finally ready to share my work, have absolutely loved this project, had a lot of fun with it. Really wanted to push myself to get a decent front end user experience. I ended up creating a few games, I will post them and the deposit/withdrawal functionality gif below as separate files. I am about to update the files on my github account too. Looking forward to phase 2 to get some true randomness happening.

blockPunt - flipOut

This is game number 2 - a simple dice roll game.

This was probably the most challenging one of the 3, but also my favourite to create. Its a high card draw game, it requires 2 transactions though and with gas fees at their current prices, another solution would definitely be needed!! haha

And this shows the deposits and withdrawals. I also put in a little password protection for the owner page link as I was challenging myself to make this like a professional end product. Uniformity through the pages, friendly user interface that is not too busy (hopefully) and a fun dApp to play.

3 Likes

Everything is available on my renamed github project at the link below (except the truffle-tests which need to be redone as they are from my original contract which has been rewritten)

1 Like

Hey @cryptocrom

I am really happy to read that you had fun and ended up creating more games :slight_smile:
Now jump into the phase two, where you will use an oracle to get a randomly generated number for your smart contracts.
Post it once it’s done, I will be happy to take a look.

Well done,
Dani

1 Like

Here you go!

Hey @Flaflaflohi

Well done!
I have some suggestions for you to help improve your contract, check it out and let me know what you think.

  • A function to deposit Ether in the contract is missing.
    In order to pay a player that just won, your contract needs to have at least msg.value*2 before a user can play.
    You are initialising uint public contractBalance = 100000000000000000000; but that is not true because your contract does not have any fund when is deployed. I coded a function to return the contract balance which shows 0 indeed, as no Ether has been deposited.
function getContractBalance () public view returns (uint){
      return address(this).balance;
  }

Schermata 2020-09-21 alle 10.43.29

  • There is an issue related to how the payout is calculated.
    I am able to play the first game when there is no real balance in your contract, and if I lose, I am not able to play anymore due to this revert:

  • This is more a suggestion, smart contracts do not like strings. Strings are really expansive to manipulate, and the verifications of those is tedious.
    Change the @parameters of flipAndPlay with a boolean or uint, would make your life much easier and the contract more performant.
    I am able to call the function flipAndPlay by sending “hello” as choice.

Correct those flows and let me know, I will be happy to give it a look once done.
If you need help I am here.

Happy learning,
Dani

2 Likes

Thank you for pointing out the Html ID I had forgotten.
I did try that version, and it doesn’t work. I get this error:

main.js:26 Uncaught ReferenceError: ether is not defined

My main objective is to pass the amount of bet data to metamask for execution. Every time I have tried, I’ve gotten some type of convertion error.

Hi @rostyslavdzhohola

I wrote an example for you:

var Web3 = require('web3');

const weiValue = Web3.utils.toWei('1', 'ether');
console.log('From ether to wei ' + weiValue);

const etherValue = Web3.utils.fromWei('1000000000000000000', 'ether');
console.log('From wei to ether ' + etherValue);

Schermata 2020-09-22 alle 10.44.23

Give it a try,
Dani

2 Likes

Thanks Dani! Yes, these are items that I meant to address, and I appreciate your help with that. My main concern currently is that when I click the “flip” button on the app, it doesn’t seem to call the flipAndPay function, but instead takes me to a “404 not found” page. Can you see why this is happening?