/
indexer-registry-dev-preview-2.clar
70 lines (70 loc) · 3.38 KB
/
indexer-registry-dev-preview-2.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
(define-constant ERR-NOT-AUTHORIZED (err u1000))
(define-constant ERR-PAUSED (err u1001))
(define-constant ERR-TX-NOT-INDEXED (err u1002))
(define-data-var contract-owner principal tx-sender)
(define-map approved-operators principal bool)
(define-data-var is-paused bool true)
(define-map bitcoin-tx-mined (buff 4096) bool)
(define-map bitcoin-tx-indexed { tx-hash: (buff 4096), output: uint, offset: uint } { tick: (string-utf8 4), amt: uint, from: (buff 128), to: (buff 128) })
(define-map user-balance { user: (buff 128), tick: (string-utf8 4) } { balance: uint, up-to-block: uint })
(define-map tick-decimals (string-utf8 4) uint)
(define-public (set-paused (paused bool))
(begin
(try! (check-is-owner))
(ok (var-set is-paused paused))))
(define-public (approve-operator (operator principal) (approved bool))
(begin
(try! (check-is-owner))
(ok (map-set approved-operators operator approved))))
(define-public (set-contract-owner (owner principal))
(begin
(try! (check-is-owner))
(ok (var-set contract-owner owner))))
(define-read-only (get-contract-owner)
(var-get contract-owner))
(define-read-only (get-paused)
(var-get is-paused))
(define-read-only (get-approved-operator-or-default (operator principal))
(default-to false (map-get? approved-operators operator))
)
(define-read-only (get-user-balance-or-default (user (buff 128)) (tick (string-utf8 4)))
(default-to { balance: u0, up-to-block: u0 } (map-get? user-balance { user: user, tick: tick })))
(define-read-only (get-bitcoin-tx-mined-or-default (tx (buff 4096)))
(default-to false (map-get? bitcoin-tx-mined tx))
)
(define-read-only (get-tick-decimals-or-default (tick (string-utf8 4)))
(default-to u18 (map-get? tick-decimals tick))
)
(define-read-only (get-bitcoin-tx-indexed-or-fail (bitcoin-tx (buff 4096)) (output uint) (offset uint))
(ok (unwrap! (map-get? bitcoin-tx-indexed { tx-hash: bitcoin-tx, output: output, offset: offset }) ERR-TX-NOT-INDEXED)))
(define-public (set-tick-decimals (tick (string-utf8 4)) (decimals uint))
(begin
(try! (check-is-approved))
(print { type: "tick-decimals-updated", tick: tick, decimals: decimals})
(ok (map-set tick-decimals tick decimals))
)
)
(define-public (set-user-balance (key { user: (buff 128), tick: (string-utf8 4) }) (value { balance: uint, up-to-block: uint }))
(begin
(try! (check-is-approved))
(print { type: "user-balance-updated", user: (get user key), tick: (get tick key), balance: (get balance value), up-to-block: (get up-to-block value) })
(ok (map-set user-balance key value))))
(define-public (set-tx-mined (key (buff 4096)) (value bool))
(begin
(try! (check-is-approved))
(print { type: "tx-mined", tx-hash: key })
(ok (map-set bitcoin-tx-mined key value))
)
)
(define-public (set-tx-indexed (key { tx-hash: (buff 4096), output: uint, offset: uint }) (value { tick: (string-utf8 4), amt: uint, from: (buff 128), to: (buff 128) }))
(begin
(try! (check-is-approved))
(print { type: "tx-indexed", tx-hash: (get tx-hash key), output: (get output key), offset: (get offset key), tick: (get tick value), amt: (get amt value), from: (get from value), to: (get to value) })
(ok (map-set bitcoin-tx-indexed key value))
)
)
(define-private (check-is-approved)
(ok (asserts! (or (get-approved-operator-or-default tx-sender) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED))
)
(define-private (check-is-owner)
(ok (asserts! (is-eq (var-get contract-owner) tx-sender) ERR-NOT-AUTHORIZED)))