2. 特性有如下几项,根据需要选择即可。
点击openzeppelin开发向导上方的“Open in Remix”,或者将代码复制到你自己在Remix中的工作区中,我选择了后者。
// SPDX-License-Identifier: MITpragma solidity ^0.8.20;import "@openzeppelin/contracts/token/ERC721/ERC721.sol";import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";import "@openzeppelin/contracts/access/Ownable.sol";contract PearlArt is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable {uint256 private _nextTokenId;constructor(address initialOwner)ERC721("PearlArt", "PAT")Ownable(initialOwner){}function safeMint(address to, string memory uri) public onlyOwner {uint256 tokenId = _nextTokenId++;_safeMint(to, tokenId);_setTokenURI(tokenId, uri);}// The following functions are overrides required by Solidity.function _update(address to,uint256 tokenId,address auth) internal override(ERC721, ERC721Enumerable) returns (address) {return super._update(to, tokenId, auth);}function _increaseBalance(address account, uint128 value)internaloverride(ERC721, ERC721Enumerable){super._increaseBalance(account, value);}function tokenURI(uint256 tokenId)publicviewoverride(ERC721, ERC721URIStorage)returns (string memory){return super.tokenURI(tokenId);}function supportsInterface(bytes4 interfaceId)publicviewoverride(ERC721, ERC721Enumerable, ERC721URIStorage)returns (bool){return super.supportsInterface(interfaceId);}}
注意:
这一步主要是配置NFT在opensea显示的一些属性,具体配置可以参考这里。这里给出一个配置文件的样例,将样例配置文件命名为metadata.json。
注:修改本示例文件中的image即可
{ "description": "This NFT proves I've created and deployed my daugther's colored pencil art on Sepolia with my first ERC721","image": "https://ipfs.io/ipfs/","name": "A cool NFT", "attributes": [{"trait_type": "Base", "value": "Starfish"}, {"trait_type": "Level", "value": 5}, {"trait_type": "Stamina", "value": 1.4}, {"display_type": "boost_number", "trait_type": "Aqua Power", "value": 40}, {"display_type": "boost_percentage", "trait_type": "Stamina Increase", "value": 10}, {"display_type": "number", "trait_type": "Generation", "value": 2}]}
生成metadata.json文件后,将该文件也上传到IPFS,同时复制该文件的CID(后面会用到)
1. 在Remix部署合约的页面,选择你上面部署的合约;
2. 调用safeMint方法,铸造你的第一个NFT;
参数说明:
3. 上述步骤完成后,你可以调用合约的tokenURI方法来查看刚才设置的结果,该函数的参数为tokenID,由于我们的ID是从0自增的,因此此处输入0查看结果;
本文中的操作均在测试网络中进行,因此我们打开opensea的测试网络来查看我们的NFT,步骤如下:
3. 登录成功后,点击用户信息–>Profile即可看到我们发布的NFT。
注:由于网络问题,你的NFT图片可以一直不显示,但我们可以看到我们NFT合约的名称,如下: