header-langage
简体中文
繁體中文
English
Tiếng Việt
한국어
日本語
ภาษาไทย
Türkçe
Scan to Download the APP

Solidity Minimalist Introduction | Lesson 33: Airdrop Contract

Read this article in 7 Minutes
This article introduces how to use Solidity to write an ERC20 token airdrop contract, greatly increasing the efficiency of airdrops.
Original Title: "Solidity Quick Start: 33. Airdrop Contract"
Original Author: 0xAA


I am currently relearning Solidity to solidify my understanding of the details and also writing a "Beginner's Guide to Solidity" for newcomers (advanced programmers can find other tutorials). I will be updating it with 1-3 lessons every week.


All codes and tutorials are open source on GitHub: github.com/AmazingAng/WTFSolidity


In the cryptocurrency industry, the happiest thing is to receive airdrops and get free tokens. In this tutorial, we will learn how to use smart contracts to airdrop ERC20 tokens.


空投 Airdrop


Airdrop


Airdrop is a marketing strategy in the cryptocurrency industry, where project teams distribute Tokens for free to a specific group of users. In order to qualify for the airdrop, users usually need to complete some simple tasks, such as testing products, sharing news, or referring friends. Through airdrops, project teams can acquire seed users, while users can obtain a sum of wealth, benefiting both parties.


Because there are many users who receive empty tokens each time, the project party cannot transfer them one by one. By using smart contracts to distribute ERC20 tokens in batches, the efficiency of airdrops can be significantly improved.


Airdrop Token Contract


Airdrop contract logic is very simple: using a loop, a transaction sends ERC20 Token to multiple addresses. The contract contains two functions.


getSum() function: returns the sum of a uint array.  



multiTransferToken() function: Send ERC20 Token airdrop, including three parameters:


_token: Token Contract address (address type)

_addresses: An array of user addresses (address[] type) that receive the airdrop.

_amounts: An array of airdrop quantities, corresponding to the quantity for each address in _addresses (uint[] type).


This function has two checks: the first require checks whether the lengths of the _addresses and _amounts arrays are equal; the second require checks whether the authorization amount of the airdrop contract is greater than the total amount of the Token to be airdropped.



Airdrop Practice


1 Deploy ERC20 Token contract and mint 10000 units of Token for yourself.


Deploy ERC20


Deploy the Airdrop contract.


Deploy Airdrop


3 Utilize the approve() function in the ERC20 Token contract to authorize the Airdrop airdrop contract with 10000 units of Token.


Authorization


4 Execute the multiTransferToken() function of the Airdrop contract for airdrop, with _token filled in with the ERC20 Token address, and _addresses and _amounts filled in as follows:



Airdrop Execution


5 Use the balanceOf() function of the ERC20 contract to query the Token balance of the user address above, and successfully become 100 and 200, the airdrop is successful!



Summary


In this lecture, we introduced how to use Solidity to write ERC20 Tokens airdrop contracts, greatly increasing the efficiency of airdrops. The biggest airdrop I ever received was the ENS airdrop. How about you?


Original article link


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

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit