[Ethereum] How to use a placeholder image during minting, and then only updating metadata with the actual URI after minting

erc-721ipfsnftopenseasmart-contract-wallets

As per the title. I want to be able to have a placeholder image for my NFTs, something like this:

https://opensea.io/collection/thealcabones

Then subsequently, after launch, I then update the metadata.

How do I do this using Pinata / IPFS ? Do I need to write a specific function in my smart contract to allow this?

Best Answer

You need to declare an empty variable at the beginning of your smart contract:

string newURI

Then you need to create a function to control the URI, this function takes a variable called _newURI as a parameter, this is the CID of pinata.

//  Modify URI with this function
function setURI(string calldata _newURI) public onlyOwner{
    newURI = _newURI;
}

//  Base URI function, this won't be callable, you will use the above 
//  function instead to invoke this one
function _baseURI() internal view override returns (string memory) {
    return newURI;
}

As you can see the _baseURI( ) function returns the URI of the files, and the setURI( ) function is the one that will override the newURI variable (which is the one that _baseURI returns)

When you deploy your smart contract you will need to set the CID of pinata with the placeholder images, and whenever you want to reveal them you just need to call setURI( ) with the updated CID.