Skip to main content

Approve Pattern

Based on ERC20 token learn the rules for transfering tokens, including the approval pattern when a user wishes to instruct a vendor contract to sell a proportion of their tokens.

ERC20 has two types of transfers.

  • transfer tokens from the total supply of the contract vendor address to another address
  • transferFrom used transfer tokens between any two users.

Most dapps implement the transferFrom functionality.

Risks

Unlimited Approval Hack

Scaffold Eth Challenge

Create a Dapp that lets users purchase your ERC20 token, transfer it, and sell it back to the vendor.

Learn about the approve pattern for ERC20 and how contract to contract interactions work.

Instructions

StepsNotes
SC: Mint tokens to user address from Scaffold Eth UIDone
App: Check balanceDone
App: Transfer tokens to another account (e.g incognito browser)Issue, could not get transfer to work
SC: Change address mint address to return to senderDone
SC: Vendor.sol create a buy functionDone
SC: Vendor.sol create a withdraw functionDone
SC: Vendor.sol create a sell functionDone
SC: Run testsDone
App: Enable interaction through UIUncomment code
App: Improve UI UXTodo
App: Deploy Smart Contract to a TestnetTodo
Hardhat: Test Smart Contract DeploymentTodo
App: Deploy Web AppTodo

Checkpoint 2

Create YourToken.sol from ERC20 standard.

  • Mint to the address from localhost:3000
  • Check the balanceOf() your frontend address
  • Transfer() your token to another account - Not working?

Mint tokens

Address taken from default user in the Scaffold Eth app.

_mint( 0xef6e7B541c239F90567F1F0dc15D0c4087603F54 , 1000 * 10 ** 18);

Transfer tokens using the App Interface.

  1. Open an incognito browser connected to localhost:3000
  2. From Debug View, attempt to transfer some tokens to the incognito account then check the balance.

Using transfer: component when attempting to send Incognito User Account get the an error that sender doesn't have enough funds to send tx. Sender's account has: 0

Checkpoint 3: ⚖️ Vendor

  • When you try to buy tokens from the vendor, you should get an error: 'ERC20: transfer amount exceeds balance'
  • 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 need to call transferOwnership() on the Vendor to make your frontend address the owner:

  • Is your frontend address the owner of the Vendor?
  • Can only the owner withdraw the ETH from the Vendor?
  • Can anyone withdraw? Test everything!
  • What if you minted 2000 and only sent 1000 to the Vendor?

Checkpoint 4: 🤔 Vendor Buyback 🤯

Implement the approve pattern.

  • Can you sell tokens back to the vendor?
  • Do you receive the right amount of ETH for the tokens?
  • Is it a good idea to disable the ability for the owner to withdraw to keep liquidity in the Vendor?
  • Display Sell Token Events
  • Run Smart Contract tests

Checkpoint 5: 💾 Deploy the Contracts

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.

Use the Vendor address as the URL to submit to SpeedRunEthereum.com

See Web3 Deployment

  • Deploy Smart Contract to a Testnet
  • Verify deployment of Smart Contract to Testnet
  • Deploy the Web App

Vendor Address

https://rinkeby.etherscan.io/address/0x983608b2d81e350d34a5c19977ed0bdb01d25fa9

Deployed UI

https://awful-frog.surge.sh/

Schema

Scaffold Eth Repo Buy and Sell Token Implementation Notes