Programming Project - Phase 2

Hi @Illidan7

Try a truffle deploy reset

truffle deploy --network ropsten --reset
1 Like

Hello,

I tried that. Still getting the same error. Not sure what the issue is

Hello @Illidan7

It seems to be an error in your deployment file what is the name of your Smart contract .sol file
Can you share the migrations file related to this file ?

Hi Filip ,gabba and everyone,
At last … waiting for your comments
Thanks


1 Like

Congrstulations, nice job. I’ll make sure to try it out. :+1:


Not the best has a lot more to be done, but covers the interesting parts like the oracle, retrieving the balance of the contract, actually playing with bets, locking bets while transaction (by transaction I mean the entire lifecycle from placing the bet to getting to know whether you won or lost) is ongoing (perhaps if I work more on it, even lock the buttons per user instead of for the current page).
1 Like

Having an issue testing the provable api my function fails saying

transact to Flip.flip errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

I have a balance in my contract and testing on remix using the standard import on github error seems to be on the call to provable_newRandomDSQuery no idea why its telling me to make it payable

function flip(uint call) public payable {
// call either = 1 or 0
        require(msg.value >= 100000000000000000,"the minimum bet is 100 finney");
        require(contractBalance >= 100000000000000000,"not enough funds in contract");

        uint value = msg.value;
        contractBalance += value;
        
       uint256 QUERY_EXECUTION_DELAY = 0;
       uint256 GAS_FOR_CALLBACK = 200000;
       Game memory newGame;
       bytes32 queryId = provable_newRandomDSQuery(QUERY_EXECUTION_DELAY,NUM_RANDOM_BYTES_REQUESTED,GAS_FOR_CALLBACK);
       newGame.player = msg.sender;
       newGame.bet = value;
       newGame.status = false;
       newGame.playerCalled = call;
       gamePool[queryId] = newGame;
       emit LogNewProvableQuery("Provable query was sent, standing by for answer.");
        
    }

Hello sir, could you send me github repository I have a feeling you have issues with your __callback function as this seems okay.

Hey thanks for the confirmation figured it out i needed to change remix environment to injected web3 with metamask on ropsten network.

1 Like

Hi Filip,

I sent my code (see above).

It allows the player to have a balance in the contract and to decide when to withdraw it (while leaving the owner the ability to withdraw only amounts that are net from obligations to players).
It shows the player his balance after flipping and gives the owner the ability to see the total contract account balance and his net part of it. seems to work smoothly, however,
I am sure that a lot of things can be done better, and because I started to code only through 'taking courses in
Ivan on Tech Academy" I will be more than happy to get some feedback and
suggestions for improvement.

It is amazing how much I learned in the courses in this academy (from scratch, even though my experience as a financial advisor /modeling complicated models on excel may help).

Really thank you. grate course.

What you recommend as the next step (in order to become good enough to start coding professionally smart contracts (even as a beginner).

Thanks
also to gabba and all the others :slight_smile: )

Hi @Guy
Sorry i have been really busy this week working on the chat.
I ll try to test and deploy your project this weekend if @ivga80 don’t .
You can start to look at the smart contract security course you will learn a lot. This course is great.
:v:

Hi @Guy

I played a bit with your smart contract this morning :slight_smile:

Well done :+1: but there is still few things you can improve.

The event listener works perfectly, a lot of people have issue to implement it but yours works fine.

The obligation variable is a really good idea.

The reset function is a good idea in case there is a callback issue but i think it should be only owner, the owner should pass the user address. Because if the user can unlock itself this security precaution doesn’t make sense anymore.

Regarding the frontend there is few function not implemented:
numberOfGames() / only in solidity
playerBalance() / An important one ! i had to call it from the console to display the value in the screen

There is an issue with your variable
newBet.creator
because it s global and set on the last flip, so if you play and i play after you you ll not be able to withdraw because

function toTransferBet( ) public returns(uint) {
     require  (msg.sender == newBet.creator);

The value of newBet.creator will be my address, so you ll have to play again to be able to withdraw.

The ownerObligoToPlayers/ accountBalance variables have an issue caused by newBet which is global.

If one player Flip the value of newBet.betAmount will be let’s say 1ETH.

This code is updated only when the callback is called by the provableApi.

  if(randomNumber==1){
  playerBalance[newBet.creator] += (newBet.betAmount)*2;
  ownerObligoToPlayers += (newBet.betAmount)*2;
  }

Now let’s say an other player play before this call with 0,1ETH so
newBet.betAmount = 0,1

When the callback will be resolved the first player balance will be incorrect, he will only win 0.2 ETH not fair hum ! :yum:

Also you are substracting
acountBalance = (address(this).balance-ownerObligoToPlayers);
twice which lead to a wrong state of your variable.

As you can see the contract balance is 2.374 but the owner balance 1.1579.
It should be zero because you own me 2.8 (or minus eheh but i ll explain this after )

So the last issue is your are not deducting the price for the provable api call.
Look on their website their is a function

provable_getPrice("RANDOM")

Double check the value of random i forgot if it s capital letters or not , you can find it in this topic i guess.

So now you own me 2.8 eth but i can’t get it back because your contract only have 2.374 left. So i’m gonna call Fake satoshi and sue you :money_mouth_face:!

I know from your posts that you just start to code with iot course and it’s amazing that you are already able to build a Dapp with a frontend, a backend on the blockchain, using asynchronous call and all this stuff. This is a lot of new things to learn. So well done to make it until the end of this project. :partying_face:

2 Likes

Hi. really thank you.
For the compliments (…yes, I assume that I have the required obsessiveness…) and for the educated improvements and corrections. Of course, I will go over it (and the security course) and improve it as necessary.
:slight_smile:

1 Like

hi sorry, i solved it, (some deployment problem…)… thanks

1 Like

Hand in for Phase 2

1 Like

Hi gabba ,Filip and everyone

Here is my upgraded code, taking into account most of gabba remarks. please review.
Thanks again for everything

https://github.com/GuyBarkay/CoinFlipProvable

Hi Gabba
I posted my corrected code below (after changes I made as result of your suggested educated corrections)
few issues for your attention:

  • gas callback costs are taking into consideration indirectly (through calling address(this).balance after callback was made) when updating accountBalance/owner net balance. i made it clearer now. hope it’s ok.
  • I think the problem with the balances was corrected (please review, I added assert function in the flip code instead of required which did not works before. I test it as much as I could).
  • ownerObligoToPlayers variable is global in purpose - as it summarizes the total obligations to all players (which is equal to the sum of their winnings bets minus their drawdowns ).
  • I didn’t understand your comment regarding the reset button but I change it to onlyOwner ( what did you mean by suggesting" the owner should pass the user address").

https://github.com/GuyBarkay/CoinFlipProvable
If possible, please review. I am learning (and enjoying) so much …
Thanks again :slight_smile:

2 Likes

Hi @Guy

Great i will look at it this weekend and come back to you :slight_smile:
Did you start the eth security course btw ?

1 Like

Yes, I did. I even used some lines from safe math.

1 Like

Here is my dapp on github, It was a great project. I have learned many thing.
Thank you Flip and Ivan.

1 Like