GameFi
  • General Intro
  • 📃R & D
  • 💻Workshop
    • 🏎️NFT Web App Integration
      • 📄Prerequirements
      • Decentralized Storage
      • Smart Contract NFTs
      • Creating the React Dapp
      • Updating Startup File, Wallet Connect & Main Menu
      • Fetching NFTs, Stacks and Hiro API
      • Rendering NFTs owned
      • Selecting an NFT
      • Mapping Scenes
      • Creating the playable game
  • 🪙Trustless Rewards - M1
    • General idea
    • Flow Lobbies
    • Smart Contract
  • 🎨Customizable NFTs - M2
    • General Idea
    • Flow Customizable NFTs
    • Smart Contracts
      • Component
      • Customizable Wrapper
  • 🎁Lootbox on Chain - M3
    • General Idea
    • Tech Explained
    • Smart Contracts
      • Item
      • Lootbox
      • Lootbox Manager
  • 📝Message Signing
    • General Idea
    • GameFi Use Cases
    • App Explained
  • 🪵SFTs
    • General Idea & Base SFTs Static Deployments
    • Metadata Structure
    • Static Flow and Smart Contracts
    • Advanced SFTs Dynamic Deployments
    • Dynamic Flow and Smart Contracts
    • Dapp Integrating SFTs
      • Front End
      • Back End
    • Resources
  • ⚡Subnets
    • Overall for subnets
  • Roadmap
  • 💾Decentralized Storage
    • Gaia
    • Pinata
    • Host SFTs and NFTs into Pinata
    • Gaia integration to host game resources
  • 🔗External Knowledge
    • Getting Started
    • Hiro API
    • Stacks Docs
    • Clarity Book
    • Hiro Tutorials
Powered by GitBook
On this page

Was this helpful?

  1. Lootbox on Chain - M3
  2. Smart Contracts

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))))
PreviousLootboxNextGeneral Idea

Last updated 2 years ago

Was this helpful?

🎁