🤖 Smart contracts are kind of like "always on" vending machines that anyone can access. Let's make a decentralized, digital currency. Then, let's build an unstoppable vending machine that will buy and sell the currency. We'll learn about the "approve" pattern for ERC20s and how contract to contract interactions work.
YourToken.solsmart contract that inherits the ERC20 token standard from OpenZeppelin. Set your token to
_mint()1000 (* 10 ** 18) tokens to the
msg.sender. Then create a
Vendor.solcontract that sells your token using a payable
🎛 Edit the frontend that invites the user to
<input\>an amount of tokens they want to buy. We'll display a preview of the amount of ETH (or USD) it will cost with a confirm button.
🔍 It will be important to verify your token's source code in the block explorer after you deploy. Supporters will want to be sure that it has a fixed supply and you can't just mint more.
🏆 The final deliverable is an app that lets users purchase and transfer your token. Deploy your contracts on your public chain of choice and then
yarn surgeyour app to a public web server. Share the url in the Challenge 2 telegram channel.
📱 Part of the challenge is making the UI/UX enjoyable and clean! 🤩
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
yarn deploy --resetwhenever you want to deploy new contracts to the frontend.
ignore any warnings, we'll get to that...
YourToken.solto inherit the ERC20 token standard from OpenZeppelin
msg.sender) and then send them to your frontend address in the
balanceOf()your frontend address in the YourToken of the
transfer()your token to another account and check that account's
transfer()function in the
👩💻 Create a
Vendor.solcontract with a payable
tokensPerEthset to 100:
tokensPerEthto calculate an amount of tokens to
📟 Emit event
BuyTokens(address buyer, uint256 amountOfETH, uint256 amountOfTokens)when tokens are purchased.
deploy/01_deploy_vendor.jsto deploy the
Vendor(uncomment Vendor deploy lines).
01_deploy_vendor.jsso you transfer the tokens to the
vendor.addressinstead of your frontend address.
YourTokenUI tab and the frontend for most of your testing. Most of the UI is already built for you for this challenge.)
Vendor.solto inherit Ownable.
deploy/01_deploy_vendor.jsyou will need to call
Vendorto make your frontend address the
📝 Finally, add a
Vendor.solthat lets the owner withdraw ETH from the vendor.
Vendoraddress start with a
ownerwithdraw the ETH from the
Vendorto buy the tokens back.
approve()pattern in ERC20s.
YourTokencontract, approving the
Vendorcontract address to take some amount of tokens.
yourToken.transferFrom(msg.sender, address(this), theAmount)and if the user has approved the
Vendorcorrectly, tokens should transfer to the
Vendorand ETH should be sent to the user.
Debug Contractstab to call the approve and sellTokens() at first but then look in the
App.jsxfor the extra approve/sell UI to uncomment.)
ownerwithdraw to keep liquidity in the
yarn accountto see if you have a deployer address.
yarn generateto create a mnemonic and save it locally for deploying.
yarn deployto deploy to your public network of choice (😅 wherever you can get ⛽️ gas)
hardhat.configfile at around line 258. The verify script is at the bottom of
00_deploy_your_token.js. You will see something like this after successful completion.
yarn buildto package up your frontend.
yarn surge(you could also
yarn s3or maybe even