;; blobs5000-v3
;; contractType: public

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

(define-non-fungible-token blobs5000-v3 uint)

;; Constants
(define-constant DEPLOYER tx-sender)
(define-constant COMM u1000)
(define-constant COMM-ADDR 'SPNWZ5V2TPWGQGVDR6T7B6RQ4XMGZ4PXTEE0VQ0S)

(define-constant ERR-NO-MORE-NFTS u100)
(define-constant ERR-NOT-ENOUGH-PASSES u101)
(define-constant ERR-PUBLIC-SALE-DISABLED u102)
(define-constant ERR-CONTRACT-INITIALIZED u103)
(define-constant ERR-NOT-AUTHORIZED u104)
(define-constant ERR-INVALID-USER u105)
(define-constant ERR-LISTING u106)
(define-constant ERR-WRONG-COMMISSION u107)
(define-constant ERR-NOT-FOUND u108)
(define-constant ERR-PAUSED u109)
(define-constant ERR-MINT-LIMIT u110)
(define-constant ERR-METADATA-FROZEN u111)
(define-constant ERR-AIRDROP-CALLED u112)
(define-constant ERR-NO-MORE-MINTS u113)
(define-constant ERR-INVALID-PERCENTAGE u114)

;; Internal variables
(define-data-var mint-limit uint u5000)
(define-data-var last-id uint u1)
(define-data-var total-price uint u99000000)
(define-data-var artist-address principal 'SP2MGXXTN1V4E1N07X2MRZ6CTNFV5ZV9VGN71BT33)
(define-data-var ipfs-root (string-ascii 80) "ipfs://ipfs/QmSHjhQckgCtNoUgXg1e68Mhw1eYsqF5ooS3dp78AhpKXM/json/")
(define-data-var mint-paused bool false)
(define-data-var premint-enabled bool false)
(define-data-var sale-enabled bool false)
(define-data-var metadata-frozen bool false)
(define-data-var airdrop-called bool false)
(define-data-var mint-cap uint u0)

(define-map mints-per-user principal uint)
(define-map mint-passes principal uint)

(define-public (claim) 
  (mint (list true)))
(define-public (claim-abunch) 
  (mint (list true true true true true true true true true true true true true true true true true true true true true true 
    true true true true true true true true true true true true true true true true true true true true true true true true
     true true true true true true true true true true true true true true true true true true true true true true true true 
     true true true true true true true true true true true true true true true true true true true true true true true true 
     true true true true true true true true true true true true true true true true true true true true true true true true
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true true true true true true true 
      true true true true true true true true true true true true true true true true true true)))

(define-public (claim-ten) (mint (list true true true true true true true true true true)))

(define-public (claim-twentyfive) (mint (list true true true true true true true true true true true true true true true true true true true true true true true true true)))

;; Default Minting
(define-private (mint (orders (list 25 bool)))
  (mint-many orders))

(define-private (mint-many (orders (list 25 bool )))  
  (let 
    (
      (last-nft-id (var-get last-id))
      (enabled (asserts! (<= last-nft-id (var-get mint-limit)) (err ERR-NO-MORE-NFTS)))
      (art-addr (var-get artist-address))
      (id-reached (fold mint-many-iter orders last-nft-id))
      (price (* (var-get total-price) (- id-reached last-nft-id)))
      (total-commission (/ (* price COMM) u10000))
      (current-balance (get-balance tx-sender))
      (total-artist (- price total-commission))
      (capped (> (var-get mint-cap) u0))
      (user-mints (get-mints tx-sender))
    )
    (asserts! (or (is-eq false (var-get mint-paused)) (is-eq tx-sender DEPLOYER)) (err ERR-PAUSED))
    (asserts! (or (not capped) (is-eq tx-sender DEPLOYER) (is-eq tx-sender art-addr) (>= (var-get mint-cap) (+ (len orders) user-mints))) (err ERR-NO-MORE-MINTS))
    (map-set mints-per-user tx-sender (+ (len orders) user-mints))
    (if (or (is-eq tx-sender art-addr) (is-eq tx-sender DEPLOYER) (is-eq (var-get total-price) u0000000))
      (begin
        (var-set last-id id-reached)
        (map-set token-count tx-sender (+ current-balance (- id-reached last-nft-id)))
      )
      (begin
        (var-set last-id id-reached)
        (map-set token-count tx-sender (+ current-balance (- id-reached last-nft-id)))
        (try! (stx-transfer? total-artist tx-sender (var-get artist-address)))
        (try! (stx-transfer? total-commission tx-sender COMM-ADDR))
      )    
    )
    (ok id-reached)))

(define-private (mint-many-iter (ignore bool) (next-id uint))
  (if (<= next-id (var-get mint-limit))
    (begin
      (unwrap! (nft-mint? blobs5000-v3 next-id tx-sender) next-id)
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u1) tx-sender) (+ next-id u1))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u2) tx-sender) (+ next-id u2) )
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u3) tx-sender) (+ next-id u3) )
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u4) tx-sender) (+ next-id u4) )
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u5) tx-sender) (+ next-id u5) )
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u6) tx-sender) (+ next-id u6) )
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u7) tx-sender) (+ next-id u7) )
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u8) tx-sender) (+ next-id u8) )
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u9) tx-sender) (+ next-id u9) )
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u10) tx-sender) (+ next-id u10))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u11) tx-sender) (+ next-id u11))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u12) tx-sender) (+ next-id u12))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u13) tx-sender) (+ next-id u13))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u14) tx-sender) (+ next-id u14))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u15) tx-sender) (+ next-id u15))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u16) tx-sender) (+ next-id u16))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u17) tx-sender) (+ next-id u17))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u18) tx-sender) (+ next-id u18))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u19) tx-sender) (+ next-id u19))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u20) tx-sender) (+ next-id u20))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u21) tx-sender) (+ next-id u21))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u22) tx-sender) (+ next-id u22))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u23) tx-sender) (+ next-id u23))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u24) tx-sender) (+ next-id u24))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u25) tx-sender) (+ next-id u25))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u26) tx-sender) (+ next-id u26))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u27) tx-sender) (+ next-id u27))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u28) tx-sender) (+ next-id u28))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u29) tx-sender) (+ next-id u29))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u30) tx-sender) (+ next-id u30))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u31) tx-sender) (+ next-id u31))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u32) tx-sender) (+ next-id u32))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u33) tx-sender) (+ next-id u33))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u34) tx-sender) (+ next-id u34))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u35) tx-sender) (+ next-id u35))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u36) tx-sender) (+ next-id u36))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u37) tx-sender) (+ next-id u37))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u38) tx-sender) (+ next-id u38))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u39) tx-sender) (+ next-id u39))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u40) tx-sender) (+ next-id u40))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u41) tx-sender) (+ next-id u41))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u42) tx-sender) (+ next-id u42))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u43) tx-sender) (+ next-id u43))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u44) tx-sender) (+ next-id u44))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u45) tx-sender) (+ next-id u45))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u46) tx-sender) (+ next-id u46))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u47) tx-sender) (+ next-id u47))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u48) tx-sender) (+ next-id u48))
      (unwrap! (nft-mint? blobs5000-v3 (+ next-id u49) tx-sender) (+ next-id u49))
      (+ next-id u50)    
    )
    next-id))

