Stanley-Moukhametzianov / Coin-Flip

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Coin-Flip

  • App uses react for the front end with web3 to make calls to the contract.
  • This contract is deployed on the Rinkeby test network at the address: 0x27Ce51AC010796AC501F06411ad1d49d824cd1b3.
  • This app is designed to test a coin flip smart contract. :D

๐Ÿ“š General info

  • This app allows users to automatically bet on a coin flip against a smart contract. The users must bet less than or equal to the balance of the contract.

  • Note: The contract is deployed on the Rinkeby test network. All eth that is used on the site is test ether.

  • Once the bet is active users wait until a pop up appears displaying their coin flip. After, the page reloads and updates the balance on the page. You can then also see the last four wins in the table below the submit button.

๐Ÿ“ท Demo

Demo.mov

๐Ÿ’ป Smart Contract

  • Lottery contract that is used in the website.
// SPDX-License-Identifier: MIT
pragma solidity 0.4.20;

contract CoinFlip {
    address public owner;
    uint public numberGenerated;

    struct Winner {
        address account;
        uint winningNumber;
        uint amountWon;
    }

    Winner[] public pastWinners;

    function CoinFlip() public {
        owner = msg.sender;
    }
    function bet(uint color) public payable{
        // 1 is red and 2 is black
        require(color == 1 || color == 2);
        uint256 num = (block.number % 10) + 1; // This isn't secure
        if (int(num) >=6) 
            numberGenerated = 2;
        else 
            numberGenerated = 1;
        if(color == numberGenerated){
            msg.sender.transfer(msg.value * 2);

            pastWinners.push(Winner(msg.sender, numberGenerated,msg.value));
        }else{
            pastWinners.push(Winner(owner, numberGenerated, msg.value));
        }
        
    }
    function addFunds() public payable{   
    }
    function withdrawBalance() public {
        require(msg.sender == owner);
        owner.transfer(this.balance);
    }
    function getBalance() public view returns (uint){
        return this.balance;
    }
    function getWinnersCount() public view returns (uint) {
        return pastWinners.length;
    }

}

๐Ÿ“ License

  • This project is licensed under the terms of the MIT license.

โœ‰๏ธ Contact

About


Languages

Language:JavaScript 63.9%Language:CSS 36.1%