Mint your NFTs with Stable Diffusion

Gelato Team

Jun 27, 2023

In the current era of rapid technological evolution, the convergence of AI and web3 presents an intriguing narrative. Rather than a competition, it is a story of synergy, where AI accelerates adoption of decentralized networks by enabling smart contracts to securely interact with machine learning models.

A key advancement here is generative modeling, AI that creates new data from learned patterns. Among these, the diffusion model is particularly powerful—turning random noise into coherent images, guided by prompts when needed.

In this tutorial, we’ll demonstrate how to combine Gelato’s Web3 Functions with StabilityAI’s Stable Diffusion to create NFTs powered by AI-generated art. Stable Diffusion will generate images, while Web3 Functions bridge them into NFTs on-chain.

Workflow

  1. GelatoNft Smart Contract: Mint NFTs with suspense until their reveal.

  2. Stable Diffusion: Generate unique images as NFT metadata.

  3. Gelato’s Web3 Functions: Automate assignment of AI images to NFTs.

Prerequisites

Set Up Your Dev Environment

git clone https://github.com/gelatodigital/W3F-NFT-AI
cd W3F-NFT-AI
yarn install

Configure Secrets

  • Alchemy ID

  • Private Key (from MetaMask)

  • Stable Diffusion API Key

  • NFT.Storage API Key

Fill these into .env files as per templates provided.

GelatoNft Contract

The contract mints NFTs with a placeholder URI until revealed by Gelato Web3 Functions.

function mint(bool _isNight) external whenNotPaused {
  require(!hasMinted[msg.sender], "Already minted!");
  tokenIds.increment();
  uint256 newItemId = tokenIds.current();
  _mint(msg.sender, newItemId);
  _setTokenURI(newItemId, notRevealedUri);
  hasMinted[msg.sender] = true;
  tokenIdByUser[msg.sender] = newItemId;
  nightTimeByToken[newItemId] = _isNight;
  emit MintEvent(newItemId);
}

function revealNft(uint256 tokenId, string memory tokenURI) 
  external onlyGelatoMsgSender {
  _setTokenURI(tokenId, tokenURI);
  emit MetadataUpdate(tokenId);
}

Web3 Function Flow

  • Validate Inputs: Fetch contract address and secrets for NFT.Storage & Stable Diffusion.

  • Process NFTs: Identify unrevealed tokens.

  • Generate Image: Call Stable Diffusion API with description prompts.

  • Upload Metadata: Store result on IPFS via NFT.Storage.

  • Update Contract: Call revealNft with tokenId and metadata URL.

Example: Image Generation

const stableDiffusionResponse = await fetch(
  "https://stablediffusionapi.com/api/v3/text2img",
  {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      key: stableDiffusionApiKey,
      prompt: nftProps.description
    })
  }
);
const stableDiffusionData = await stableDiffusionResponse.json();

Upload to IPFS

const imageBlob = (await axios.get(imageUrl, { responseType: "blob" })).data;
const nftStorage = new NFTStorage({ token: nftStorageApiKey });
const imageFile = new File([imageBlob], `gelato_nft_${tokenId}.png`, { type: "image/png" });
const metadata = await nftStorage.store({
  name: `GelatoNFT #${tokenId}`,
  description: nftProps.description,
  image: imageFile,
  attributes: nftProps.attributes,
});

Deploy & Run

  1. Deploy Contract:

    npx hardhat run deploy/deploy-contract.ts --network mumbai
  2. Deploy Web3 Function:

    npx hardhat w3f-deploy stable-diffusion-nft
  3. Create Task: Visit Gelato App with the IPFS CID to schedule your function.

Monitor Your Task

  • Executions over time

  • Logs and outputs

  • Secrets and storage state

Conclusion

By combining Stable Diffusion AI with Gelato’s Web3 Functions, you can mint NFTs with dynamic AI-generated art seamlessly connected to your smart contracts. This blend of AI and blockchain enriches NFT experiences with automation, creativity, and decentralization.

Dive Deeper with Gelato

Join the community on Discord. Web3 Functions are live in private beta. Read the documentation and explore the tutorials. Apply here to be among the first testers.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.