(define-public (set-artist-address (address principal))
  (begin
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-INVALID-USER))
    (ok (var-set artist-address address))))

(define-public (set-price (price uint))
  (begin
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-INVALID-USER))
    (ok (var-set total-price price))))

(define-public (toggle-pause)
  (begin
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-INVALID-USER))
    (ok (var-set mint-paused (not (var-get mint-paused))))))

(define-public (set-mint-limit (limit uint))
  (begin
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-INVALID-USER))
    (asserts! (< limit (var-get mint-limit)) (err ERR-MINT-LIMIT))
    (ok (var-set mint-limit limit))))

(define-public (burn (token-id uint))
  (begin 
    (asserts! (is-owner token-id tx-sender) (err ERR-NOT-AUTHORIZED))
    (nft-burn? blobs5000-v3 token-id tx-sender)))

(define-private (is-owner (token-id uint) (user principal))
    (is-eq user (unwrap! (nft-get-owner? blobs5000-v3 token-id) false)))

(define-public (set-base-uri (new-base-uri (string-ascii 80)))
  (begin
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-NOT-AUTHORIZED))
    (asserts! (not (var-get metadata-frozen)) (err ERR-METADATA-FROZEN))
    (var-set ipfs-root new-base-uri)
    (ok true)))

(define-public (freeze-metadata)
  (begin
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-NOT-AUTHORIZED))
    (var-set metadata-frozen true)
    (ok true)))

