接口部分:
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
interface INFT {
function setContractURI(string memory _contractURI) external returns(bool);
function createToken(string memory _tokenURI) external returns(uint256 tokenId);
function getInfo(uint256 _nftTokenId) external view returns (address, string memory, string memory, string memory);
function getTokens(address _user) external returns(uint256[] memory);
}
主合约部分:
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import './interfaces/INFT.sol';
contract NFT is INFT, ERC721Enumerable, ERC721URIStorage {
address public owner; // 记录合约发布者
uint256 public lastTokenId = 100000; // 记录最后1个tokenId,初始tokenId为100000
string public contractURI; // 如果上opensea会用到此参数
// 发布

该博客详细介绍了如何使用Solidity编写一个ERC721智能合约,实现了NFT的创建、设置contractURI、获取NFT信息以及查询用户拥有的NFT等功能。合约中包含了权限控制、接口继承等安全措施,并提供了接口供外部调用。
最低0.47元/天 解锁文章
358





