🏗 scaffold-eth | 🏰 BuidlGuidl
🚩 Challenge 1: Decentralized Staking App
🏦 Build aStaker.sol
contract that collects ETH from numerous addresses using a payablestake()
function and keeps track ofbalances
. After somedeadline
if it has at least somethreshold
of ETH, it sends it to anExampleExternalContract
and triggers thecomplete()
action sending the full balance. If not enough ETH is collected, allow users towithdraw()
.
🎛 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 aStake(address,uint256)
event to all stakes.
🏆 The final deliverable is deploying a decentralized application to a public blockchain and thenyarn build
andyarn 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! 🤩
Staker.sol
in packages/hardhat/contracts
📦 install 📚
Checkpoint 0: 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
🔭 Environment 📺
Checkpoint 1: 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.
🥩 Staking 💵
Checkpoint 2: 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 yourstake()
function and test it with theDebug Contracts
tab in the frontend
🥅 Goals
- Do you see the balance of the
Staker
contract go up when youstake()
? - Is your
balance
correctly tracked? - Do you see the events in the
Staker UI
tab?
🔬 Testing ⏱
Checkpoint 3: Set a deadline
of now + 30 seconds
uint256 public deadline = now + 30 seconds;
👩💻 Write yourexecute()
function and test it with theDebug 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 atimeLeft()
function includingpublic view returns (uint256)
that returns how much time is left.
now >= deadline
you want to return 0;
👩💻 You can call
yarn deploy
any time you want a fresh contract
🥅 Goals
- Can you see
timeLeft
counting down in theStaker UI
tab when you trigger a transaction with the faucet? - If you
stake()
enough ETH before thedeadline
, does it callcomplete()
? - If you don't
stake()
enough can youwithdraw(address payable)
your funds?
- 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?
- 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 thatExampleExternalContract
is not completed yet. Use it to protect yourexecute
andwithdraw
functions.
🚢 Ship it 🚁
Checkpoint 4: defaultNetwork
to your choice of public EVM networks in packages/hardhat/hardhat.config.js
yarn account
to see if you have a deployer address
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)
🚀 Runyarn deploy
to deploy to your public network of choice (wherever you can get⛽️ gas)
🎚 Frontend 🧘♀️
Checkpoint 5: 👩🎤 Take time to craft your user experience.
...
yarn build
to package up your frontend.
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