0xJayPi / dice-game-c3

Challenge 3 of the Speed Run Ethereum

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ— scaffold-eth | 🏰 BuidlGuidl

🚩 Challenge 3: 🎲 Dice Game

🎰 Randomness is tricky on a public deterministic blockchain. The block hash is the result proof-of-work (for now) and some builders use this as a weak form of randomness. This challenge will give you an example of a contract using block hash to create random numbers. This randomness is exploitable. Other, stronger forms of randomness include commit/reveal schemes, oracles, or VRF from Chainlink.

πŸ‘ One day soon, randomness will be built into the Ethereum protocol!

πŸ’¬ Dice Game is a contract that allows users to roll the dice to try and win the prize. If players roll either a 0, 1, or 2 they will win the current prize amount. The initial prize is 10% of the contract's balance, which starts out at .05 Eth.

🧀 Every time a player rolls the dice, they are required to send .002 Eth. 40 percent of this value is added to the current prize amount while the other 60 percent stays in the contract to fund future prizes. Once a prize is won, the new prize amount is set to 10% of the total balance of the DiceGame contract.

🧨 Your job is to attack the Dice Game contract! You will create a new contract that will predict the randomness ahead of time and only roll the dice when you're guaranteed to be a winner!

πŸ’¬ Meet other builders working on this challenge and get help in the Challenge 3 telegram!

=======

Checkpoint 0: πŸ“¦ install πŸ“š

Want a fresh cloud environment? Click this to open a gitpod workspace, then skip to Checkpoint 1 after the tasks are complete.

challenge-3-dice-game

Open in Gitpod

git clone https://github.com/scaffold-eth/scaffold-eth-challenges.git challenge-3-dice-game
cd challenge-3-dice-game
git checkout challenge-3-dice-game
yarn install

Checkpoint 1: πŸ”­ Environment πŸ“Ί

You'll have three terminals up for:

yarn chain   (hardhat backend)
yarn start   (react app frontend)
yarn deploy  (to compile, deploy, and publish your contracts to the frontend)

πŸ‘€ Visit your frontend at http://localhost:3000

πŸ‘©β€πŸ’» Rerun yarn deploy --reset whenever you want to deploy new contracts to the frontend.


<<<<<<< HEAD

🚩 Challenge 2: 🏡 Token Vendor

=======

Checkpoint 2: 🎲 Dice Game

πŸ” Inspect the code in the DiceGame.sol contract in packages/hardhat/contracts

πŸ”’ You will not be changing any code in the DiceGame.sol contract in this challenge. You will write your own contract to predict the outcome, then only roll the dice when it is favourable.

πŸ’Έ Grab some funds from the faucet and roll the dice a few times. Watch the balance of the DiceGame contract in the Debug tab. It increases on a failed roll and decreases by the prize amount on a successful roll.

image

challenge-3-dice-game

πŸ₯… Goals

  • Track the solidity code to find out how the DiceGame contract is generating random numbers.
  • Is it possible to predict the random number for any given roll?

<<<<<<< HEAD

🚩 Challenge 3: 🎲 Dice Game

🎰 Randomness is tricky on a public deterministic blockchain. In this challenge you will explore creating random numbers using block hash and how that may be exploitable. Attack the dice game with your own contract by predicting the randomness ahead of time to always roll a winner!

https://github.com/scaffold-eth/scaffold-eth-challenges/tree/challenge-3-dice-game


πŸŽ‰ Checkpoint: Eligible to join 🏰️ BuidlGuidl

The BuidlGuidl is a curated group of Ethereum builders creating products, prototypes, and tutorials to enrich the web3 ecosystem. A place to show off your builds and meet other builders. Start crafting your Web3 portfolio by submitting your DEX, Multisig or SVG NFT build.

https://buidlguidl.com/


βš–οΈ Build a DEX Challenge

πŸ’΅ Build an exchange that swaps ETH to tokens and tokens to ETH. πŸ’° This is possible because the smart contract holds reserves of both assets and has a price function based on the ratio of the reserves. Liquidity providers are issued a token that represents their share of the reserves and fees...

DEX Telegram Channel: https://t.me/+_NeUIJ664Tc1MzIx

https://github.com/scaffold-eth/scaffold-eth-challenges/tree/challenge-4-dex


πŸ“Ί State Channel Application Challenge

πŸ›£οΈ The Ethereum blockchain has great decentralization & security properties but these properties come at a price: transaction throughput is low, and transactions can be expensive. This makes many traditional web applications infeasible on a blockchain... or does it? State channels look to solve these problems by allowing participants to securely transact off-chain while keeping interaction with Ethereum Mainnet at a minimum.

State Channels Telegram Channel: https://t.me/+k0eUYngV2H0zYWUx

https://github.com/scaffold-eth/scaffold-eth-challenges/tree/challenge-9-state-channels


πŸ‘› Multisig Wallet Challenge

πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘§ Using a smart contract as a wallet we can secure assets by requiring multiple accounts to "vote" on transactions. The contract will keep track of transactions in an array of structs and owners will confirm or reject each one. Any transaction with enough confirmations can "execute".

