Giter Club home page Giter Club logo

decentralized-staking's Introduction

๐Ÿ— scaffold-eth | ๐Ÿฐ BuidlGuidl

๐Ÿšฉ Challenge 1: Decentralized Staking App

๐Ÿฆ Build a Staker.sol contract that collects ETH from numerous addresses using a payable stake() function and keeps track of balances. After some deadline if it has at least some threshold of ETH, it sends it to an ExampleExternalContract and triggers the complete() action sending the full balance. If not enough ETH is collected, allow users to withdraw().

๐ŸŽ› Building the frontend to display the information and UI is just as important as writing the contract. The goal is to deploy the contract and the app to allow anyone to stake using your app. Use a Stake(address,uint256) event to all stakes.

๐Ÿ† The final deliverable is deploying a decentralized application to a public blockchain and then yarn build and yarn surge your app to a public webserver. Share the url in the Challenge 1 telegram channel to earn a collectible and cred! Part of the challenge is making the UI/UX enjoyable and clean! ๐Ÿคฉ

๐Ÿงซ Everything starts by โœ๏ธ Editing Staker.sol in packages/hardhat/contracts


Checkpoint 0: ๐Ÿ“ฆ install ๐Ÿ“š

git clone https://github.com/austintgriffith/scaffold-eth.git challenge-1-decentralized-staking
cd challenge-1-decentralized-staking
git checkout challenge-1-decentralized-staking
yarn install

๐Ÿ” Edit your smart contract Staker.sol in packages/hardhat/contracts


Checkpoint 1: ๐Ÿ”ญ Environment ๐Ÿ“บ

You'll have three terminals up for:

yarn start (react app frontend)

yarn chain (harthat backend)

yarn deploy (to compile, deploy, and publish your contracts to the frontend)

๐Ÿ‘ฉโ€๐Ÿ’ป Rerun yarn deploy whenever you want to deploy new contracts to the frontend.


Checkpoint 2: ๐Ÿฅฉ Staking ๐Ÿ’ต

You'll need to track individual balances using a mapping:

mapping ( address => uint256 ) public balances;

And also track a constant threshold at 1 ether

uint256 public constant threshold = 1 ether;

๐Ÿ‘ฉโ€๐Ÿ’ป Write your stake() function and test it with the Debug Contracts tab in the frontend

๐Ÿฅ… Goals

  • Do you see the balance of the Staker contract go up when you stake()?
  • Is your balance correctly tracked?
  • Do you see the events in the Staker UI tab?

Checkpoint 3: ๐Ÿ”ฌ Testing โฑ

Set a deadline of now + 30 seconds

uint256 public deadline = now + 30 seconds;

๐Ÿ‘ฉโ€๐Ÿ’ป Write your execute() function and test it with the Debug Contracts tab

If the address(this).balance of the contract is over the threshold by the deadline, you will want to call: exampleExternalContract.complete{value: address(this).balance}()

If the balance is less than the threshold, you want to set a openForWithdraw bool to true and allow users to withdraw(address payable) their funds.

(You'll have 30 seconds after deploying until the deadline is reached)

๐Ÿ‘ฉโ€๐Ÿ’ป Create a timeLeft() function including public view returns (uint256) that returns how much time is left.

โš ๏ธ Be careful! if now >= deadline you want to return 0;

โณ The time will only update if a transaction occurs. You can see the time update by getting funds from the faucet just to trigger a new block.

๐Ÿ‘ฉโ€๐Ÿ’ป You can call yarn deploy any time you want a fresh contract

๐Ÿฅ… Goals

  • Can you see timeLeft counting down in the Staker UI tab when you trigger a transaction with the faucet?
  • If you stake() enough ETH before the deadline, does it call complete()?
  • If you don't stake() enough can you withdraw(address payable) your funds?

โš”๏ธ Side Quests

  • Can execute get called more than once, and is that okay?
  • Can you deposit and withdraw freely after the deadline, and is that okay?
  • What are other implications of anyone being able to withdraw for someone?
  • Can you implement your own modifier that checks whether deadline was passed or not? Where can you use it?

๐Ÿธ It's a trap!

  • Make sure funds can't get trapped in the contract! Try sending funds after you have executed!
  • Try to create a modifier called notCompleted. It will check that ExampleExternalContract is not completed yet. Use it to protect your execute and withdraw functions.

Checkpoint 4: ๐Ÿšข Ship it ๐Ÿš

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

๐Ÿ‘ฉโ€๐Ÿš€ 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 an instantwallet.io to fund your deployer address (run yarn account to view balances)

๐Ÿš€ Run yarn deploy to deploy to your public network of choice (wherever you can get โ›ฝ๏ธ gas)


Checkpoint 5: ๐ŸŽš Frontend ๐Ÿง˜โ€โ™€๏ธ

๐Ÿ‘ฉโ€๐ŸŽค Take time to craft your user experience.

...

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

๐Ÿ“ฆ 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?)

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

๐ŸŽ– Show off your app by pasting the url in the Challenge 1 telegram channel


๐Ÿ‘ฉโ€๐Ÿ”ฌ Need a longer form tutorial to guide your coding? Try this one!

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

decentralized-staking's People

Contributors

0xsama avatar a180024 avatar austintgriffith avatar azf20 avatar btogzhan2000 avatar calvbore avatar changoman avatar clacladev avatar codenamejason avatar dmaulick-cb avatar forshtat avatar gregplumbly avatar hodlplus2021 avatar internnos avatar iqdecay avatar ironsoul0 avatar j2r5m3 avatar jacobwillemsma avatar pabloruiz55 avatar philippzentner avatar ro5s avatar shravansunder avatar stermi avatar taldenv avatar thesuperb1 avatar tomafc330 avatar tomafrench avatar xalava avatar xjjda22 avatar yutingzhao1991 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.