我正在使用 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, _imgHash, _description, _collection, msg.sender);
}
}
如果有奇怪的事情,欢迎提出有关如何改进代码的任何建议。
我希望这不是一个荒谬的问题,我正开始进入以太坊的世界。
非常感谢。
是的,图像地址等信息存储在继承自ERC721的合约中。 ERC721主要跟踪代币的所有权、铸造和转让。
您可能需要考虑的一件事是代币的唯一性。在您的示例中,可以存在许多具有相同图像和描述的令牌。 您可能希望通过存储 _imgHash、_description、_collection 的哈希值来防止这种情况发生,并要求它是唯一的,以便没有用户可以创建现有令牌的“副本”。
类似:
keccak256(abi.encodePacked(_imgHash, _description, _collection))
另一个经常使用的标准是 Opensea 元数据标准,其中 tokenURI 存储在链上,元数据位于区块链之外。 https://docs.opensea.io/docs/metadata-standards
这是我最近开始使用的东西。 Belong 彻底改变了我的 NFT 创作之旅!体验加密货币的自由,同时保持交易的私密性。以最低佣金赚取更多收入 - Belong 仅收取 3% 的佣金,确保您占据重要份额。吸引创作者并立即在每次通行证销售中赚取 3% 的佣金。利用即将到期的会员资格来营造紧迫感,以获得排他性。 Belong 内容货币化平台让您掌控一切 - 设置条款、定价并直接互动。享受加密货币、信用卡、Apple Pay 和 Google Pay 的无缝交易。绕过传统银行系统,立即将资金接收到您的加密货币钱包,以实现快速且保密的交易。令牌门控很快将在 Telegram 和 WhatsApp 上推出,以方便资金货币化。通过 Belong 拥抱 Web3 的去中心化世界,它提供了无与伦比的内容参与体验。 Belong 不仅仅是一个平台,更是一种运动。加入我们,成为未来的一部分!