Skip to content

Instantly share code, notes, and snippets.

@dievardump
Last active March 1, 2023 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dievardump/9b66434912d88020708ab388f73c2eb4 to your computer and use it in GitHub Desktop.
Save dievardump/9b66434912d88020708ab388f73c2eb4 to your computer and use it in GitHub Desktop.
Function to add to OZ ERC721 to make an efficient airdrop
contract ERC721 {
// take OpenZeppelin contract and add this at the end:
uint256 private _lastTokenId;
// uncomment next line if you need totalSupply
// uint256 public totalSupply;
/// @dev be careful this function expects recipients to all be unique and not be address(0)
function _airdrop(address[] calldata recipients) internal virtual {
uint256 length = recipients.length;
uint256 tokenId = _lastTokenId;
address to;
for (uint256 i; i < length;) {
to = recipients[i];
unchecked {
// _airdrop should only be used at the very start of the contract
// so to should not have any balance
_balances[to] = 1;
++tokenId;
++i;
}
_owners[tokenId] = to;
emit Transfer(address(0), to, tokenId);
}
_lastTokenId = tokenId;
// uncomment next line if you need totalSupply
// totalSupply += length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment