Airdrop Tokens with Python

Airdrop Tokens with Python

ยท

5 min read

Intro

Want to send some tokens to your audience? Perhaps you got a list of loyal customers or NFT holders you want to reward with some tokens. You can do that with this script!

This python script will mint ERC20 tokens and send them to a wallet in your allowlist. The allowlist will be a csv file. We're going to use pandas to read and load our allowlist into our python program and use thirdweb to mint the tokens and send them to our wallets.

I just want the code

OK if you just want to the code, check it out here or copy it below

from thirdweb import ThirdwebSDK
import pandas as pd
import os
from dotenv import load_dotenv

load_dotenv()
#Load the private keys
PRIVATE_KEY = os.environ.get("PRIVATE_KEY")
#The column with wallets from our dataset
col_list = ['wallet']
#Read our dataset with our wallet selection
data = pd.read_csv('wallet.csv',usecols=col_list)
#Instantiate and connect it to the thirdweb SDK
sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, "mumbai")
#Connect our smart contract to our program
token = sdk.get_token("0xB91fc509Fe99AeEef70D6d7Ce234039F2e97F48a")
#Create an array/list from our data
wallets = data['wallet']
#Define length for the for loop
n_wallets = len(wallets)
#Mint and transfer to the wallet
for i in range(n_wallets):
    print("start minting for" + wallets[i])
    minting = token.mint_to(wallets[i],1000)
    print("done minting for " + wallets[i] + "\n"+"Receipt: " + minting.transactionHash.hex())

Script Requirements

A couple of things you need to do before you can use the script.

Smart Contract

This script will work on any smart contract already deployed on an EVM chain. Now if you don't have an ERC20 smart contract, don't worry I got you. Go to thirdweb's dashboard and:

  1. Connect your wallet
  2. Double check you're connected to the chain you want to deploy to (i.e. I'm deploying to Mumbai, the test net of Polygon)
  3. Click on Deploy new contract
  4. Pick the pre-built contracts
  5. And choose the Token contract

Python libraries

Make sure you have a virtual environment fired up. If you don't know how, type this in your terminal ๐Ÿ‘‡

python3 -m env ./myv

And then to activate the virtual environment...

source ./myv/bin/activate

To use the script, we need to install Pandas, thirdweb and python-dotenv.

pip install pandas thirdweb-sdk python-dotenv

Private keys & environment variables

Finally you need to create a .env file and inside it store the private keys of your wallet.

BEFORE you do this. If you're going to push to GitHub, make sure you have a .gitignore file first and inside include the filename .env. You should never ever share your Private Keys with anyone. Anyone holding these keys, is able to empty your wallets. No questions asked.

To create the .env file type this in your terminal.

touch .env

Inside the file you can store your Private Keys like this.

PRIVATE_KEY="owiqhf32234hj5hj2vhj2234jlb52345jkl24"

Sweet, now it's time to code the Python program. Ow wait ๐Ÿ˜…

You also need a csv file with some wallet addresses. Grab an example here.

I created mine in excel, but you can create a file called wallet.csv, open it in your code editor and then add the wallet addresses like this ๐Ÿ‘‡ Screenshot 2022-07-11 at 13.40.34.png

OK. We're good to start for real now.

Let's go!

Code the script

My script has comments in them if you're looking for clarifications.

Create the python file for your program.

touch airdrop.py

Import the necessary packages.

from thirdweb import ThirdwebSDK
import pandas as pd
import os
from dotenv import load_dotenv

Load your environment variables. This will tell your program there are environment variables you will use from a .env file.

load_dotenv

Next we need to tell which environment variable we want to grab. In this case our Private Keys.

PRIVATE_KEY = os.environ.get("PRIVATE_KEY")

Inside my data (the csv file) I have a column called wallet. We want to select that column later on, but I want to define a variable for it. So my code looks cleaner.

col_list = ['wallet']

Time to load my data into the program and select the column I want. This is where we will use Pandas.

data = pd.read_csv('wallet.csv',usecols=col_list)

Now we want to use thirdweb 's sdk to connect to our desired blockchain, connect to our smart contract and use their library to interact with our smart contract.

First we instantiate the sdk and tell it which wallet and blockchain we want to connect too.

sdk = ThirdwebSDK.from_private_key(PRIVATE_KEY, "mumbai")

Next we connect our smart contract

token = sdk.get_token("0xB91fc509Fe99AeEef70D6d7Ce234039F2e97F48a")

I'm going to prep my data to use in my program. This stores a list with all the wallets in our program from our data frame.

#Create an array/list from our data
wallets = data['wallet']

OK. We have our data ready and we're connected to our smart contracts. Time to write the code to airdrop tokens to the wallets in our allowlist(the csv file).

We're going to use the mint_to method inside `thirdweb's sdk. This method takes 2 parameters. An address and the amount of tokens you want to mint and send to the address.

PS. if you already have your tokens minted and you just want to airdrop them, swap out the mint_to method with the transfer method. The transfer method also takes the same two arguments, but the tokens need to be minted already.

We will use a loop to airdrop all the tokens. First we define the amount of times we want to loop, which is equal to the length of our list.

n_wallets = len(wallets)

Next we create the loop. In this case we're minting and sending a 1000 tokens.

for i in range(n_wallets):
    minting = token.mint_to(wallets[i],1000)

We want our program to tells us what it's doing, so we'll add some messages.

for i in range(n_wallets):
    print("start minting for" + wallets[i])
    minting = token.mint_to(wallets[i],1000)
    print("done minting for " + wallets[i] + "\n"+"Receipt: " + minting.transactionHash.hex())

The last line prints out the transaction hash in the terminal. You can copy and past this on the scanner of your blockchain to look up if the transaction really happened on chain (it did).

That's it!

With using only Python you have minted and airdropped ERC20 tokens to the wallets of your choice! I will continue to share simple scripts in this series. Check out the GitHub repo, if you're looking for more scripts. Check out thirdweb's awesome python sdk here!

ย