|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +pragma solidity 0.8.10; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; |
| 5 | + |
| 6 | +/// @notice Challenge Description |
| 7 | +/// You can mint awesome nfts. but the nft contract limits the number of nft you can buy at a time. Mint at least 100 nfts within one transaction. |
| 8 | +interface INft { |
| 9 | + function mint(uint256 numberOfNfts) external payable; |
| 10 | + |
| 11 | + function getNFTPrice() external returns (uint256 price); |
| 12 | +} |
| 13 | + |
| 14 | +contract Nft is INft, ERC721("Awesome NFT", "AWESOMENFT") { |
| 15 | + uint256 private constant MAX_NFT_SUPPLY = 1_000; |
| 16 | + uint256 private constant ONE_ETHER = 1e18; |
| 17 | + |
| 18 | + uint256 public totalSupply; |
| 19 | + |
| 20 | + address public owner; |
| 21 | + |
| 22 | + address public pendingOwner; |
| 23 | + |
| 24 | + function mint(uint256 numberOfNfts) public payable override { |
| 25 | + uint256 _totalSupply = totalSupply; |
| 26 | + |
| 27 | + require(numberOfNfts > 0, "numberOfNfts cannot be 0"); |
| 28 | + require( |
| 29 | + numberOfNfts <= 30, |
| 30 | + "You may not buy more than 30 NFTs at once" |
| 31 | + ); |
| 32 | + require( |
| 33 | + _totalSupply + numberOfNfts <= MAX_NFT_SUPPLY, |
| 34 | + "Exceeds MAX_NFT_SUPPLY" |
| 35 | + ); |
| 36 | + require( |
| 37 | + getNFTPrice() * numberOfNfts == msg.value, |
| 38 | + "Ether value sent is not correct" |
| 39 | + ); |
| 40 | + |
| 41 | + for (uint256 i; i < numberOfNfts; i++) { |
| 42 | + uint256 tokenId = totalSupply; |
| 43 | + _safeMint(msg.sender, tokenId); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + function getNFTPrice() public view override returns (uint256 price) { |
| 48 | + price = ONE_ETHER / MAX_NFT_SUPPLY; |
| 49 | + } |
| 50 | + |
| 51 | + // transfer ownership to a new address |
| 52 | + function transferOwnership(address newOwner) public { |
| 53 | + require(msg.sender == owner); |
| 54 | + |
| 55 | + pendingOwner = newOwner; |
| 56 | + } |
| 57 | + |
| 58 | + // accept the ownership transfer |
| 59 | + function acceptOwnership() public { |
| 60 | + require(msg.sender == pendingOwner); |
| 61 | + |
| 62 | + owner = pendingOwner; |
| 63 | + pendingOwner = address(0); |
| 64 | + } |
| 65 | + |
| 66 | + function _beforeTokenTransfer( |
| 67 | + address from, |
| 68 | + address to, |
| 69 | + uint256 |
| 70 | + ) internal override { |
| 71 | + if (from == address(0)) { |
| 72 | + totalSupply += 1; |
| 73 | + } |
| 74 | + if (to == address(0)) { |
| 75 | + totalSupply -= 1; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +contract NftSaleChallenge { |
| 81 | + Nft public immutable token; |
| 82 | + |
| 83 | + constructor() { |
| 84 | + token = new Nft(); |
| 85 | + } |
| 86 | +} |
0 commit comments