๐ค 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.
๐ต CreateYourToken.sol
smart contract that inherits the ERC20 token standard from OpenZeppelin. Set your token to_mint()
1000 (* 10 ** 18) tokens to themsg.sender
. Then create aVendor.sol
contract that sells your token using a payablebuyTokens()
function.
๐ 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 thenyarn build
andyarn surge
your 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! ๐คฉ
YourToken.sol
in packages/hardhat/contracts
YourToken.sol
in packages/hardhat/contracts
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
๐ฉโ๐ป Rerunyarn deploy --reset
whenever you want to deploy new contracts to the frontend.
ignore any warnings, we'll get to that...
๐ฉโ๐ป EditYourToken.sol
to inherit the ERC20 token standard from OpenZeppelin
msg.sender
) and then send them to your frontend address in the deploy/00_deploy_your_token.js
:balanceOf()
your frontend address in the YourToken of the Debug Contracts
tab?transfer()
your token to another account and check that account's balanceOf
?transfer()
function in the Debug Contracts
tab.)๐ฉโ๐ป Create aVendor.sol
contract with a payablebuyTokens()
function
tokensPerEth
set to 100:๐ ThebuyTokens()
function inVendor.sol
should usemsg.value
andtokensPerEth
to calculate an amount of tokens toyourToken.transfer()
tomsg.sender
.
๐ Emit eventBuyTokens(address buyer, uint256 amountOfETH, uint256 amountOfTokens)
when tokens are purchased.
deploy/01_deploy_vendor.js
to deploy the Vendor
(uncomment Vendor deploy lines).00_deploy_your_token.js
and 01_deploy_vendor.js
so you transfer the tokens to the vendor.address
instead of your frontend address.YourToken
UI tab and the frontend for most of your testing. Most of the UI is already built for you for this challenge.)๐ EditVendor.sol
to inherit Ownable.
deploy/01_deploy_vendor.js
you will need to call transferOwnership()
on the Vendor
to make your frontend address the owner
:๐ Finally, add awithdraw()
function inVendor.sol
that lets the owner withdraw ETH from the vendor.
Vendor
address start with a balanceOf
1000 in YourToken
on the Debug Contracts
tab?owner
withdraw the ETH from the Vendor
?Vendor
?Vendor
to buy the tokens back.approve()
pattern in ERC20s.approve()
on the YourToken
contract, approving the Vendor
contract address to take some amount of tokens.Vendor
contract to sellTokens()
.Vendor
should call yourToken.transferFrom(msg.sender, address(this), theAmount)
and if the user has approved the Vendor
correctly, tokens should transfer to the Vendor
and ETH should be sent to the user.Debug Contracts
tab to call the approve and sellTokens() at first but then look in the App.jsx
for the extra approve/sell UI to uncomment.)owner
withdraw to keep liquidity in the Vendor
?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โyarn account
to see if you have a deployer address.yarn generate
to create a mnemonic and save it locally for deploying.yarn account
again to view balances)๐ Runyarn deploy
to deploy to your public network of choice (๐ wherever you can get โฝ๏ธ gas)
hardhat.config
file 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 build
to package up your frontend.yarn surge
(you could also yarn s3
or maybe even yarn ipfs
?)constants.js
in packages/ract-app/src
.Now you are ready to run theyarn verify --network your_network
command to verify your contracts on etherscan ๐ฐ This will be the URL you submit to SpeedRun.
๐ฌ Problems, questions, comments on the stack? Post them to the ๐ scaffold-eth developers chatโ