Programming Project - Phase 1

Enter bet amount ($1000) and click on Place Bet ::

1 Like

Did I win or lose (click on Get Bet Result ) :

Amount lost (click on Get Bet Amount ) ::

1 Like

Hi @saoirse
Yes this is correct, you can add a require at the beginning of this function to make sure the user balance is not empty. Then check the balance in your frontend after this call with a getUserBalance method.

1 Like

Hi @JesusIsLord
What is the unit used here ?
Are you converting dollar to ETH ? If yes how are you getting the current Eth value in dollar ?

Hello @filip, and everyone, I’m facing some problems trying out giving an option to the player to choose between 2 possibilities(like red or black at the roulette, or head or tail).

i checked the code of @Laserbach that was working on a similar concept, it gave me a slight idea but i have to admit i lost track of all the things i couldnt understand in his code.

i past here some code sketch …i know is not syntactically correct, but i hope is gonna give you an idea without making you laugh too much​:joy::face_vomiting:. every suggestion is accepted and if you see that i really did not understand something please let me know, it is completely possible.

pragma solidity 0.5.12;
 
contract HelloWorld{
                   
            struct Bet {
              //defined betting hodl (1) or sodl (2)
              uint optionChoosen;
              //defined running draw()function.
              bool win;
              //defined in makeBet from draw()function
              uint betAmount;
              //defined in makeBet msg.sender
              address player;
            }
            
            
            mapping(address => uint) private playerBalance;
            
                
            uint public balance;
            
            
            function  makeBet(uint optionBid) payable public {
              // check if contract has enough balance
              require(msg.value <= balance);
              // set minimum bettable amount
              require(msg.value >= 0.1 ether);
            
              //This creates a newBet based on Bet struct 
              Bet memory newBet;
              newBet.optionChoosen = optionBid;
              newBet.betAmount = msg.value;
              newBet.player = msg.sender;
            
              draw(optionBid);
            
            }
            
            function draw(uint optionChoosen) private {
            // Generate random data
              uint randomResult = now % 2;
            //if win true pay double to player
              if(randomResult == optionChoosen){
                Bet.win = true;
                balance = balance - Bet.betAmount;
                Bet.playerBalance += Bet.betAmount *2;
              }
              //if win false pay betAmount to contract
              else{
                Bet.win = false;
                balance = balance + Bet.betAmount;
              }
              
            }
}

thanks again,:muscle:t3:
Gab

Hi @Gab
I moved this question to the “Project phase 1” topic i hope you don’t mind.
I think you should test this contract on remix it will help you.
First why are you using the memory keyword in your fuction makeBet ?
In your function draw you are assigning a value to Bet.playerBalance but this is not an element of your structure.
Where is your Bet structure initialized, where it should be ?

I think your should remove complex things in your contract, restart from the beginning and test every time you are adding code in https://remix.ethereum.org/ if your code is still working

i guess to keep it in memory because before in my structure was also string value types and then i forgot to change it

this is probably the only thing i understand f all of this

sorry i dont understand the question…

sincerely I’m pretty annoyed as now because i realize how little i understand. i use remix as possible but it really doesnt help me as much…
what do you mean with remove the complex parts?
i would also start again but i dont think is going to help so much to start a clean slate every 2 hours…

@Gab

You need to look again at memory and storage keyword definition, memory is used to declare a variable locally (so this variable will only exist inside your makeBet function) when you go out of it it doesn’t exist anymore.

If you want to modify a variable of type (Bet) which should be declare in your contract (but it’s not) you need to use storage, or you need to access directly to this variable.

Try to think about this exercise

https://forumtest.ivanontech.com/t/data-location-assignment/7348/104

See the struct Bet as a special type (in reality it’s more like an object but let’s keep it simple).
What you have done in your contract is :
Creating a new type (the Bet type) now you need to initialise an object of type Bet

For example you can do this after creating the structure, it will make your newBet object available everywhere in your contract

            struct Bet {
              //defined betting hodl (1) or sodl (2)
              uint optionChoosen;
              //defined running draw()function.
              bool win;
              //defined in makeBet from draw()function
              uint betAmount;
              //defined in makeBet msg.sender
              address player;
              
              uint playerBalance;
            }

            Bet public newBet;

Don’t worry about that there is a lot of information in those courses and the best way to understand it is to practice that’s why remix is a good way to test things.

I mean start with a simple function, you can start just with makeBet for example . When you are testing your contract in remix do you see the value of newBet.player being modified ?
You don’t need to call the draw function if the first function is not working yet.

You should see some errors with your contract when you are compiling and also when you are trying to use makeBet. Your require will not pass require(msg.value <= balance); because you have no way to send funds to your contract.

Add a payable constructor or add a function to send funds to your contract

            constructor() public payable{
                balance = msg.value;
            }
1 Like

Screenshots:

And video:

2 Likes

https://github.com/tjma2001/coinflip-dapp (phase 1)

Kept it simple in terms of design. :slight_smile:

2 Likes

Hello, Gabba :

I thought the purpose of this exercise was to simply generate a random number (0 - you lose : 1 - you win) and display the results in the dapp.

The way I wrote the dapp, the amount wagered was meaningless (just for display purposes).

Yes indeed :wink: i was just curious i though you had implemented an oracle function to get the current price.

Github repo: https://github.com/tito6666/flip-coin

2 Likes

Here is my first CoinFlip Dapp.
Works fine :slight_smile:


https://drive.google.com/file/d/1VxuoG9EjW7uSULmqtMGgTWxX_sTuoUMT/view
It works well, but I haven´t checked whether everything works without mistakes. For example, I have to add a require statement that you can only play when the jackpos has enough founds for playing the game. Of couse, you could also make a nicer design, but this takes too much time and I am not a good designer :wink:

2 Likes

Here is my project.The solidity part was fun and easy but the javascript part took me a while to get working completely. Guess I should take the javascript class next.

1 Like

Hello guys, i have on my app 2 buttons(a, b) to set a value(1 or 0) in a function but i dont want this function(bet) to execute at the click of the button, i just want this value to be stored, and used in that function(bet) execution at the click of another button(send bet). i am looking around without success for quite some time, maybe some of you could give me an help…

Hi @Gab
Something like

var value = NaN;
$( "#functionA" ).click(function() {
  value = 1;
});
$( "#functionB" ).click(function() {
  value = 0;
});
$( "#functionSendBet" ).click(function() {
  let amount = $("#amount").val();
  function(value, amount);
});

?

4 Likes

saint gabba :innocent::star_struck:I’m at work in this moment but at the first possibility ill try it out.

1 Like

Glenn_CostaRica
PROGRAMMING PROJECT - PHASE 1
in Google Drive (PowerPoint presentation with VIDEO):
GO: Glenn-CostaRica_IvanOnTech_EthereumProgamming201_CoinFlip-Project-Phase1

2 Likes

Great presentation @Glenn_CostaRica good job !

2 Likes