Oh cool, you’re making an NFT project!
Let’s talk about keeping your NFTs secret until they’re minted, and how to accomplish it.
Reveal-on-mint is the concept of “revealing” (to the internet/world) the metadata and digital art (static image, video clip, whatever) of a specific NFT, only after that NFT has been bought and paid for on-chain. Big surprise huh.
Things to notice in the above contract:
We’ll build an <code>express<code> server in NodeJS to implement our revealing logic.
The idea is simple.
We are building a “metadata / image API”, which obviously can accept requests from anywhere, such as OpenSea or our own web app. The API technically has access to the full set of images and metadata, as all of those were generated ahead of time. We can write logic to intelligently return actual data or an error message, for any request, based on the most recently minted ID.
The following code exports a function which has an ethers.js contract object as an input parameter, and returns an express object. The returned express object from this code should be used to start up an http server.
If you’re familiar with express apps, you'll recognize the last two “sections” of this code. They are simple middleware functions attached to the express object, which act as the routes for getting metadata and getting images.
The first few lines are interesting.
First, we read the most currently minted token ID from the contract, and save that in a variable in memory. (We’re doing this without a database yay)
Then, we create an event listener for the Mint event, with a callback handler function that simply updates that in-memory variable with the new ID. That’s the sauce right there. As people mint tokens, our API passively receives events and updates its own state to reflect the most recently minted ID.
Finally, we create a simple helper function called checkApproval which accepts a tokenId, and returns true or false indicating whether or not this ID has been minted or not.
In our request handlers, we extract the tokenId for the request from the URL, pass that into checkApproval, then respond appropriately with actual data, or error messages.
Now go get that project launched, King 👑
We are always interested in connecting with people who want to fund, innovate
or work in the open financial system.