Programming Project - Phase 1

That is really sharp looking!

Really cool animation!

1 Like

Hi Filip,
Here is my version of the CoinFlip DApp (first stage).
I would appreciate if you could check it out at https://github.com/GuyBarkay/CoinFlip.

Any tips regarding how to improve my beginners DApp code, are more than welcomeā€¦:slight_smile:

It is really a great course (and a very demanding oneā€¦). I feel that I learned so much that itā€™s unbelievableā€¦

Thanks

1 Like

Im also with issues to payā€¦did you solve it?

this is my code

function flip(uint bet) public payable returns(bool){

        require((bet == 0 || bet == 1));  // only 2 posible results 

        require(msg.value >= 0.001 ether);  //minimum arbitrarian bet

        require(balance > msg.value * 2);   //to be sure to be able to pay if user wins

        uint flip_result = pseudo_random();

        bool user_wins;

        

        if(flip_result == bet){ // win! should pay

            //pay_to_user();

            uint reward = msg.value * 2;

            balance = balance - reward;

            msg.sender.transfer(msg.value * 2);

            user_wins = true; // for UI

        } else { // lose, try next time

            //just enjoy

            balance = balance + msg.value;

            user_wins = false;

        }

        emit flipDone(user_wins);

        return user_wins;

    }

also made this function to testā€¦ but nothing

function test_send() public payable returns(address){
        require(msg.value > 0);   //to test transfer
        msg.sender.transfer(msg.value);
        return msg.sender;
   }
type or paste code here

Hi @Eric
I tried your contract on remix and it works.

pragma solidity ^0.5.10;

contract DappEric{
    uint256 public balance;
    address owner;
    event flipDone(bool);

    constructor()public payable{
        balance = msg.value;
        owner = msg.sender;
    }

function pseudo_random() public pure returns(uint){
    return 1;
}

function flip(uint bet) public payable returns(bool){

        require((bet == 0 || bet == 1), "Bet should be one or two");  // only 2 posible results 
        require(msg.value >= 0.001 ether, "Bet should be more than 0.001");  //minimum arbitrarian bet
        require(balance > msg.value * 2, "Balance of the contract should be twice the msg value ");   //to be sure to be able to pay if user wins
        uint flip_result = pseudo_random();
        bool user_wins;

        if(flip_result == bet){ // win! should pay
            //pay_to_user();
            uint reward = msg.value * 2;
            balance = balance - reward;
            msg.sender.transfer(msg.value * 2);
            user_wins = true; // for UI
        } else { // lose, try next time
            //just enjoy
            balance = balance + msg.value;
            user_wins = false;
        }

        emit flipDone(user_wins);
        return user_wins;
    }    
}

I deployed you contract with 10 eth, the pseudo rand function return always 1.
I bet on the 1 and send 1 eth in the flip transaction.
I also add messages in your require to have more explicite errors
Try on remix.

Oh, I will do that. Butā€¦it not suposed to work with ganache, browser, and metamask? ā€¦

I just tested it on remix because it was faster for me to test, but if it works on remix, it should works on ganache, browser and metamask.
Let me know if it works for you on remix first, maybe the issue is not from your smart contract but from something else in your configuration.

@filip I have finished the phase one project. I also liked the format of this project and learned a lot. I did hit a few changes that were frustrating. Like some others above from time to time an RPC error is thrown though sometimes the transaction still completes. Did anyone find a repeatable cause for this issue? I also ran into an issue where a transaction got stuck and nothing worked until I reset the accounts in MetaMask.

I am really enjoying this course - thank you!

Project Repository

1 Like

Hi,
in the withdrawal function, I used the same structure which was used in the people.sol (checks effects interaction pattern) in order to eliminate re- entrency fallback risks as follows:

function withdrawAll() public onlyOwner returns(uint) {
uint toTransfer = acountBalance;
acountBalance = 0;
msg.sender.transfer(toTransfer);
return toTransfer;

However, it is not clear to me

  • why do we need to return (the last line)?
  • Is it really necessary to keep the checks effects interact pattern? (as transfer() should be safe against re-entrancy anyway)?

Thanks

The return will only be needed if you want to check the value in remix or in your frontend itā€™s not really needed.
There was a debate not long ago about saying the transfert function was not safe in case the gas cost of other op code change. So itā€™s an additional precaution.
This is an interesting post about it.

(The title is a bit a Clickbait donā€™t worry about transfert for what we are doing :slight_smile: )
https://diligence.consensys.net/blog/2019/09/stop-using-soliditys-transfer-now/

Woo really nice implementation :+1:
Using a storage contract is a good idea !
Even the proxy feature is great, well done.
Just by curiosity what is your programming background @xactant ?

@gabba Thank you! I have been a profession software engineer since 1997 but started as a hobbiest in highschool in 1984. Currently I am a Java Dev Lead for the company I am currently with. My experience is mainly Enterprise systems integration but I have also done a good share of Web UI (js, angular, etc) and hardware device drivers (mainly blue tooth). Trying to break into defi now.

How about you?

I have definitely less skin in the game than you ahah i m coding since 2014 i started with C. Currently software engineer too i m working on a C network library since 2 years, but i had C++ , reactjs and rails experience before.

I m also interested into Defi, we have a project to implement a smart contract in top of compound right now with few students if you want to share knowledge have a look at this post from Bam:

1 Like

Submitting my phase 1 project.

Below is the download link of my files:
https://we.tl/t-qTPq9tVoGh

Need to say, that implementing process that requires simultaneous writing functions in solidity and testing them with truffle was the biggest thing I learned in this course so far, It makes development quicker and much more secure.

During the development of this project, I had a problem when calling transactions using metamask (rpc error with payload), At first, I used accounts[0] (owner address), and it was throwing this error time to time. After I sit on this project day later, every transaction was failed. After switching to another account, I managed to get it running again with errors throwing back from time to time. Did anyone had this issue? Any ideas on how to fix it? When using truffle, never had errors, and transactions were always out.

2 Likes

Phase 1 complete https://drive.google.com/open?id=1E5xUMbFLIAV2pOBIDgLmIO7htDCjPBKz
just a note that the metamask dialog doesnā€™t show up in my recording but is present.

1 Like
1 Like

Hi everyone! I finished Phase 1, but I couldnā€™t get my dapp listening to the bet Solidity event, so I can display a box saying You won 2*betAmout or you lost your betAmount. @filip , how is this done, I only found outdated documentation and an old video of yours from 2018 on this. Here is the link.

Hi @Dominik_Clemente
I just checked your project it could be nice to implement the other functions of your smart contract in your web application. Because as you are not sending fund when the contract is deployed i had to send eth via truffle console. Even for getBalance it could be useful for the player to check if they have something to win by playing :slight_smile:

For your event you can catch it this way

async function bet(){
    var betAmount = $("#name_input").val();

    var config = {
        value: web3.utils.toWei(betAmount.toString(), "ether"),
        gas: 100000
    }
    try {
    let res = await contractInstance.methods.flip().send(config);
        try{
            await contractInstance.getPastEvents(['bet'], {fromBlock: 'latest', toBlock: 'latest'},
            async (err, events) => {
                console.log(events[0].returnValues);
            });            
        }catch(err){
        console.log(err)
        }
    }catch(err){
        console.log(err)
    }
}

It seems their is an issue with the last version of ganache Gui it doesnā€™t work for me too i canā€™t get the event. But i got them using ganache-cli so if you have any issues with this code try to run ganache-cli.

1 Like

Hi @gabba, I tried your recommendation, however when I switch to ganache-cli and send the transaction I get a MetaMask error MetaMask - RPC Error: Error: [ethjs-rpc] rpc error with payload {"id":9541774025292,"jsonrpc":"2.0","params":["0xf877808504a817c800830186a094d2ac41e712f94f1b3484c04568ac47d3304a85618829a2241af62c000084cde4efa98602e1b4904f8ba0461575789f079b1385b7e8bd8616f7917d6b1006d8869049d704287477426023a028746270940a9d0dc0fcf64b9abafde8af2745df5ddc28e2e3884ff5e9cf9cb1"],"method":"eth_sendRawTransaction"} [object Object]

I did check and corrected the abi and the deployment address, however it still had the same error. How can I fix it? Thanks for your help

Hi again :slight_smile:
Check the port on metamask because ganache-cli by default run on port 8545 also check if you have imported the right account.
If it doesnā€™t work try to logout from metamask, restart your server and reload the page. Itā€™s a common issue with metamask when you are switching to an other network.
How did you deployed it with remix or truffle migrate ?

1 Like