How to Airdrop NFTs on Ethereum: A Step-by-Step Guide

Idowu Daniel
Crossmint Tech
Published in
8 min readJul 1, 2022

Introduction

NFTs are growing rapidly in the crypto world, but not everyone understands their true potential. They’ve progressed from “random jpegs” to self-contained apps, access tokens, and communities, among other things. NFTs are only continuing to grow, thanks to greater inventiveness and support across various blockchains.

Everyone is ecstatic about the once-in-a-lifetime opportunity to start their own non-fungible token enterprise and eventually sell their NFTs to the general public. Airdrops are used to spread awareness about the cryptocurrency project with the aim of generating awareness about the project.

Simply said, if you own an NFT token, you own a digital representation of something one-of-a-kind that only exists in one copy, such as a work of art or a digital right. At the same time, using NFT in a broader sense allows you to digitize any real-world services or items by producing a digital equivalent. In this article, we will show you how to do NFT drops on Ethereum.

Let’s get started!

What are NFT airdrops?

An NFT airdrop is a free release of NFTs or digital assets to a web3 wallet address as a promotion, or as a bonus for completing an activity or purchasing another digital asset.

NFT airdrops are quite similar to crypto airdrops and the main purpose why NFT airdrops are done is to increase the awareness of an NFT collection. Airdrops are a means for NFT projects to continue to give value to their holders by essentially giving them a free product.

Users will provide their wallet address upon registration in order to receive the free NFT, and once the Ethereum airdrop for NFTs is complete, every successful user will receive an NFT into their wallet, exactly as if they had purchased the NFT on a sales platform. Furthermore, for newer projects, if they announce or even indicate that they will be doing an airdrop in the future, people will be lining up to try out their platform. Given that some people have gotten thousands of dollars in airdrops simply by being early adopters, the risk to reward ratio is highly appealing.

The Project

In this project we will focus on the project setup, airdrop smart contract and to simply interact with it. This will help you understand how NFT airdrops and minting works.

What You Will Learn

  • Upload the NFT image and metadata to IPFS via NFT storage
  • Set up the Rinkeby Testnet on MetaMask and get test ETH
  • Set up Truffle Development Environment
  • Write our NFT smart contract using OpenZepplin’s secure smart contract implementations for the drop.
  • Test deployment locally using Ganache
  • Deploy to Ethereum testnet using Truffle
  • Interact with your contract and airdrop to friends.

Setting Up The Development Environment

Install Truffle and Ganache

Truffle

Truffle is a world-class development environment, testing framework, and asset pipeline for Ethereum Virtual Machine (EVM)-based blockchains that aims to make life easier for developers. To install it from the command line, we’ll need NodeJS v16, as well as Node Package Manager (npm). You can install node from here be sure to download the LTS release. Once it’s installed, we can run the following command in the terminal to install Truffle:

npm install -g truffle

You can run the command below to confirm Node was successfully installed

truffle version

Ganache

Ganache is a personal blockchain server that allows for the rapid development of Ethereum and Corda distributed applications. Ganache can be used throughout the development cycle, allowing you to develop, deploy, and test your dApps in a secure and deterministic environment. It can be installed using this command:

npm install ganache — global

To also confirm that Ganache was successfully installed run the command below

ganache version

Setting up our project

Next you will create a Folder and name it agate-nft, this is where we are doing to be doing everything pertaining to the project. To do this run:

mkdir agate-nft && cd agate-nft

Next, we have to run a command that will set up all of the file structure that we’ll be working with.

truffle init

Once this unboxing is completed, you’ll now have a project structure:

  • contracts/: Directory for Solidity contracts
  • migrations/: Directory for scriptable deployment files
  • test/: Directory for test files for testing your application and contracts
  • truffle-config.js: Truffle configuration file
  • LICENSE: License for your project

We can get rid of the Migrations.sol file under the contracts directory because we won’t be using them.

Finally, in this section we will be installing the OpenZepplin contract library for secure smart contract development as well as the implementation of standards like ERC20 and ERC721.

npm install @openzeppelin/contracts

Now that all our installation is setup, we move to writing our smart contract.

Writing smart contracts

We will not be going into what smart contracts are here, if you would like to learn more read our article on Ethereum smart contracts.

https://medium.com/crossmint-tech/ethereum-smart-contracts-explained-7dec280443ce

Below you can see a sample ERC-721 token smart contract extended from OpenZepplin. In your contracts folder, create a file and give it the name AgateNFT.sol Inside of the file, define the following structures.

Now, let’s break this down:

// SPDX-License-Identifier: MIT

