Summary
In this tutorial, we’ll guide you through Hedera’s Frictionless Airdrop flow, which streamlines token distribution while giving recipients greater control over the tokens they receive. Throughout the tutorial, you’ll work with four new transaction types designed to simplify the airdrop process and safeguard users’ token holdings. Each transaction type plays a unique role in enabling frictionless, user-friendly airdrops. Before diving into the step-by-step guide, here’s a quick overview of the transaction types you’ll encounter:- TokenAirdropTransaction: This transaction allows you to distribute tokens to multiple recipients in a single transaction, even if the receiving accounts haven’t pre-associated with the token. It also supports pending transfers when accounts don’t have the necessary token associations.
- TokenClaimAirdropTransaction: When a recipient doesn’t have available token associations, this transaction allows them to claim their tokens from a pending airdrop, ensuring full control over which tokens they want to accept.
- TokenRejectTransaction: If a user receives unwanted tokens, they can use this transaction to transfer them back to the token’s treasury without incurring any custom fees or royalties.
- TokenCancelAirdropTransaction: This transaction allows the sender to cancel a pending airdrop if the recipient hasn’t claimed it yet. It gives the sender flexibility to manage unclaimed token transfers.
Prerequisites
We recommend you complete the following introduction to get a basic understanding of Hedera transactions. This example does not build upon the previous examples.- Get a Hedera testnet account.
- Set up your environment here.
.envfile with this additional variable added:
In this tutorial, we would like to airdrop a fungible token, “GameGold,” and a non-fungible token to our gaming community. We would like to send an equal amount of tokens to the top three players in our community without them knowing about the upcoming airdrop. Let’s learn!
Step 1: Setup and Account Creation
First, we need to create the three accounts that will receive our airdrop and a treasury account. Each account will have a different configuration for automatic token association slots:- Alice: Unlimited token association slots by setting the value to
-1which means she can receive as many tokens as she wants, and they will be automatically associated with her account. - Bob: Only has 1 free token association slot.
- Charlie: Has no free token association slots.
- Treasury: This account will send the airdrop as it controls the treasury of our token collections.
dotenv package to load the required environment variables. Further, it creates the accounts for Alice, Bob, and Charlie.
Step 2: Create a New Token
Let’s create a new token called “GameGold” that we will airdrop to our gaming community. We’ll mint300 tokens in total and distribute them equally among our top three players, Alice, Bob, and Charlie, with each player receiving 100 tokens.
Step 3: Airdrop Tokens with Frictionless Airdrop
Ok, we are ready to airdrop theGameGold token to the top 3 players Alice, Bob, and Charlie. Each of them receives 100 tokens. The code should print one pending airdrop for Charlie because Alice and Bob had free token association slots. Only Charlie didn’t have any free slots, so he had to claim the token himself.
null.
Step 4: Charlie Claims the Airdrop
Alright, Charlie still needs to claim his tokens. Let’s do this first using theTokenClaimAirdropTransaction and verify its balance to ensure he has received the 100 units of our GameGold token.
Step 5: Airdrop NFTs to Alice, Bob, and Charlie
We would like to airdrop Game NFTs to Alice, Bob, and Charlie in this step. Bob has already used his free association slot, and Charlie also doesn’t have any free association slots. This part of the tutorial will teach you how to use theTokenCancelAirdropTransaction and TokenRejectTransaction.
First, we need to create the NFT collection and mint new NFts. Then, we can airdrop the NFTs. Two pending airdrops should be created for Bob and Charlie as they don’t have free auto association slots available.
Step 6: Cancel Airdrop for Charlie
When airdropping the NFTs, we realized we made a mistake: Charlie is not actually our third-best player, but Greg is. To fix this, we need to cancel the pending airdrop to Charlie so that the NFT returns to our treasury account. Once the NFT is back, we can airdrop it to Greg, who is the rightful recipient. Let’s learn how to use theTokenCancelAirdropTransaction to cancel an airdrop for a specific account.
Step 7: Token Reject Already Received Airdrop
And lastly, as Charlie is not the rightful owner of the received fungible token, we’ve asked him if he wants to return the 100 units of ourGameGold token. Charlie has agreed and can return the token without paying any fees or royalties using the TokenRejectTransaction.
The
reject functionality can be used to return an already claimed or a
successful airdrop into your account. It can’t be used to reject a pending
airdrop.GameGold tokens will be returned to the treasury account.
Concluding Notes on Transaction Costs
When working with Frictionless Airdrops on Hedera, it’s important to consider the transaction costs associated with each step. Here are some key points to keep in mind:- TokenAirdropTransaction Costs: The sender pays all fees for distributing tokens, including fees for token association and the first auto-renewal period for newly created associations. This makes it easier and less costly for recipients, as they don’t need to pre-associate tokens or cover those fees themselves.
- TokenClaimAirdropTransaction Costs: When a recipient claims tokens from a pending airdrop, they only need to cover the transaction cost for claiming. The sender still covers token association costs, ensuring the user experience remains frictionless.
- TokenRejectTransaction Costs: Rejecting unwanted tokens is free from custom fees or royalties. This ensures users can easily clean up their accounts without worrying about incurring additional costs for getting rid of spam or unwanted tokens.
- TokenCancelAirdropTransaction Costs: Canceling a pending airdrop comes with a nominal fee for the sender, incentivizing mindful use of the airdrop system. It provides flexibility for managing unclaimed tokens but also discourages excessive or careless airdrops.
Code Check ✅
.env file example
.env file example
JavaScript
JavaScript
Go
Go