Last active
August 24, 2022 22:47
-
-
Save Jamil/ee608763b00f640fd20795f5cdb5e319 to your computer and use it in GitHub Desktop.
NFT contract marketplace functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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? stella 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))) | |
| ;; 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))) | |
| (define-private (trnsfr (id uint) (sender principal) (recipient principal)) | |
| (match (nft-transfer? stella 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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment