Source Code

(impl-trait .nft-trait.nft-trait)

(define-non-fungible-token Songs-of-Innocence uint)

;; Constants
(define-constant DEPLOYER 'SP7AMEBV4Z6V4J9ENAFBH0T1DGQ67X6NF79GX3V3)
(define-constant COMM u99000)
(define-constant COMM-ADDR-ONE 'SP7AMEBV4Z6V4J9ENAFBH0T1DGQ67X6NF79GX3V3)
(define-constant COMM-ADDR-TWO 'SP7AMEBV4Z6V4J9ENAFBH0T1DGQ67X6NF79GX3V3)

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

;; Internal variables
(define-data-var mint-limit uint u420420000000)
(define-data-var last-id uint u1)
(define-data-var total-price uint u0)
(define-data-var artist-address principal 'SP7AMEBV4Z6V4J9ENAFBH0T1DGQ67X6NF79GX3V3)
(define-data-var ipfs-root (string-ascii 80) "https://cloudflare-ipfs.com/ipfs/QmUCSNZ674fgKfbzZobXu3gYpKu7fW9zdNXBUvfjqNjnFN/")
(define-data-var mint-paused bool false)
(define-data-var premint-enabled bool false)
(define-data-var sale-enabled bool true)
(define-data-var metadata-frozen bool false)
(define-data-var airdrop-called bool false)
(define-data-var mint-cap uint u420420000000)

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

(define-public (claim (addr principal))
  (begin
    (asserts! (is-eq tx-sender) (err ERR-INVALID-USER))
    (mint-many (list addr))
  )
)

(define-public (claim-for (addr principal))
  (mint-many (list addr)))

(define-public (claim-five-for (addrs (list 5 principal)))
  (mint-many addrs))

(define-public (claim-ten-for (addrs (list 10 principal)))
  (mint-many addrs))

(define-public (claim-twenty-five-for (addrs (list 25 principal)))
  (mint-many addrs))

(define-private (mint (orders (list 25 principal)))
  (let 
    (
      (passes (get-passes tx-sender))
    )
    (if (var-get premint-enabled)
      (begin
        (asserts! (>= passes (len orders)) (err ERR-NOT-ENOUGH-PASSES))
        (map-set mint-passes tx-sender (- passes (len orders)))
        (mint-many orders)
      )
      (begin
        (asserts! (var-get sale-enabled) (err ERR-PUBLIC-SALE-DISABLED))
        (mint-many orders)))))

(define-private (mint-many (orders (list 25 principal)))  
  (let 
    (
      (last-nft-id (var-get last-id))
      (enabled true)
      (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 u2) u10000))
      (total-artist (- price total-commission))
    )
      (var-set last-id id-reached)
      (ok last-nft-id)))

(define-private (mint-many-iter (user principal) (next-id uint))
  (if (<= next-id (var-get mint-limit))
    (begin
      (print "'U2 sent a message to everyone that music is free, and that's disturbing. It's easy to do that when you're a multi-millionaire-billionaire and money isn't really something that you worry about, but when you're a working rock 'n' roll band and you count on every dollar, it's disappointing to see someone do that.' -Keith Nelson")
      (unwrap! (nft-mint? Songs-of-Innocence next-id user) next-id)
      (+ next-id u1)    
    )
    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-private (is-owner (token-id uint) (user principal))
    (is-eq user (unwrap! (nft-get-owner? Songs-of-Innocence 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? Songs-of-Innocence 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) "Songs-of-Innocence") ".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 .commission-trait.commission)

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

(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? Songs-of-Innocence id sender recipient)
        success
          (let
            ((sender-balance (get-balance sender))
            (recipient-balance (get-balance recipient)))
              (map-set token-count
                    recipient
                    (+ recipient-balance u1))
              (try! (nft-mint? Songs-of-Innocence (var-get last-id) sender))
              (var-set last-id (+ (var-get last-id) u1))
              (print "'The delivery mechanism amounts to nothing more than spam with forced downloads.' -Wired ")
              (ok success))
        error (err error)))

(define-private (is-sender-owner (id uint))
  (let ((owner (unwrap! (nft-get-owner? Songs-of-Innocence 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)}))
    (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? Songs-of-Innocence id) (err ERR-NOT-FOUND)))
      (listing (unwrap! (map-get? market id) (err ERR-LISTING)))
      (price (get price listing)))
    (asserts! (is-eq (contract-of comm-trait) (get commission listing)) (err ERR-WRONG-COMMISSION))
    (try! (stx-transfer? price tx-sender owner))
    (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)))

;; Extra functionality required for mintpass
(define-public (toggle-sale-state)
  (let 
    (
      ;; (premint (not (var-get premint-enabled)))
      (sale (not (var-get sale-enabled)))
    )
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-NOT-AUTHORIZED))
    (var-set premint-enabled false)
    (var-set sale-enabled sale)
    (print { sale: sale })
    (ok true)))

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

(define-public (disable-premint)
  (begin
    (asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-NOT-AUTHORIZED))
    (ok (var-set premint-enabled false))))

(define-read-only (get-passes (caller principal))
  (default-to u0 (map-get? mint-passes caller)))

(define-read-only (get-premint-enabled)
  (ok (var-get premint-enabled)))

(define-read-only (get-sale-enabled)
  (ok (var-get sale-enabled)))  

(define-public (burn (token-id uint))
  (let
    (
      (next-id (var-get last-id))
    )
    (var-set last-id (+ u1 next-id))
    (try! (nft-mint? Songs-of-Innocence next-id tx-sender))
    (print "You said burn, so I burned you another copy of the album.")
    (ok true)))

Functions (37)

FunctionAccessArgs
claimpublicaddr: principal
claim-forpublicaddr: principal
claim-five-forpublicaddrs: (list 5 principal
claim-ten-forpublicaddrs: (list 10 principal
claim-twenty-five-forpublicaddrs: (list 25 principal
mintprivateorders: (list 25 principal
mint-manyprivateorders: (list 25 principal
mint-many-iterprivateuser: principal, next-id: uint
set-artist-addresspublicaddress: principal
set-pricepublicprice: uint
toggle-pausepublic
set-mint-limitpubliclimit: uint
is-ownerprivatetoken-id: uint, user: principal
set-base-uripublicnew-base-uri: (string-ascii 80
freeze-metadatapublic
transferpublicid: uint, sender: principal, recipient: principal
get-ownerread-onlytoken-id: uint
get-last-token-idread-only
get-token-uriread-onlytoken-id: uint
get-pausedread-only
get-priceread-only
get-mintsread-onlycaller: principal
get-mint-limitread-only
get-balanceread-onlyaccount: principal
trnsfrprivateid: uint, sender: principal, recipient: principal
is-sender-ownerprivateid: uint
get-listing-in-ustxread-onlyid: uint
list-in-ustxpublicid: uint, price: uint, comm-trait: <commission-trait>
unlist-in-ustxpublicid: uint
buy-in-ustxpublicid: uint, comm-trait: <commission-trait>
toggle-sale-statepublic
enable-premintpublic
disable-premintpublic
get-passesread-onlycaller: principal
get-premint-enabledread-only
get-sale-enabledread-only
burnpublictoken-id: uint