-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbns-1677282895910-v1.clar
115 lines (99 loc) · 4.36 KB
/
bns-1677282895910-v1.clar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
;; version: 1
;; name: mappa
;; namespace: btc
(use-trait commission-trait 'SP3D6PV2ACBPEKYJTCMH7HEN02KP87QSP8KTEH335.commission-trait.commission)
(define-constant DEPLOYER_CONTRACT_PRINCIPAL (as-contract tx-sender))
(define-constant COMM-ADDR 'SP7NDX6YRAH6C99WCZJWKF2SYR1GQRF7X6894QSJ)
(define-constant ERR-ALREADY-LISTED (err u401))
(define-constant ERR-WRONG-COMMISSION (err u402))
(define-constant ERR-NOT-AUTHORIZED (err u403))
(define-constant ERR-NOT-FOUND (err u404))
(define-constant ERR-WRONG-PRICE (err u405))
(define-constant ERR-TRANSFER-FAILED (err u500))
(define-data-var current-namespace (buff 20) 0x00)
(define-data-var current-name (buff 48) 0x00)
(define-map listings { namespace: (buff 20), name: (buff 48) } { price: uint, lister: principal, commission: principal })
(define-read-only (is-admin)
(is-eq tx-sender COMM-ADDR)
)
(define-read-only (get-listing)
(map-get? listings { namespace: (var-get current-namespace), name: (var-get current-name) })
)
(define-read-only (get-current-name)
{ namespace: (var-get current-namespace), name: (var-get current-name) }
)
(define-private (list-name (namespace (buff 20)) (name (buff 48)) (price uint) (commission-trait <commission-trait>))
(begin
(asserts! (is-none (get-listing)) ERR-ALREADY-LISTED)
(try! (to-bool-response (contract-call?
'SP000000000000000000002Q6VF78.bns
name-transfer
namespace
name
DEPLOYER_CONTRACT_PRINCIPAL
none
)))
(var-set current-namespace namespace)
(var-set current-name name)
(ok (map-set listings {name: name, namespace: namespace}
{price: price, lister: tx-sender, commission: (contract-of commission-trait)}))
)
)
(define-public (change-price (namespace (buff 20)) (name (buff 48)) (new-price uint) (commission-trait <commission-trait>))
(let (
(listing (unwrap! (map-get? listings {namespace: namespace, name: name}) ERR-NOT-FOUND))
(price (get price listing))
(lister (get lister listing)))
(asserts! (is-eq tx-sender lister) ERR-NOT-AUTHORIZED)
(ok (map-set listings { namespace: namespace, name: name } { price: new-price, lister: lister, commission: (contract-of commission-trait) }))
)
)
(define-public (unlist-name (namespace (buff 20)) (name (buff 48)))
(let (
(listing (unwrap! (map-get? listings {namespace: namespace, name: name}) ERR-NOT-FOUND))
(price (get price listing))
(lister (get lister listing)))
(asserts! (or (is-eq tx-sender lister) (is-admin)) ERR-NOT-AUTHORIZED)
(map-delete listings {namespace: namespace, name: name})
(as-contract
(to-bool-response (contract-call?
'SP000000000000000000002Q6VF78.bns
name-transfer
namespace
name
lister
none
))
)
)
)
(define-public (purchase-name (namespace (buff 20)) (name (buff 48)) (expected-price uint) (commission-trait <commission-trait>) (recipient (optional principal)))
(let (
(new-owner (if (is-some recipient) (unwrap-panic recipient) tx-sender))
(listing (unwrap! (map-get? listings {namespace: namespace, name: name}) ERR-NOT-FOUND))
(price (get price listing))
(lister (get lister listing))
(list-commission (get commission listing))
)
(asserts! (is-eq (contract-of commission-trait) list-commission) ERR-WRONG-COMMISSION)
(asserts! (is-eq price expected-price) ERR-WRONG-PRICE)
(try! (contract-call? commission-trait pay u0 price))
(try! (stx-transfer? price tx-sender lister))
(map-delete listings {namespace: namespace, name: name})
(to-bool-response (as-contract
(contract-call?
'SP000000000000000000002Q6VF78.bns
name-transfer
namespace
name
new-owner
none
)
))
)
)
(define-private (to-bool-response (value (response bool int)))
(match value
success (ok success)
error (err (to-uint error))))
(list-name 0x627463 0x6d61707061 u9468599034 'SPNWZ5V2TPWGQGVDR6T7B6RQ4XMGZ4PXTEE0VQ0S.gamma-commission-3-5)