以下是一份基于ERC721标准的NFT工厂合约,每一个NFT都拥有唯一的序列id且每一个NFT都能进行溯源功能。同时,还提供了一个代理合约,用于管理工厂合约,可以发布不同种类的NFT。代理合约可以进行NFT的转移并且代理合约能够查看每一个NFT的信息状态
时间: 2024-05-19 19:13:46 浏览: 90
。pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract NFTFactory is ERC721, Ownable {
string private _name;
string private _symbol;
struct NFT {
uint256 id;
string name;
string metadata;
}
NFT[] private _nfts;
constructor(string memory name_, string memory symbol_) ERC721(name_, symbol_) {
_name = name_;
_symbol = symbol_;
}
function createNFT(string memory name_, string memory metadata_) public onlyOwner returns (uint256) {
uint256 newId = _nfts.length;
_nfts.push(NFT(newId, name_, metadata_));
_mint(msg.sender, newId);
return newId;
}
function getNFT(uint256 id) public view returns (NFT memory) {
require(_exists(id), "NFT does not exist");
return _nfts[id];
}
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override {
super._beforeTokenTransfer(from, to, tokenId);
if (from == address(0)) {
// Minting
return;
}
require(ownerOf(tokenId) == msg.sender, "Not owner of NFT");
NFT storage nft = _nfts[tokenId];
string memory metadata = nft.metadata;
emit NFTTransfer(tokenId, from, to, metadata);
}
event NFTTransfer(uint256 indexed tokenId, address indexed from, address indexed to, string metadata);
}
contract NFTFactoryProxy {
address private _factoryAddress;
constructor(address factoryAddress) {
_factoryAddress = factoryAddress;
}
function createNFT(string memory name_, string memory metadata_) public returns (uint256) {
return NFTFactory(_factoryAddress).createNFT(name_, metadata_);
}
function getNFT(uint256 id) public view returns (NFTFactory.NFT memory) {
return NFTFactory(_factoryAddress).getNFT(id);
}
function transferNFT(address to, uint256 tokenId) public {
NFTFactory(_factoryAddress).safeTransferFrom(msg.sender, to, tokenId);
}
function getNFTOwner(uint256 tokenId) public view returns (address) {
return NFTFactory(_factoryAddress).ownerOf(tokenId);
}
function getNFTMetadata(uint256 tokenId) public view returns (string memory) {
NFTFactory.NFT memory nft = getNFT(tokenId);
return nft.metadata;
}
}
阅读全文