Solidity 0.6.8 adds SPDX license identifiers, allowing developers to specify which license the contract uses. The MIT license is also used by OpenZeppelin Contracts.

pragma solidity ^0.8.0;

The second line is used to tell the solidity compiler what version of solidity we’re using, this is important as this language is relatively new & thus has ongoing changes. The pragma directive specifies the compiler version to be used for the current Solidity file.

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

This imports the smart contract library from OpenZepplin library and the function Counters which we will be using later in the code.

contract AgateNFT is ERC721, Ownable

The code above declares our contract name as AgateNFT and inherit it from the OpenZeppelin ERC721 and that it is a Ownable contract.

using Counters for Counters.Counter;

Means that assign all the functions inside Counters library like current()or increment() to the counter struct. It helps in counting safely and also converting data to strings.

Counters.Counter private _tokenIdCounter;

The above represent the state variable for storing the current token Id

constructor() ERC721(“AgateNFT”, “AGN”) {}

The above helps us Initialize the constructor whilst defining the name, and symbol of our token.

function safeMint(address to, string memory uri) public onlyOwner {uint256 tokenId = _tokenIdCounter.current();_tokenIdCounter.increment();_safeMint(to, tokenId);_setTokenURI(tokenId, uri);}

Function safeMint, mints the NFT to the provided address, using the provided metadata URI and only the wallet address that deployed this contract can call this function.

function tokenURI(uint256 tokenId)publicviewoverride(ERC721, ERC721URIStorage)returns (string memory)

The function above return the Token URI which is important for viewing on Opensea.

Configuring Truffle

Open up truffle-config.js and modify solc (Solidity Compiler) as shown below. Notice the changes we made.

We’ll be using Ganache as our local Ethereum test network. So, uncomment networks and replace ‘development’ with ‘ganache’.

Testing Deployment With Ganache

Let’s create the migration script in the migrations folder before we start testing the smart contract. Head to the migrations folder and create a new file called deploy_contracts.js. Paste the following code inside:

2_deploy_contracts.js file.const AgateNFT = artifacts.require(“AgateNFT”);module.exports = function (deployer) {deployer.deploy(AgateNFT);};

Before we move to configure Rinkeby, we need to save your wallet mnemonic from Metamask, Get some test ether either from the Chainlink or Alchemy faucet, and get a Rinkeby API key from Infura.

We will need to install the Truffle HD wallet provider — a convenient and easy to configure network connection to Ethereum. You can do that by installing the simple configuration below:

npm i @truffle/hdwallet-provider

In truffle-config.js, update the sections as shown below:

Your mnemonic should be stored in touch .secret && open .secret and the account will need to have Rinkeby testnet. Now deploy (or migrate) your contract to Rinkeby as follows. By default, Truffle only deploys to the local developer network.

truffle deploy — network rinkeby

You should see a log message as shown below:

Finally, we have our contract successfully migrated to our local blockchain.

Interacting with the Smart Contract

We will go ahead and interact with it using Truffle console. The Truffle Console is a powerful tool that allows us to use JavaScript to interact directly with our contract without having to set up a frontend. To use it, type the command:

truffle console — network rinkeby

Access your deployed contract instance via:

AgateNFT.deployed().then(function(instance){return instance });

Or you can retrieve the instance by its public address via:

new web3.eth.contract(AgateNFT.abi, contractAddress)

Where AgateNFT.abi is the locally compile abi, and contractAddress is your publicly deployed contract instance. The contractAddress is wrapped in quotes and you can find that in the deployment log.

Conclusion

In this tutorial, we have gone through an easy way of doing NFT airdrops on Ethereum.

What is Crossmint?

Our goal at Crossmint is to help make NFTs as accessible as possible. Our first product, Crossmint Pay, is a tool for NFT creators to accept credit card payments. It allows anyone to create an NFT in under a minute using only their email and credit card, with no need for a wallet or cryptocurrency.

We also have our minting API as a live product in beta, you can reach out to us to get access. If you want to skip all these steps, you can also create an NFT smart contract with one line of code making it easy for anyone to produce NFT collections and expand the audience that can own them. Creators can use Crossmint for free, and we currently support Solana, Ethereum, Polygon, and Avalanche.

Follow us on Medium so you are notified when we post another article! If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet.

Are you a developer ready to get started? Visit the Crossmint documentation and see how you can integrate Crossmint into your project in less than 5 lines of code.

--

--

Idowu Daniel
Crossmint Tech

Daniel is a Software Developer & Technical Writer. He is also a Web3 advocate passionate about the developers' community.