-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment 1
In this assignment we'll go through the basic concepts of the ERC-20 standard. We'll use the ERC-20 smart contract implementation by the OpenZeppelin library. The assignment is adapter from the Speed Run Token Vendor Challenge.
The smart contract we are going to test is the MemeCoin.sol. It is a simple smart contract that follows the ERC-20 standard for fungible tokens on Ethereum. The token symbol is MC.
For a coin to have value it must have a price. For example the current price of the TRUMP token is $7.84 USDC according to Market Cap. This means for whatever reason (speculation and hype in many cases 🤑), users can buy the TRUMP token using another crypto currency like USDC. In a free market, the forces of supply and demand determine this price. When the demand for TRUMP goes up relative to USDC, TRUMP price goes up relative to USDC. DEXs are the markets for crypto currency pairs. Part 2 of this assignment looks at a popular DEX called Uniswap.
For part 1, we are going to have a simple exchange contract for our MC token called Vendor.sol. Our Vendor contract is going to be an exchange between our MC and ETH (so it is a simple MC/ETH liquidity pool). So it will sell MC in exchange for ETH and also buy MC and give out ETH. The price of MC is going to be fixed at 0.01. That means to get 1 MC a user must deposit 0.01 ETH or to get 100 MC a user must deposit 1 ETH.
We'll use the main branch for Part 1 of the assignment.
- Run the tests. They should fail.
yarn test test/Assignment1.ts - We'll fix the constructor method
TODO: create a _totalSupply of 1000 MC tokens with 18 decimal places for divisibility
NOTE: ETH also has 18 decimal places for divisibility. The smallest unit is called Wei. All values in smart contracts are measured in the smallest units. This is true for tokens as well.
TODO: create a vendor smart contract. This contract will be responsible for buying and selling your token in ETH
NOTE: Smart contracts can deploy other smart contracts. This is what Decentralized Exchanges do when creating liquidity pools for crypto currency pairs. Each pair has it's own smart contract that is deployed from the Factory smart contract of the DEX.
TODO: assign the vendorAddress using the newly created vendor smart contractTODO: mint the _totalSupply of tokens to the vendor address
- We'll fix the Vendor smart contract
TODO: make the Vendor contract Ownable using Openzeppelin smart contractsTODO: edit the constructor to set the owner's address
- We'll fix the buyTokens method
TODO: make the buyTokens method a payable functionTODO: check that the msg.value is setTODO: calculate the amount of tokens buyable
HINT: use the tokensPerEth. Tokens should be expressed in the lowest unit. Do some math to express in the simplest form.
TODO: check that vendor has enough tokens to sell to msg.senderTODO: transfer the tokens from the vendor to the msg.senderTODO: calculate any change left from the MC token purchase and refund any excessTODO: emit a BuyTokens event
- We'll fix the sellTokens method
TODO: check that the requested tokens to sell > 0TODO: check that the user has enough tokens to do the swapTODO: check that the vendor has enough ETH to pay for the tokensTODO: transfer tokens to the vendor and check for successTODO: transfer ETH to the seller and check for success
- We'll fix the withdraw method
TODO: make sure only the owner can withdrawTODO: withdraw the total amount to the owner
- Run the tests. They should all pass.
yarn test test/Assignment1.ts - We'll now buy some tokens from the Vendor smart contract on the UI
- Deploy the MemeCoin.sol smart contract on your local hardhat network
- Start the application
- Open your browser and open the app on http://localhost:3000 and click on
Debug Contracts.
NOTE: You will only see MemeCoin smart contract and it's variables but not the Vendor smart contract.
- To show the Vendor smart contract and it's variables, we must provide our Scaffold-Eth next.js app with a blue-print of Vendor.sol
QUESTION: Why didn't we need to do this for MemeCoin?
- In Visual Studio file explorer, open packages/hardhat/artifacts/contracts/Vendor.sol/Vendor.json
- Copy the
abiportion of that file - In Visual Studio file explorer, open packages/nextjs/contracts/deployedContracts.ts
TODO: Under MemeCoin object, create Vendor object. Put the address and also the abi you copied earlier
HINT: Check the MemeCoin for the vendor address
- Refresh the
http://localhost:3000/debugpage. You should now see theVendorsmart contract button.
- Now we will buy some MC tokens from the vendor
TODO: Take a snapshot of the http://localhost:3000/debug page and add it to your readme in Checkpoint 1TODO: Purchase 100 MC tokens from the Vendor contractTODO: Take a snapshot of the http://localhost:3000/token-vendor page and add it to your readme in Checkpoint 2