Programming Project - Phase 1

Hi @dan-i

I see now that I can simplify this contract but it is pasted below.
My initial thought was to have contractFunds.balance and contractFunds.availableBalance and was meant to be used as full balance vs available to bet with.

Ill rewrite it.
However, did you see why my function didn´t output to my site? Didnt output to console either.

Hey @Spartak

I need some context to help you.
When are you getting the error? How can I reproduce it?
Would you please provide these info and a github repo with your project?

@tomas819

The react course is really well structured. Feel free to dive into it as soon as you want.
Also post the project phase two when ready, I will take a look :slight_smile:

Happy learning,
Dani

Hey @jbo

Still need the info asked yesterday :slight_smile:

Hey @jbo

`return contractFunds.availableBalance / 4;`

Why are you using a dot notation? Can you please post the whole smart contract?

Thanks!
Dani

Let me know :slight_smile:
Dani

done, info here Programming Project - Phase 1

https://github.com/Spartak91/Project-coinFlip

when i try to interact with dapp on local host thru ganache network, when i use place bet function , i get this error " Couldn’t decode uint256 from ABI: 0x"

could you check the code pls?

I have my repo on Github: https://github.com/CatalystJesal/CoinFlip

Would appreciate feedback, especially with testing that was carried out. I realised I need to do more work on my Bootstrap skills as I’m not very experienced with it and it took me a while to do the front-end stuff since my html skills have declined since the last time I used it which was almost two years ago.

Thanks!

EDIT: I forgot to push the stake amount to the mapping and delete after the outcome. Something I will come back to changing later on.

1 Like

https://github.com/pragmaGeorge/coinflip

  • Contract is funded with 20 eth at migration
  • Tested for not being able to bet amount 0 or bet more than half of contract balance
  • to do: a nice popup that opens when win/loss event is triggered
2 Likes

@dan-i programming is really strange XD , now everything works and i didn’ even change a thing. Here are screenshots:

Thanks for patience and help!

1 Like

@dan-i can I ask for some feedback on the code, if there is something I could do better ?

You are always welcome :slight_smile:
Ready to smash phase two !

Hey @Jesal_Patel

The code seems to be well written, I just have this to point out as I did for other students, require(balance >= msg.value, "contract has insufficient funds for a payout in the event of a win"); If the user wins the game you pay msg.value*2 so this requirement has to be adjusted :slight_smile:

Other than that the code is clean, go to phase two where stuff gets interesting!

Happy coding,
Dani

1 Like

Hi @filip

I have a question.
When I am using truffle console to check the return of the random function, it returns <BN: 0>.
I was searching to convert this into a normal integer but could not find it.
Returning <BN: 2> also happens when I try to return the amount bet.
I tried .toNumber() but did not work…
Can I have advice?
Thank you!

Hey Dani,

Thanks a lot for reviewing my code! Doing the second phase of the project and the push/pull will be adjusted. :slight_smile:

1 Like

Hey @Jun_0113

Try to convert the BN to string for example.

use .toString()

Happy learning,
Dani

Looking forward to working on this !!! Big steps ahead but im eager to tackle it !!! :). @thecil @dan-i @Mauro :dancer:

2 Likes

hello,

I deployed successfully my contract with ganache and metamask and everything worked fine.
After that I just changed a little the contract in order to create a player balance (rather than send the money to the winner).

At this point when I migrate the contract i get this error:

Deploying 'coin3'
   -----------------

Error:  *** Deployment Failed ***

"coin3" hit a require or revert statement somewhere in its constructor. Try:
   * Verifying that your constructor params satisfy all require conditions.
   * Adding reason strings to your require statements.

    at C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\deployer\src\deployment.js:364:1    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at Migration._deploy (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\migration.js:70:1)
    at Migration._load (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\migration.js:57:1)
    at Migration.run (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\migration.js:167:1)
    at Object.runMigrations (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:148:1)
    at Object.runFrom (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:110:1)
    at Object.runAll (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:114:1)
    at Object.run (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:79:1)
    at runMigrations (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:253:1)
    at C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:218:1
truffle(ganache)>

this is the contract (is working fine in remix)

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

 contract coin3 is Ownable{

    struct player {
      uint amount;                          // amount the player want to bet in ether
      uint bet;                             // what the player want to bet: 0 or 1
      string message;                       // message after flip: WIN or LOSE
      uint result;                                     // result of the coin flip return from Provable
      uint playerBalance;
    }

    uint public balance;                                    // updated balance of the contract
    uint public minBet;                                     // minimum bet set by the owner                                 
    
       
    modifier costs(uint cost){
        require(msg.value >= cost);
        _;
    }

// owner send money to the contract at the moment of deployment    
    constructor() public payable{
    require(msg.value >= 9*1000000000000000000);
       balance += msg.value;
}
    
    mapping (address => player) private players;  // link player to his address
    
    
    
    
    
// player set his bet (0 or 1), his bet amount and send that amount to the contract
    function setBet(uint amount, uint bet) public payable costs( (players[msg.sender].amount)*1000000000000000000 ){
        
      
        
        
        
        player memory newPlayer;
        newPlayer.amount = amount;
        newPlayer.bet = bet;
        
        newPlayer.playerBalance = players[msg.sender].playerBalance;    
        
        insertPlayer(newPlayer);   // players[msg.sender] = newPlayer
        
        require((players[msg.sender].amount)*1000000000000000000 <= balance/2, "Bet over contract funds");
        balance += msg.value;
        
        
 
    
      if(random() == (players[msg.sender].bet)){
        balance = balance - (players[msg.sender].amount)*1000000000000000000*2;
       players[msg.sender].playerBalance = players[msg.sender].playerBalance + (players[msg.sender].amount)*1000000000000000000*2;
       
       players[msg.sender].message = "WIN";
       players[msg.sender].result = random();
         }
        else{
        
        players[msg.sender].message = "LOOSE";
       players[msg.sender].result = random();    
        }
        
       
        } 
        
      
function random() public view returns(uint){
    return now % 2;
    } 
        
  
   function getResult() public view returns(uint amount, uint bet, string memory message, uint result, uint playerBalance){
        address creator = msg.sender; 
        
        return (players[creator].amount, players[creator].bet, players[creator].message, players[creator].result, players[creator].playerBalance);
    }

// only owner can withdraw money   
    function withdrawFunds(uint withdraw) public onlyOwner {
     
       balance = balance - withdraw*1000000000000000000;
       msg.sender.transfer(withdraw*1000000000000000000);

   }
   
       function withdrawFundsPlayer(uint withdraw) public {
       require(withdraw<players[msg.sender].playerBalance);
     
       players[msg.sender].playerBalance = players[msg.sender].playerBalance - (withdraw*1000000000000000000);           
       msg.sender.transfer(withdraw*1000000000000000000);

   }

  function insertPlayer(player memory newPlayer) private {
        address creator = msg.sender; 
        players[creator] = newPlayer;  
  }



}

Hi @enrico

The error message is related to a revert statement hit in the constructor.
By checking your contract I see that you have this require in your constructor

  constructor() public payable{
    require(msg.value >= 9*1000000000000000000);

Are you sending 9 eth when deploying the contract?

Cheers,
Dani

Waoow this is amazing Im going to enjoy this Phase. Just completed my environment and building the foundations Really looking forward to HOPEFULLY getting it finished.

Back at it tomorrow.

:smiley:

1 Like

Congratz Rob, keep going :muscle: