> For the complete documentation index, see [llms.txt](https://docs.degenlab.io/gamefistacks/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.degenlab.io/gamefistacks/lootbox-on-chain-m3/smart-contracts/lootbox-manager.md).

# 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.&#x20;

```lisp
(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.&#x20;

{% code lineNumbers="true" %}

```lisp
(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)))))
```

{% endcode %}

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.

{% code lineNumbers="true" %}

```lisp
(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))))
```

{% endcode %}

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`.

{% code lineNumbers="true" %}

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

{% endcode %}

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

{% code lineNumbers="true" %}

```lisp
(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))))
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.degenlab.io/gamefistacks/lootbox-on-chain-m3/smart-contracts/lootbox-manager.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