;; Non-custodial SIP-009 transfer function
(define-public (transfer (id uint) (sender principal) (recipient principal))
  (begin
    (asserts! (is-eq tx-sender sender) (err ERR-NOT-AUTHORIZED))
    (asserts! (is-none (map-get? market id)) (err ERR-LISTING))
    (trnsfr id sender recipient)))

;; read-only functions
(define-read-only (get-owner (token-id uint))
  (ok (nft-get-owner? blobs5000-v3 token-id)))

(define-read-only (get-last-token-id)
  (ok (- (var-get last-id) u1)))

(define-read-only (get-token-uri (token-id uint))
  (ok (some (concat (concat (var-get ipfs-root) "{id}") ".json"))))

(define-read-only (get-paused)
  (ok (var-get mint-paused)))

(define-read-only (get-price)
  (ok (var-get total-price)))

(define-read-only (get-mints (caller principal))
  (default-to u0 (map-get? mints-per-user caller)))

(define-read-only (get-mint-limit)
  (ok (var-get mint-limit)))

;; Non-custodial marketplace extras
(use-trait commission-trait 'SP3D6PV2ACBPEKYJTCMH7HEN02KP87QSP8KTEH335.commission-trait.commission)

(define-map token-count principal uint)
(define-map market uint {price: uint, commission: principal, royalty: uint})

(define-read-only (get-balance (account principal))
  (default-to u0
    (map-get? token-count account)))

(define-private (trnsfr (id uint) (sender principal) (recipient principal))
  (match (nft-transfer? blobs5000-v3 id sender recipient)
    success
      (let
        ((sender-balance (get-balance sender))
        (recipient-balance (get-balance recipient)))
          (map-set token-count
            sender
            (- sender-balance u1))
          (map-set token-count
            recipient
            (+ recipient-balance u1))
          (ok success))
    error (err error)))

(define-private (is-sender-owner (id uint))
  (let ((owner (unwrap! (nft-get-owner? blobs5000-v3 id) false)))
    (or (is-eq tx-sender owner) (is-eq contract-caller owner))))

(define-read-only (get-listing-in-ustx (id uint))
  (map-get? market id))

(define-public (list-in-ustx (id uint) (price uint) (comm-trait <commission-trait>))
  (let ((listing  {price: price, commission: (contract-of comm-trait), royalty: (var-get royalty-percent)}))
    (asserts! (is-sender-owner id) (err ERR-NOT-AUTHORIZED))
    (map-set market id listing)
    (print (merge listing {a: "list-in-ustx", id: id}))
    (ok true)))

(define-public (unlist-in-ustx (id uint))
  (begin
    (asserts! (is-sender-owner id) (err ERR-NOT-AUTHORIZED))
    (map-delete market id)
    (print {a: "unlist-in-ustx", id: id})
    (ok true)))

(define-public (buy-in-ustx (id uint) (comm-trait <commission-trait>))
  (let ((owner (unwrap! (nft-get-owner? blobs5000-v3 id) (err ERR-NOT-FOUND)))
      (listing (unwrap! (map-get? market id) (err ERR-LISTING)))
      (price (get price listing))
      (royalty (get royalty listing)))
    (asserts! (is-eq (contract-of comm-trait) (get commission listing)) (err ERR-WRONG-COMMISSION))
    (try! (stx-transfer? price tx-sender owner))
    (try! (pay-royalty price royalty))
    (try! (contract-call? comm-trait pay id price))
    (try! (trnsfr id owner tx-sender))
    (map-delete market id)
    (print {a: "buy-in-ustx", id: id})
    (ok true)))
    
(define-data-var royalty-percent uint u500)

(define-read-only (get-royalty-percent)
  (ok (var-get royalty-percent)))

(define-public (set-royalty-percent (royalty uint))
  (begin
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-INVALID-USER))
    (asserts! (and (>= royalty u0) (<= royalty u1000)) (err ERR-INVALID-PERCENTAGE))
    (ok (var-set royalty-percent royalty))))

(define-private (pay-royalty (price uint) (royalty uint))
  (let (
    (royalty-amount (/ (* price royalty) u10000))
  )
  (if (> royalty-amount u0)
    (try! (stx-transfer? royalty-amount tx-sender (var-get artist-address)))
    (print false)
  )
  (ok true)))
  

