简体   繁体   English

如何在 ERC721 代币中添加自定义属性(链上)?

[英]How to add custom attributes (on-chain) in ERC721 token?

i'm learning solidity using Openzeppelin Framework.我正在使用 Openzeppelin 框架学习可靠性。

Currently i'm using the ERC721 Preset Contract ( https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol )目前我正在使用 ERC721 预设合同( https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol

I would like to add custom attributes for each token minted in order to store some important data on the blockchain (i would not use for certain attributes the REST API, but i prefer to write them on the chain).我想为每个铸造的代币添加自定义属性,以便在区块链上存储一些重要数据(我不会将 REST API 用于某些属性,但我更喜欢将它们写在链上)。

Each token will have different data.每个令牌将有不同的数据。

string: Name
string: Surname
bytes32: 0x63383a61613a62323a30373a63383a323020

Is it possible to do it creating some new functions (without editing the mint function), allowing only the addresses that have the minter role?是否可以创建一些新功能(无需编辑铸币功能),只允许具有铸币者角色的地址?

Yes.是的。

You can create a mapping between the tokenId and whatever you want it to have.你可以在 tokenId 和你想要的任何东西之间创建一个映射。 Let's call it name .我们就叫它name吧。

mapping(uint256 => string) name;

If you have a lot of attributes, you can do a mapping of a struct.如果你有很多属性,你可以做一个结构的映射。

    struct Character {
        uint256 strength;
        uint256 dexterity;
        uint256 constitution;
        uint256 intelligence;
        uint256 wisdom;
        uint256 charisma;
        uint256 experience;
        string name;
    }

    Character[] public characters;

    mapping(uint256 => uint256) tokenIdToCharacterIndex;

Then, just make sure to update your tokenURI to include the on-chain attributes so that they show up in NFT marketplaces.然后,只需确保更新您的 tokenURI 以包含链上属性,以便它们出现在 NFT 市场中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM