简体   繁体   English

铸造新 ERC721 令牌时添加自定义字段

[英]Add custom field when mint new ERC721 token

i'm using the ECR721 preset smart contract from Openzeppelin for studying.我正在使用 Openzeppelin 的 ECR721 预设智能合约进行学习。

I would like to add a new field when i mint new token to store a string (public).当我铸造新令牌来存储字符串(公共)时,我想添加一个新字段。

At the moment there is only the field "to:address" (screenshot below)目前只有“to:address”字段(截图如下)

铸造新令牌

I'm sure i need to add something in the mint function:我确定我需要在薄荷 function 中添加一些东西:

薄荷功能

Easiest way to store the message on blockchain is to emit an event.将消息存储在区块链上的最简单方法是发出事件。 Event is permanently stored and publicly readable.事件被永久存储并且公开可读。

  1. Define new MintMessage event outside the mint() function.mint() function 之外定义新的MintMessage事件。 I don't recommend expanding the default Transfer event that is used during minting, because external tools (such as Etherscan) might ignore the non-standard event and not show minted tokens as a result.我不建议扩展在铸造期间使用的默认Transfer事件,因为外部工具(例如 Etherscan)可能会忽略非标准事件并且因此不会显示铸造的代币。
  2. Add a new argument to the mint() functionmint() function 添加一个新参数
  3. Emit the MintMessage event within the mint() functionmint() function 中发出MintMessage事件
event MintMessage(string message);

function mint(address to, string message) public virtual {
   // keep the rest of your function as is
   // add a new line emiting the event to the end of the function
   emit MintMessage(message);
}

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

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