https://github.com/0xdmitry/speedrun-ethereum-challenge-2
Token Vendor
https://github.com/0xdmitry/speedrun-ethereum-challenge-2
Last synced: about 2 months ago
JSON representation
Token Vendor
- Host: GitHub
- URL: https://github.com/0xdmitry/speedrun-ethereum-challenge-2
- Owner: 0xDmitry
- License: mit
- Created: 2022-04-05T10:01:27.000Z (about 3 years ago)
- Default Branch: challenge-2-token-vendor
- Last Pushed: 2022-04-05T10:28:29.000Z (about 3 years ago)
- Last Synced: 2024-10-03T12:18:57.827Z (8 months ago)
- Language: CSS
- Size: 6.83 MB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
You can try it here: https://delicious-grandmother.surge.sh/
# ๐ scaffold-eth | ๐ฐ BuidlGuidl
## ๐ฉ Challenge 2: ๐ต Token Vendor ๐ค
> ๐ค 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.
> ๐ต Create `YourToken.sol` smart 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.sol` contract that sells your token using a payable `buyTokens()` function.
> ๐ Edit the frontend that invites the user to `` 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 your ERC20 token, transfer it, and sell it back to the vendor. Deploy your contracts on your public chain of choice and then `yarn build` and `yarn surge` your app to a public web server. Submit the url on [SpeedRunEthereum.com](https://speedrunethereum.com)!
> ๐ฌ Meet other builders working on this challenge and get help in the [Challenge 2 telegram](https://t.me/joinchat/IfARhZFc5bfPwpjq)!
๐งซ Everything starts by โ๏ธ Editing `YourToken.sol` in `packages/hardhat/contracts`
---
### Checkpoint 0: ๐ฆ install ๐
```bash
git clone https://github.com/scaffold-eth/scaffold-eth-challenges challenge-2-token-vendor
cd challenge-2-token-vendor
git checkout challenge-2-token-vendor
yarn install
```๐ Edit your smart contract `YourToken.sol` in `packages/hardhat/contracts`
---
### Checkpoint 1: ๐ญ Environment ๐บ
You'll have three terminals up for:
`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
> ๐ฉโ๐ป Rerun `yarn deploy --reset` whenever you want to deploy new contracts to the frontend.
> ignore any warnings, we'll get to that...
---
### Checkpoint 2: ๐ตYour Token ๐ต
> ๐ฉโ๐ป Edit `YourToken.sol` to inherit the **ERC20** token standard from OpenZeppelin
> Mint **1000** (\* 10 \*\* 18) to your frontend address using the `constructor()`.
(Your frontend address is the address in the top right of http://localhost:3000)
> You can `yarn deploy --reset` to deploy your contract until you get it right.
#### ๐ฅ Goals
- [ ] Can you check the `balanceOf()` your frontend address in the **YourToken** of the `Debug Contracts` tab?
- [ ] Can you `transfer()` your token to another account and check _that_ account's `balanceOf`?(Use an incognito window to create a new address and try sending to that new address. Use the `transfer()` function in the `Debug Contracts` tab.)
---
### Checkpoint 3: โ๏ธ Vendor ๐ค
> ๐ฉโ๐ป Edit the `Vendor.sol` contract with a **payable** `buyTokens()` function
Use a price variable named `tokensPerEth` set to **100**:
```solidity
uint256 public constant tokensPerEth = 100;
```> ๐ The `buyTokens()` function in `Vendor.sol` should use `msg.value` and `tokensPerEth` to calculate an amount of tokens to `yourToken.transfer()` to `msg.sender`.
> ๐ Emit **event** `BuyTokens(address buyer, uint256 amountOfETH, uint256 amountOfTokens)` when tokens are purchased.
Edit `deploy/01_deploy_vendor.js` to deploy the `Vendor` (uncomment Vendor deploy lines).
#### ๐ฅ Goals
- [ ] When you try to buy tokens from the vendor, you should get an error: **'ERC20: transfer amount exceeds balance'**
โ ๏ธ this is because the Vendor contract doesn't have any YourTokens yet!
โ๏ธ Side Quest: send tokens from your frontend address to the Vendor contract address and *then* try to buy them.
> โ๏ธ We can't hard code the vendor address like we did above when deploying to the network because we won't know the vender address at the time we create the token contract.
> โ๏ธ So instead, edit `YourToken.sol` to transfer the tokens to the `msg.sender` (deployer) in the **constructor()**.
> โ๏ธ Then, edit `deploy/01_deploy_vendor.js` to transfer 1000 tokens to `vendor.address`.
```js
await yourToken.transfer( vendor.address, ethers.utils.parseEther("1000") );
```> You can `yarn deploy --reset` to deploy your contract until you get it right.
(You will use the `YourToken` UI tab and the frontend for most of your testing. Most of the UI is already built for you for this challenge.)
#### ๐ฅ Goals
- [ ] Does the `Vendor` address start with a `balanceOf` **1000** in `YourToken` on the `Debug Contracts` tab?
- [ ] Can you buy **10** tokens for **0.1** ETH?
- [ ] Can you transfer tokens to a different account?> ๐ Edit `Vendor.sol` to inherit *Ownable*.
In `deploy/01_deploy_vendor.js` you will need to call `transferOwnership()` on the `Vendor` to make *your frontend address* the `owner`:
```js
await vendor.transferOwnership("**YOUR FRONTEND ADDRESS**");
```#### ๐ฅ Goals
- [ ] Is your frontend address the `owner` of the `Vendor`?
> ๐ Finally, add a `withdraw()` function in `Vendor.sol` that lets the owner withdraw ETH from the vendor.
#### ๐ฅ Goals
- [ ] Can **only** the `owner` withdraw the ETH from the `Vendor`?
#### โ๏ธ Side Quests
- [ ] Can _anyone_ withdraw? Test _everything_!
- [ ] What if you minted **2000** and only sent **1000** to the `Vendor`?---
### Checkpoint 4: ๐ค Vendor Buyback ๐คฏ
๐ฉโ๐ซ The hardest part of this challenge is to build your `Vendor` to buy the tokens back.
๐ง The reason why this is hard is the `approve()` pattern in ERC20s. You can read more about the `approve()` pattern [here](https://docs.ethhub.io/guides/a-straightforward-guide-erc20-tokens/).
๐ First, the user has to call `approve()` on the `YourToken` contract, approving the `Vendor` contract address to take some amount of tokens.
๐คจ Then, the user makes a *second transaction* to the `Vendor` contract to `sellTokens(uint256 amount)`.
๐ค The `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.
> ๐ Edit `Vendor.sol` and add a `sellTokens()` function!
โ ๏ธ You will need extra UI for calling `approve()` before calling `sellTokens(uint256 amount)`.
๐จ Use the `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!
#### ๐ฅ Goal
- [ ] Can you sell tokens back to the vendor?
- [ ] Do you receive the right amount of ETH for the tokens?#### โ๏ธ Side Quest
- [ ] Should we disable the `owner` withdraw to keep liquidity in the `Vendor`?
- [ ] It would be a good idea to display Sell Token Events. Create the `event` and `emit` it in your `Vendor.sol` and look at `buyTokensEvents` in your `App.jsx` for an example of how to update your frontend.#### โ ๏ธ Test it!
- Now is a good time to run `yarn test` to run the automated testing function. It will test that you hit the core checkpoints. You are looking for all green checkmarks and passing tests!----
### Checkpoint 5: ๐พ Deploy it! ๐ฐ
๐ก Edit the `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](https://ethereum.org/en/developers/docs/networks/)
๐ฉโ๐ 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 a faucet like [faucet.paradigm.xyz](https://faucet.paradigm.xyz/) to fund your **deployer address** (run `yarn account` again to view balances)
> ๐ Run `yarn deploy` to deploy to your public network of choice (๐ wherever you can get โฝ๏ธ gas)
๐ฌ Inspect the block explorer for the network you deployed to... make sure your contract is there.
---
### Checkpoint 6: ๐ข Ship it! ๐๐ฆ 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`?)
> ๐ฌ Windows users beware! You may have to change the surge code in `packages/react-app/package.json` to just `"surge": "surge ./build",`
โ If you get a permissions error `yarn surge` again until you get a unique URL, or customize it in the command line.
๐ Traffic to your url might break the [Infura](https://infura.io/) rate limit, edit your key: `constants.js` in `packages/ract-app/src`.
---
### Checkpoint 7: ๐ Contract VerificationUpdate the `api-key` in `packages/hardhat/package.json`. You can get your key [here](https://etherscan.io/myapikey).
> Now you are ready to run the `yarn verify --network your_network` command to verify your contracts on etherscan ๐ฐ
๐ You may see an address for both YouToken and Vendor. You will want the Vendor address.
๐ This will be the URL you submit to ๐โโ๏ธ[SpeedRunEthereum.com](https://speedrunethereum.com).
---
๐ฌ Problems, questions, comments on the stack? Post them to the [Challenge 2 telegram channel](https://t.me/joinchat/IfARhZFc5bfPwpjq)