betanoox
betanoox

Reputation: 81

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)

mint new token

I'm sure i need to add something in the mint function:

mint function

Upvotes: 1

Views: 1110

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43491

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);
}

Upvotes: 3

Related Questions