Build an airdrop NFT app

Build an airdrop NFT app

Airdropping is a process of sending one or more tokens that you own to one or more address
Now why would we need AirDrop NFTs?

  • create a better user experience for users to claim NFTs instead, we can just airdrop them and bear the gas fee, this helps to onboard new users and create a smoother experience altogether.

  • create a better user experience for users to claim NFTs instead, we can just airdrop them and bear the gas fee, this helps to onboard new users and create a smoother experience altogether.

  • It doesn’t have to be only ERC721 tokens, the user can airdrop any type of token, imagine Doodles creating it’s own DoodleVerse and now you can only spend using doodle coin over there, so Doodles creator can airdrop some tokens to the holders of the project as an incentive and they can use it in the DoodleVerse to earn more.

So in short, An airdrop is a marketing strategy used by companies, organizations, or individuals to distribute free tokens or cryptocurrencies to a large number of people in a short period. The distribution is usually done through various channels such as social media, forums, or email lists.

Airdrops are often used as a way to raise awareness about a new cryptocurrency or token, reward loyal users, or encourage the adoption and usage of a particular platform. In some cases, airdrops are used as a way to incentivize users to complete certain tasks, such as joining a Telegram group, following a social media account, or referring friends to a platform.

Airdrops typically require participants to complete certain tasks or meet certain criteria to receive the free tokens. These tasks may include sharing a post on social media, filling out a survey, or downloading a mobile application.

Airdrops can be beneficial for both the distributing party and the recipients. The distributing party can raise awareness about their project, increase adoption and usage, and potentially attract new investors. For recipients, airdrops can provide an opportunity to obtain tokens or cryptocurrencies at no cost, which can potentially increase in value over time.

However, it is important to note that not all airdrops are legitimate. Some may be scams or phishing attempts aimed at stealing personal information or funds. It is important to thoroughly research and verify the legitimacy of an airdrop before participating.

The Coding Flow

Firstly remember that this contract is not restricted to airdropping only ERC721 tokens but you can even airdrop ERC1155 and ERC20 tokens you just need to change the airdrop functions a bit.

Firstly you need to create a contract that generates an ERC token. After that, you need to deploy it along with the base URI pointing to the NFT.

Secondly, you need to create a new contract that takes the address of the contract that generates the NFT as an ERC721 (if you're working with NFT) datatype.

This is where things get messed up. In the first contract, once I generate the NFT, there is a new Owner for that newly generated NFT, which is the person who. deployed it. In this case me. When I use the Second contract to send the NFT to someone else. or if someone calls for an airdrop, This Airdrop contract should be able to send the NFT to the given addresses.
Currently, if we try out any of the 2 cases, we'll run into the following error.

Now, the reason for the above error is that we have not given this contract permission to take our NFTs directly without asking us.
To give the AirDrop contract permission to send our NFTs on our behalf, which is currently owned by me, we have to tell the ERC contract that although you're giving me the NFT I also want a 3rd party to have control of my NFTs.
So that is why we go to the contract that generates the NFT, and call the setApprovalForAll function. Here we give the Contract our 3rd party address(in our case the Airdrop contract) and set the bool to true.

Lets Talk Code

TimelessChild Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts@4.7.3/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@4.7.3/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts@4.7.3/access/Ownable.sol";
import "@openzeppelin/contracts@4.7.3/utils/Counters.sol";

contract TimelessChild is ERC721, ERC721URIStorage, Ownable {
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    constructor() ERC721("TimelessChild", "TCN") {}

    function _baseURI() internal pure override returns (string memory) {
        return "ipfs://QmcFDx8avDtPXWzTFrJbdRd8m7aBJsdV95VsCum8i17qZH";
    }

    function safeMint() public {
        _tokenIdCounter.increment();
        uint256 tokenId = _tokenIdCounter.current();
        _safeMint(msg.sender, tokenId);
    }

    function bulkMint(uint16 amount) public {
        for(uint16 i = 0; i < amount; i++) {
            safeMint();
        }
    }
    // The following functions are overrides required by Solidity.

    function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }

    function tokenURI(uint256 tokenId)
        public
        pure
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return string(abi.encodePacked(_baseURI(), "/", Strings.toString(tokenId)));
    }
}

The Airdrop contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

contract Airdrop {
    function bulkAirdropNFT(
//IERC721 is the datatype of the contracts address,
// what I mean is _token is the address of the contract that generates ERC721 token.

//IERC721 stands for "Interface for ERC721 Non-Fungible Token Standard".         


IERC721 _token,
        address[] calldata _to,
        uint256[] calldata _id
    ) public {
        require(
            _to.length == _id.length,
            "Token ID's don't match the number of receivers"
        );
        for (uint256 i = 0; i < _to.length; i++) {
            _token.safeTransferFrom(msg.sender, _to[i], _id[i]);
        }
    }
}
  • _token: address of the timeless children contract

  • _to: who do you want to send the NFT to

  • _id: Id of the token to be sent

IT WORKED!!!!

I airdropped my NFT to another account I owned! One more thing I'd like to mention here. When I initially tried to add the NFT by adding the details i.e the Interacted with(to) and the token number. it threw an error saying I'm not the current owner. just wait for a while like a minute or two and try again.
It got resolved automatically. I assume its because blockchain waits for a certain number of blocks before the transaction can happen.

The previous account NFT will show you what was the NFT you sent under "Previously Owned"