Giter VIP home page Giter VIP logo

spdrun-eth-dicegame's Introduction

๐Ÿšฉ Challenge 3: ๐ŸŽฒ Dice Game

readme-3

๐ŸŽฐ Randomness is tricky on a public deterministic blockchain. The block hash is an easy to use, but very 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, 2, 3, 4 or 5 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: ๐Ÿ“ฆ Environment ๐Ÿ“š

Before you begin, you need to install the following tools:

Then download the challenge to your computer and install dependencies by running:

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

in the same terminal, start your local network (a blockchain emulator in your computer):

yarn chain

in a second terminal window, ๐Ÿ›ฐ deploy your contract (locally):

cd challenge-3-dice-game
yarn deploy

in a third terminal window, start your ๐Ÿ“ฑ frontend:

cd challenge-3-dice-game
yarn start

๐Ÿ“ฑ Open http://localhost:3000 to see the app.

๐Ÿ‘ฉโ€๐Ÿ’ป Rerun yarn deploy whenever you want to deploy new contracts to the frontend. If you haven't made any contract changes, you can run yarn deploy --reset for a completely fresh deploy.


Checkpoint 1: ๐ŸŽฒ 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.

Faucet

๐Ÿฅ… 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?

Checkpoint 2: ๐Ÿ”‘ Rigged Contract

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.ts file in packages/hardhat/deploy

๐Ÿ’ธ You will need to send some funds to your RiggedRoll contract before doing your first roll, can use the Faucet button at the bottom left of the page.

โ“ 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 packages/nextjs/pages/dice.tsx 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()?

RiggedLosingRoll


Checkpoint 3: ๐Ÿ’ต 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.

RiggedRollAddress

๐Ÿ“ฅ 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.

WithdrawOnlyOwner

โš ๏ธ But wait, I am not the owner! You will want to set your front end address as the owner in 01_deploy_riggedRoll.ts. This will allow your front end address to call the withdraw function.

Checkpoint 4: ๐Ÿ’พ Deploy your contracts! ๐Ÿ›ฐ

๐Ÿ“ก Edit the defaultNetwork to your choice of public EVM networks in packages/hardhat/hardhat.config.ts

๐Ÿ” You will need to generate a deployer address using yarn generate This creates a mnemonic and saves it locally.

๐Ÿ‘ฉโ€๐Ÿš€ Use yarn account to view your deployer account balances.

โ›ฝ๏ธ You will need to send ETH to your deployer address with your wallet, or get it from a public faucet of your chosen network.

๐Ÿš€ Run yarn deploy to deploy your smart contract to a public network (selected in hardhat.config.ts)

๐Ÿ’ฌ Hint: You can set the defaultNetwork in hardhat.config.ts to sepolia OR you can yarn deploy --network sepolia.


Checkpoint 5: ๐Ÿšข Ship your frontend! ๐Ÿš

โœ๏ธ Edit your frontend config in packages/nextjs/scaffold.config.ts to change the targetNetwork to chains.sepolia or any other public network.

๐Ÿ’ป View your frontend at http://localhost:3000 and verify you see the correct network.

๐Ÿ“ก When you are ready to ship the frontend app...

๐Ÿ“ฆ Run yarn vercel to package up your frontend and deploy.

Follow the steps to deploy to Vercel. Once you log in (email, github, etc), the default options should work. It'll give you a public URL.

If you want to redeploy to the same production URL you can run yarn vercel --prod. If you omit the --prod flag it will deploy it to a preview/test URL.

๐ŸฆŠ Since we have deployed to a public testnet, you will now need to connect using a wallet you own or use a burner wallet. By default ๐Ÿ”ฅ burner wallets are only available on hardhat . You can enable them on every chain by setting onlyLocalBurnerWallet: false in your frontend config (scaffold.config.ts in packages/nextjs/)

Configuration of Third-Party Services for Production-Grade Apps.

By default, ๐Ÿ— Scaffold-ETH 2 provides predefined API keys for popular services such as Alchemy and Etherscan. This allows you to begin developing and testing your applications more easily, avoiding the need to register for these services.
This is great to complete your SpeedRunEthereum.

For production-grade applications, it's recommended to obtain your own API keys (to prevent rate limiting issues). You can configure these at:

  • ๐Ÿ”ทALCHEMY_API_KEY variable in packages/hardhat/.env and packages/nextjs/.env.local. You can create API keys from the Alchemy dashboard.

  • ๐Ÿ“ƒETHERSCAN_API_KEY variable in packages/hardhat/.env with your generated API key. You can get your key here.

๐Ÿ’ฌ Hint: It's recommended to store env's for nextjs in Vercel/system env config for live apps and use .env.local for local testing.


Checkpoint 6: ๐Ÿ“œ Contract Verification

Run the yarn verify --network your_network command to verify your contracts on etherscan ๐Ÿ›ฐ

๐Ÿ‘‰ Search this address on Etherscan to get the URL you submit to ๐Ÿƒโ€โ™€๏ธSpeedRunEthereum.com.


๐Ÿƒ Head to your next challenge here.

๐Ÿ’ฌ Problems, questions, comments on the stack? Post them to the ๐Ÿ— scaffold-eth developers chat

spdrun-eth-dicegame's People

Contributors

rin-st avatar carletex avatar technophile-04 avatar pabl0cks avatar hotmanics avatar pankaj512 avatar printrh avatar zakgriffith avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.