Programming Project - Phase 1

@gabba I’m very thankful to be here <3 Even this phase 1 gave me a few headaches. Some look now as very easy and silly details :):slight_smile: But, in the end, it was so awesome to see this first phase running <3 Programming feels so gooood <3 I’m falling in love with Smart Contract programming, more and more. My dream now is to be one day 100% in Crypto!! I feel very lucky to be part of the Academy!! This is the University of the future!!

1 Like

Well, after many distractions, including completing a number of other academy courses, and finally beating typescript into submission so I could use Angular… here is my phase 1 coin flip project!

Would be great to polish it up for phase 2, but I’m just happy I finally got typescript and web3 playing nice together. Not as pretty as some I’ve seen the comments here, but it’s got good bones :sunglasses:

Imgur

Hi @mayjer

Well done using angular :+1: :partying_face:, your smart contract code is correct, but i wasn’t able to test the front i had an issue with the enable function

chunk {main} main.js, main.js.map (main) 161 kB [initial] [rendered]
Time: 285ms
ℹ 「wdm」: Compiled successfully.
    
    ERROR in src/app/coin-flip-bet.service.ts(28,28): error TS2339: Property 'ethereum' does not exist on type 'Window'.

How did you solve it locally ?

1 Like

Thanks, it was a quite a battle :crazy_face:

Despite the error message it runs anyway!

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
i 「wdm」: Failed to compile.

I’ll update the project readme, but to get it running locally you’ll need to do the following:

  1. Deploy the smart contracts in Truffle. Import an account from Gnache into MetaMask.
  2. Copy the PlaceCoinFlipBet contract address into the CoinFlipBetService in /src/coin-flip-bet-service.ts
    • private readonly contractAddress = '0x...';
    • I’ll be looking into how to move this into a config file for phase 2 (Angular makes this more difficult)… or is there some way to query it at runtime?
  3. Start app with npm start or ng serve
  4. Open http://localhost:4200 in the browser

Also note:

  • The smart contract tests should pass when run via truffle console
  • I haven’t added front end tests on the angular side so those will not run
  • If you want a peek at my phase 2 progress check out the phase2 branch. I pushed a commit yesterday that updates the contract model to support storing player balances and added UI functionality to handle deposit/withdraw
1 Like

So…I took my sweet time i have to admit…since this is my first ever programming anything i played around with some stuff to make it look a bit different and to get some html,css action too.

i made a screen recording to have a look to it moving: https://youtu.be/jT2xUdTQmzg

here a coup of screenshots:

metamask still throw an error that i can’t figure out in the function “delete player”:

Is to say that without some help of the O.G.(original @gabba ) i wouldn’t have been able to get to this point.

P.S. i had quite some problem with ganache aswell…i cant look into the events and transaction because it crashes (or goes white and never come back since i reboot the program) when i try to get specific informations(like i try to look into a specific contract call or event) this is quite annoyng because i am never sure if it does involve other problems(but for now it does not seems like making my dapp not work) here is a screenshot of that:


seems i am not the onli one having this problem thou, but i did not see a solution for now.

1 Like

Hi @Gab

Congratz, i love the design of your Dapp, a lot of improvement since the last time i look at it :partying_face: :ok_hand: great job really !

2 small thing you can change in your contract.

You don’t need to have a uint in your constructor because you are not using it

constructor (uint)

