Lootbox Manager

This smart contract manages who can mint lootboxes, and how many, by using a map remaining-mints with the key as a principal and the value being the number of mints available.

(define-map remaining-mints principal uint)

It checks if the address selected can mint NFTs through the read-only can-mint-lootbox returning true if minting is possible, false otherwise.

(define-read-only (can-mint-lootbox (address principal))
  (let ((remains (map-get? remaining-mints address)) )
  ;; map-get address not is-none
  (if (is-none remains) (ok false) 
    ;; map-get > u1
    (if (< u0 (unwrap! remains err-invalid-remains)) (ok true)
      (ok false)))))

If the address can-mint, it can call the claim-lootbox and get a lootbox, while decreasing by 1 the number of mints available, directly in the map where we store them.

(define-public (claim-lootbox ) 
  (if (is-eq (ok true) (can-mint-lootbox tx-sender))
    (let ((address tx-sender)) 
      (is-ok (set-remaining-mints address (- (unwrap! (map-get? remaining-mints address) err-invalid-remains) u1)))
      (as-contract (contract-call? .lootbox-background create-lootbox address)))
    (ok false)))

(define-public (set-remaining-mints (address principal) (number uint))
  (begin 
    (asserts! (is-eq tx-sender (var-get contract-admin)) err-admin-only) 
    (ok (map-set remaining-mints address number))))

If a user knows he can mint multiple but doesn't know how many he has left, he can call the read-only get-remaining-mints.

(define-read-only (get-remaining-mints (address principal)) 
  (map-get? remaining-mints address))

Admin can always update the number of the available mints for a given address based on any off-chain event.

(define-constant err-admin-only (err u100))
(define-public (set-remaining-mints (address principal) (number uint))
  (begin 
    (asserts! (is-eq tx-sender (var-get contract-admin)) err-admin-only) 
    (ok (map-set remaining-mints address number))))

Last updated