/
spaghetti-punk-staking.clar
309 lines (281 loc) · 16.4 KB
/
spaghetti-punk-staking.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
(use-trait nft-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
(use-trait token-trait .token-trait.token-trait)
(use-trait lookup-trait .lookup-trait.lookup-trait)
(define-constant ERR-NOT-AUTHORIZED u401)
(define-constant ERR-INVALID-TRADE u103)
(define-constant ERR-INVALID-STAKE u104)
(define-constant ERR-ITEM-PRICE-TOO-LOW u102)
(define-constant CONTRACT-OWNER tx-sender)
(define-data-var blocks-per-token uint u29)
(define-data-var blocks-per-token-bonus uint u288)
(define-data-var staking-fees (list 1000 uint) (list u0 u0))
(define-data-var unstaking-fees (list 1000 uint) (list u3000000 u2000000))
(define-data-var payout-addresses (list 1000 principal) (list 'SP2H3TTG3MQK9CEF59S7VQ86H4FX9CH596ZXSE2EK 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C))
(define-data-var approved-principals (list 1000 principal) (list ))
(define-data-var shutoff-valve bool false)
(define-data-var removing-item-id uint u0)
(define-data-var token-address principal .test-coin)
(define-data-var bonus-check (list 1000 principal) (list ))
(define-data-var bonus-period bool true)
(define-map stakes { staker: principal, collection: principal, item: uint } { stake-time: uint, points: uint, multiplier: uint })
(define-map pool { staker: principal } { stake-time: uint, lifetime-points: uint, points-balance: uint, total-multiplier: uint })
(define-map staked-nfts { staker: principal, collection: principal } { ids: (list 2500 uint) })
(define-map owners { collection: principal, item: uint } { owner: principal })
(define-public (stake (collection <nft-trait>) (lookup-table <lookup-trait>) (item uint) (addresses (list 1000 principal)))
(let (
(points (if (is-some (map-get? stakes { staker: tx-sender, collection: (contract-of collection), item: item })) (unwrap-panic (get points (map-get? stakes { staker: tx-sender, collection: (contract-of collection), item: item }))) u0))
(balance (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get points-balance (map-get? pool { staker: tx-sender }))) u0))
(lifetime (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get lifetime-points (map-get? pool { staker: tx-sender }))) u0))
(multiplier (unwrap-panic (contract-call? lookup-table lookup (- item u1))))
(total-multiplier (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get total-multiplier (map-get? pool { staker: tx-sender }))) u0))
(block block-height)
(prev-time (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get stake-time (map-get? pool { staker: tx-sender }))) u0))
(points-added (/ (/ (* u1000000 (* total-multiplier (- block prev-time))) u10000) (var-get blocks-per-token)))
(bonus-points (/ (* u100000000 (- block prev-time)) (var-get blocks-per-token-bonus)))
(ids (get-staked-nfts tx-sender (contract-of collection)))
)
(asserts! (is-eq (var-get shutoff-valve) false) (err ERR-NOT-AUTHORIZED))
(asserts! (is-some (index-of (var-get approved-principals) (contract-of collection))) (err ERR-NOT-AUTHORIZED))
(asserts! (is-some (index-of (var-get approved-principals) (contract-of lookup-table))) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq (unwrap-panic (unwrap-panic (contract-call? collection get-owner item))) tx-sender) (err ERR-NOT-AUTHORIZED))
(begin
(if (> (unwrap-panic (get-staking-fee)) u0)
(begin
(print (map pay (var-get payout-addresses) (var-get staking-fees)))
(try! (contract-call? collection transfer item tx-sender (as-contract tx-sender)))
)
(begin
(try! (contract-call? collection transfer item tx-sender (as-contract tx-sender)))
)
)
(map-set stakes { staker: tx-sender, collection: (contract-of collection), item: item } { stake-time: block, points: points, multiplier: multiplier })
(if (bonus-check-all addresses)
(map-set pool { staker: tx-sender } { stake-time: block, lifetime-points: lifetime, points-balance: (+ (+ balance points-added) bonus-points), total-multiplier: (+ total-multiplier multiplier) })
(map-set pool { staker: tx-sender } { stake-time: block, lifetime-points: lifetime, points-balance: (+ balance points-added), total-multiplier: (+ total-multiplier multiplier) })
)
;; (map-set pool { staker: tx-sender } { stake-time: block, lifetime-points: (+ lifetime points-added), points-balance: (+ balance points-added), total-multiplier: (+ total-multiplier multiplier) })
(map-set owners { collection: (contract-of collection), item: item } { owner: tx-sender })
(map-set staked-nfts { staker: tx-sender, collection: (contract-of collection) }
{ ids: (unwrap-panic (as-max-len? (append ids item) u2500)) }
)
(print (map-get? pool { staker: tx-sender }))
(print (map-get? stakes { staker: tx-sender, collection: (contract-of collection), item: item }))
(ok true)
)
)
)
(define-public (unstake (collection <nft-trait>) (lookup-table <lookup-trait>) (item uint) (addresses (list 1000 principal)))
(let (
(owner (unwrap-panic (get owner (map-get? owners { collection: (contract-of collection), item: item }))))
(block block-height)
(points (if (is-some (map-get? stakes { staker: tx-sender, collection: (contract-of collection), item: item })) (unwrap-panic (get points (map-get? stakes { staker: tx-sender, collection: (contract-of collection), item: item }))) u0))
(balance (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get points-balance (map-get? pool { staker: tx-sender }))) u0))
(lifetime (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get lifetime-points (map-get? pool { staker: tx-sender }))) u0))
(multiplier (unwrap-panic (contract-call? lookup-table lookup (- item u1))))
(total-multiplier (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get total-multiplier (map-get? pool { staker: tx-sender }))) u0))
(prev-time (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get stake-time (map-get? pool { staker: tx-sender }))) u0))
(points-added (/ (/ (* u1000000 (* total-multiplier (- block prev-time))) u10000) (var-get blocks-per-token)))
(bonus-points (/ (* u100000000 (- block prev-time)) (var-get blocks-per-token-bonus)))
(ids (get-staked-nfts owner (contract-of collection)))
)
(asserts! (is-eq (var-get shutoff-valve) false) (err ERR-NOT-AUTHORIZED))
(asserts! (is-some (index-of (var-get approved-principals) (contract-of collection))) (err ERR-NOT-AUTHORIZED))
(asserts! (is-some (index-of (var-get approved-principals) (contract-of lookup-table))) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq owner tx-sender) (err ERR-NOT-AUTHORIZED))
(var-set removing-item-id item)
(begin
(print points)
(if (> (unwrap-panic (get-unstaking-fee)) u0)
(begin
(print (map pay (var-get payout-addresses) (var-get unstaking-fees)))
(try! (as-contract (contract-call? collection transfer item (as-contract tx-sender) owner )))
)
(begin
(try! (as-contract (contract-call? collection transfer item (as-contract tx-sender) owner )))
)
)
(map-set stakes { staker: tx-sender, collection: (contract-of collection), item: item } { stake-time: block, points: points-added, multiplier: multiplier })
(if (bonus-check-all addresses)
(map-set pool { staker: tx-sender } { stake-time: block, lifetime-points: lifetime, points-balance: (+ (+ balance points-added) bonus-points), total-multiplier: (+ total-multiplier multiplier) })
(map-set pool { staker: tx-sender } { stake-time: block, lifetime-points: lifetime, points-balance: (+ balance points-added), total-multiplier: (+ total-multiplier multiplier) })
)
(map-set staked-nfts { staker: tx-sender, collection: (contract-of collection) }
{ ids: (filter remove-item-id ids) }
)
(print (map-get? stakes { staker: tx-sender, collection: (contract-of collection), item: item }))
(print (map-get? pool { staker: tx-sender }))
(ok true)
)
)
)
(define-private (remove-item-id (item-id uint))
(if (is-eq item-id (var-get removing-item-id))
false
true
)
)
(define-public (collect (fungible <token-trait>) (addresses (list 1000 principal)))
(let (
(block block-height)
(owner tx-sender)
(balance (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get points-balance (map-get? pool { staker: tx-sender }))) u0))
(lifetime (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get lifetime-points (map-get? pool { staker: tx-sender }))) u0))
(total-multiplier (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get total-multiplier (map-get? pool { staker: tx-sender }))) u0))
(prev-time (if (is-some (map-get? pool { staker: tx-sender })) (unwrap-panic (get stake-time (map-get? pool { staker: tx-sender }))) u0))
(points-added (/ (/ (* u1000000 (* total-multiplier (- block prev-time))) u10000) (var-get blocks-per-token)))
(bonus-points (/ (* u100000000 (- block prev-time)) (var-get blocks-per-token-bonus)))
(to-collect (+ bonus-points (+ balance points-added)))
)
(asserts! (is-eq (var-get shutoff-valve) false) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq (var-get token-address) (contract-of fungible)) (err ERR-NOT-AUTHORIZED))
(begin
(try! (as-contract (contract-call? fungible collect owner to-collect)))
(if (bonus-check-all addresses)
(map-set pool { staker: tx-sender } { stake-time: block, lifetime-points: (+ (+ lifetime points-added) bonus-points), points-balance: u0, total-multiplier: total-multiplier })
(map-set pool { staker: tx-sender } { stake-time: block, lifetime-points: (+ lifetime points-added), points-balance: u0, total-multiplier: total-multiplier })
)
(print (map-get? pool { staker: tx-sender }))
(ok true)
)
)
)
(define-public (admin-unstake (collection <nft-trait>) (lookup-table <lookup-trait>) (item uint) (addresses (list 1000 principal)))
(let (
(owner (unwrap-panic (get owner (map-get? owners { collection: (contract-of collection), item: item }))))
(block block-height)
(points (if (is-some (map-get? stakes { staker: owner, collection: (contract-of collection), item: item })) (unwrap-panic (get points (map-get? stakes { staker: owner, collection: (contract-of collection), item: item }))) u0))
(balance (if (is-some (map-get? pool { staker: owner })) (unwrap-panic (get points-balance (map-get? pool { staker: owner }))) u0))
(lifetime (if (is-some (map-get? pool { staker: owner })) (unwrap-panic (get lifetime-points (map-get? pool { staker: owner }))) u0))
(multiplier (unwrap-panic (contract-call? lookup-table lookup (- item u1))))
(total-multiplier (if (is-some (map-get? pool { staker: owner })) (unwrap-panic (get total-multiplier (map-get? pool { staker: owner }))) u0))
(prev-time (if (is-some (map-get? pool { staker: owner })) (unwrap-panic (get stake-time (map-get? pool { staker: owner }))) u0))
(points-added (/ (/ (* u1000000 (* total-multiplier (- block prev-time))) u10000) (var-get blocks-per-token)))
(bonus-points (/ (* u100000000 (- block prev-time)) (var-get blocks-per-token-bonus)))
(ids (get-staked-nfts owner (contract-of collection)))
)
(asserts! (is-eq tx-sender CONTRACT-OWNER) (err ERR-NOT-AUTHORIZED))
(begin
(print points)
(try! (as-contract (contract-call? collection transfer item (as-contract tx-sender) owner )))
(map-set stakes { staker: owner, collection: (contract-of collection), item: item } { stake-time: block, points: points-added, multiplier: multiplier })
(if (bonus-check-all addresses)
(map-set pool { staker: owner } { stake-time: block, lifetime-points: lifetime, points-balance: (+ (+ balance points-added) bonus-points), total-multiplier: (+ total-multiplier multiplier) })
(map-set pool { staker: owner } { stake-time: block, lifetime-points: lifetime, points-balance: (+ balance points-added), total-multiplier: (+ total-multiplier multiplier) })
)
(map-set staked-nfts { staker: owner, collection: (contract-of collection) }
{ ids: (filter remove-item-id ids) }
)
(print (map-get? stakes { staker: owner, collection: (contract-of collection), item: item }))
(print (map-get? pool { staker: owner }))
(ok true)
)
)
)
(define-read-only (check-staker (staker principal))
(ok (map-get? pool { staker: staker }))
)
(define-read-only (check-collect (staker principal) (addresses (list 1000 principal)))
(let (
(block block-height)
(owner staker)
(balance (if (is-some (map-get? pool { staker: owner })) (unwrap-panic (get points-balance (map-get? pool { staker: owner }))) u0))
(lifetime (if (is-some (map-get? pool { staker: owner })) (unwrap-panic (get lifetime-points (map-get? pool { staker: owner }))) u0))
(total-multiplier (if (is-some (map-get? pool { staker: owner })) (unwrap-panic (get total-multiplier (map-get? pool { staker: owner }))) u0))
(prev-time (if (is-some (map-get? pool { staker: owner })) (unwrap-panic (get stake-time (map-get? pool { staker: owner }))) u0))
(points-added (/ (/ (* u1000000 (* total-multiplier (- block prev-time))) u10000) (var-get blocks-per-token)))
(bonus-points (/ (* u100000000 (- block prev-time)) (var-get blocks-per-token-bonus)))
(to-collect (+ balance points-added))
)
(if (bonus-check-all addresses)
(+ to-collect bonus-points)
to-collect
)
)
)
(define-public (pay (receiver principal) (price uint))
(begin
(try! (stx-transfer? price tx-sender receiver))
(ok true)
)
)
(define-public (principal-add (address principal))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set approved-principals (unwrap-panic (as-max-len? (append (var-get approved-principals) address) u1000))))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (bonus-add (address principal))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set bonus-check (unwrap-panic (as-max-len? (append (var-get bonus-check) address) u1000))))
(err ERR-NOT-AUTHORIZED)
)
)
(define-read-only (get-staked-nfts (staker principal) (address principal))
(default-to
(list )
(get ids (map-get? staked-nfts { staker: staker, collection: address }))
)
)
(define-read-only (get-staking-fee)
(ok (fold + (var-get staking-fees) u0))
)
(define-read-only (bonus-check-one (address principal) (collection principal))
(is-some (map-get? staked-nfts { staker: address, collection: collection }))
)
(define-read-only (bonus-check-all (addresses (list 1000 principal)))
(if (is-some (index-of (map bonus-check-one addresses (var-get bonus-check)) false))
false
true
)
)
(define-read-only (get-unstaking-fee)
(ok (fold + (var-get unstaking-fees) u0))
)
(define-public (shutoff-switch (switch bool))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set shutoff-valve switch))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (bonus (switch bool))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set bonus-period switch))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (change-emissions (blocks uint))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set blocks-per-token blocks))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (bonus-check-change (addresses (list 1000 principal)))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set bonus-check addresses))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (staking-fee-change (amounts (list 1000 uint)))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set staking-fees amounts))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (unstaking-fee-change (amounts (list 1000 uint)))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set unstaking-fees amounts))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (token-change (address principal))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set token-address address))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (payout-addresses-change (addresses (list 1000 principal)))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set payout-addresses addresses))
(err ERR-NOT-AUTHORIZED)
)
)