Minting with Rarible

Rarible is a multichain NFT protocol and marketplace that provides SDKs and APIs to build NFT apps across several EVM networks.

For NFT minting, the Rarible Multichain SDK lets you mint into your own or shared collections and immediately create sell orders.

Because the Rarible SDK doesn't handle IPFS upload, our examples here use Pinata as an IPFS host. You will need a Pinata account in order to obtain a JWT key.

They will also require you to have:

  • An already-deployed ERC-721 contract on Chiliz Chain. You need to deploy an OpenZeppelin ERC-721 contract on Chiliz Chain yourself. Remix IDE gives you an in-browser to do it. Don't forget to verify the contract using a block explorer!

  • A Rarible API key. See their Getting Started on how to obtain one:

To use the Rarible SDK, we need a few installations:

npm i @rarible/sdk @rarible/types ethers dotenv pinata

Minting a collection of NFTs

First, build your .env file:

# Chiliz RPC
RPC_URL=https://spicy-rpc.chiliz.com       # or a Mainnet RPC
PRIVATE_KEY=0xabc...                       # minter's key (server-side ONLY)

# Rarible
RARIBLE_API_KEY=your_rarible_api_key
COLLECTION_ADDRESS=0xYourCollectionOnChiliz
SUPPLY=1                                   # 1 = ERC-721, >1 = ERC-1155

# Pinata
PINATA_JWT=eyJhbGciOi...                   # Pinata JWT from dashboard
PINATA_GATEWAY=your-subdomain.mypinata.cloud

# Media / metadata inputs
IMAGE_PATH=./art/image.png
NAME=My Chiliz NFT
DESCRIPTION=Minted on-chain via Rarible SDK

Our workflow of choice is:

  1. Upload media to IPFS via Pinata

  2. Build & upload metadata file

  3. Rarible SDK + on-chain mint

Here is the full sample script which you can get inspiration from:

import 'dotenv/config'
import fs from 'fs'
import path from 'path'
import { Wallet, JsonRpcProvider } from 'ethers'
import { createRaribleSdk } from '@rarible/sdk'
import { toCollectionId, toUnionAddress } from '@rarible/types'
import { PinataSDK } from 'pinata'

function guessMime(p: string) {
  const ext = path.extname(p).toLowerCase()
  if (ext === '.png') return 'image/png'
  if (ext === '.jpg' || ext === '.jpeg') return 'image/jpeg'
  if (ext === '.webp') return 'image/webp'
  if (ext === '.gif') return 'image/gif'
  if (ext === '.mp4') return 'video/mp4'
  if (ext === '.webm') return 'video/webm'
  return 'application/octet-stream'
}

async function main() {
  // Upload media to IPFS
  const pinata = new PinataSDK({
    pinataJwt: process.env.PINATA_JWT!,
  })

  const mediaPath = process.env.IMAGE_PATH!
  const mediaBlob = new Blob([fs.readFileSync(mediaPath)], { type: guessMime(mediaPath) })
  const mediaFile = new File([mediaBlob], path.basename(mediaPath), { type: guessMime(mediaPath) })

  const mediaUpload = await pinata.upload.public.file(mediaFile)
  const imageUri = `ipfs://${mediaUpload.cid}`
  // console.log('imageUri:', imageUri)

  // Build & upload metadata file
  const metadata = {
    name: process.env.NAME!,
    description: process.env.DESCRIPTION!,
    image: imageUri,
    // animation_url: "ipfs://<cid>/video.mp4",
    // attributes: [{ trait_type: "Tier", value: "Gold" }],
  }
  const metaUpload = await pinata.upload.public.json(metadata)
  const metadataUri = `ipfs://${metaUpload.cid}`
  // console.log('metadataUri:', metadataUri)

  // Rarible on-chain mint
  const provider = new JsonRpcProvider(process.env.RPC_URL!)
  const wallet = new Wallet(process.env.PRIVATE_KEY!, provider)
  const sdk = createRaribleSdk(wallet, 'prod', { apiKey: process.env.RARIBLE_API_KEY! })

  // Union IDs use "<BLOCKCHAIN>:<address>" — here BLOCKCHAIN is "CHILIZ"
  const collectionId = toCollectionId(`CHILIZ:${process.env.COLLECTION_ADDRESS}`)
  const creator = toUnionAddress(`CHILIZ:${wallet.address}`)

  const { transaction, itemId } = await sdk.nft.mint({
    collectionId,
    uri: metadataUri,
    supply: Number(process.env.SUPPLY || 1), // 1 for 721, >1 for 1155
  })

  const { hash } = await transaction.wait()
  console.log('Minted item:', itemId, 'tx:', hash)
}