Multisig Telegram Channel: https://t.me/+mkNNF_yHsK8yMTcx

https://github.com/scaffold-eth/scaffold-eth-challenges/tree/challenge-5-multi-sig


🎁 SVG NFT 🎫 Building Cohort Challenge

πŸ§™ Tinker around with cutting edge smart contracts that render SVGs in Solidity. 🧫 We quickly discovered that the render function needs to be public... πŸ€” This allows NFTs that own other NFTs to render their stash. Just wait until you see an Optimistic Loogie and a Fancy Loogie swimming around in the same Loogie Tank!

SVG NFT Telegram Channel: https://t.me/+J9PRea84c1U0Mzkx

https://github.com/scaffold-eth/scaffold-eth-challenges/tree/challenge-6-svg-nft


πŸ’¬ Support Chat

=======

Checkpoint 3: πŸ”‘ Rigged Contract

challenge-3-dice-game

Start by creating a receive() function in the RiggedRoll.sol contract to allow it to receive Eth. This will allow us to fund the RiggedRoll contract from the faucet which is required for our contract to call the rollTheDice() function.

Next add a riggedRoll() function. This function should predict the randomness of a roll, and if the outcome will be a winner, call rollTheDice() on the DiceGame contract.

πŸƒ Predict the outcome by generating your random numbers in the exact same way as the DiceGame contract.

πŸ“£ Reminder! Calling rollTheDice() will fail unless you send a message value of at least .002 Eth! Here is one example of how to send value with a function call.

πŸš€ To deploy your RiggedRoll contract, uncomment the appropriate lines in the 01_deploy_riggedRoll.js file in packages/hardhat/deploy

❓ If you're struggling to get the exact same random number as the DiceGame contract, try adding some console.log() statements in both contracts to help you track the values. These messages will appear in the Hardhat node terminal.

βš”οΈ Side Quest

  • Add a statement to require address(this).balance >= .002 ether in your riggedRoll function. This will help prevent calling the rollTheDice() function without enough value.
  • Uncomment the code in App.jsx to show a riggedRoll button and contract balance on the main UI tab. Now you can test your function without switching tabs.
  • Does your riggedRoll function only call rollTheDice() when it's going to be a winning roll? What happens when it does call rollTheDice()?

Checkpoint 4: πŸ’΅ Where's my money?!?

You have beaten the game, but where is your money? Since the RiggedRoll contract is the one calling rollTheDice(), that is where the prize money is being sent.

πŸ“₯ Create a withdraw(address _addr, uint256 _amount) function to allow you to send Eth from RiggedRoll to another address.

πŸ₯… Goals

  • Can you send value from the riggedRoll contract to your front end address?
  • Is anyone able to call the withdraw function? What would be the downside to that?

βš”οΈ Side Quest

  • Lock the withdraw function so it can only be called by the owner.

⚠️ But wait, I am not the owner! You will want to set your front end address as the owner in 01_deploy_riggedRoll.js. This will allow your front end address to call the withdraw function.

Checkpoint 5: πŸ’Ύ Deploy it! πŸ›°

πŸ“‘ Edit the defaultNetwork in packages/hardhat/hardhat.config.js, as well as targetNetwork in packages/react-app/src/App.jsx, to your choice of public EVM networks

πŸ‘©β€πŸš€ You will want to run yarn account to see if you have a deployer address.

πŸ” If you don't have one, run yarn generate to create a mnemonic and save it locally for deploying.

πŸ›° Use a faucet like faucet.paradigm.xyz to fund your deployer address (run yarn account again to view balances)

⚠️ Make sure you fund your account with enough Eth! .05 is required to initially fund the DiceGame contract and .01 more is required to fund the riggedRoll contract. Plus a bit extra to pay the gas.

πŸš€ Run yarn deploy to deploy to your public network of choice (πŸ˜… wherever you can get ⛽️ gas)

πŸ”¬ Inspect the block explorer for the network you deployed to... make sure your contract is there.


Checkpoint 6: 🚒 Ship it! 🚁

πŸ“¦ Run yarn build to package up your frontend.

πŸ’½ Upload your app to surge with yarn surge (you could also yarn s3 or maybe even yarn ipfs?)

😬 Windows users beware! You may have to change the surge code in packages/react-app/package.json to just "surge": "surge ./build",

βš™ If you get a permissions error yarn surge again until you get a unique URL, or customize it in the command line.

πŸš” Traffic to your url might break the Infura rate limit, edit your key: constants.js in packages/react-app/src


Checkpoint 7: πŸ“œ Contract Verification

Update the apikey in packages/hardhat/package.json. You can get your key here.

Now you are ready to run the yarn verify --network your_network command to verify your contracts on etherscan πŸ›°

Copy the verified address for your RiggedRoll contract and enter that into the appropriate Etherscan testnet.


About

Challenge 3 of the Speed Run Ethereum

License:MIT License