πŸ–ΌοΈ Minting an Asset per User

Learn how to mint a unique asset for your users with the Owl API.

Minting an Asset per User

Facilitating the minting of assets for individual users is effortlessly achieved with Owl Protocol.

This process is especially beneficial for airdrops, giveaways, or any campaign that aims to provide your users with a unique asset.

Phygital collectibles serve as another practical application.Upon selling a physical item, you can mint a digital equivalent for the user, adding a functional aspect to the transaction. All you need is an email address and corresponding asset metadata.

Overview

Minting assets per user involves four key steps:

  1. Create a project.
  2. Create an ERC-721 collection.
  3. Adding users to your project.
  4. Minting unique assets for each user.

Let's begin your journey to enhancing user engagement and distribution strategies through personalized assets.

Step 1: Create a Project

First, you need to create a project for your collection. To do so, you need to initialize the Owl Protocol client and load your environment variables. This includes your API key, which is necessary for interacting with the Owl Protocol API. You can grab it from the Owl Protocol Dashboard.

// Initialize Owl Protocol client
import { createClient } from "@owlprotocol/contracts-api-client-trpc";
import dotenv from "dotenv";

// Load environment variables
dotenv.config();

const { API_KEY } = process.env;
if (!API_KEY) {
    throw new Error("API_KEY not found! Ensure it's added to your environment.");
}

// Create client with API credentials
const client = createClient({ apiKey: API_KEY });
const { id: projectId } = await client.projects.createProject({
    name: "My Project",
    description: "A description for My Project"
});

Step 2: Launch a Collection

Now, let's deploy an ERC-721 collection using the client. This type of non-fungible token (NFT) represents unique assets. You have the flexibility to set the name and symbol of your collection, along with any other optional parameters or metadata.

// Define network ID (replace with your choice)
const networkId = "80001"; // Polygon Mumbai Testnet

// Deploy ERC-721 collection
const contract = await client.collection.deploy.mutate({
    networkId,
    projectId, // Use the project ID from the previous step
    name: "My Collection", // Set your NFT collection name
    symbol: "MY", // Set collection symbol
    // Add other optional parameters or metadata here
});

// `contract` now contains data about the deployed ERC-721 collection

After deploying the collection, you can view it on the Owl Protocol Dashboard in your projects.
Note: If you opt not to create a project, your collection will appear in the 'Default Project,' which is automatically generated for each user upon signing up.

Step 3: Add a user to your project

To mint an asset for a user, you first need to add the user to your project. You can do this by using the getOrCreateByEmail method, which either fetches an existing user or creates a new one.

const contractProjectId = contract.projectId;
const userEmail = "[email protected]"; // Fetch from your database. Replace with your user's email address.
const user = await client.users.getOrCreateByEmail.mutate({
    projectId, // Use the project ID from the step 1
    email: userEmail,
});

// The 'user' object now contains details of the user

If you want to interact with an existing user, you can use the getByEmail method instead. This method will return a user object if the user exists in your project, or an error otherwise.

const userEmail = "[email protected]";
const user = await client.users.getByEmail.query({
    projectId,
    email: userEmail,
});

If the user is in your project you will receive the user object. If the user is not in your project you will receive an error indicating that the user in not found.

Step 4: Mint to New Users

Finally, you can mint an asset to the user. This involves calling the mint method on collection.erc721AutoId. You need to provide the network ID, the address of the contract (i.e., the collection), and the details of the mint operation.

The mint operation includes the recipient's address, the metadata of the asset (such as its name and description), and the content of the image represented as a base64 encoded string. You should also specify the image suffix (e.g., png, jpg, jpeg, etc.).

let encodedImage; //this needs to be a base64 encoded image. Replace with your encoded image. It should also be formatted as a string
await client.collection.erc721AutoId.mint.mutate({
    networkId: networkId,
    address: contract.address,
    mints: [
        {
            to: user.address,
            metadata: { name: "My NFT", description: "My NFT description" }, // Add other optional metadata here
            imageContent: encodedImage, // Add the encoded image here
            imageSuffix: "png", // Or jpg, jpeg, etc.
        },
    ],
});

Alternatively, if you have an image hosted online, you can directly pass the image URL to the metadata object:

const imageUrl = "https://example.com/image.png"; // Replace with your image URL