Check if the player age is greater than zero when you are creating it

      function createPlayer(string memory name, uint age, string memory email, uint pin,uint repeatPin) public payable costs(1 ether){
              require(age < 100, "Age needs to be below 100");
              require(pin == repeatPin, "repeatPin not equal");
              require(msg.value >= 1 ether);

For ganache and events use ganache-cli instead, i also have a lot of issues with the GUI and i didn’t found a suitable solution for now. I can’t wait to see your Project 201 :wink:

2 Likes

thanks for the review really much!

shit… :sweat_smile:

last thing… do you know why this is happening?

I tried and it was working for me :man_shrugging:

1 Like

Hi @gabba
I’m having trouble getting started. I don’t know how to get the funds transferred back to the user when they win the coin flip

import "./Ownable.sol";
pragma solidity 0.5.12;

contract CoinFlip is Ownable{

  struct Coin {
    bool guess;
}

  event Result (string msg, address player, uint amountBet);

  uint public balance;



  function FlipCoin () public payable returns(uint) {
    
    Coin memory newCoin;
    
    
    if((now % 2) == 1) {
      newCoin.guess = true;
        emit  Result ("Congratulations You Won", msg.sender, msg.value);
        msg.sender.transfer(msg.value);
    } else {
        newCoin.guess = false;
          emit  Result ("Sorry You Lost, Better Luck Next Time", msg.sender, msg.value);
    }
    
   
  }

Hi @mjwatson10

Are you testing your contract on remix ?
You are missing a closing } but i guess it’s just an error when copying your contract here.
Your code is correct, this function

msg.sender.transfer(msg.value);

Will send to msg.sender the value of msg.value.
What is the issue here ?

@gabba, I am using remix to test it, I am having trouble seeing the players balance. I was able see the contract balance increase and decrease depending on whether or not the player won.

  function getContractBalance() public view returns(uint){
      return address(this).balance; 
  }



  function FlipCoin (uint) public payable returns(string memory, address) {
      
    Coin memory newCoin;
    
    
    if((now % 2) == 1) {
      newCoin.guess = true;
        
        msg.sender.transfer(msg.value * 2);
        
        return ("Congratualtions, You Won!", msg.sender);
    } else {
        newCoin.guess = false;

        return ("Sorry You Lost, Better Luck Next Time", msg.sender);
    }
    
 
   
  }

}

Hey, everyone!

I finally finished phase one. The UI isn’t great; however, I’m going to learn React in conjunction with phase two of this course. A big shoutout to everyone who already posted their questions in this forum. I learned so much!

And screenshots:

@gabba would i need to make a createPlayer function prior to creating a getPlayerBalance, or are those not necessary?

Hi, everybody
I almost finished phase one, but I am having some trouble with catching events. More specifically, I don’t get any return values from my “Bet” event.

function inputData(){
  var value = valueInput.getRawValue();
  var side = getSelectedSide().id;

  let config = {
    value: web3.utils.toWei(String(value), "ether")
  }

  contractInstance.methods.play(String(side)).send(config)
  .on("receipt", function(receipt){
    console.log(receipt);
  })
}

And here is my contract code
https://github.com/antonrom1/DCoinFlip/blob/master/contracts/CoinFlip.sol

Hi @mjwatson10

The ETH is sent directly to your wallet when you are using transfer. If you want to see the player balance add a user mapping and increase the value of this variable or decrease it regarding if he win or loose in flipcoin function.
Then create a new function were you will use transfert you can call it withdraw.
If you want to see your contract balance add a variable balance or check the contract balance this way

address(this).balance
1 Like

Hello,
I think I’m ready with phase one. It seemed simple.
It took a lot of effort, but I learned a lot.
video: https://www.youtube.com/watch?v=WqBO-er3r8I
code : https://drive.google.com/drive/folders/1b9vu6jwGF5p9n7-ZYDfsV38pkLtNFUhF?usp=sharing

1 Like

Hi @frivolous

Good job, try to create a github repo to share your code for the project 201 :slight_smile:

1 Like

Hi guys, I have a problem. I’m currently working on the coinflip dApp and I could send some data to the contract but for some reason I can’t send it anymore. I get the following error when I click on the Metamask “send” button:

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

Has anybody seen this error before? I read on google that some people reinstalled metamask to fix it but I guess there should be a better way. Hope somebody can help.
Thank you

Regards, Martijn

I’ve “fixed” the error by resetting my metaMask account: basically clearing my transaction history. (Can do it at the settings).
Still don’t know what it is…

Hi @Martijncvv

A lot of people were having the same issue, it could because you are switching to an other account an the inject account is not detected. You can try this:

Or on the current version by listening to an event

window.ethereum.on('accountsChanged', function (accounts) {
  // Time to reload your interface with accounts[0]!
})
2 Likes