Created
June 16, 2023 04:44
-
-
Save naddika/b3b1ec7541fada49808566c823687cb3 to your computer and use it in GitHub Desktop.
MyNFT ERC1155
This file contains hidden or 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
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.18; | |
// import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; | |
import {ERC1155} from "@rari-capital/solmate/src/tokens/ERC1155.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
import {Auth, Authority} from "@rari-capital/solmate/src/auth/Auth.sol"; | |
import {ERC2981} from "@openzeppelin/contracts/token/common/ERC2981.sol"; | |
contract MyNft is ERC1155, ERC2981, Ownable { | |
constructor() ERC1155() { } | |
//TODO | |
uint public constant TOTAL_SUPPLY = 1234; | |
//tokenId -> price | |
mapping(uint => uint) private _prices; | |
function getPrice(uint tokenId) external view returns (uint) { | |
return _prices[tokenId]; | |
} | |
function setPrice(uint tokenId, uint newPrice) external onlyOwner { | |
_prices[tokenId] = newPrice; | |
} | |
string private _uri; | |
mapping(uint256 => string) private _uris; | |
function uri(uint256 id) public view override returns (string memory) { | |
string memory uri2 = _uris[id]; | |
if (bytes(uri2).length > 0) { | |
return uri2; | |
} | |
return _uri; | |
} | |
function setUri(uint256 id, string memory newUri) external onlyOwner { | |
_uris[id] = newUri; | |
if (bytes(newUri).length == 0) { | |
emit URI(_uri, id); | |
} else { | |
emit URI(newUri, id); | |
} | |
} | |
function mint(address acc, uint256 tokenId, uint256 amount, bytes memory data) external onlyOwner { | |
require(tokenId <= TOTAL_SUPPLY, "TOTAL_SUPPLY must not be exceeded"); | |
_mint(acc, tokenId, amount, data); | |
} | |
//tokenId ==> wallets | |
mapping(uint => address[]) private _whitelistAddresses; | |
//TODO RENAME | |
function mint2(uint256 tokenId, uint256 amount) public payable { | |
require(msg.value == _prices[tokenId]); | |
require(tokenId <= TOTAL_SUPPLY, "TOTAL_SUPPLY must not be exceeded"); | |
bool isFound = isInWhitelist(tokenId, msg.sender); | |
require(isFound, "wallet must be in the whitelist"); | |
(bool success, ) = payable(owner()).call{value: msg.value}(""); | |
require(success); | |
_mint(msg.sender, tokenId, amount, ""); | |
} | |
function batchMint(address acc, uint256[] memory ids, uint256[] memory amounts, bytes memory data) external onlyOwner { | |
_batchMint(acc, ids, amounts, data); | |
} | |
function addIntoWhitelist(uint tokenId, address addr) external onlyOwner { | |
bool exists = isInWhitelist(tokenId, addr); | |
if (!exists) { | |
_whitelistAddresses[tokenId].push(addr); | |
} | |
} | |
function removeFromWhitelist(uint tokenId, address addr) external onlyOwner { | |
require(addr != address(0), "address must not be 0"); | |
address[] memory wlAddresses = _whitelistAddresses[tokenId]; | |
if (wlAddresses.length > 0) { | |
for (uint i = 0; i < wlAddresses.length; i++) { | |
if (wlAddresses[i] == addr) { | |
delete _whitelistAddresses[tokenId][i]; | |
break; | |
} | |
} | |
} | |
} | |
function isInWhitelist(uint tokenId, address addr) public view returns(bool) { | |
require(addr != address(0), "address must not be 0"); | |
bool isFound = false; | |
address[] memory wlAddresses = _whitelistAddresses[tokenId]; | |
if (wlAddresses.length > 0) { | |
for (uint i = 0; i < wlAddresses.length; i++) { | |
if (wlAddresses[i] == addr) { | |
isFound = true; | |
break; | |
} | |
} | |
} | |
return isFound; | |
} | |
function burn( | |
address acc, | |
uint256 id, | |
uint256 amount | |
) external { | |
require(msg.sender == acc); | |
_burn(acc, id, amount); | |
} | |
function setRoyalty( | |
uint256 id, | |
address receiver, | |
uint96 feeNumerator | |
) external onlyOwner { | |
if (receiver == address(0)) { | |
return _resetTokenRoyalty(id); | |
} | |
_setTokenRoyalty(id, receiver, feeNumerator); | |
} | |
function setRoyalty(address receiver, uint96 feeNumerator) external onlyOwner /*requiresAuth*/ { | |
if (receiver == address(0)) { | |
return _deleteDefaultRoyalty(); | |
} | |
_setDefaultRoyalty(receiver, feeNumerator); | |
} | |
function supportsInterface(bytes4 interfaceId) | |
public | |
pure | |
override(ERC1155, ERC2981) | |
returns (bool) | |
{ | |
return | |
// ERC165 Interface ID for ERC2981 | |
interfaceId == 0x2a55205a || | |
// ERC165 Interface ID for ERC1155 | |
interfaceId == 0xd9b67a26; | |
} | |
function withdraw() public onlyOwner { | |
address receiver = msg.sender; | |
(bool res, ) = receiver.call{value: address(this).balance}(""); | |
if (!res) { | |
revert("withdrawal error"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment