Giter Site home page Giter Site logo

egeatesalp / token-vendor Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 11.51 MB

My Implementation of Challenge 2: Token Vendor (https://speedrunethereum.com/challenge/token-vendor)

License: MIT License

Solidity 0.45% TypeScript 84.45% Shell 4.70% JavaScript 7.59% Dockerfile 1.16% HTML 0.84% CSS 0.56% Less 0.26%

token-vendor's Introduction

๐Ÿ— 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 <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 build and yarn surge your app to a public web server. Submit the url on SpeedRunEthereum.com!

๐Ÿ’ฌ Meet other builders working on this challenge and get help in the Challenge 2 telegram!

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


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

git clone https://github.com/scaffold-eth/scaffold-eth-typescript-challenges.git 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-ts/contracts


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

You'll have three terminals up for:

yarn chain (hardhat backend)

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

yarn start (react app frontend)

Make sure you run the commands in the above order. The contract types get generated as part of the deploy, which will be required to build and start the app.

๐Ÿ‘€ 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) in the constructor (to the msg.sender) and then send them to your frontend address in the deploy/00_deploy_your_token.ts:

const result = await yourToken.transfer(
  "**YOUR FRONTEND ADDRESS**",
  ethers.utils.parseEther("1000")
);

(Your frontend address is the address in the top right of your frontend. Go to localhost:3000 and copy the address from the top 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 ๐Ÿค–

๐Ÿ‘ฉโ€๐Ÿ’ป Create a Vendor.sol contract with a payable buyTokens() function

Use a price variable named tokensPerEth set to 100:

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.ts to deploy the Vendor (uncomment Vendor deploy lines).

You will also want to change 00_deploy_your_token.ts and 01_deploy_vendor.ts so you transfer the tokens to the vendor.address instead of your frontend address.

const result = await yourToken.transfer(
  vendor.address,
  ethers.utils.parseEther("1000")
);

(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.)

๐Ÿ“ Edit Vendor.sol to inherit Ownable.

In deploy/01_deploy_vendor.ts you will need to call transferOwnership() on the Vendor to make your frontend address the owner:

await vendor.transferOwnership("**YOUR FRONTEND ADDRESS**");

๐Ÿ“ Finally, add a withdraw() function in Vendor.sol that lets the owner withdraw ETH from the vendor.

๐Ÿฅ… 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?
  • Can 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.

๐Ÿ˜• 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().

๐Ÿค“ 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.

(Use the Debug Contracts tab to call the approve and sellTokens() at first but then look in the YourToken.tsx for the extra approve/sell UI to uncomment.)

๐Ÿฅ… Goal

  • Can you sell tokens back to the vendor and receive ETH?

โš”๏ธ Side Quest

  • Should we disable the owner withdraw to keep liquidity in the Vendor?

โš ๏ธ 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-ts/hardhat.config.ts, as well as targetNetworkInfo in packages/vite-app-ts/src/config/providersConfig.ts, to your choice of public EVM 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 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?)

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


Checkpoint 7: ๐Ÿ“œ Contract Verification

Update the api-key in packages/hardhat-ts/package.json file. You can get your key here.

Screen Shot 2021-11-30 at 10 21 01 AM

Now you are ready to run the yarn verify --network your_network command to verify your contracts on etherscan ๐Ÿ›ฐ

This will be the URL you submit to SpeedRun.


๐Ÿƒ Head to your next challenge here.

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

token-vendor's People

Contributors

shravansunder avatar grothem avatar shravansunderxero avatar zakgriffith avatar codenamejason avatar danielesalatti avatar

Watchers

Ege Atesalp 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.