Created
February 17, 2022 12:15
-
-
Save himawari2021/80f9526abe843694912c89bd78b55aa1 to your computer and use it in GitHub Desktop.
cursed NFT which can not be transfered
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Strings.sol"; | |
import "https://github.com/Brechtpd/base64/blob/main/base64.sol"; | |
contract MyNFT4 is ERC721 { | |
bool public lock = false; | |
address owner; | |
uint public nextTokenId = 0; | |
constructor() ERC721("Cursed NFT2", "CNFT"){ | |
owner = msg.sender; | |
} | |
function mint() public { | |
uint256 tokenId = nextTokenId; | |
nextTokenId = nextTokenId + 1; | |
super._safeMint(msg.sender, tokenId); | |
} | |
function _transfer( | |
address from, | |
address to, | |
uint256 tokenId | |
) internal virtual override { | |
//lock=trueのとき、自分宛(ミント時)以外は持ち主変更はエラーにする | |
if(to != msg.sender){ | |
//revert("can not trasfer!"); | |
require(!lock, "can not transfer!"); | |
} | |
super._transfer(from, to, tokenId); | |
} | |
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { | |
string memory svg = getSvg(tokenId); | |
bytes memory json = abi.encodePacked( | |
'{"name": "#' | |
,Strings.toString(tokenId) | |
,'", "description": "this NFT can not be transfer!", "image": "data:image/svg+xml;base64,' | |
,Base64.encode(bytes(svg)) | |
,'", "attributes": [{"value": "Cursed"}]' | |
,'}' | |
); | |
return string(abi.encodePacked("data:application/json;base64,", Base64.encode(json))); | |
} | |
function getSvg(uint tokenId) public pure returns (string memory) { | |
return string(abi.encodePacked( | |
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">' | |
,'<rect width="100%" height="100%" fill="#000000" />' | |
,'<text x="10%" y="70%" font-size="50px" fill="#ffffff">' | |
,string(unicode"呪") | |
,'</text>' | |
,'<text x="10%" y="90%" font-size="8px" fill="#ffffff">' | |
,string(unicode"(非売品) #") | |
,Strings.toString(tokenId) | |
,'</text>' | |
,'</svg>' | |
)); | |
} | |
function setTransferLock(bool _lock) public { | |
require (owner == msg.sender); | |
lock = _lock; | |
emit LockEvent(lock); | |
} | |
event LockEvent(bool _lock); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment