简体   繁体   中英

Add custom field when mint new ERC721 token

i'm using the ECR721 preset smart contract from Openzeppelin for studying.

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)

铸造新令牌

I'm sure i need to add something in the mint 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. 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.
  2. Add a new argument to the mint() function
  3. Emit the MintMessage event within the mint() function
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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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