Created
December 5, 2022 15:02
-
-
Save ajays97/e4aa4c9935d268aeac8fd2e6686263e3 to your computer and use it in GitHub Desktop.
Solidity Smart Contract to publish Bulk NFTs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
// ERC-721 token interface | |
interface IERC721 { | |
function totalSupply() external view returns (uint256); | |
function balanceOf(address _owner) external view returns (uint256); | |
function ownerOf(uint256 _tokenId) external view returns (address); | |
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable; | |
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes memory _data) external payable; | |
function approve(address _approved, uint256 _tokenId) external payable; | |
function setApprovalForAll(address _operator, bool _approved) external payable; | |
function getApproved(uint256 _tokenId) external view returns (address); | |
function isApprovedForAll(address _owner, address _operator) external view returns (bool); | |
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId); | |
event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId); | |
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved); | |
} | |
// This contract extends the ERC-721 interface | |
// and adds the ability to mint new NFTs in bulk | |
contract NFTBatchMinter is IERC721 { | |
address public owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
// Add a new batch of NFTs to the supply | |
function mintBatch(uint256[] memory _tokenIds) external onlyOwner { | |
// Validate that the batch is not empty | |
require(_tokenIds.length > 0, "Batch must not be empty"); | |
for (uint256 i = 0; i < _tokenIds.length; i++) { | |
// Mint a new NFT with the specified token ID | |
_mint(_tokenIds[i]); | |
// Emit the Transfer event | |
emit Transfer(address(0), owner, _tokenIds[i]); | |
} | |
} | |
// Internal function to mint a new NFT | |
function _mint(uint256 _tokenId) internal { | |
// Validate that the token ID is not already in use | |
require(_tokenId >= totalSupply(), "Token ID must not be in use"); | |
// Add the token to the owner's balance | |
// and set the owner as the contract owner | |
_balances[owner].push(_tokenId); | |
_owners[_tokenId] = owner; | |
} | |
// Restrict access to the mintBatch function | |
// to the contract owner | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Only the owner can mint new tokens"); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment