Product Module

The Product Module is a core component of the Cart.fun protocol that enables digital product management and secure content sales. It provides functionality for vendors to create, list, and sell digital products with built-in content protection through Lit Protocol integration.

Overview

The Product Module serves as the foundation for digital commerce in the Cart.fun ecosystem. It enables vendors to create, manage, and sell digital products across marketplaces with robust content protection mechanisms. The module handles the entire product lifecycle, from creation to purchase to delivery, with integrated security measures that ensure only legitimate buyers can access the purchased content.

Products can range from digital art and collectibles to software, music, e-books, and other digital assets. With Lit Protocol integration, content is securely encrypted until purchase, then dynamically made available to authorized buyers through on-chain access controls.

Key Features

  • Name
    Product Creation
    Type
    feature
    Description

    Create and list digital products with metadata

  • Name
    Digital Content Protection
    Type
    feature
    Description

    Integration with Lit Protocol for content encryption

  • Name
    Purchase Processing
    Type
    feature
    Description

    Complete purchase flow with fee distribution

  • Name
    Inventory Management
    Type
    feature
    Description

    Track and update product quantities

  • Name
    Validation
    Type
    feature
    Description

    Comprehensive product validation and error handling

  • Name
    Multi-tier Fee Management
    Type
    feature
    Description

    Distribute fees to protocol, platform, and marketplace

Core Components

Product Account

The primary data structure that stores product information:

pub struct Product {
    pub name: String,             // Product name
    pub description: String,      // Detailed description
    pub image_url: String,        // Product image URL
    pub price: u64,               // Price in lamports
    pub quantity: u64,            // Available inventory
    pub vendor: Pubkey,           // Reference to the vendor account
    pub encrypted_content_uri: String,  // URI to encrypted content
    pub lit_condition: LitCondition,    // Lit Protocol access conditions
    pub is_active: bool,          // Product active status
    pub bump: u8,                 // PDA derivation bump
}

Lit Protocol Integration

The Product module integrates with Lit Protocol for content protection:

pub struct LitCondition {
    pub condition_id: String,     // Unique identifier for the Lit condition
    pub content_hash: String,     // Hash of the encrypted content
    pub encrypted_symmetric_key: String,  // Encrypted symmetric key
}

After purchase, a LitConditionAccount is created for the buyer:

pub struct LitAccessCondition {
    pub product: Pubkey,          // Reference to the product
    pub buyer: Pubkey,            // The authorized buyer
    pub purchase_id: String,      // Unique purchase identifier
    pub valid_until: i64,         // Expiration timestamp (if applicable)
    pub access_condition: String, // Serialized condition for Lit nodes
    pub bump: u8,                 // PDA derivation bump
}

Product Metadata

Additional details about products are stored in metadata:

pub struct ProductMetadata {
    pub category: String,         // Product category
    pub tags: Vec<String>,        // Product tags for search
    pub file_type: String,        // Type of digital content
    pub preview_url: Option<String>, // Optional preview URL
    pub size_bytes: u64,          // Size of the content in bytes
    pub attributes: Vec<ProductAttribute>, // Custom attributes
}

pub struct ProductAttribute {
    pub trait_type: String,       // Attribute name
    pub value: String,            // Attribute value
}

Product Operations

The Product Module supports the following key operations:

  • Name
    create_product
    Type
    instruction
    Description

    Create a new digital product listing

  • Name
    update_product
    Type
    instruction
    Description

    Update an existing product's details or price

  • Name
    purchase_product
    Type
    instruction
    Description

    Process a product purchase and set up access rights

  • Name
    verify_purchase
    Type
    instruction
    Description

    Verify a buyer's purchase access rights

  • Name
    toggle_product_status
    Type
    instruction
    Description

    Activate or deactivate a product listing

  • Name
    update_inventory
    Type
    instruction
    Description

    Update product inventory quantities

  • Name
    get_product_data
    Type
    instruction
    Description

    Retrieve product details and metadata

  • Name
    get_purchase_history
    Type
    instruction
    Description

    Get purchase history for a product

Usage Examples

Creating a Digital Product

import { Connection, Keypair } from '@solana/web3.js'
import { CartProtocol, NetworkType } from '@cartdotfun/sdk'
import { encryptWithLit } from '@cartdotfun/sdk/lit'

async function createDigitalProduct(vendorId, marketplaceId, contentBuffer) {
  const connection = new Connection(
    'https://api.devnet.solana.com',
    'confirmed',
  )
  const vendorWallet = Keypair.fromSecretKey(/* your secret key */)

  const cartProtocol = new CartProtocol({
    connection,
    wallet: vendorWallet,
    network: NetworkType.Devnet,
  })

  // First, encrypt the content using Lit Protocol
  const { encryptedContentUri, litCondition } = await encryptWithLit({
    content: contentBuffer,
    contentType: 'application/pdf',
    name: 'Premium Digital Art Tutorial.pdf',
  })

  // Then create the product with the encrypted content reference
  const { transaction, productId } = await cartProtocol.product.createProduct({
    vendorId,
    marketplaceId,
    name: 'Premium Digital Art Tutorial',
    description:
      'A comprehensive guide to creating premium digital art with professional techniques',
    price: 500000000, // 0.5 SOL in lamports
    quantity: 1000, // Limited edition with 1000 copies
    imageUrl: 'https://example.com/tutorial-cover.png',
    encryptedContentUri,
    litCondition,
    metadata: {
      category: 'education',
      tags: ['art', 'tutorial', 'digital', 'professional'],
      fileType: 'application/pdf',
      sizeBytes: contentBuffer.length,
      attributes: [
        { traitType: 'pages', value: '42' },
        { traitType: 'difficulty', value: 'intermediate' },
        { traitType: 'includes_source_files', value: 'true' },
      ],
    },
  })

  const signature = await cartProtocol.sendAndConfirmTransaction(transaction)
  console.log('Product created:', signature)
  console.log('Product ID:', productId)

  return { productId, signature }
}

Purchasing a Product

async function purchaseProduct(productId) {
  // Assuming cartProtocol is initialized with the buyer's wallet

  // First, get product details
  const product = await cartProtocol.product.getProduct(productId)
  console.log('Product price:', product.price / 1e9, 'SOL')

  // Purchase the product
  const { transaction, purchaseId } =
    await cartProtocol.product.purchaseProduct({
      productId,
    })

  const signature = await cartProtocol.sendAndConfirmTransaction(transaction)
  console.log('Purchase successful:', signature)
  console.log('Purchase ID:', purchaseId)

  // Get access to the purchased content
  const contentAccess = await cartProtocol.product.getContentAccess({
    productId,
    purchaseId,
  })

  console.log('Content available at:', contentAccess.contentUrl)

  return { purchaseId, contentAccess }
}

Managing Product Inventory

async function updateProductInventory(productId, newQuantity) {
  // Assuming cartProtocol is initialized with the vendor's wallet

  const { transaction } = await cartProtocol.product.updateInventory({
    productId,
    quantity: newQuantity,
  })

  const signature = await cartProtocol.sendAndConfirmTransaction(transaction)
  console.log('Inventory updated:', signature)

  // Check updated product data
  const updatedProduct = await cartProtocol.product.getProduct(productId)
  console.log('New quantity:', updatedProduct.quantity)
}

For more detailed information about the Product Module, refer to these resources:

Was this page helpful?