Original title: "Introduction to Solidity Minimalist: 32. Token faucet"
Original author: 0xAA
I am re-learning solidity recently, to consolidate the details, and also write a "Solidity minimalist introduction" for beginners to use (programmers can find another Tutorials), 1-3 lectures are updated every week.
All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity
We In the 31st lecture, I learned the ERC20 Token standard. In this lecture, we will learn the smart contract of ERC20 faucet. In this contract, users can receive free ERC20 Token.
When people are thirsty, they have to go to the faucet to receive water; When people want free Token, they have to go to the Token faucet to get it. Token Faucet is a website/application that allows users to receive Token for free.
The earliest Token faucet was Bitcoin (BTC) faucet: now a BTC costs $30,000, but in 2010, the price of BTC was less than $0.1, and Holders are few. In order to expand its influence, Gavin Andresen of the Bitcoin community developed the BTC faucet, allowing others to receive BTC for free. Everyone likes to brush wool. At that time, many people did it, and some of them became believers in BTC. A total of over 19,700 BTC was given away from the BTC faucet, which is now worth about $600 million!
Here, we implement a simplified version of the ERC20 faucet, the logic is very Simple: We transfer some ERC20 Tokens to the faucet contract, and users can receive 100 units of Token through the requestToken() function of the contract, and each address can only receive one time.
We define 3 state variables in the faucet contract
amountAllowed sets the number of Tokens that can be received each time (the default is 100. It is not a hundred, because Token has decimal places).
tokenContract records the issued ERC20 Token contract address.
requestedAddress records the address where the Token has been received.

The faucet contract defines a SendToken event, which records the address and quantity of receiving Token each time , released when the requestTokens() function is called.

There are only two functions in the contract:
Constructor: Initialize the tokenContract state variable to determine the issued ERC20 Token address.

requestTokens() function, the user calls it You can receive ERC20 Token.
< /p>
1 First, deploy the ERC20 Token contract and give yourself mint 10000 units of Token.

Deploy `ERC20`
< br>
2 Deploy the Faucet faucet contract, and fill in the contract address of the ERC20 Token above for the initialization parameters.

Deploy faucet contract
3 Using ERC20 Token The trasnfer() function of the contract transfers 10,000 units of Token to the Faucet contract address.

Transfer money to the faucet
4 Change to a new account, call the requestTokens() function of the Faucet contract, and receive the Token. You can see the SendToken event being released in the terminal.

Get Token
5 Use balanceOf to check the account balance on the ERC20 Token contract, you can see that the balance has changed to 100, and the claim is successful!

Collected successfully
In this lecture, we introduced the history of the Token faucet and the ERC20 faucet contract . Where do you think the next BTC faucet will be?
Original link blockquote>
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia