Clarity Contract

Allowing authorized users to issue badges to multiple recipients

Clarity:

The Clarity contract facilitates batch endorsements, allowing authorized users to issue badges to multiple recipients. It verifies the issuer's authority and manages the process of adding badges (endorsements) to users' profiles. Key functions include add-badge (adds a badge to a user) and endorse (issues badges to multiple users at once).

Here's the formatted clarity code for docs.

;; title: template-batch-endorsements
;; version: 1.0
;; summary: A template for batch minting endorsements as non-fungible tokens (NFTs).
;; description: This contract allows for the minting of endorsement NFTs in batches, with predefined error handling and ownership management.

(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)

;; traits
;; This contract implements the NFT trait for endorsement tokens.

;; token definitions
(define-non-fungible-token endorsement uint)

;; constants
;; (VAR) changes based on the number of recipients
(define-constant TOTAL u500) ; Total number of tokens to be minted
(define-constant ERROR-NOT-IMPLEMENTED u1) ; Error code for not implemented functionality
(define-constant ERROR-UNAUTHORIZED u1000) ; Error code for unauthorized actions
(define-constant ERROR-ALREADY-MINTED u1001) ; Error code for already minted tokens

;; (VAR) changes based on the collection IPFS hash
(define-constant IPFS-ROOT "ipfs://ipfs/bafybeicvtqlmtj6hoed4ctxbmpxegk7fdhid437qmafbhlweuwgbqtwtky/json/{id}.json")

;; data vars
(define-data-var last-token-id uint u0) ; Variable to track the last minted token ID

;; data maps
(define-map awarded-addresses principal bool) ; Map to track awarded addresses
(define-map minters principal bool) ; Map to track authorized minters

;; public functions
;; Function to mint new NFTs
(define-private (mint (address principal))
    (let (
        (token-id (+ u1 (var-get last-token-id))) ; Generate the next token ID
    ) 
    (var-set last-token-id token-id) ; Update the last token ID
    (nft-mint? endorsement token-id tx-sender))) ; Mint the NFT

;; Function to prevent transfer of NFTs
(define-public (transfer (id uint) (sender principal) (recipient principal)) 
    (err ERROR-NOT-IMPLEMENTED)) ; Returns error for transfer attempts

;; Function to burn an NFT
(define-public (burn (id uint)) 
    (let (
        (owner (unwrap! (nft-get-owner? endorsement id) (err ERROR-UNAUTHORIZED))) ; Get current owner
    ) 
    (asserts! (is-eq owner tx-sender) (err ERROR-UNAUTHORIZED)) ; Ensure caller is the owner
    (nft-burn? endorsement id owner))) ; Burn the NFT

;; read-only functions
;; Function to get the last token ID
(define-read-only (get-last-token-id) 
    (ok TOTAL)) ; Returns the total number of tokens

;; Function to get the owner of a token
(define-read-only (get-owner (id uint))
    (ok (nft-get-owner? endorsement id))) ; Returns the owner of the specified token

;; Function to get the token URI
(define-read-only (get-token-uri (token-id uint))
    (ok (some IPFS-ROOT))) ; Returns the IPFS URI for the token

;;; Mint calls here just need to change the addresses
(mint 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM) ; Minting for address 1
(mint 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5) ; Minting for address 2
(mint 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG) ; Minting for address 3

Code Breakdown

  • Metadata Section: Provides basic information about the contract.

  • Constants: Defines total tokens and error codes.

  • Data Variables and Maps: Tracks the last minted token and addresses involved in minting and awards.

  • Public Functions:

    • Minting Function: Allows minting of NFTs and updates the last token ID.

    • Transfer Function: Prevents token transfer with an error message.

    • Burn Function: Allows the owner to burn their NFT.

  • Read-Only Functions: For retrieving token ID, owner, and token URI.

  • Mint Calls: Examples of minting NFTs for specific addresses.

Feel free to modify the descriptions and version number as needed!

Last updated