[英]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.
事件被永久存储并且公开可读。
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)可能会忽略非标准事件并且因此不会显示铸造的代币。mint()
functionmint()
function 添加一个新参数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.