await client.collection.erc721AutoId.mint.mutate({
    networkId: networkId,
    address: contract.address,
    mints: [
        {
            to: user.address,
            metadata: { name: "My NFT", description: "My NFT description", image: imageUrl }, // Add other optional metadata here

        },
    ],
});

This flexibility allows you to either upload an image directly or reference an existing online image, providing convenience based on your specific needs.

This process mints a unique NFT to the specified user, complete with metadata and uploads an image for that token.

Please note that while the metadata field is entirely optional, adding metadata is advisable if you intend to view the created assets on web3 marketplaces like OpenSea.


Example Use Case 1: A Limited Edition Membership Badge

Imagine you're running an exclusive membership program and want to reward each member with a unique NFT badge. By following the steps outlined in this guide, you can seamlessly mint personalized badges for each member, complete with their name, membership level, and a distinct design. This approach not only adds a personalized touch to your membership program but also creates a sense of exclusivity among your community.

Steps to Implement the Use Case:

  1. Create a Project:

    • Initialize the Owl Protocol client and create a project named "Membership Program"
  2. Deploy a Collection:

    • Deploy an ERC-721 collection named "Membership Badges"
  3. Add Members to Your Project:

    • Use the getOrCreateByEmail method to add each member to your project.
  4. Mint Unique Badges:

    • Utilize the mint method on collections.erc721AutoId to mint personalized badges for each member.
  5. View Badges on the Dashboard:

    • Members can easily view their exclusive badges on the Owl Protocol Dashboard, providing a visual representation of their membership status.

By applying this use case, you can enhance your membership program with unique digital badges that members can proudly showcase in their NFT collections.


Example Use Case 2: Event Tickets

Transform traditional event tickets into unique NFTs, providing attendees with a personalized and secure entry pass.

Event Ticket Minting Process
Extend your minting script to include event tickets. Each ticket contains event details, ensuring a secure and verifiable entry process.

// Initialize Owl Protocol client
import { createClient } from "@owlprotocol/contracts-api-client-trpc";
import dotenv from "dotenv";

// Load environment variables
dotenv.config();

const { API_KEY } = process.env;
if (!API_KEY) {
    throw new Error("API_KEY not found! Ensure it's added to your environment.");
}

// Create client with API credentials
const client = createClient({ apiKey: API_KEY });

// Create project
const { id: projectId } = await client.projects.createProject({
    name: "Event Ticket Project",
    description: "This project is for all our events that require tickets."
});

// Define network ID (replace with your choice)
const networkId = "80001"; // Polygon Mumbai Testnet

// Deploy ERC-721 collection
const contract = await client.collection.deploy.mutate({
    networkId,
    projectId, // ID of the project you created
    name: "Exclusive Concert 1", // Set your NFT collection name
    symbol: "EC1", // Set collection symbol
});

const userEmail = "[email protected]"; // Fetch from your database. Replace with your user's email address.
const user = await client.users.getOrCreateByEmail.mutate({
    projectId,
    email: userEmail,
});

// Mint an Event Ticket NFT
const eventTicket = await client.collection.ERC721AutoId.mint.mutate({
    networkId: networkId, // Replace with your network ID (e.g., "1" for Maintnet)
    address: contract.address, // Replace with the address of your collection
    mints: [
        {
            to: user.address,
            metadata: {
                name: "Event Ticket",
                description: "Admission to Exclusive Event on DD/MM/YYYY",
                eventName: "Exclusive Event",
                date: "DD/MM/YYYY",
                venue: "Virtual Venue or Physical Location",
            },
            imageContent: encodedTicketImage, // Replace with the base64 encoded ticket image
            imageSuffix: "png",
        },
    ],
});

Customizing Event Details

The metadata object within the minting script allows you to include default fields such as name and description. However, the flexibility of the Owl Protocol extends further. You can customize this object by adding additional fields to suit your specific use case. For instance, in the provided example, we include fields such as eventName, date, and venue to offer a more detailed and personalized event ticket.


Workflow Recap

Minting assets per user with Owl Protocol is a powerful feature that opens up possibilities for engaging campaigns and distribution strategies.

With these clear instructions and code snippets, you should now be equipped to seamlessly integrate this functionality into your projects. Feel free to explore further customization options and leverage the Owl API for your unique use cases.