main().catch((e) => (console.error(e), process.exit(1)))

Lazy-minting an NFT collection

Lazy Minting is an option for those who don't want the upfront cost of minting an NFT before putting it on sale. They can list their NFT for purchase, then mint it only when it is purchased or transferred. The gas fees are therefore part of the minting process, and paid-for by the buyer.

In short, lazy-minting means putting the NFT on the blockchain only when someone buys the NFT, not before.

You'll need a properly defined .env file:

# Chiliz RPC / wallet
RPC_URL=https://spicy-rpc.chiliz.com
PRIVATE_KEY=0xabc...                         # creator key (keep secret)

# Rarible
RARIBLE_API_KEY=your_rarible_api_key
COLLECTION_ADDRESS=0xYourCollectionOnChiliz   # ERC-721 or ERC-1155

# Pinata
PINATA_JWT=eyJhbGciOi...                      # Pinata JWT from dashboard
PINATA_GATEWAY=your-subdomain.mypinata.cloud  # optional (for previews)

# Media / metadata
IMAGE_PATH=./art/image.png
NAME=My Chiliz NFT
DESCRIPTION=Lazy-minted on Rarible (Chiliz)
SUPPLY=1                                      # 1 for 721; >1 for 1155 editions

And now the sample code to lazy-mint your project:

import 'dotenv/config'
import fs from 'fs'
import path from 'path'
import { Wallet, JsonRpcProvider } from 'ethers'
import { createRaribleSdk } from '@rarible/sdk'
import { toCollectionId, toUnionAddress } from '@rarible/types'
import { PinataSDK } from 'pinata'

function guessMime(p: string) {
  const ext = path.extname(p).toLowerCase()
  if (ext === '.png') return 'image/png'
  if (ext === '.jpg' || ext === '.jpeg') return 'image/jpeg'
  if (ext === '.webp') return 'image/webp'
  if (ext === '.gif') return 'image/gif'
  if (ext === '.mp4') return 'video/mp4'
  if (ext === '.webm') return 'video/webm'
  return 'application/octet-stream'
}

async function main() {
  // Upload media to IPFS
  const pinata = new PinataSDK({
    pinataJwt: process.env.PINATA_JWT!,
  })

  const mediaPath = process.env.IMAGE_PATH!
  const mediaBlob = new Blob([fs.readFileSync(mediaPath)], { type: guessMime(mediaPath) })
  const mediaFile = new File([mediaBlob], path.basename(mediaPath), { type: guessMime(mediaPath) })
  const mediaUp = await pinata.upload.public.file(mediaFile)
  const imageUri = `ipfs://${mediaUp.cid}`

  const metadata = {
    name: process.env.NAME!,
    description: process.env.DESCRIPTION!,
    image: imageUri,
    // animation_url: "ipfs://<cid>/video.mp4",
    // attributes: [{ trait_type: "Tier", value: "Gold" }],
  }
  const metaUp = await pinata.upload.public.json(metadata)
  const metadataUri = `ipfs://${metaUp.cid}`

  // Rarible lazy mint
  const provider = new JsonRpcProvider(process.env.RPC_URL!)
  const wallet = new Wallet(process.env.PRIVATE_KEY!, provider)
  const sdk = createRaribleSdk(wallet, 'prod', { apiKey: process.env.RARIBLE_API_KEY! })
  const collectionId = toCollectionId(`CHILIZ:${process.env.COLLECTION_ADDRESS}`)
  const creator = toUnionAddress(`CHILIZ:${wallet.address}`)

  // Prepare & submit lazy mint (off-chain item; buyer mints on purchase)
  const prepared = await sdk.nft.mint.prepare({ collectionId })
  const result = await prepared.submit({
    uri: metadataUri,
    supply: Number(process.env.SUPPLY || 1),     // 1 for 721; >1 for 1155
    lazyMint: true,                              // Yes, make it lazy please
    creators: [{ account: creator, value: 10000 }], // 100% go to your wallet
    royalties: [],                               // e.g. [{ account: creator, value: 500 }] for 5%
  })

  // console.log('Lazy itemId:', result.itemId)
}

main().catch((e) => (console.error(e), process.exit(1)))

Last updated

Was this helpful?