soliditynft

How to add attributes to NFT during minting in Solidity?


I have a code in Solidity to mint a NFT.

When code is executed, I have a visible NFT on testnets.opensea.io. But I would like to see on OpenSea the attributes name and surname entered in my smart contract. This surname and first name change regularly. I want to do this dynamically.

Is it possible ? How ?

Thanks !

// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
 
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
 
contract MyNFT is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
 
    struct Reward{
        string name,
        string surname
    }
    Reward[] rewards;
 
    constructor() ERC721 ("MyNFT", "MYN") {}
 
    function mint(address _user, string memory _tokenURI, string memory _name, string memory _surname) public returns (uint256)
    {
        _tokenIds.increment();
    rewards.push(Reward(_name, _surname));
        uint256 newItemId = _tokenIds.current();
        _mint(_user, newItemId);
        _setTokenURI(newItemId, _tokenURI);
 // what about _name & _surname added to the NFT ???
        return newItemId;
    }
}

Solution

  • Assuming you modified the metadata JSON file, and you want this change reflected on OpenSea.

    After you modified the file, you need to call their endpoint Refresh NFT Metadata so that they can register the change.

    Note: They might reflect the new metadata with a delay.