如何解决哪些信息持有 NFT?
我正在使用 solidity 开发 NFT 市场,特别是我正在 OpenZeppelin 的 ERC-721 智能合约之上创建我自己的智能合约。目前我的 NFT 有 5 个图像属性(id、图像、描述、集合和图像),我在上传时保存了 ipfs 开发的哈希值。
我的问题是在哪里保存所有这些属性,因为我有具有上述属性的 Image 结构,我将它添加到一个数组中,并使用数组中 Image 对象的 id 和地址创建 NFT创作者。我的意思是,我保存了 ERC-721 合约之外的所有信息,所以我不太明白 NFT 是什么,因为属性不是来自 NFT,而是 NFT 是我的结构的一个属性。>
我是否正确地实施了它并且 ERC-721 标准只是 NFT 的必要功能,还是我将信息保存在它不接触的地方?
我的代码目前如下:
pragma solidity ^0.5.0;
import "./ERC721Full.sol";
contract NftShop is ERC721Full {
string public name;
Image[] public nft;
uint public imageId = 0;
mapping(uint => bool) public _nftExists;
mapping(uint => Image) public images;
struct Image {
uint id; //id of the nft
string hash; //hash of the ipfs
string description; //nft description
string collection; //what collection the nft bellongs
address payable author; //creator of the nft
}
//Event used when new Token is created
event TokenCreated(
uint id,string hash,string description,string collection,address payable author
);
constructor() public payable ERC721Full("NftShop","NFTSHOP") {
name = "NftShop";
}
//uploadImage to the blockchain and mint the nft.
function uploadImage(string memory _imgHash,string memory _description,string memory _collection) public {
// Make sure the image hash exists
require(bytes(_imgHash).length > 0);
// Make sure image description exists
require(bytes(_description).length > 0);
// Make sure collectionage exists
require(bytes(_collection).length > 0);
// Make sure uploader address exists
require(msg.sender!=address(0));
// Increment image id
imageId ++;
// Add Image to the contract
images[imageId] = Image(imageId,_imgHash,_description,_collection,msg.sender);
//Mint the token
require(!_nftExists[imageId]);
uint _id = nft.push(images[imageId]);
_mint(msg.sender,_id);
_nftExists[imageId] = true;
// Trigger an event
emit TokenCreated(imageId,msg.sender);
}
}
欢迎任何关于如何改进代码的建议(如果有奇怪的地方)。
我希望这不是一个荒谬的问题,我是从以太坊的世界开始的。
非常感谢。
解决方法
是的,图片地址等信息存储在继承自ERC721的合约中。 ERC721 主要跟踪代币的所有权、铸造和转移。
您可能需要考虑的一件事是令牌的唯一性。在您的示例中,可以存在许多具有相同图像和描述的标记。 您可能希望通过存储 _imgHash、_description、_collection 的哈希值来防止这种情况发生,并要求它是唯一的,这样任何用户都不能创建现有令牌的“副本”。
类似:
keccak256(abi.encodePacked(_imgHash,_description,_collection))
另一个常用的标准是 Opensea 元数据标准,其中 tokenURI 存储在链上,元数据位于区块链之外。 https://docs.opensea.io/docs/metadata-standards
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。