contract_name
stringlengths 1
40
| code_body
stringlengths 0
100k
| txid
stringlengths 66
66
| block_height
int32 0
165k
| status
stringclasses 3
values |
---|---|---|---|---|
get-activation-block
|
;; get-activation-bloc
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2EMP9PXNNNEZ51FCBDA0X7HYFWGDEXR8T84MMK6)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x7a5d167f84c4b12a7859f49f279005de2c6a93d6646aad1d526896d560bbf95c
| 24,646 |
success
|
register-user
|
;; get-activation-bloc
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2EMP9PXNNNEZ51FCBDA0X7HYFWGDEXR8T84MMK6)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x7cf0694f59ff63b328a3522629c68be9f07b578506a15decc552e4e8840f3ac6
| 24,648 |
success
|
mine-tokens
|
;; get-activation-bloc
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2EMP9PXNNNEZ51FCBDA0X7HYFWGDEXR8T84MMK6)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0xb67ef179131b8f972eab3b001b58ff149734cf713b3a3591fabc7163948742c7
| 24,652 |
success
|
claim-mining-reward
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2EMP9PXNNNEZ51FCBDA0X7HYFWGDEXR8T84MMK6)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0xf3cce76a5af330ae2102246fc3b99c27f24a132e5f2985f829f98ef8ac875c29
| 24,655 |
success
|
vs
|
(define-constant ERR_BET_TYPE 1001)
(define-constant ERR_ROUND_NOT_RIGHT 1002)
(define-constant ERR_INVALID_COST 1003)
(define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004)
(define-constant ERR_HAS_BET_CURRENT_ROUND 1005)
(define-constant ERR_INVALID_BET_VALUE 1006)
(define-constant ERR_BALANCE_UNENOUGH 1007)
(define-constant ERR_TRANSFER_STX_ERR 1008)
(define-constant ERR_ROUND_NOT_FOUND 1009)
(define-constant ERR_ROUND_NOT_CURRENT 1010)
(define-constant ERR_CANCAL_IN_CD 1011)
(define-constant ERR_NO_BET 1012)
(define-constant ERR_ALREADY_DRAW 1013)
(define-constant ERR_NO_AUTHORITY 1014)
;;
(define-constant OWNER tx-sender)
(define-constant BET_TYPE_LIST (list u1 u2 u3))
(define-constant MAX_PLAYERS_PER_ROUND u360)
(define-constant MIN_BET_COUNT_PER_ROUND u10)
(define-constant HIT_SHARES u1000000)
(define-constant MAX_BLOCKS_PER_ROUND u36) ;; 6 hours
(define-constant DRAW_CD_BLOCKS u3) ;; 0.5 hour
(define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours
(define-constant UPDATE_PRICE_COST u1000000)
(define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50))
(define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100))
(define-constant BUFF_TO_BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
(define-constant LIST_360 (list
u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25
u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50
u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75
u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100
u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125
u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150
u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175
u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200
u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225
u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250
u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275
u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300
u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325
u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350
u351 u352 u353 u354 u355 u356 u357 u358 u359 u360
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-map bet-round-map
uint ;; bet-type
uint ;; round
)
(define-map bet-summary-map
{ bet-type: uint, round: uint }
{
start-at: uint, ;; round start block
end-at: uint, ;; round end block
player-num: uint,
v-num: uint, ;; bet v count (v means left side [1-50])
s-num: uint, ;; bet s count (s means right side [51-100])
rand-num: uint, ;; the result random number
total-shares: uint ;; total shares of the winners
}
)
(define-map bet-record-map
{ bet-type: uint, round: uint, index: uint } ;; index start from 1
{
player: principal,
bet-value: uint, ;; 101*v+s
shares: uint,
win-num: int ;; >0 means win, <0 means lose
}
)
;; compress so that per round data can be fetched by single function call
(define-map bet-records-compress-map
{ bet-type: uint, round: uint }
(list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1)
)
(define-map player-times-map
{ player: principal, bet-type: uint }
uint ;; times
)
(define-map player-record-map
{ player: principal, bet-type: uint, index: uint } ;; index start from 1
{
round: uint,
bet-value: uint, ;; 101*v+s
win-num: int ;; >0 means win, <0 means lose
}
)
(define-map price-map
uint ;; bet-type
uint ;; mstx
)
(define-data-var tips (string-utf8 128) u"I WISH YOU WIN!")
;; draw logic related
(define-data-var draw-bet-type uint u0)
(define-data-var draw-round uint u0)
(define-data-var draw-bet-price uint u0)
(define-data-var draw-rand-num uint u0)
(define-data-var draw-rand-is-v bool false)
(define-data-var draw-total-shares uint u0)
(define-data-var draw-award-pool-num uint u0)
(define-data-var draw-pool-remain uint u0)
(define-map will-draw-map
uint ;; bet-type
uint ;; round-will-draw
)
;; compress logic used
(define-data-var compress-bet-type uint u0)
(define-data-var compress-round uint u0)
(define-map will-compress-map
uint ;; bet-type
uint ;; round-will-compress
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; bet-round = 0 means always current round
(define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint))
(let
(
(cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE)))
(player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type })))
(player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times }))
(bet-v-flag (and (>= bet-v u1) (<= bet-v u50)))
(bet-s-flag (and (>= bet-s u51) (<= bet-s u100)))
(bet-price (* (unwrap-panic (map-get? price-map bet-type)) (if (and bet-v-flag bet-s-flag) u2 u1)))
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round })))
(player-num (+ (get player-num bet-summary-tuple) u1))
(v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0)))
(s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0)))
(is-first-player (is-eq player-num u1))
(start-at (if is-first-player block-height (get start-at bet-summary-tuple)))
)
(asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT))
(asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND))
(asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT))
(asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE))
(asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE))
(asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE))
(asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST))
(asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH))
;; deduct
(unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR))
(let
(
(bet-value (+ (* u101 bet-v) bet-s))
)
;; note bet related map
(map-set bet-summary-map
{ bet-type: bet-type, round: cur-round }
(merge bet-summary-tuple {
start-at: start-at,
player-num: player-num,
v-num: v-num,
s-num: s-num
})
)
(map-set bet-record-map
{ bet-type: bet-type, round: cur-round, index: player-num }
{
player: tx-sender,
bet-value: bet-value,
shares: u0,
win-num: 0
}
)
;; note player related map
(map-set player-times-map
{ player: tx-sender, bet-type: bet-type }
(+ player-times u1)
)
(map-set player-record-map
{ player: tx-sender, bet-type: bet-type, index: (+ player-times u1) }
{
round: cur-round,
bet-value: bet-value,
win-num: 0
}
)
)
;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or
;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then
;; (end cur round, start next round)
(if
(or
(>= player-num MAX_PLAYERS_PER_ROUND)
(and
(> block-height (+ start-at MAX_BLOCKS_PER_ROUND))
(>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND)
(> (* v-num u2) s-num)
(> (* s-num u2) v-num)
)
)
(let
(
(cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified
)
(map-set bet-summary-map
{ bet-type: bet-type, round: cur-round }
(merge cur-bet-summary-tuple {
end-at: block-height
})
)
(restart-round bet-type (+ cur-round u1))
)
true
)
;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)):
;; draw round-will-draw
;; elseif ((has round-will-compress) and (round-will-compress has draw):
;; compress round-will-compress
(let
(
(round-will-draw (unwrap-panic (map-get? will-draw-map bet-type)))
(will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw })))
(round-will-compress (unwrap-panic (map-get? will-compress-map bet-type)))
(will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress })))
)
(if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1
(draw bet-type round-will-draw)
(if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0))
(compress-records bet-type round-will-compress)
true
)
)
)
;;
(ok true)
)
)
(define-private (draw (bet-type uint) (round uint))
(let
(
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))
(crf-rand-num (calc-round-rand-num bet-type round))
(rand-num (process-crf-rand-num crf-rand-num))
(is-v (< rand-num u51))
)
(print {
title: "draw",
bet-type: bet-type,
round: round,
})
;;
(map-set will-draw-map bet-type (+ round u1))
(var-set draw-bet-type bet-type)
(var-set draw-round round)
(var-set draw-rand-num rand-num)
(var-set draw-rand-is-v is-v)
(var-set draw-total-shares u0)
(var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type)))
(var-set draw-award-pool-num
(*
(var-get draw-bet-price)
(if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple))
)
)
(var-set draw-pool-remain (var-get draw-award-pool-num))
;; calc each player's shares
(filter calc-shares-loop LIST_360)
(map-set bet-summary-map
{ bet-type: bet-type, round: round }
(merge bet-summary-tuple {
rand-num: rand-num,
total-shares: (var-get draw-total-shares)
})
)
;; distribute award
(filter award-loop LIST_360)
;; as the use of integer divide, there may be still a little remain here
(if (> (var-get draw-pool-remain) u0)
(unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false)
true
)
;; finish
true
)
)
(define-private (process-crf-rand-num (num uint))
(if (and (>= num u21) (<= num u30))
(unwrap-panic (element-at MAPPING_21_30_LIST (- num u21)))
(if (and (>= num u71) (<= num u80))
(unwrap-panic (element-at MAPPING_71_80_LIST (- num u71)))
num
)
)
)
(define-private (restart-round (bet-type uint) (round uint))
(begin
(print {
title: "restart-round",
bet-type: bet-type,
round: round
})
(map-set bet-round-map bet-type round)
(map-set bet-summary-map
{ bet-type: bet-type, round: round }
{
start-at: u0,
end-at: u0,
player-num: u0,
v-num: u0,
s-num: u0,
rand-num: u0,
total-shares: u0
}
)
)
)
(define-public (update-tips (in-tips (string-utf8 128)))
(begin
(asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY))
(ok (var-set tips in-tips))
)
)
;;;;;;;; calc-shares-loop begin ;;;;;;;;
(define-private (calc-shares-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
(rand-num (var-get draw-rand-num))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-value (get bet-value bet-record-tuple))
(bet-v (/ bet-value u101))
(bet-s (mod bet-value u101))
(shares (
if (<= rand-num u50)
(if (is-eq bet-v u0)
u0
(if (>= bet-v rand-num)
(/ HIT_SHARES (+ (- bet-v rand-num) u10))
(/ HIT_SHARES (+ (- rand-num bet-v) u10))
)
)
(if (is-eq bet-s u0)
u0
(if (>= bet-s rand-num)
(/ HIT_SHARES (+ (- bet-s rand-num) u10))
(/ HIT_SHARES (+ (- rand-num bet-s) u10))
)
)
)
)
)
(var-set draw-total-shares (+ (var-get draw-total-shares) shares))
(map-set bet-record-map
{ bet-type: bet-type, round: round, index: index }
(merge bet-record-tuple {
shares: shares
})
)
false
)
false
)
)
)
;;;;;;;; calc-shares-loop end ;;;;;;;;
;;;;;;;; award-loop begin ;;;;;;;;
(define-private (award-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
(total-shares (var-get draw-total-shares))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-price (var-get draw-bet-price))
(award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0))
(player (get player bet-record-tuple))
(bet-value (get bet-value bet-record-tuple))
(is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0)))
(win-num
(if is-bet-both-side
(- (to-int award) (to-int bet-price))
(if (> award u0) (to-int award) (- 0 (to-int bet-price)))
)
)
(player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type })))
(player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times })))
)
(and
(> award u0)
(unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false)
(var-set draw-pool-remain (- (var-get draw-pool-remain) award))
)
;;
(map-set bet-record-map
{ bet-type: bet-type, round: round, index: index }
(merge bet-record-tuple {
win-num: win-num
})
)
;;
(map-set player-record-map
{ player: player, bet-type: bet-type, index: player-times }
(merge player-record {
win-num: win-num
})
)
false
)
false
)
)
)
;;;;;;;; award-loop end ;;;;;;;;
(define-private (calc-round-rand-num (bet-type uint) (round uint))
(let
(
(end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))))
(vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS)))
)
(+ (mod (/ vrf-rand-num u10) u100) u1)
)
)
;;;;;;;; cancel round begin ;;;;;;;;
(define-public (cancel (bet-type uint) (round uint))
(let
(
(cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE)))
(bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND)))
)
(asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT))
(asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD))
(asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET))
(asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW))
(ok (cancel-round bet-type round))
)
)
(define-private (cancel-round (bet-type uint) (round uint))
(let
(
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))
)
(var-set draw-bet-type bet-type)
(var-set draw-round round)
(var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type)))
;; cancel each player
(filter cancel-loop LIST_360)
;; reset current round for each bet-type
(restart-round bet-type round)
)
)
(define-private (cancel-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-value (get bet-value bet-record-tuple))
(bet-v-flag (>= bet-value u101))
(bet-s-flag (> (mod bet-value u101) u0))
(player (get player bet-record-tuple))
(player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })))
)
;;
(map-delete bet-record-map { bet-type: bet-type, round: round, index: index })
;;
(if (> player-times u1)
(map-set player-times-map
{ player: player, bet-type: bet-type }
(- player-times u1)
)
(map-delete player-times-map { player: player, bet-type: bet-type })
)
(map-delete player-record-map { player: player, bet-type: bet-type, index: player-times })
;;
(unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false)
;;
false
)
false
)
)
)
;;;;;;;; cancel round end ;;;;;;;;
;;;;;;;; update price begin ;;;;;;;;
(define-public (update-price (bet-type uint) (price uint))
(begin
(asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY))
(unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH))
(force-cancel-bet-type bet-type)
(map-set price-map bet-type price)
(ok true)
)
)
(define-private (force-cancel-bet-type (bet-type uint))
(let
(
(cur-round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple
(if
(and
(> (get player-num bet-summary-tuple) u0)
(> (get start-at bet-summary-tuple) u0)
(is-eq (get rand-num bet-summary-tuple) u0)
)
(cancel-round bet-type cur-round)
true
)
true
)
)
)
;;;;;;;; update price end ;;;;;;;;
;;;;;;;; compress begin ;;;;;;;;
(define-private (compress-records (bet-type uint) (round uint))
(begin
(print {
title: "compress",
bet-type: bet-type,
round: round,
})
(map-set will-compress-map bet-type (+ round u1))
(var-set compress-bet-type bet-type)
(var-set compress-round round)
(let
(
(record-opt-list (map compress-record LIST_360))
(record-some-list (filter s-is-some record-opt-list))
(record-list (map s-unwrap record-some-list))
)
(map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list)
(map delete-record LIST_360)
true
)
)
)
(define-private (s-is-some (a (optional { p: principal, v: int })))
(is-some a)
)
(define-private (s-unwrap (a (optional { p: principal, v: int })))
(unwrap-panic a)
)
(define-private (compress-record (index uint))
(match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple
(let
(
(ori-win-num (get win-num bet-record-tuple))
(flag (if (>= ori-win-num 0) 1 -1))
(abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num)))
)
(some
{
p: (get player bet-record-tuple),
v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num)))
}
)
)
none
)
)
(define-private (delete-record (index uint))
(map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index })
)
;;;;;;;; compress end ;;;;;;;;
;;;;;;;; web use begin ;;;;;;;;
(define-read-only (get-summary (player-opt (optional principal)))
(ok {
bet-summarys: (map get-cur-bet-summary BET_TYPE_LIST),
extra-summarys: (map get-extra-summary BET_TYPE_LIST),
player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)),
block-height: block-height,
tips: (var-get tips),
})
)
(define-read-only (get-extra-summary (bet-type uint))
{
price: (unwrap-panic (map-get? price-map bet-type)),
;; comment as will exceed readonly function's read cost
;; will-draw: (unwrap-panic (map-get? will-draw-map bet-type)),
;; will-compress: (unwrap-panic (map-get? will-compress-map bet-type))
}
)
(define-read-only (get-will-summary (bet-type uint))
{
will-draw: (unwrap-panic (map-get? will-draw-map bet-type)),
will-compress: (unwrap-panic (map-get? will-compress-map bet-type))
}
)
(define-read-only (get-cur-bet-summary (bet-type uint))
(let
(
(round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(get-bet-summary bet-type round)
)
)
(define-read-only (get-bet-summary (bet-type uint) (round uint))
(let
(
(bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round }))
(start-at (default-to u0 (get start-at bet-summary)))
(start-time (default-to u0 (get-block-info? time start-at)))
(end-at (default-to u0 (get end-at bet-summary)))
(end-time (default-to u0 (get-block-info? time end-at)))
)
{
bet-type: bet-type,
round: round,
summary: bet-summary,
start-time: start-time,
end-time: end-time,
}
)
)
(define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal)))
(match player-opt player
(match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times
(match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record
(let
(
(cur-round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(if (is-eq cur-round (get round player-record))
(ok player-record)
(err none)
)
)
(err none)
)
(err none)
)
(err none)
)
)
(define-read-only (get-player-times (player principal) (bet-type uint))
(default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))
)
(define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint })))
(map get-player-bet-info key-list)
)
(define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint }))
(map-get? player-record-map key)
)
(define-read-only (get-compress-bet-records (bet-type uint) (round uint))
(map-get? bet-records-compress-map { bet-type: bet-type, round: round })
)
(define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint })))
(map get-bet-record key-list)
)
(define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint }))
(map-get? bet-record-map key)
)
;;;;;;;; web use end ;;;;;;;;
;;;;;;;; borrow from citycoins begin ;;;;;;;;
;; (https://github.com/citycoins/citycoin/tree/main/contracts)
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF_TO_BYTE byte))
)
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
(define-read-only (get-rand-uint-at-block (block-index uint))
(buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index))))
)
;;;;;;;; borrow from citycoins end ;;;;;;;;
;;;;;;;; initialize begin ;;;;;;;;
(define-private (init-bet-type (bet-type uint))
(begin
(restart-round bet-type u1)
(map-set will-draw-map bet-type u1)
(map-set will-compress-map bet-type u1)
(map-set price-map bet-type (* u100 bet-type))
)
)
(map init-bet-type BET_TYPE_LIST)
;;;;;;;; initialize end ;;;;;;;;
|
0xed4bfd9ff237cbe4d9257556822bb0d9bda399cc530fa1669c0f6e8599a7465b
| 24,667 |
success
|
claim-mining-reward
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP22JVYH52F33R584TM195J9Y84F1F9ARM4CW3R8Q)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x861d5b3b580c5b5b809c5a22ecaf414136ef160905a7a5f268ade5db05020c90
| 24,722 |
success
|
claim-mining-reward
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP22JVYH52F33R584TM195J9Y84F1F9ARM4CW3R8Q)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x861d5b3b580c5b5b809c5a22ecaf414136ef160905a7a5f268ade5db05020c90
| 24,721 |
success
|
versus
|
(define-constant ERR_BET_TYPE 1001)
(define-constant ERR_ROUND_NOT_RIGHT 1002)
(define-constant ERR_INVALID_COST 1003)
(define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004)
(define-constant ERR_HAS_BET_CURRENT_ROUND 1005)
(define-constant ERR_INVALID_BET_VALUE 1006)
(define-constant ERR_BALANCE_UNENOUGH 1007)
(define-constant ERR_TRANSFER_STX_ERR 1008)
(define-constant ERR_ROUND_NOT_FOUND 1009)
(define-constant ERR_ROUND_NOT_CURRENT 1010)
(define-constant ERR_CANCAL_IN_CD 1011)
(define-constant ERR_NO_BET 1012)
(define-constant ERR_ALREADY_DRAW 1013)
(define-constant ERR_NO_AUTHORITY 1014)
;;
(define-constant OWNER tx-sender)
(define-constant BET_TYPE_LIST (list u1 u2 u3))
(define-constant MAX_PLAYERS_PER_ROUND u360)
(define-constant MIN_BET_COUNT_PER_ROUND u10)
(define-constant HIT_SHARES u1000000)
(define-constant MAX_BLOCKS_PER_ROUND u36) ;; 6 hours
(define-constant DRAW_CD_BLOCKS u3) ;; 0.5 hour
(define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours
(define-constant UPDATE_PRICE_COST u1000000)
(define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50))
(define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100))
(define-constant BUFF_TO_BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
(define-constant LIST_360 (list
u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25
u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50
u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75
u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100
u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125
u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150
u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175
u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200
u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225
u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250
u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275
u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300
u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325
u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350
u351 u352 u353 u354 u355 u356 u357 u358 u359 u360
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-map bet-round-map
uint ;; bet-type
uint ;; round
)
(define-map bet-summary-map
{ bet-type: uint, round: uint }
{
start-at: uint, ;; round start block
end-at: uint, ;; round end block
player-num: uint,
v-num: uint, ;; bet v count (v means left side [1-50])
s-num: uint, ;; bet s count (s means right side [51-100])
rand-num: uint, ;; the result random number
total-shares: uint ;; total shares of the winners
}
)
(define-map bet-record-map
{ bet-type: uint, round: uint, index: uint } ;; index start from 1
{
player: principal,
bet-value: uint, ;; 101*v+s
shares: uint,
win-num: int ;; >0 means win, <0 means lose
}
)
;; compress so that per round data can be fetched by single function call
(define-map bet-records-compress-map
{ bet-type: uint, round: uint }
(list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1)
)
(define-map player-times-map
{ player: principal, bet-type: uint }
uint ;; times
)
(define-map player-record-map
{ player: principal, bet-type: uint, index: uint } ;; index start from 1
{
round: uint,
bet-value: uint, ;; 101*v+s
win-num: int ;; >0 means win, <0 means lose
}
)
(define-map price-map
uint ;; bet-type
uint ;; mstx
)
(define-data-var tips (string-utf8 128) u"I WISH YOU WIN!")
;; draw logic related
(define-data-var draw-bet-type uint u0)
(define-data-var draw-round uint u0)
(define-data-var draw-bet-price uint u0)
(define-data-var draw-rand-num uint u0)
(define-data-var draw-rand-is-v bool false)
(define-data-var draw-total-shares uint u0)
(define-data-var draw-award-pool-num uint u0)
(define-data-var draw-pool-remain uint u0)
(define-map will-draw-map
uint ;; bet-type
uint ;; round-will-draw
)
;; compress logic used
(define-data-var compress-bet-type uint u0)
(define-data-var compress-round uint u0)
(define-map will-compress-map
uint ;; bet-type
uint ;; round-will-compress
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; bet-round = 0 means always current round
(define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint))
(let
(
(cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE)))
(player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type })))
(player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times }))
(bet-v-flag (and (>= bet-v u1) (<= bet-v u50)))
(bet-s-flag (and (>= bet-s u51) (<= bet-s u100)))
(bet-price (* (unwrap-panic (map-get? price-map bet-type)) (if (and bet-v-flag bet-s-flag) u2 u1)))
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round })))
(player-num (+ (get player-num bet-summary-tuple) u1))
(v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0)))
(s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0)))
(is-first-player (is-eq player-num u1))
(start-at (if is-first-player block-height (get start-at bet-summary-tuple)))
)
(asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT))
(asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND))
(asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT))
(asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE))
(asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE))
(asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE))
(asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST))
(asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH))
;; deduct
(unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR))
(let
(
(bet-value (+ (* u101 bet-v) bet-s))
)
;; note bet related map
(map-set bet-summary-map
{ bet-type: bet-type, round: cur-round }
(merge bet-summary-tuple {
start-at: start-at,
player-num: player-num,
v-num: v-num,
s-num: s-num
})
)
(map-set bet-record-map
{ bet-type: bet-type, round: cur-round, index: player-num }
{
player: tx-sender,
bet-value: bet-value,
shares: u0,
win-num: 0
}
)
;; note player related map
(map-set player-times-map
{ player: tx-sender, bet-type: bet-type }
(+ player-times u1)
)
(map-set player-record-map
{ player: tx-sender, bet-type: bet-type, index: (+ player-times u1) }
{
round: cur-round,
bet-value: bet-value,
win-num: 0
}
)
)
;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or
;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then
;; (end cur round, start next round)
(if
(or
(>= player-num MAX_PLAYERS_PER_ROUND)
(and
(> block-height (+ start-at MAX_BLOCKS_PER_ROUND))
(>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND)
(> (* v-num u2) s-num)
(> (* s-num u2) v-num)
)
)
(let
(
(cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified
)
(map-set bet-summary-map
{ bet-type: bet-type, round: cur-round }
(merge cur-bet-summary-tuple {
end-at: block-height
})
)
(restart-round bet-type (+ cur-round u1))
)
true
)
;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)):
;; draw round-will-draw
;; elseif ((has round-will-compress) and (round-will-compress has draw):
;; compress round-will-compress
(let
(
(round-will-draw (unwrap-panic (map-get? will-draw-map bet-type)))
(will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw })))
(round-will-compress (unwrap-panic (map-get? will-compress-map bet-type)))
(will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress })))
)
(if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1
(draw bet-type round-will-draw)
(if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0))
(compress-records bet-type round-will-compress)
true
)
)
)
;;
(ok true)
)
)
(define-private (draw (bet-type uint) (round uint))
(let
(
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))
(crf-rand-num (calc-round-rand-num bet-type round))
(rand-num (process-crf-rand-num crf-rand-num))
(is-v (< rand-num u51))
)
(print {
title: "draw",
bet-type: bet-type,
round: round,
})
;;
(map-set will-draw-map bet-type (+ round u1))
(var-set draw-bet-type bet-type)
(var-set draw-round round)
(var-set draw-rand-num rand-num)
(var-set draw-rand-is-v is-v)
(var-set draw-total-shares u0)
(var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type)))
(var-set draw-award-pool-num
(*
(var-get draw-bet-price)
(if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple))
)
)
(var-set draw-pool-remain (var-get draw-award-pool-num))
;; calc each player's shares
(filter calc-shares-loop LIST_360)
(map-set bet-summary-map
{ bet-type: bet-type, round: round }
(merge bet-summary-tuple {
rand-num: rand-num,
total-shares: (var-get draw-total-shares)
})
)
;; distribute award
(filter award-loop LIST_360)
;; as the use of integer divide, there may be still a little remain here
(if (> (var-get draw-pool-remain) u0)
(unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false)
true
)
;; finish
true
)
)
(define-private (process-crf-rand-num (num uint))
(if (and (>= num u21) (<= num u30))
(unwrap-panic (element-at MAPPING_21_30_LIST (- num u21)))
(if (and (>= num u71) (<= num u80))
(unwrap-panic (element-at MAPPING_71_80_LIST (- num u71)))
num
)
)
)
(define-private (restart-round (bet-type uint) (round uint))
(begin
(print {
title: "restart-round",
bet-type: bet-type,
round: round
})
(map-set bet-round-map bet-type round)
(map-set bet-summary-map
{ bet-type: bet-type, round: round }
{
start-at: u0,
end-at: u0,
player-num: u0,
v-num: u0,
s-num: u0,
rand-num: u0,
total-shares: u0
}
)
)
)
(define-public (update-tips (in-tips (string-utf8 128)))
(begin
(asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY))
(ok (var-set tips in-tips))
)
)
;;;;;;;; calc-shares-loop begin ;;;;;;;;
(define-private (calc-shares-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
(rand-num (var-get draw-rand-num))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-value (get bet-value bet-record-tuple))
(bet-v (/ bet-value u101))
(bet-s (mod bet-value u101))
(shares (
if (<= rand-num u50)
(if (is-eq bet-v u0)
u0
(if (>= bet-v rand-num)
(/ HIT_SHARES (+ (- bet-v rand-num) u10))
(/ HIT_SHARES (+ (- rand-num bet-v) u10))
)
)
(if (is-eq bet-s u0)
u0
(if (>= bet-s rand-num)
(/ HIT_SHARES (+ (- bet-s rand-num) u10))
(/ HIT_SHARES (+ (- rand-num bet-s) u10))
)
)
)
)
)
(var-set draw-total-shares (+ (var-get draw-total-shares) shares))
(map-set bet-record-map
{ bet-type: bet-type, round: round, index: index }
(merge bet-record-tuple {
shares: shares
})
)
false
)
false
)
)
)
;;;;;;;; calc-shares-loop end ;;;;;;;;
;;;;;;;; award-loop begin ;;;;;;;;
(define-private (award-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
(total-shares (var-get draw-total-shares))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-price (var-get draw-bet-price))
(award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0))
(player (get player bet-record-tuple))
(bet-value (get bet-value bet-record-tuple))
(is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0)))
(win-num
(if is-bet-both-side
(- (to-int award) (to-int bet-price))
(if (> award u0) (to-int award) (- 0 (to-int bet-price)))
)
)
(player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type })))
(player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times })))
)
(and
(> award u0)
(unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false)
(var-set draw-pool-remain (- (var-get draw-pool-remain) award))
)
;;
(map-set bet-record-map
{ bet-type: bet-type, round: round, index: index }
(merge bet-record-tuple {
win-num: win-num
})
)
;;
(map-set player-record-map
{ player: player, bet-type: bet-type, index: player-times }
(merge player-record {
win-num: win-num
})
)
false
)
false
)
)
)
;;;;;;;; award-loop end ;;;;;;;;
(define-private (calc-round-rand-num (bet-type uint) (round uint))
(let
(
(end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))))
(vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS)))
)
(+ (mod (/ vrf-rand-num u10) u100) u1)
)
)
;;;;;;;; cancel round begin ;;;;;;;;
(define-public (cancel (bet-type uint) (round uint))
(let
(
(cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE)))
(bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND)))
)
(asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT))
(asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD))
(asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET))
(asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW))
(ok (cancel-round bet-type round))
)
)
(define-private (cancel-round (bet-type uint) (round uint))
(let
(
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))
)
(var-set draw-bet-type bet-type)
(var-set draw-round round)
(var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type)))
;; cancel each player
(filter cancel-loop LIST_360)
;; reset current round for each bet-type
(restart-round bet-type round)
)
)
(define-private (cancel-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-value (get bet-value bet-record-tuple))
(bet-v-flag (>= bet-value u101))
(bet-s-flag (> (mod bet-value u101) u0))
(player (get player bet-record-tuple))
(player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })))
)
;;
(map-delete bet-record-map { bet-type: bet-type, round: round, index: index })
;;
(if (> player-times u1)
(map-set player-times-map
{ player: player, bet-type: bet-type }
(- player-times u1)
)
(map-delete player-times-map { player: player, bet-type: bet-type })
)
(map-delete player-record-map { player: player, bet-type: bet-type, index: player-times })
;;
(unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false)
;;
false
)
false
)
)
)
;;;;;;;; cancel round end ;;;;;;;;
;;;;;;;; update price begin ;;;;;;;;
(define-public (update-price (bet-type uint) (price uint))
(begin
(asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY))
(unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH))
(force-cancel-bet-type bet-type)
(map-set price-map bet-type price)
(ok true)
)
)
(define-private (force-cancel-bet-type (bet-type uint))
(let
(
(cur-round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple
(if
(and
(> (get player-num bet-summary-tuple) u0)
(> (get start-at bet-summary-tuple) u0)
(is-eq (get rand-num bet-summary-tuple) u0)
)
(cancel-round bet-type cur-round)
true
)
true
)
)
)
;;;;;;;; update price end ;;;;;;;;
;;;;;;;; compress begin ;;;;;;;;
(define-private (compress-records (bet-type uint) (round uint))
(begin
(print {
title: "compress",
bet-type: bet-type,
round: round,
})
(map-set will-compress-map bet-type (+ round u1))
(var-set compress-bet-type bet-type)
(var-set compress-round round)
(let
(
(record-opt-list (map compress-record LIST_360))
(record-some-list (filter s-is-some record-opt-list))
(record-list (map s-unwrap record-some-list))
)
(map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list)
(map delete-record LIST_360)
true
)
)
)
(define-private (s-is-some (a (optional { p: principal, v: int })))
(is-some a)
)
(define-private (s-unwrap (a (optional { p: principal, v: int })))
(unwrap-panic a)
)
(define-private (compress-record (index uint))
(match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple
(let
(
(ori-win-num (get win-num bet-record-tuple))
(flag (if (>= ori-win-num 0) 1 -1))
(abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num)))
)
(some
{
p: (get player bet-record-tuple),
v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num)))
}
)
)
none
)
)
(define-private (delete-record (index uint))
(map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index })
)
;;;;;;;; compress end ;;;;;;;;
;;;;;;;; web use begin ;;;;;;;;
(define-read-only (get-summary (player-opt (optional principal)))
(ok {
bet-summarys: (map get-cur-bet-summary BET_TYPE_LIST),
extra-summarys: (map get-extra-summary BET_TYPE_LIST),
player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)),
block-height: block-height,
tips: (var-get tips),
})
)
(define-read-only (get-extra-summary (bet-type uint))
{
price: (unwrap-panic (map-get? price-map bet-type)),
;; comment as will exceed readonly function's read cost
;; will-draw: (unwrap-panic (map-get? will-draw-map bet-type)),
;; will-compress: (unwrap-panic (map-get? will-compress-map bet-type))
}
)
(define-read-only (get-will-summary (bet-type uint))
{
will-draw: (unwrap-panic (map-get? will-draw-map bet-type)),
will-compress: (unwrap-panic (map-get? will-compress-map bet-type))
}
)
(define-read-only (get-cur-bet-summary (bet-type uint))
(let
(
(round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(get-bet-summary bet-type round)
)
)
(define-read-only (get-bet-summary (bet-type uint) (round uint))
(let
(
(bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round }))
(start-at (default-to u0 (get start-at bet-summary)))
(start-time (default-to u0 (get-block-info? time start-at)))
(end-at (default-to u0 (get end-at bet-summary)))
(end-time (default-to u0 (get-block-info? time end-at)))
)
{
bet-type: bet-type,
round: round,
summary: bet-summary,
start-time: start-time,
end-time: end-time,
}
)
)
(define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal)))
(match player-opt player
(match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times
(match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record
(let
(
(cur-round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(if (is-eq cur-round (get round player-record))
(ok player-record)
(err none)
)
)
(err none)
)
(err none)
)
(err none)
)
)
(define-read-only (get-player-times (player principal) (bet-type uint))
(default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))
)
(define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint })))
(map get-player-bet-info key-list)
)
(define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint }))
(map-get? player-record-map key)
)
(define-read-only (get-compress-bet-records (bet-type uint) (round uint))
(map-get? bet-records-compress-map { bet-type: bet-type, round: round })
)
(define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint })))
(map get-bet-record key-list)
)
(define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint }))
(map-get? bet-record-map key)
)
;;;;;;;; web use end ;;;;;;;;
;;;;;;;; borrow from citycoins begin ;;;;;;;;
;; (https://github.com/citycoins/citycoin/tree/main/contracts)
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF_TO_BYTE byte))
)
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
(define-read-only (get-rand-uint-at-block (block-index uint))
(buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index))))
)
;;;;;;;; borrow from citycoins end ;;;;;;;;
;;;;;;;; initialize begin ;;;;;;;;
(define-private (init-bet-type (bet-type uint))
(begin
(restart-round bet-type u1)
(map-set will-draw-map bet-type u1)
(map-set will-compress-map bet-type u1)
(map-set price-map bet-type (* u50000 (pow u10 bet-type)))
)
)
(map init-bet-type BET_TYPE_LIST)
;;;;;;;; initialize end ;;;;;;;;
|
0xa28c33304bbf77f1a1f53be09bab4d9f42b0aef415fb7328661c99529ef0bc4b
| 24,745 |
success
|
claim-mining-reward
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x6cfbcc91c7e94673a5c134ae96be38f5e6abe010f245cce4ab82aa3916608825
| 24,767 |
success
|
fuck-this-scam-bullshit
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0xb0b12ccc4d053fcfd4f771f6705737c5a6d63bee115428fe735cfce0055fd561
| 24,767 |
success
|
dick-sucking-hoes
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x6d27ad2a52fed8365b36f96c192f0bc066517abee2d964f53ed17e0f89a9d0b9
| 24,777 |
success
|
hello-world
|
(define-public (say-hi)
(ok "hello world its albi.btc"))
(define-read-only (echo-number (val int))
(ok val))
|
0x5faad0329ade536d4e66ea61b05f310000758e8e10521b3bf8cf3feff5b825a6
| 24,785 |
success
|
passive-silver-condor
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2CBFJ4P6KPMG4JHXEEPDD9QB7NCQZBYG2DW0QQW)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x0beba499b486ac4baeb44dfc87db3d5f873b66ba4afc7ac61ad58486956d11f4
| 24,801 |
success
|
itchy-red-aardvark
|
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-data-var amountOfMIA uint u0)
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
(var-set amountOfMIA (+ (var-get amountOfMIA) amount))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(var-set amountOfMIA (- (var-get amountOfMIA) amount))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-eq contract-caller POOL_ADDRESS)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(var-set amountOfMIA (- (var-get amountOfMIA) amount))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED))
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (var-get amountOfMIA))
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0xb061b9dd1a67266aa14bedd7385e40f863e998c079d14e025045aa3ac6c2919b
| 24,997 |
success
|
near-yellow-coyote
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SPT6XVXWW1MAJPMVKQKGVBD3KAVD0N4R6HGTET12)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0xa1045be48ebd58a8c2088fce489b3b199038c4c4420130dbc7de345bcb87d378
| 24,997 |
success
|
buy-mia-v1
|
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-data-var amountOfMIA uint u0) ;; amount available to buy
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
;; update number of MIA available to buy
(var-set amountOfMIA (+ (var-get amountOfMIA) amount))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
;; update number of MIA available to buy
(var-set amountOfMIA (- (var-get amountOfMIA) amount))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-eq contract-caller POOL_ADDRESS)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
;; update number of MIA available to buy
(var-set amountOfMIA (- (var-get amountOfMIA) amount))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (var-get amountOfMIA))
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0xc32aef647a1bc6077eba4621f0f970d61691db2fd211e1e0f3f45bc5758e978a
| 25,007 |
success
|
managing-beige-koi
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2PF4965A4TAY2S3189H9ZSHVFHQ4CC3KQ52M0AS)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0xa914ac8eb8dd93c544af3c61d98da0e1eb23e48e1e3ba11146e33affbb6bf60e
| 25,088 |
success
|
buy-mia-v2
|
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-data-var amountOfMIA uint u0) ;; amount available to buy
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-auth-pool)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance (as-contract tx-sender)))
)
(define-read-only (get-contract-stx-balance)
(stx-get-balance (as-contract tx-sender))
)
(define-read-only (get-pool-mia-balance)
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance POOL_ADDRESS)
)
(define-read-only (get-pool-stx-balance)
(stx-get-balance POOL_ADDRESS)
)
(define-private (is-auth-pool)
(is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED)
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0x4b23b896d6b6d17d9641bb99169372cc363d633e11be55d4ea6fd80eee5deb20
| 25,110 |
abort_by_response
|
buy-mia-v3
|
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-auth-pool)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance (as-contract tx-sender)))
)
(define-read-only (get-contract-stx-balance)
(stx-get-balance (as-contract tx-sender))
)
(define-read-only (get-pool-mia-balance)
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance POOL_ADDRESS)
)
(define-read-only (get-pool-stx-balance)
(stx-get-balance POOL_ADDRESS)
)
(define-private (is-auth-pool)
(is-eq contract-caller POOL_ADDRESS)
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0xc83788a1b7bccb43e8c3d94a915f3ddf5bc6290dfa171082ba3a4c00c946524c
| 25,131 |
success
|
buy-mia-v3
|
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-auth-pool)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance (as-contract tx-sender)))
)
(define-read-only (get-contract-stx-balance)
(stx-get-balance (as-contract tx-sender))
)
(define-read-only (get-pool-mia-balance)
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance POOL_ADDRESS)
)
(define-read-only (get-pool-stx-balance)
(stx-get-balance POOL_ADDRESS)
)
(define-private (is-auth-pool)
(is-eq contract-caller POOL_ADDRESS)
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0xc83788a1b7bccb43e8c3d94a915f3ddf5bc6290dfa171082ba3a4c00c946524c
| 25,131 |
success
|
buy-mia-v4
|
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (as-contract (transfer-mia amount (as-contract tx-sender) contract-caller)))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-auth-pool)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (as-contract (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS)))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance (as-contract tx-sender)))
)
(define-read-only (get-contract-stx-balance)
(stx-get-balance (as-contract tx-sender))
)
(define-read-only (get-pool-mia-balance)
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance POOL_ADDRESS)
)
(define-read-only (get-pool-stx-balance)
(stx-get-balance POOL_ADDRESS)
)
(define-private (is-auth-pool)
(is-eq contract-caller POOL_ADDRESS)
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0xa7ba4ab1da03e677fa36cb920e1c3cb2bc19359dd2b47773f76d84abeb92d4ff
| 25,152 |
success
|
buy-mia-v5
|
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (as-contract (transfer-mia amount (as-contract tx-sender) contract-caller)))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-auth-pool)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))
;; send MIA to caller
(try! (as-contract (transfer-mia amount (as-contract tx-sender) contract-caller)))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance (as-contract tx-sender)))
)
(define-read-only (get-contract-stx-balance)
(stx-get-balance (as-contract tx-sender))
)
(define-read-only (get-pool-mia-balance)
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance POOL_ADDRESS)
)
(define-read-only (get-pool-stx-balance)
(stx-get-balance POOL_ADDRESS)
)
(define-private (is-auth-pool)
(is-eq contract-caller POOL_ADDRESS)
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0xc7ad15cd69e98deac753a8a965af95ac785c48e03df463af74401bcaf9b65424
| 25,159 |
success
|
homely-lime-bass
|
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant CONTRACT_ADDRESS (as-contract tx-sender))
(define-constant ERR_UNAUTHORIZED u1000)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller CONTRACT_ADDRESS))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to POOL owner address
(try! (as-contract (transfer-mia amount CONTRACT_ADDRESS POOL_ADDRESS)))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(let
((user contract-caller))
(asserts! (not (is-auth-pool)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) user POOL_ADDRESS))
;; send MIA to caller
(try! (as-contract (transfer-mia amount CONTRACT_ADDRESS user)))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance (as-contract tx-sender)))
)
(define-read-only (get-contract-stx-balance)
(stx-get-balance CONTRACT_ADDRESS)
)
(define-read-only (get-pool-mia-balance)
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance POOL_ADDRESS)
)
(define-read-only (get-pool-stx-balance)
(stx-get-balance POOL_ADDRESS)
)
(define-private (is-auth-pool)
(is-eq contract-caller POOL_ADDRESS)
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0xe8245eb0b74476a711185b3616427f6b176c0eed2b3b3964455d2ad77d5f22c8
| 25,182 |
success
|
billboard
|
;; error consts
(define-constant ERR_STX_TRANSFER u0)
;; data vars
(define-data-var billboard-message (string-utf8 500) u"Hello World!")
(define-data-var price uint u100)
;; public functions
(define-read-only (get-price)
(var-get price)
)
(define-read-only (get-message)
(var-get billboard-message)
)
(define-public (set-message (message (string-utf8 500)))
(let ((cur-price (var-get price))
(new-price (+ cur-price u10)))
;; pay the contract
(unwrap! (stx-transfer? cur-price tx-sender (as-contract tx-sender)) (err ERR_STX_TRANSFER))
;; update the billboard's message
(var-set billboard-message message)
;; update the price
(var-set price new-price)
;; return the updated price
(ok new-price)
)
)
|
0xb73f362725311b33ac9c72a253e4f5ef94065eccafbf725467af49eaee3d9086
| 25,184 |
success
|
claim-mining-reward
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2TY4F6R5X68N26ETCE10ANM1QKZ99WE6C9M9582)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0xd7d9a1c3034c859ee6a1c14161dfb8eab1717c48c7b5d87b4da53cfa41cdf7e7
| 25,190 |
success
|
buy-mia-v2
|
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-auth-pool)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance (as-contract tx-sender)))
)
(define-read-only (get-contract-stx-balance)
(stx-get-balance (as-contract tx-sender))
)
(define-read-only (get-pool-mia-balance)
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance POOL_ADDRESS)
)
(define-read-only (get-pool-stx-balance)
(stx-get-balance POOL_ADDRESS)
)
(define-private (is-auth-pool)
(is-eq contract-caller POOL_ADDRESS)
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)
|
0x242a96c8fd14cf906d9a4abf44cc2ba9a51aeddefb60c5396a2399ff05b5bd4e
| 25,244 |
success
|
sip-010-v0a
|
;; sip-010 trait
;; use https://explorer.stacks.co/txid/SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-10-ft-standard
(define-trait ft-trait
(
;; Transfer from the caller to a new principal
(transfer (uint principal principal) (response bool uint))
;; the human readable name of the token
(get-name () (response (string-ascii 32) uint))
;; the ticker symbol, or empty if none
(get-symbol () (response (string-ascii 32) uint))
;; the number of decimals used, e.g. 6 would mean 1_000_000 represents 1 token
(get-decimals () (response uint uint))
;; the balance of the passed principal
(get-balance-of (principal) (response uint uint))
;; the current total supply (which does not need to be a constant)
(get-total-supply () (response uint uint))
;; an optional URI that represents metadata of this token
(get-token-uri () (response (optional (string-utf8 256)) uint))
)
)
|
0xf2f37656f5327ca71d4dc1f5f960c77d010e1136765c2108728b1e0253b99f6f
| 25,385 |
success
|
liquidity-token-trait-v0a
|
;; this is an SIP-010 method with an additional functions used by stackswap-swap
;; as Clarity does not support "includes", copy the needed functions, and add new ones
(define-trait liquidity-token-trait
(
;; Transfer from the caller to a new principal
(transfer (uint principal principal) (response bool uint))
;; the human readable name of the token
(get-name () (response (string-ascii 32) uint))
;; the ticker symbol, or empty if none
(get-symbol () (response (string-ascii 32) uint))
;; the number of decimals used, e.g. 6 would mean 1_000_000 represents 1 token
(get-decimals () (response uint uint))
;; the balance of the passed principal
(get-balance-of (principal) (response uint uint))
;; the current total supply (which does not need to be a constant)
(get-total-supply () (response uint uint))
;; an optional URI that represents metadata of this token
(get-token-uri () (response (optional (string-utf8 256)) uint))
;; additional functions specific to stackswap-swap
;; mint function only stackswap-swap contract can call
(mint (principal uint) (response bool uint))
;; burn function only stackswap-swap contract can call
(burn (principal uint) (response bool uint))
;;
(get-data (principal) (response {
name: (string-ascii 32),
symbol: (string-ascii 32),
decimals: uint,
uri: (optional (string-utf8 256)),
supply: uint,
balance: uint,
} uint))
)
)
|
0xa0248a1ecdc767bfae22630e4b684b5be3184eedf0a52ac5ff221382d47b0343
| 25,387 |
success
|
restricted-token-trait-v0a
|
(define-trait restricted-token-trait
(
;; Called to detect if a transfer restriction will take place. Returns the
;; error code that explains why the transfer failed.
(detect-transfer-restriction (uint principal principal) (response uint uint))
;; Returns human readable string for a specific transfer restriction error code
;; which is returned from (detect-transfer-restriction).
;; This is a convenience function for end user wallets.
(message-for-restriction (uint) (response (string-ascii 1024) uint))
)
)
|
0x1724d9960537247f54c329581c388950ef3adb66a8d43c3605172a8c409d7d0a
| 25,389 |
success
|
stackswap--dao-token-trait-v0a
|
(define-trait dao-token-trait
(
;; Mint
(mint-for-dao (uint principal) (response bool uint))
;; Burn
(burn-for-dao (uint principal) (response bool uint))
)
)
|
0x8ba6e07a9035afc1341dbd1bc724ae41be9c70523dc2fea41b930a0990a7d237
| 25,391 |
success
|
initializable-trait-v0a
|
;; trait from project one-click-token
(define-trait initializable-token-trait
(
(initialize ((string-ascii 32) (string-ascii 32) uint (string-utf8 256) (string-utf8 256) principal uint) (response uint uint))
(set-token-uri ((string-utf8 256)) (response bool uint))
(approve (bool) (response bool uint))
)
)
(define-trait initializable-poxl-token-trait
(
(initialize ((string-ascii 32) (string-ascii 32) uint (string-utf8 256) (string-utf8 256) uint uint uint uint uint ) (response uint uint))
(set-token-uri ((string-utf8 256)) (response bool uint))
(approve (bool) (response bool uint))
)
)
(define-trait initializable-liquidity-token-trait
(
;; (set-token-uri ((string-utf8 256)) (response bool uint))
(initialize ((string-ascii 32) (string-ascii 32) uint (string-utf8 256)) (response uint uint))
)
)
|
0xdd8f2e61243c18083a0dfa58daddd979ed2b554d11faf5d65dba295a73dd8a15
| 25,397 |
success
|
stackswap-swap-fee-v0a
|
;; contract for intiial stackswap fee
;; start from 26000
;; daily block 170
;; 3 year block 186150
(define-constant FEE-START-BLOCK u212150)
(define-read-only (get-fee-1)
(if (> block-height FEE-START-BLOCK)
(ok u997)
(ok u997)
)
)
(define-read-only (get-fee-2)
(if (> block-height FEE-START-BLOCK)
(ok u1000)
(ok u1000)
)
)
(define-read-only (get-fee-3)
(if (> block-height FEE-START-BLOCK)
(ok u5)
(ok u5)
)
)
(define-read-only (get-fee-4)
(if (> block-height FEE-START-BLOCK)
(ok u10000)
(ok u10000)
)
)
|
0x0c50cc7f069354e49e9ad281d408dec491dd6f86f3cfe461548367d2eaff9599
| 25,399 |
success
|
stackswap-swap-v1c
|
(use-trait sip-010-token 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(use-trait liquidity-token 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(define-constant contract-owner tx-sender)
(define-constant no-liquidity-err (err u61))
;; (define-constant transfer-failed-err (err u62))
(define-constant not-owner-err (err u63))
(define-constant no-fee-to-address-err (err u64))
(define-constant invalid-pair-err (err u65))
(define-constant no-such-position-err (err u66))
(define-constant balance-too-low-err (err u67))
(define-constant too-many-pairs-err (err u68))
(define-constant pair-already-exists-err (err u69))
(define-constant wrong-token-err (err u70))
(define-constant too-much-slippage-err (err u71))
(define-constant transfer-x-failed-err (err u72))
(define-constant transfer-y-failed-err (err u73))
(define-constant value-out-of-range-err (err u74))
(define-constant no-fee-x-err (err u75))
(define-constant no-fee-y-err (err u76))
(define-constant pair-token-already-used-err (err u77))
(define-constant fee-contract-err (err u78))
;; for future use, or debug
(define-constant e10-err (err u20))
(define-constant e11-err (err u21))
(define-constant e12-err (err u22))
(define-map pairs-map
{ pair-id: uint }
{
token-x: principal,
token-y: principal,
}
)
(define-map pairs-token-map
{ pair-token: principal}
{ exist: uint}
)
(define-map pairs-data-map
{
token-x: principal,
token-y: principal,
}
{
shares-total: uint,
balance-x: uint,
balance-y: uint,
fee-balance-x: uint,
fee-balance-y: uint,
fee-to-address: (optional principal),
liquidity-token: principal,
name: (string-ascii 32),
}
)
(define-data-var pair-count uint u0)
(define-read-only (get-name (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>))
(let
(
(token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))
)
(ok (get name pair))
)
)
(define-public (get-symbol (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>))
(ok
(concat
(unwrap-panic (as-max-len? (unwrap-panic (contract-call? token-x-trait get-symbol)) u15))
(concat "-"
(unwrap-panic (as-max-len? (unwrap-panic (contract-call? token-y-trait get-symbol)) u15))
)
)
)
)
(define-read-only (get-total-supply (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>))
(let
(
(token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))
)
(ok (get shares-total pair))
)
)
;; get the total number of shares in the pool
(define-read-only (get-shares (token-x principal) (token-y principal))
(ok (get shares-total (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)))
)
;; get overall balances for the pair
(define-public (get-balances (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>))
(let
(
(token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))
)
(ok (list (get balance-x pair) (get balance-y pair)))
)
)
(define-public (get-data (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-liquidity-trait <liquidity-token>) (owner principal))
(let
(
(token-data (unwrap-panic (contract-call? token-liquidity-trait get-data owner)))
(balances (unwrap-panic (get-balances token-x-trait token-y-trait)))
)
(ok (merge token-data { balances: balances }))
)
)
;; since we can't use a constant to refer to contract address, here what x and y are
;; (define-constant x-token 'SP2NC4YKZWM2YMCJV851VF278H9J50ZSNM33P3JM1.my-token)
;; (define-constant y-token 'SP1QR3RAGH3GEME9WV7XB0TZCX6D5MNDQP97D35EH.my-token)
(define-public (add-to-position (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-liquidity-trait <liquidity-token>) (x uint) (y uint) )
(let
(
(token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(pair (unwrap-panic (map-get? pairs-data-map { token-x: token-x, token-y: token-y })))
(contract-address (as-contract tx-sender))
(recipient-address tx-sender)
(balance-x (get balance-x pair))
(balance-y (get balance-y pair))
(new-shares
(if (is-eq (get shares-total pair) u0)
(sqrti (* x y)) ;; burn a fraction of initial lp token to avoid attack as described in WP https://uniswap.org/whitepaper.pdf
(/ (* x (get shares-total pair)) balance-x)
)
)
(new-y
(if (is-eq (get shares-total pair) u0)
y
(/ (* x balance-y) balance-x)
)
)
(pair-updated (merge pair {
shares-total: (+ new-shares (get shares-total pair)),
balance-x: (+ balance-x x),
balance-y: (+ balance-y new-y)
}))
)
(asserts! (is-ok (contract-call? token-x-trait transfer x tx-sender contract-address)) transfer-x-failed-err)
(asserts! (is-ok (contract-call? token-y-trait transfer new-y tx-sender contract-address)) transfer-y-failed-err)
(map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-updated)
(try! (contract-call? token-liquidity-trait mint recipient-address new-shares))
(print { object: "pair", action: "liquidity-added", data: pair-updated })
(ok true)
)
)
(define-read-only (get-pair-details (token-x principal) (token-y principal))
(unwrap-panic (map-get? pairs-data-map { token-x: token-x, token-y: token-y }))
)
(define-read-only (get-pair-contracts (pair-id uint))
(unwrap-panic (map-get? pairs-map { pair-id: pair-id }))
)
(define-read-only (get-pair-count)
(ok (var-get pair-count))
)
(define-public (create-pair (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-liquidity-trait <liquidity-token>) (pair-name (string-ascii 32)) (x uint) (y uint))
(let
(
(name-x (unwrap-panic (contract-call? token-x-trait get-name)))
(name-y (unwrap-panic (contract-call? token-y-trait get-name)))
(token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(token-liquidity (contract-of token-liquidity-trait))
(pair-id (+ (var-get pair-count) u1))
(pair-data {
shares-total: u0,
balance-x: u0,
balance-y: u0,
fee-balance-x: u0,
fee-balance-y: u0,
fee-to-address: none,
liquidity-token: (contract-of token-liquidity-trait),
name: pair-name,
})
)
(asserts!
(and
(is-none (map-get? pairs-data-map { token-x: token-x, token-y: token-y }))
(is-none (map-get? pairs-data-map { token-x: token-y, token-y: token-x }))
(is-none (map-get? pairs-token-map { pair-token: token-liquidity}))
)
pair-already-exists-err
)
(map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-data)
(map-set pairs-token-map { pair-token: token-liquidity} {exist: u1})
(map-set pairs-map { pair-id: pair-id } { token-x: token-x, token-y: token-y })
(var-set pair-count pair-id)
(try! (add-to-position token-x-trait token-y-trait token-liquidity-trait x y))
(print { object: "pair", action: "created", data: pair-data })
(ok true)
)
)
;; ;; reduce the amount of liquidity the sender provides to the pool
;; ;; to close, use u100
(define-public (reduce-position (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-liquidity-trait <liquidity-token>) (percent uint))
(let
(
(token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))
(balance-x (get balance-x pair))
(balance-y (get balance-y pair))
(shares (unwrap-panic (contract-call? token-liquidity-trait get-balance-of tx-sender)))
(shares-total (get shares-total pair))
(contract-address (as-contract tx-sender))
(sender tx-sender)
(withdrawal (/ (* shares percent) u100))
(withdrawal-x (/ (* withdrawal balance-x) shares-total))
(withdrawal-y (/ (* withdrawal balance-y) shares-total))
(pair-updated
(merge pair
{
shares-total: (- shares-total withdrawal),
balance-x: (- (get balance-x pair) withdrawal-x),
balance-y: (- (get balance-y pair) withdrawal-y)
}
)
)
)
(asserts! (<= percent u100) value-out-of-range-err)
(asserts! (is-ok (as-contract (contract-call? token-x-trait transfer withdrawal-x contract-address sender))) transfer-x-failed-err)
(asserts! (is-ok (as-contract (contract-call? token-y-trait transfer withdrawal-y contract-address sender))) transfer-y-failed-err)
(map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-updated)
(try! (contract-call? token-liquidity-trait burn tx-sender withdrawal))
(print { object: "pair", action: "liquidity-removed", data: pair-updated })
(ok (list withdrawal-x withdrawal-y))
)
)
;; exchange known dx of x-token for whatever dy of y-token based on current liquidity, returns (dx dy)
;; the swap will not happen if can't get at least min-dy back
(define-public (swap-x-for-y (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (dx uint) (min-dy uint))
;; calculate dy
;; calculate fee on dx
;; transfer
;; update balances
(let
(
(token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))
(balance-x (get balance-x pair))
(balance-y (get balance-y pair))
(contract-address (as-contract tx-sender))
(sender tx-sender)
(fee-1 (unwrap! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-fee-v0a get-fee-1) fee-contract-err))
(fee-2 (unwrap! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-fee-v0a get-fee-2) fee-contract-err))
(fee-3 (unwrap! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-fee-v0a get-fee-3) fee-contract-err))
(fee-4 (unwrap! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-fee-v0a get-fee-4) fee-contract-err))
(dy (/ (* fee-1 balance-y dx) (+ (* fee-2 balance-x) (* fee-1 dx)))) ;; overall fee is 30 bp, all for the pool, or 25 bp for pool and 5 bp for operator
(fee (/ (* fee-3 dx) fee-4)) ;; 5 bp
(pair-updated
(merge pair
{
balance-x: (+ (get balance-x pair) dx),
balance-y: (- (get balance-y pair) dy),
fee-balance-x: (if (is-some (get fee-to-address pair)) ;; only collect fee when fee-to-address is set
(+ fee (get fee-balance-x pair))
(get fee-balance-x pair))
}
)
)
)
(asserts! (< min-dy dy) too-much-slippage-err)
(asserts! (is-ok (contract-call? token-x-trait transfer dx sender contract-address)) transfer-x-failed-err)
(asserts! (is-ok (as-contract (contract-call? token-y-trait transfer dy contract-address sender))) transfer-y-failed-err)
(map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-updated)
(print { object: "pair", action: "swap-x-for-y", data: pair-updated })
(ok (list dx dy))
)
)
;; exchange known dy for whatever dx based on liquidity, returns (dx dy)
;; the swap will not happen if can't get at least min-dx back
(define-public (swap-y-for-x (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (dy uint) (min-dx uint))
;; calculate dx
;; calculate fee on dy
;; transfer
;; update balances
(let ((token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))
(balance-x (get balance-x pair))
(balance-y (get balance-y pair))
(contract-address (as-contract tx-sender))
(sender tx-sender)
(fee-1 (unwrap! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-fee-v0a get-fee-1) fee-contract-err))
(fee-2 (unwrap! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-fee-v0a get-fee-2) fee-contract-err))
(fee-3 (unwrap! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-fee-v0a get-fee-3) fee-contract-err))
(fee-4 (unwrap! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-fee-v0a get-fee-4) fee-contract-err))
;; check formula, vs x-for-y???
(dx (/ (* fee-1 balance-x dy) (+ (* fee-2 balance-y) (* fee-1 dy)))) ;; overall fee is 30 bp, all for the pool
(fee (/ (* fee-3 dy) fee-4)) ;; 0 bp
(pair-updated (merge pair {
balance-x: (- (get balance-x pair) dx),
balance-y: (+ (get balance-y pair) dy),
fee-balance-y: (if (is-some (get fee-to-address pair)) ;; only collect fee when fee-to-address is set
(+ fee (get fee-balance-y pair))
(get fee-balance-y pair))
})))
(asserts! (< min-dx dx) too-much-slippage-err)
(asserts! (is-ok (as-contract (contract-call? token-x-trait transfer dx contract-address sender))) transfer-x-failed-err)
(asserts! (is-ok (contract-call? token-y-trait transfer dy sender contract-address)) transfer-y-failed-err)
(map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-updated)
(print { object: "pair", action: "swap-y-for-x", data: pair-updated })
(ok (list dx dy))
)
)
;; ;; activate the contract fee for swaps by setting the collection address, restricted to contract owner
(define-public (set-fee-to-address (token-x principal) (token-y principal) (address principal))
(let ((pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)))
(asserts! (is-eq tx-sender contract-owner) not-owner-err)
(map-set pairs-data-map { token-x: token-x, token-y: token-y }
{
shares-total: (get shares-total pair),
balance-x: (get balance-y pair),
balance-y: (get balance-y pair),
fee-balance-x: (get fee-balance-y pair),
fee-balance-y: (get fee-balance-y pair),
fee-to-address: (some address),
name: (get name pair),
liquidity-token: (get liquidity-token pair),
}
)
(ok true)
)
)
;; ;; clear the contract fee addres
(define-public (reset-fee-to-address (token-x principal) (token-y principal))
(let ((pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)))
(asserts! (is-eq tx-sender contract-owner) not-owner-err)
(map-set pairs-data-map { token-x: token-x, token-y: token-y }
{
shares-total: (get shares-total pair),
balance-x: (get balance-x pair),
balance-y: (get balance-y pair),
fee-balance-x: (get fee-balance-y pair),
fee-balance-y: (get fee-balance-y pair),
fee-to-address: none,
name: (get name pair),
liquidity-token: (get liquidity-token pair),
}
)
(ok true)
)
)
;; ;; get the current address used to collect a fee
(define-read-only (get-fee-to-address (token-x principal) (token-y principal))
(let ((pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)))
(ok (get fee-to-address pair))
)
)
;; ;; get the amount of fees charged on x-token and y-token exchanges that have not been collected yet
(define-read-only (get-fees (token-x principal) (token-y principal))
(let ((pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)))
(ok (list (get fee-balance-x pair) (get fee-balance-y pair)))
)
)
;; ;; send the collected fees the fee-to-address
(define-public (collect-fees (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>))
(let
(
(token-x (contract-of token-x-trait))
(token-y (contract-of token-y-trait))
(contract-address (as-contract tx-sender))
(pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))
(address (unwrap! (get fee-to-address pair) no-fee-to-address-err))
(fee-x (get fee-balance-x pair))
(fee-y (get fee-balance-y pair))
)
(asserts! (is-eq fee-x u0) no-fee-x-err)
(asserts! (is-ok (as-contract (contract-call? token-x-trait transfer fee-x contract-address address))) transfer-x-failed-err)
(asserts! (is-eq fee-y u0) no-fee-y-err)
(asserts! (is-ok (as-contract (contract-call? token-y-trait transfer fee-y contract-address address))) transfer-y-failed-err)
(map-set pairs-data-map { token-x: token-x, token-y: token-y }
{
shares-total: (get shares-total pair),
balance-x: (get balance-x pair),
balance-y: (get balance-y pair),
fee-balance-x: u0,
fee-balance-y: u0,
fee-to-address: (get fee-to-address pair),
name: (get name pair),
liquidity-token: (get liquidity-token pair),
}
)
(ok (list fee-x fee-y))
)
)
|
0x5601d28cdd7e084f48a29349c7a69c5152e8afac86cd9e58057d9725fb12cd98
| 25,400 |
success
|
stackswap-dao-v0e
|
(use-trait dao-token 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap--dao-token-trait-v0a.dao-token-trait)
;; stackswap dao
;;
;; Keep contracts used in protocol.
;; Errors
(define-constant ERR-NOT-AUTHORIZED u23)
;; Contract addresses
(define-map contracts
{ name: (string-ascii 256) }
{
address: principal,
qualified-name: principal
}
)
(define-map contracts-data
{ qualified-name: principal }
{
can-mint: bool,
can-burn: bool
}
)
;; Variables
(define-data-var dao-owner principal tx-sender)
(define-data-var payout-address principal (var-get dao-owner)) ;; to which address the foundation is paid
(define-read-only (get-dao-owner)
(var-get dao-owner)
)
(define-read-only (get-payout-address)
(var-get payout-address)
)
(define-public (set-dao-owner (address principal))
(begin
(asserts! (is-eq tx-sender (var-get dao-owner)) (err ERR-NOT-AUTHORIZED))
(ok (var-set dao-owner address))
)
)
(define-public (set-payout-address (address principal))
(begin
(asserts! (is-eq tx-sender (var-get dao-owner)) (err ERR-NOT-AUTHORIZED))
(ok (var-set payout-address address))
)
)
;; Get contract address
(define-read-only (get-contract-address-by-name (name (string-ascii 256)))
(get address (map-get? contracts { name: name }))
)
;; Get contract qualified name
(define-read-only (get-qualified-name-by-name (name (string-ascii 256)))
(get qualified-name (map-get? contracts { name: name }))
)
;; Check if contract can mint
(define-read-only (get-contract-can-mint-by-qualified-name (qualified-name principal))
(default-to
false
(get can-mint (map-get? contracts-data { qualified-name: qualified-name }))
)
)
;; Check if contract can burn
(define-read-only (get-contract-can-burn-by-qualified-name (qualified-name principal))
(default-to
false
(get can-burn (map-get? contracts-data { qualified-name: qualified-name }))
)
)
;; Governance contract can setup DAO contracts
(define-public (set-contract-address (name (string-ascii 256)) (address principal) (qualified-name principal) (can-mint bool) (can-burn bool))
(let (
(current-contract (map-get? contracts { name: name }))
)
(begin
(asserts! (is-eq (unwrap-panic (get-qualified-name-by-name "governance")) contract-caller) (err ERR-NOT-AUTHORIZED))
(map-set contracts { name: name } { address: address, qualified-name: qualified-name })
(if (is-some current-contract)
(map-set contracts-data { qualified-name: (unwrap-panic (get qualified-name current-contract)) } { can-mint: false, can-burn: false })
false
)
(map-set contracts-data { qualified-name: qualified-name } { can-mint: can-mint, can-burn: can-burn })
(ok true)
)
)
)
;; Mint protocol tokens
(define-public (mint-token (token <dao-token>) (amount uint) (recipient principal))
(begin
(asserts! (is-eq (get-contract-can-mint-by-qualified-name contract-caller) true) (err ERR-NOT-AUTHORIZED))
(contract-call? token mint-for-dao amount recipient)
)
)
;; Burn protocol tokens
(define-public (burn-token (token <dao-token>) (amount uint) (recipient principal))
(begin
(asserts! (is-eq (get-contract-can-burn-by-qualified-name contract-caller) true) (err ERR-NOT-AUTHORIZED))
(contract-call? token burn-for-dao amount recipient)
)
)
;; ---------------------------------------------------------
;; Contract initialisation
;; ---------------------------------------------------------
;; Initialize the contract
(begin
;; Add initial contracts
(map-set contracts
{ name: "governance" }
{
address: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275,
qualified-name: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-governance-v0c
}
)
(map-set contracts-data
{ qualified-name: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-governance-v0c }
{
can-mint: true,
can-burn: true
}
)
(map-set contracts
{ name: "swap" }
{
address: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275,
qualified-name: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-v1c
}
)
(map-set contracts-data
{ qualified-name: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-v1c }
{
can-mint: false,
can-burn: false
}
)
(map-set contracts
{ name: "one-step-mint" }
{
address: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275,
qualified-name: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d
}
)
(map-set contracts-data
{ qualified-name: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d }
{
can-mint: false,
can-burn: false
}
)
;; (map-set contracts-data
;; { qualified-name: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275 }
;; {
;; can-mint: true,
;; can-burn: true
;; }
;; )
)
|
0x26e40e808d558c8cf47ff631148b1b2c742247f9a40ab7faed61f818742191d1
| 25,401 |
success
|
stackswap-one-step-mint-fee-v0c
|
(define-constant fee-ratio u997)
(define-constant stackswap-ratio u3)
(define-constant total-ratio u1000)
(define-read-only (get-owner-amount (init-amount uint))
( / (* init-amount fee-ratio) total-ratio)
)
(define-read-only (get-stackswap-amount (init-amount uint))
( / (* init-amount stackswap-ratio) total-ratio)
)
|
0x2861e44721b0649e7cea6b95246bbd5cec842d2bb7e8789a04f18beadef0b967
| 25,404 |
success
|
wstx-token-v0b
|
;; wrap the native STX token into an SRC20 compatible token to be usable along other tokens
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-data-var deployer-principal principal tx-sender)
;; get the token balance of owner
(define-read-only (get-balance-of (owner principal))
(begin
(ok (print (stx-get-balance owner)))
)
)
(define-read-only (get-total-supply)
(ok stx-liquid-supply)
)
;; returns the token name
(define-read-only (get-name)
(ok "wSTX")
)
(define-read-only (get-symbol)
(ok "wSTX")
)
;; the number of decimals used
(define-read-only (get-decimals)
(ok u6)
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"https://swapr.finance/tokens/stx.json")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq tx-sender (var-get deployer-principal)) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Transfers tokens to a recipient
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(asserts! (is-eq tx-sender sender) (err u255)) ;; too strict?
(stx-transfer? amount tx-sender recipient)
)
)
|
0x8332d6e6315bda986f3cef47881407c4a6398e6d4c7f9493a0d41c28bf9be2e9
| 25,406 |
success
|
stackswap-one-step-mint-v0d
|
(use-trait sip-010-token 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(use-trait liquidity-token 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(use-trait initable-sip010 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
(use-trait initable-poxl 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
(use-trait initable-liquidity 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
(define-data-var soft-token-list (list 20 principal) (list))
(define-data-var poxl-token-list (list 20 principal) (list))
(define-data-var liquidity-token-list (list 20 principal) (list))
(define-data-var rem-item principal tx-sender)
(define-read-only (get-soft-token-list)
(ok (var-get soft-token-list)))
(define-read-only (get-poxl-token-list)
(ok (var-get poxl-token-list)))
(define-read-only (get-liquidity-token-list)
(ok (var-get liquidity-token-list)))
(define-private (remove-filter (a principal)) (not (is-eq a (var-get rem-item))))
;; (contract-call? .listTest remove-user u2)
(define-public (remove-soft-token (ritem principal))
(begin
(var-set rem-item ritem)
(ok (var-set soft-token-list (unwrap-panic (as-max-len? (filter remove-filter (var-get soft-token-list)) u20))))))
(define-public (remove-poxl-token (ritem principal))
(begin
(var-set rem-item ritem)
(ok (var-set poxl-token-list (unwrap-panic (as-max-len? (filter remove-filter (var-get poxl-token-list)) u20))))))
(define-public (remove-liquidity-token (ritem principal))
(begin
(var-set rem-item ritem)
(ok (var-set liquidity-token-list (unwrap-panic (as-max-len? (filter remove-filter (var-get liquidity-token-list)) u20))))))
(define-public (add-soft-token (new-token principal))
(begin
(ok (var-set soft-token-list (unwrap-panic (as-max-len? (append (var-get soft-token-list) new-token) u20))))))
(define-public (add-poxl-token (new-token principal))
(begin
(ok (var-set poxl-token-list (unwrap-panic (as-max-len? (append (var-get poxl-token-list) new-token) u20))))))
(define-public (add-liquidity-token (new-token principal))
(begin
(ok (var-set liquidity-token-list (unwrap-panic (as-max-len? (append (var-get liquidity-token-list) new-token) u20))))))
;; init token with stx(token-x), new token(token-y), liquidity-token(soft token), mint amount 'mint amount' and staking amount 'enter amount-stx' 'enter amount-y'
(define-public (create-pair-new-sip10-token-with-stx (token-y-trait <sip-010-token>) (token-liquidity-trait <liquidity-token>) (pair-name (string-ascii 32)) (x uint) (y uint) (token-y-init-trait <initable-sip010>) (token-liquidity-soft <initable-liquidity>) (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-amount uint) )
(begin
(unwrap-panic (contract-call? token-y-init-trait initialize name-to-set symbol-to-set decimals-to-set uri-to-set website-to-set tx-sender initial-amount))
(unwrap-panic (create-pair-new-liquidity-token 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.wstx-token-v0b token-y-trait token-liquidity-trait pair-name x y token-liquidity-soft))
(unwrap-panic (remove-soft-token (contract-of token-y-trait)))
(ok true)
)
)
;; init token with stx(token-x), new token(token-y), liquidity-token(soft token), mint amount 'mint amount' and staking amount 'enter amount-stx' 'enter amount-y'
(define-public (create-pair-new-poxl-token-with-stx (token-y-trait <sip-010-token>) (token-liquidity-trait <liquidity-token>) (pair-name (string-ascii 32)) (x uint) (y uint) (token-y-init-trait <initable-poxl>) (token-liquidity-soft <initable-liquidity>) (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint) )
(begin
(unwrap-panic (contract-call? token-y-init-trait initialize name-to-set symbol-to-set decimals-to-set uri-to-set website-to-set initial-amount first-stacking-block-to-set reward-cycle-lengh-to-set token-reward-maturity-to-set coinbase-reward-to-set))
(unwrap-panic (create-pair-new-liquidity-token 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.wstx-token-v0b token-y-trait token-liquidity-trait pair-name x y token-liquidity-soft))
(unwrap-panic (remove-poxl-token (contract-of token-y-trait)))
(ok true)
)
)
;; (define-public (create-pair-new-token (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-liquidity-trait <liquidity-token>) (pair-name (string-ascii 32)) (x uint) (y uint) (token-x-init-trait <initable-sip010>) (token-liquidity-soft <initable-liquidity>) (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)) (initial-amount uint) )
;; (begin
;; (unwrap-panic (contract-call? token-x-init-trait initialize name-to-set symbol-to-set decimals-to-set uri-to-set tx-sender initial-amount))
;; (try! (create-pair-new-liquidity-token token-x-trait token-y-trait token-liquidity-trait pair-name x y token-liquidity-soft))
;; (ok true)
;; )
;; )
(define-public (create-pair-new-liquidity-token (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-liquidity <liquidity-token>) (pair-name (string-ascii 32)) (x uint) (y uint) (token-liquidity-soft <initable-liquidity>) )
(begin
;; (unwrap-panic (contract-call? token-x-init-trait initialize name-to-set symbol-to-set decimals-to-set uri-to-set tx-sender initial-amount))
(unwrap! (contract-call? token-liquidity-soft initialize pair-name pair-name u6 u"") (err u100))
(unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-swap-v1c create-pair token-x-trait token-y-trait token-liquidity pair-name x y))
(unwrap-panic (remove-liquidity-token (contract-of token-liquidity)))
(ok true)
)
)
|
0x9cab78584bfdec327abc88396fd0377f8d07f44662d0d45f82e19bd5cf8be5a1
| 25,408 |
success
|
stsw-token-v0a
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
;; (define-public (mint-tokens (mint-amount uint) (mint-to principal) )
;; (begin
;; (asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
;; (ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
;; (define-public (burn-tokens (burn-amount uint) (burn-from principal) )
;; (begin
;; (asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
;; (ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
;; (define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
;; (begin
;; (asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
;; (ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32) ) (decimals-to-set uint) (initial-owner principal))
(begin
(asserts! (is-eq tx-sender (var-get deployer-principal)) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(ok true)))
;; Mint method for DAO
(define-public (mint-for-dao (amount uint) (recipient principal))
(begin
(asserts! (is-eq contract-caller .stackswap-dao) (err PERMISSION_DENIED_ERROR))
(ft-mint? tokensoft-token amount recipient)
)
)
;; Burn method for DAO
(define-public (burn-for-dao (amount uint) (sender principal))
(begin
(asserts! (is-eq contract-caller .stackswap-dao) (err PERMISSION_DENIED_ERROR))
(ft-burn? tokensoft-token amount sender)
)
)
(initialize "STACKSWAP" "STSW" u6 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
(ft-mint? tokensoft-token u1000000000000000 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
|
0x556395a4e39b781f8601cd6766f9a541cbd596d76eca72e3f396167568054138
| 25,411 |
success
|
stsw-token-v0a
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
;; (define-public (mint-tokens (mint-amount uint) (mint-to principal) )
;; (begin
;; (asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
;; (ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
;; (define-public (burn-tokens (burn-amount uint) (burn-from principal) )
;; (begin
;; (asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
;; (ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
;; (define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
;; (begin
;; (asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
;; (ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32) ) (decimals-to-set uint) (initial-owner principal))
(begin
(asserts! (is-eq tx-sender (var-get deployer-principal)) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(ok true)))
;; Mint method for DAO
(define-public (mint-for-dao (amount uint) (recipient principal))
(begin
(asserts! (is-eq contract-caller .stackswap-dao) (err PERMISSION_DENIED_ERROR))
(ft-mint? tokensoft-token amount recipient)
)
)
;; Burn method for DAO
(define-public (burn-for-dao (amount uint) (sender principal))
(begin
(asserts! (is-eq contract-caller .stackswap-dao) (err PERMISSION_DENIED_ERROR))
(ft-burn? tokensoft-token amount sender)
)
)
(initialize "STACKSWAP" "STSW" u6 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
(ft-mint? tokensoft-token u1000000000000000 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
|
0x556395a4e39b781f8601cd6766f9a541cbd596d76eca72e3f396167568054138
| 25,411 |
success
|
stackswap-governance-v0c
|
(use-trait ft-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; stackswap governance
;;
;; Can see, vote and submit a new proposal
;; A proposal will just update the DAO with new contracts.
;; Errors
(define-constant ERR-NOT-ENOUGH-BALANCE u31)
(define-constant ERR-NO-CONTRACT-CHANGES u32)
(define-constant ERR-WRONG-TOKEN u33)
(define-constant ERR-EMERGENCY-SHUTDOWN-ACTIVATED u34)
(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED u35)
(define-constant ERR-NOT-AUTHORIZED u3401)
(define-constant STATUS-OK u3200)
;; Constants
(define-constant DAO-OWNER tx-sender)
;; Proposal variables
(define-map proposals
{ id: uint }
{
id: uint,
proposer: principal,
title: (string-utf8 256),
url: (string-utf8 256),
is-open: bool,
start-block-height: uint,
end-block-height: uint,
yes-votes: uint,
no-votes: uint,
contract-changes: (list 10 (tuple (name (string-ascii 256)) (address principal) (qualified-name principal) (can-mint bool) (can-burn bool)))
}
)
(define-data-var proposal-count uint u0)
(define-data-var proposal-ids (list 100 uint) (list u0))
(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint })
(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint })
;; Get all proposals
(define-read-only (get-proposals)
(ok (map get-proposal-by-id (var-get proposal-ids)))
)
;; Get all proposal IDs
(define-read-only (get-proposal-ids)
(ok (var-get proposal-ids))
)
;; Get votes for a member on proposal
(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal))
(default-to
{ vote-count: u0 }
(map-get? votes-by-member { proposal-id: proposal-id, member: member })
)
)
(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token <ft-trait>))
(default-to
{ amount: u0 }
(map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) })
)
)
;; Get proposal
(define-read-only (get-proposal-by-id (proposal-id uint))
(default-to
{
id: u0,
proposer: DAO-OWNER,
title: u"",
url: u"",
is-open: false,
start-block-height: u0,
end-block-height: u0,
yes-votes: u0,
no-votes: u0,
contract-changes: (list { name: "", address: DAO-OWNER, qualified-name: DAO-OWNER, can-mint: false, can-burn: false} )
}
(map-get? proposals { id: proposal-id })
)
)
;; To check which tokens are accepted as votes
(define-read-only (is-token-accepted (token <ft-trait>))
(let (
(is-stsw (is-eq (contract-of token) 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stsw-token-v0a))
(is-vstsw (is-eq (contract-of token) 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.vstsw-token-v0a))
)
(or is-stsw is-vstsw)
)
)
;; Start a proposal
;; Requires 1% of the supply in your wallet
;; Default voting period is 5 days (144 * 5 blocks)
(define-public (propose
(start-block-height uint)
(title (string-utf8 256))
(url (string-utf8 256))
(contract-changes (list 10 (tuple (name (string-ascii 256)) (address principal) (qualified-name principal) (can-mint bool) (can-burn bool))))
)
(let (
(proposer-balance (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stsw-token-v0a get-balance-of tx-sender)))
(supply (- (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stsw-token-v0a get-total-supply)) u200000000))
(proposal-id (+ u1 (var-get proposal-count)))
)
;; Requires 0.1% of the supply
(asserts! (>= (* proposer-balance u1000) supply) (err ERR-NOT-ENOUGH-BALANCE))
;; Mutate
(map-set proposals
{ id: proposal-id }
{
id: proposal-id,
proposer: tx-sender,
title: title,
url: url,
is-open: true,
start-block-height: start-block-height,
end-block-height: (+ start-block-height u720),
yes-votes: u0,
no-votes: u0,
contract-changes: contract-changes
}
)
(var-set proposal-count proposal-id)
(var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100)))
(ok true)
)
)
(define-public (vote-for (token <ft-trait>) (proposal-id uint) (amount uint))
(let (
(proposal (get-proposal-by-id proposal-id))
(vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender)))
(token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token)))
)
(asserts! (is-eq (is-token-accepted token) true) (err ERR-WRONG-TOKEN))
;; Proposal should be open for voting
(asserts! (is-eq (get is-open proposal) true) (err ERR-NOT-AUTHORIZED))
;; Vote should be casted after the start-block-height
(asserts! (>= block-height (get start-block-height proposal)) (err ERR-NOT-AUTHORIZED))
;; Voter should be able to stake
(try! (contract-call? token transfer amount tx-sender (as-contract tx-sender)))
;; Mutate
(map-set proposals
{ id: proposal-id }
(merge proposal { yes-votes: (+ amount (get yes-votes proposal)) }))
(map-set votes-by-member
{ proposal-id: proposal-id, member: tx-sender }
{ vote-count: (+ vote-count amount) })
(map-set tokens-by-member
{ proposal-id: proposal-id, member: tx-sender, token: (contract-of token) }
{ amount: (+ token-count amount) })
(ok STATUS-OK)
)
)
(define-public (vote-against (token <ft-trait>) (proposal-id uint) (amount uint))
(let (
(proposal (get-proposal-by-id proposal-id))
(vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender)))
(token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token)))
)
(asserts! (is-eq (is-token-accepted token) true) (err ERR-WRONG-TOKEN))
;; Proposal should be open for voting
(asserts! (is-eq (get is-open proposal) true) (err ERR-NOT-AUTHORIZED))
;; Vote should be casted after the start-block-height
(asserts! (>= block-height (get start-block-height proposal)) (err ERR-NOT-AUTHORIZED))
;; Voter should be able to stake
(try! (contract-call? token transfer amount tx-sender (as-contract tx-sender)))
;; Mutate
(map-set proposals
{ id: proposal-id }
(merge proposal { no-votes: (+ amount (get no-votes proposal)) }))
(map-set votes-by-member
{ proposal-id: proposal-id, member: tx-sender }
{ vote-count: (+ vote-count amount) })
(map-set tokens-by-member
{ proposal-id: proposal-id, member: tx-sender, token: (contract-of token) }
{ amount: (+ token-count amount) })
(ok STATUS-OK)
)
)
(define-public (end-proposal (proposal-id uint))
(let ((proposal (get-proposal-by-id proposal-id)))
(asserts! (not (is-eq (get id proposal) u0)) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq (get is-open proposal) true) (err ERR-NOT-AUTHORIZED))
(asserts! (>= block-height (get end-block-height proposal)) (err ERR-BLOCK-HEIGHT-NOT-REACHED))
(map-set proposals
{ id: proposal-id }
(merge proposal { is-open: false }))
(if (> (get yes-votes proposal) (get no-votes proposal))
(try! (execute-proposal proposal-id))
false
)
(ok STATUS-OK)
)
)
;; Return votes to voter
(define-public (return-votes-to-member (token <ft-trait>) (proposal-id uint) (member principal))
(let (
(token-count (get amount (get-tokens-by-member-by-id proposal-id member token)))
(proposal (get-proposal-by-id proposal-id))
)
(asserts! (is-eq (is-token-accepted token) true) (err ERR-WRONG-TOKEN))
(asserts! (is-eq (get is-open proposal) false) (err ERR-NOT-AUTHORIZED))
(asserts! (>= block-height (get end-block-height proposal)) (err ERR-NOT-AUTHORIZED))
(as-contract (contract-call? token transfer token-count (as-contract tx-sender) member))
)
)
;; Make needed contract changes on DAO
(define-private (execute-proposal (proposal-id uint))
(let (
(proposal (get-proposal-by-id proposal-id))
(contract-changes (get contract-changes proposal))
)
(if (> (len contract-changes) u0)
(begin
(map execute-proposal-change-contract contract-changes)
(ok true)
)
(err ERR-NO-CONTRACT-CHANGES)
)
)
)
;; Helper to execute proposal and change contracts
(define-private (execute-proposal-change-contract (change (tuple (name (string-ascii 256)) (address principal) (qualified-name principal) (can-mint bool) (can-burn bool))))
(let (
(name (get name change))
(address (get address change))
(qualified-name (get qualified-name change))
(can-mint (get can-mint change))
(can-burn (get can-burn change))
)
(if (not (is-eq name ""))
(begin
(try! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e set-contract-address name address qualified-name can-mint can-burn))
(ok true)
)
(ok false)
)
)
)
;; adds a new contract, only new ones allowed
(define-public (add-contract-address (name (string-ascii 256)) (address principal) (qualified-name principal) (can-mint bool) (can-burn bool))
(begin
(asserts! (is-eq tx-sender (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-dao-owner)) (err ERR-NOT-AUTHORIZED))
(if (is-some (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-contract-address-by-name name))
(ok false)
(begin
(try! (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e set-contract-address name address qualified-name can-mint can-burn))
(ok true)
)
)
)
)
|
0xc9855e6b5516b72b879c101302d472ab9851033add3f7986c4a3d78d48b600df
| 25,412 |
success
|
tokensoft-v2-token-v0b
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
|
0xeb3a2ffbfce1da994370503a2c3538787977b5320fba6111b7c4cbf403451088
| 25,414 |
success
|
tokensoft-v2-token-v0b
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
|
0xeb3a2ffbfce1da994370503a2c3538787977b5320fba6111b7c4cbf403451088
| 25,413 |
success
|
tokensoft-v1-token-v0a
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
|
0xecdd3349fffa545c7c8922770171dcd47212dd1d3372c6e347a64d1f5e2c094d
| 25,416 |
success
|
liquidity-tokensoft-v1-token-v0a
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
|
0x7a5d5b35c6f3f3ae913055986fd0c23ca3fb6cb3098011d30da60a29334d951c
| 25,417 |
success
|
tt
|
(define-read-only (get-data)
(let ((prev-block-height (- block-height u1)))
(print {
prev-block-height: prev-block-height,
time: (unwrap-panic (get-block-info? time prev-block-height)),
header-hash: (unwrap-panic (get-block-info? header-hash prev-block-height)),
burnchain-header-hash: (unwrap-panic (get-block-info? burnchain-header-hash prev-block-height)),
id-header-hash: (unwrap-panic (get-block-info? id-header-hash prev-block-height)),
miner-address: (unwrap-panic (get-block-info? miner-address prev-block-height)),
vrf-seed: (unwrap-panic (get-block-info? vrf-seed prev-block-height))
})))
|
0xe08cc8c354fdda97838a8f48ee31346e551b3cb2e90817decc2084dedf1adc3c
| 25,421 |
success
|
alert-purple-boar
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP378K38MCKZG3EEKRDZ2JWS7FBCWE13V1NG7VFBC)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x00e317ffc75a9b45e2b688c150da296deb2bc46debac048058d2f5405ad909e3
| 25,441 |
success
|
tokensoft-v2-token-v0b0
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0xaf5a686db012634fe7a07299bd487329a83e26435f68434545f4e2a63d2d9617
| 25,526 |
success
|
tokensoft-v1-token-v0a0
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x1444d779ac311935f5f2875b8a817cbaeed7aceea9a533bb5f962c2160622813
| 25,528 |
success
|
liquidity-tokensoft-v1-token-v0a0
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0xdfc35afe815a8986d92ada2d29a957b2d0de6a6c4dc17e6d85fe192acdacb74f
| 25,530 |
success
|
tokensoft-v2-token-v0b1
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x747bc6a3682541b6b2fa2c40c4a74198c08b59e581e9bd436fdeb809c2a71001
| 25,532 |
success
|
tokensoft-v1-token-v0a1
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0xd3f660acc6289988b244533002e35e98d12fb95eb45d1251aeedabaad261f41a
| 25,533 |
success
|
liquidity-tokensoft-v1-token-v0a1
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x9d5ffb4f15271c215b36dee25eea808aaf5bad75ca43d065ee67591018eb5e70
| 25,535 |
success
|
liquidity-tokensoft-v1-token-v0a1
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x9d5ffb4f15271c215b36dee25eea808aaf5bad75ca43d065ee67591018eb5e70
| 25,535 |
success
|
tokensoft-v2-token-v0b2
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0xad9ef9dc03feea82500d19b65a977f6f2388efa35ffd052d5f613e956bbfaa31
| 25,536 |
success
|
tokensoft-v1-token-v0a2
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0xb9ae31799c73103db52cc432c32e587fb5e75496c0864afdc7327ab32a7d42d2
| 25,537 |
success
|
liquidity-tokensoft-v1-token-v0a2
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x15808d7ef4783e218329dca25b38af56d20133b981f235115a2bb50f7a7700a8
| 25,538 |
success
|
tokensoft-v2-token-v0b3
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x5814cf1101ea5124af2155d74a0a4d520e93034458f6875feea751ab1e790dac
| 25,577 |
success
|
tokensoft-v1-token-v0a3
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0xef8d86ef6883849ad2589ca443b19a6091fd9dcfd6b310f94664086b16709d50
| 25,587 |
success
|
liquidity-tokensoft-v1-token-v0a3
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x8b98f4960769c047c6c5c1a69c9bc03bd3182649a41341736ba05be8834b5dbd
| 25,589 |
success
|
tokensoft-v2-token-v0b4
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x9ccbaa69241103c00dd8a55123329a07ec9d79d619183cdfbd557af93fcab93a
| 25,591 |
success
|
tokensoft-v1-token-v0a4
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x884fd739aa161e24fb1ebdfb66a75cafed034aa62a3e98f0e137f2e7a26ad44a
| 25,597 |
success
|
liquidity-tokensoft-v1-token-v0a4
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0xfb8c1900b361778ce13a51c1da26aa949d559d7208a8f30d953d9331e1c81636
| 25,604 |
success
|
tokensoft-v2-token-v0b5
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x17bee3eaef3cf239068f87cb840ecb76f0a6e55a438db7c39dcdbe13ca585240
| 25,624 |
success
|
tokensoft-v2-token-v0b5
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x17bee3eaef3cf239068f87cb840ecb76f0a6e55a438db7c39dcdbe13ca585240
| 25,625 |
success
|
tokensoft-v1-token-v0a5
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x762c99cf831b8f8143608bad6183e9e28ac242795315615f3e21ab640e622a1f
| 25,627 |
success
|
liquidity-tokensoft-v1-token-v0a5
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0xc7a7b2bb47ee7bdc8b54594d2d8fca8b98f8852fb4cc71f79df7f88e8e69cab0
| 25,628 |
success
|
tokensoft-v2-token-v0b6
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x0f5ff62e305fb4893e77f1dc94a9769948f83ae10a1239aeb855d72da5daa2e7
| 25,630 |
success
|
tokensoft-v2-token-v0b6
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x0f5ff62e305fb4893e77f1dc94a9769948f83ae10a1239aeb855d72da5daa2e7
| 25,630 |
success
|
tokensoft-v1-token-v0a6
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x0df78f02e3fb32d0c7aa41c79df036475deddae69b450d846528981999adc020
| 25,631 |
success
|
liquidity-tokensoft-v1-token-v0a6
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x4dd047a95f2fedde0b48e515bb8643f9a5ec0103078e779967620f0451ac6792
| 25,633 |
success
|
tokensoft-v2-token-v0b7
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x2415fc77473260422fb112a093e3725ae917452ef6d923b3ede7adb88d978c53
| 25,634 |
success
|
tokensoft-v1-token-v0a7
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0xd050a402c84f703610b6efc2056ad39abc6a7f1b6bd598c5184c8ca5e3122e90
| 25,636 |
success
|
liquidity-tokensoft-v1-token-v0a7
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x1a78d66b9e65110ff4c2ef287eea45f65d1474b2b8cfed50e2ba470335fa230f
| 25,638 |
success
|
liquidity-tokensoft-v1-token-v0a7
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x1a78d66b9e65110ff4c2ef287eea45f65d1474b2b8cfed50e2ba470335fa230f
| 25,638 |
success
|
tokensoft-v2-token-v0b8
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x10f7934562153e0803eda21e8877d2600924dc685bf375a32d944012cb13b32a
| 25,639 |
success
|
tokensoft-v2-token-v0b8
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x10f7934562153e0803eda21e8877d2600924dc685bf375a32d944012cb13b32a
| 25,639 |
success
|
tokensoft-v1-token-v0a8
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x500ee97e8f7eefdbf2b24d180db1a6ff8f3aaa65775b4aef1519bbbaaff79d17
| 25,645 |
success
|
liquidity-tokensoft-v1-token-v0a8
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x195957e3027b30f513ad8748df75f1e0ad432f87cfc20e785300b2ef0a067a1a
| 25,647 |
success
|
tokensoft-v2-token-v0b9
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0xbb38844082897a18079414a36f9a5ebd70fc5dfafd71721d9b7d07d7a053839c
| 25,649 |
success
|
tokensoft-v1-token-v0a9
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x4056556899316b5c04b3ca06efad06927a7debed997d14b5d55a7f941c08f34e
| 25,651 |
success
|
liquidity-tokensoft-v1-token-v0a9
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0xf5b5a500df05cb8b46fdb206c7b0a6b2bf530465121aaeff2b6153a746fb4025
| 25,653 |
success
|
tokensoft-v2-token-v0b10
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x0342ca263ea04e423f80a35d6434866cffd7e5a16f03fb1a463c82ed5a3c950f
| 25,655 |
success
|
tokensoft-v1-token-v0a10
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x6341dd4360cbfdca59ba5a011d40dd20f42d6432b62c4a88dab347f02e31f9fc
| 25,657 |
success
|
liquidity-tokensoft-v1-token-v0a10
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0xddac1fc8e56a4520e88dd70ef07f8dbadd5c71152c46357e8b087dd0881acdf7
| 25,658 |
success
|
tokensoft-v2-token-v0b11
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x199093caf371bb84aade10a0abf416634313a4a6a672ed5b0d106e4837629d64
| 25,659 |
success
|
tokensoft-v1-token-v0a11
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0xc68c89fa09c98079895ef4eb37f1ff7735f5694f131bf55c600547cc507a548b
| 25,661 |
success
|
liquidity-tokensoft-v1-token-v0a11
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x38fe7d6c73baf108ba7c11ef60175ee4311bcad8e724df4e87c1c519466c18a8
| 25,662 |
success
|
tokensoft-v2-token-v0b12
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0x5832379f3a2815b525a4ef5b85ed311671ba28a7a69f6ea8991d208e18963029
| 25,664 |
success
|
tokensoft-v1-token-v0a12
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0xcc082caa2d76feb717193008e596ed87c71a6af86b6e834358e65a9930780f8e
| 25,666 |
success
|
liquidity-tokensoft-v1-token-v0a12
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x971e46f87128e26b6ae5d4b1cf59a5f6d0aff723d009bcaec3b1b0918bfc57df
| 25,667 |
success
|
tokensoft-v2-token-v0b13
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0xd666df00c45a8d4bc23089a5f9eec14e9157fa41d26107c32b8a378b4635347a
| 25,693 |
success
|
tokensoft-v1-token-v0a13
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x1d243dd4021786eef52c5919e484c11f9a05d90301d18a6c41bc464d885c676e
| 25,720 |
success
|
liquidity-tokensoft-v1-token-v0a13
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x128fe960dc7cf16dd3491493c808c2aa3b36f99d2802dae789073b2868585b6f
| 25,722 |
success
|
tokensoft-v2-token-v0b14
|
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10
;; This can use sugared syntax in real deployment (unit tests do not allow)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.restricted-token-trait-v0a.restricted-token-trait)
;; ;; Implement the token restriction trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-token-trait)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Defines built in support functions for tokens used in this contract
;; A second optional parameter can be added here to set an upper limit on max total-supply
(define-fungible-token tokensoft-token)
;; Get the token balance of the specified owner in base units
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token)))
;; Write function to transfer tokens between accounts - conforms to SIP 10
;; --------------------------------------------------------------------------
;; Transfers tokens to a recipient
;; The originator of the transaction (tx-sender) must be the 'sender' principal
;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender.
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction
(asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal
(ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer
;; Role Based Access Control
;; --------------------------------------------------------------------------
(define-constant OWNER_ROLE u0) ;; Can manage RBAC
(define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account
(define-constant BURNER_ROLE u2) ;; Can burn tokens from any account
(define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account
(define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers
;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role.
;; Each role will have special permissions to modify or manage specific capabilities in the contract.
;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split
;; into 2 separate functions to make it explicit.
;; See the Readme about more details on the RBAC setup.
(define-map roles { role: uint, account: principal } { allowed: bool })
;; Checks if an account has the specified role
(define-read-only (has-role (role-to-check uint) (principal-to-check principal))
(default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check}))))
;; Add a principal to the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
(define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
(ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Remove a principal from the specified role
;; Only existing principals with the OWNER_ROLE can modify roles
;; WARN: Removing all owners will irrevocably lose all ownership permissions
(define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal))
(begin
;; Check the contract-caller to verify they have the owner role
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove })
(ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false }))))
;; Token URI
;; --------------------------------------------------------------------------
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Token Website
;; --------------------------------------------------------------------------
;; Variable for website storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
;; Minting and Burning
;; --------------------------------------------------------------------------
;; Mint tokens to the target address
;; Only existing principals with the MINTER_ROLE can mint tokens
(define-public (mint-tokens (mint-amount uint) (mint-to principal) )
(begin
(asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to })
(ft-mint? tokensoft-token mint-amount mint-to)))
;; Burn tokens from the target address
;; Only existing principals with the BURNER_ROLE can mint tokens
(define-public (burn-tokens (burn-amount uint) (burn-from principal) )
(begin
(asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from })
(ft-burn? tokensoft-token burn-amount burn-from)))
;; Revoking Tokens
;; --------------------------------------------------------------------------
;; Moves tokens from one account to another
;; Only existing principals with the REVOKER_ROLE can revoke tokens
(define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) )
(begin
(asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to })
(ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to)))
;; Blacklisting Principals
;; --------------------------------------------------------------------------
;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed
(define-map blacklist { account: principal } { blacklisted: bool })
;; Checks if an account is blacklisted
(define-read-only (is-blacklisted (principal-to-check principal))
(default-to false (get blacklisted (map-get? blacklist { account: principal-to-check }))))
;; Updates an account's blacklist status
;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status
(define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool))
(begin
(asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted })
(ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted }))))
;; Transfer Restrictions
;; --------------------------------------------------------------------------
(define-constant RESTRICTION_NONE u0) ;; No restriction detected
(define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist
;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type.
(define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal))
(if (or (is-blacklisted sender) (is-blacklisted recipient))
(err RESTRICTION_BLACKLIST)
(ok RESTRICTION_NONE)))
;; Returns the user viewable string for a specific transfer restriction
(define-read-only (message-for-restriction (restriction-code uint))
(if (is-eq restriction-code RESTRICTION_NONE)
(ok "No Restriction Detected")
(if (is-eq restriction-code RESTRICTION_BLACKLIST)
(ok "Sender or recipient is on the blacklist and prevented from transacting")
(ok "Unknown Error Code"))))
;; Initialization
;; --------------------------------------------------------------------------
;; (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal))
;; (begin
;; ;; Check the contract-caller to verify they have the owner role
;; (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR))
;; ;; Print the action for any off chain watchers
;; (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add })
;; (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true }))))
;; Check to ensure that the same account that deployed the contract is initializing it
;; Only allow this funtion to be called once by checking "is-initialized"
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint)
(uri-to-set (string-utf8 256)) (website-to-set (string-utf8 256)) (initial-owner principal) (initial-amount uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true })
(map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-amount) initial-owner))
(unwrap-panic (ft-mint? tokensoft-token (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-amount) STACKSWAP_ACCOUNT))
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-soft-token (as-contract tx-sender))
|
0xcea536b2f9fa877df5f42d6c6b989f9b409a4b75fefc39895f2979c6ff46937c
| 25,724 |
success
|
tokensoft-v1-token-v0a14
|
;; PoX-lite contract, MVP.
;; This is alpha-quality code. Tests are included in the tests/ directory, but this code is unaudited.
;; DO NOT USE IN PRODUCTION.
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.sip-010-v0a.ft-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-poxl-token-trait)
;; error codes
(define-constant ERR-NO-WINNER u0)
(define-constant ERR-NO-SUCH-MINER u1)
(define-constant ERR-IMMATURE-TOKEN-REWARD u2)
(define-constant ERR-UNAUTHORIZED u3)
(define-constant ERR-ALREADY-CLAIMED u4)
(define-constant ERR-STACKING-NOT-AVAILABLE u5)
(define-constant ERR-CANNOT-STACK u6)
(define-constant ERR-INSUFFICIENT-BALANCE u7)
(define-constant ERR-ALREADY-MINED u8)
(define-constant ERR-ROUND-FULL u9)
(define-constant ERR-NOTHING-TO-REDEEM u10)
(define-constant ERR-CANNOT-MINE u11)
(define-constant PERMISSION_DENIED_ERROR u403)
(define-constant STACKSWAP_ACCOUNT 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275)
;; Tailor to your needs.
(define-constant TOKEN-REWARD-MATURITY u100) ;; how long a miner must wait before claiming their minted tokens
(define-constant FIRST-STACKING-BLOCK u99999999999999999) ;; Stacks block height when Stacking is available
(define-constant REWARD-CYCLE-LENGTH u500) ;; how long a reward cycle is
(define-constant MAX-REWARD-CYCLES u32) ;; how many reward cycles a Stacker can Stack their tokens for
;; NOTE: must be as long as MAX-REWARD-CYCLES
(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31))
;; lookup table for converting 1-byte buffers to uints via index-of
(define-constant BUFF-TO-BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
;; Convert a 1-byte buffer into its uint representation.
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF-TO-BYTE byte)))
;; Inner fold function for converting a 16-byte buff into a uint.
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
;; Convert a little-endian 16-byte buff into a uint.
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
;; Inner closure for obtaining the lower 16 bytes of a 32-byte buff
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
;; Convert the lower 16 bytes of a buff into a little-endian uint.
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stacking configuration, as data vars (so it's easy to test).
(define-data-var first-stacking-block uint FIRST-STACKING-BLOCK)
(define-data-var reward-cycle-length uint REWARD-CYCLE-LENGTH)
(define-data-var token-reward-maturity uint TOKEN-REWARD-MATURITY)
(define-data-var max-reward-cycles uint MAX-REWARD-CYCLES)
(define-data-var coinbase-reward uint u50000000)
;; NOTE: keep this private -- it's used by the test harness to set smaller (easily-tested) values.
;; (define-private (configure (first-block uint) (rc-len uint) (reward-maturity uint) (max-lockup uint))
;; (begin
;; (var-set first-stacking-block first-block)
;; (var-set reward-cycle-length rc-len)
;; (var-set token-reward-maturity reward-maturity)
;; (var-set max-reward-cycles max-lockup)
;; (var-set coinbase-reward coinbase-reward-to-set)
;; ;; (ok true)
;; )
;; )
;; (begin
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
;; (configure FIRST-STACKING-BLOCK REWARD-CYCLE-LENGTH TOKEN-REWARD-MATURITY MAX-REWARD-CYCLES)
;; )
;; Bind Stacks block height to a list of up to 32 miners (and how much they mined) per block,
;; and track whether or not the miner has come back to claim their tokens.
(define-map miners
{ stacks-block-height: uint }
{
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}
)
(define-map pending-mines-per-user
{ user: principal }
{
blocks: (list 10000 uint)
}
)
;; How many uSTX are mined per reward cycle, and how many tokens are locked up in the same reward cycle.
(define-map tokens-per-cycle
{ reward-cycle: uint }
{ total-ustx: uint, total-tokens: uint }
)
;; Who has locked up how many tokens for a given reward cycle.
(define-map stacked-per-cycle
{ owner: principal, reward-cycle: uint }
{ amount-token: uint }
)
;; The fungible token that can be Stacked.
(define-fungible-token stackables)
;; Function for deciding how many tokens to mint, depending on when they were mined.
;; Tailor to your own needs.
(define-read-only (get-coinbase-amount (stacks-block-ht uint))
(var-get coinbase-reward)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-miners-at-block (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
miner-rec (get miners miner-rec)
(list )
)
)
;; Getter for getting how many tokens are Stacked by the given principal in the given reward cycle.
(define-read-only (get-stacked-in-cycle (miner-id principal) (reward-cycle uint))
(match (map-get? stacked-per-cycle { owner: miner-id, reward-cycle: reward-cycle })
stacked-rec (get amount-token stacked-rec)
u0
)
)
;; Getter for getting the list of miners and uSTX committments for a given block.
(define-read-only (get-mined-blocks-per-user)
(match (map-get? pending-mines-per-user { user: tx-sender })
user-rec (get blocks user-rec)
(list )
)
)
;; Getter for getting how many uSTX are committed and tokens are Stacked per reward cycle.
(define-read-only (get-tokens-per-cycle (rc uint))
(match (map-get? tokens-per-cycle { reward-cycle: rc })
token-info token-info
{ total-ustx: u0, total-tokens: u0 }
)
)
;; API endpoint for getting statistics about this PoX-lite contract.
;; Compare to /v2/pox on the Stacks node.
(define-read-only (get-pox-lite-info)
(match (get-reward-cycle block-height)
cur-reward-cycle
(ok
(let (
(token-info (get-tokens-per-cycle cur-reward-cycle))
(total-ft-supply (ft-get-supply stackables))
(total-ustx-supply (stx-get-balance (as-contract tx-sender)))
)
{
reward-cycle-id: cur-reward-cycle,
first-block-height: (var-get first-stacking-block),
reward-cycle-length: (var-get reward-cycle-length),
total-supply: total-ft-supply,
total-ustx-locked: total-ustx-supply,
cur-liquid-supply: (- total-ft-supply (get total-tokens token-info)),
cur-locked-supply: (get total-tokens token-info),
cur-ustx-committed: (get total-ustx token-info)
})
)
(err ERR-STACKING-NOT-AVAILABLE)
)
)
;; Produce the new tokens for the given claimant, who won the tokens at the given Stacks block height.
(define-private (mint-coinbase (recipient principal) (stacks-block-ht uint))
(begin
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount (get-coinbase-amount stacks-block-ht)) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount (get-coinbase-amount stacks-block-ht)) STACKSWAP_ACCOUNT))
(ok true)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
;; (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)
)
)
;; Getter to obtain the list of miners and uSTX commitments at a given Stacks block height,
;; OR, an empty such structure.
(define-private (get-block-miner-rec-or-default (stacks-block-ht uint))
(match (map-get? miners { stacks-block-height: stacks-block-ht })
rec rec
{ miners: (list ), claimed: false })
)
(define-private (get-user-rec-or-default (miner-principal principal))
(match (map-get? pending-mines-per-user { user: miner-principal })
rec rec
{ blocks: (list )})
)
;; Inner fold function for getting how many uSTX were committed by a list of miners.
(define-private (get-block-commit-total-closure (idx uint) (input { sum: uint, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
)
{
sum: (+ sum commit-at-index),
miners: miners-list
})
)
;; Given a list of miners and uSTX commitments, return how many uSTX were committed in total.
(define-read-only (get-block-commit-total (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(get sum
(fold get-block-commit-total-closure REWARD-CYCLE-INDEXES
{ sum: u0, miners: miners-list })
)
)
;; Inner fold function to determine which miner won the token batch at a particular Stacks block height, given a sampling value.
(define-private (get-block-winner-closure (idx uint) (input { sum: uint, sample: uint, winner-index: (optional uint), miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(sum (get sum input))
(sample (get sample input))
(miners-list (get miners input))
(commit-at-index (match (element-at miners-list idx)
miner-rec (get amount-ustx miner-rec)
u0))
(next-sum (+ sum commit-at-index))
(next-winner-index
(if (and (>= sample sum) (< sample next-sum) (> commit-at-index u0))
(some idx)
(get winner-index input)))
)
{
sum: next-sum,
sample: sample,
winner-index: next-winner-index,
miners: miners-list
})
)
;; Determine who won a given batch of tokens, given a random sample and a list of miners and commitments.
;; The probability that a given miner wins the batch is proportional to how many uSTX it committed out of the
;; sum of commitments for this block.
(define-read-only (get-block-winner (random-sample uint) (miners-list (list 32 { miner: principal, amount-ustx: uint })))
(let (
(commit-total (get-block-commit-total miners-list))
(winner-index-opt
(if (> commit-total u0)
(get winner-index
(fold get-block-winner-closure REWARD-CYCLE-INDEXES
{ sum: u0, sample: (mod random-sample commit-total), winner-index: none, miners: miners-list }))
none))
)
(match winner-index-opt
winner-index (match (element-at miners-list winner-index)
winning-miner-rec (some winning-miner-rec)
none)
none))
)
;; Inner fold function for finding a given miner in a list of miners.
(define-private (has-mined-in-list-closure (idx uint) (input { found: bool, candidate: principal, miners: (list 32 { miner: principal, amount-ustx: uint }) }))
(let (
(already-found (get found input))
(miner-candidate (get candidate input))
(miners-list (get miners input))
)
{
found: (match (element-at miners-list idx)
miner-rec (or already-found (is-eq miner-candidate (get miner miner-rec)))
already-found),
candidate: miner-candidate,
miners: miners-list
})
)
;; Determine if a given miner has already mined in a list of miners.
(define-read-only (has-mined-in-list (miner principal) (miner-list (list 32 { miner: principal, amount-ustx: uint })))
(get found
(fold has-mined-in-list-closure REWARD-CYCLE-INDEXES
{ found: false, candidate: miner, miners: miner-list }))
)
;; Determine whether or not the given principal can claim the mined tokens at a particular block height,
;; given the miners record for that block height, a random sample, and the current block height.
(define-read-only (can-claim-tokens (claimer principal)
(claimer-stacks-block-height uint)
(random-sample uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
})
(current-stacks-block uint))
(let (
(reward-maturity (var-get token-reward-maturity))
(maximum-stacks-block-height
(if (>= current-stacks-block reward-maturity)
(- current-stacks-block reward-maturity)
u0))
)
(if (< claimer-stacks-block-height maximum-stacks-block-height)
(begin
(asserts! (not (get claimed miners-rec))
(err ERR-ALREADY-CLAIMED))
(match (get-block-winner random-sample (get miners miners-rec))
winner-rec (if (is-eq claimer (get miner winner-rec))
(ok true)
(err ERR-UNAUTHORIZED))
(err ERR-NO-WINNER))
)
(err ERR-IMMATURE-TOKEN-REWARD)))
)
;; Mark a batch of mined tokens as claimed, so no one else can go and claim them.
(define-private (set-tokens-claimed (claimed-stacks-block-height uint))
(let (
(miner-rec (unwrap!
(map-get? miners { stacks-block-height: claimed-stacks-block-height })
(err ERR-NO-WINNER)))
)
(begin
(asserts! (not (get claimed miner-rec))
(err ERR-ALREADY-CLAIMED))
(map-set miners
{ stacks-block-height: claimed-stacks-block-height }
{
miners: (get miners miner-rec),
claimed: true
}
)
(ok true)))
)
;; Determine whether or not the given miner can actually mine tokens right now.
;; * Stacking must be active for this smart contract
;; * No more than 31 miners must have mined already
;; * This miner hasn't mined in this block before
;; * The miner is committing a positive number of uSTX
;; * The miner has the uSTX to commit
(define-read-only (can-mine-tokens (miner-id principal)
(stacks-bh uint)
(amount-ustx uint)
(miners-rec {
miners: (list 32 { miner: principal, amount-ustx: uint }),
claimed: bool
}))
(begin
(asserts! (is-some (get-reward-cycle stacks-bh))
(err ERR-STACKING-NOT-AVAILABLE))
(asserts! (< (len (get miners miners-rec)) u32)
(err ERR-ROUND-FULL))
(asserts! (not (has-mined-in-list miner-id (get miners miners-rec)))
(err ERR-ALREADY-MINED))
(asserts! (> amount-ustx u0)
(err ERR-CANNOT-MINE))
(asserts! (>= (stx-get-balance miner-id) amount-ustx)
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
)
)
;; Determine if a Stacker can Stack their tokens. Like PoX, they must supply
;; a future Stacks block height at which Stacking begins, as well as a lock-up period
;; in reward cycles.
;; * The Stacker's start block height must be in the future
;; * The first reward cycle must be _after_ the current reward cycle
;; * The lock period must be valid (positive, but no greater than the maximum allowed period)
;; * The Stacker must have tokens to Stack.
(define-read-only (can-stack-tokens (stacker-id principal) (amount-tokens uint) (now-stacks-ht uint) (start-stacks-ht uint) (lock-period uint))
(let (
(cur-reward-cycle (unwrap! (get-reward-cycle now-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE)))
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
(max-lockup (var-get max-reward-cycles))
)
(begin
(asserts! (< now-stacks-ht start-stacks-ht)
(err ERR-CANNOT-STACK))
(asserts! (< cur-reward-cycle start-reward-cycle)
(err ERR-CANNOT-STACK))
(asserts! (and (> lock-period u0) (<= lock-period max-lockup))
(err ERR-CANNOT-STACK))
(asserts! (> amount-tokens u0)
(err ERR-CANNOT-STACK))
(asserts! (<= amount-tokens (ft-get-balance stackables stacker-id))
(err ERR-INSUFFICIENT-BALANCE))
(ok true)
))
)
;; Determine how many uSTX a Stacker is allowed to claim, given the reward cycle they Stacked in and the current block height.
;; This method only returns a positive value if:
;; * The current block height is in a subsequent reward cycle
;; * The Stacker actually did lock up some tokens in the target reward cycle
;; * The Stacker locked up _enough_ tokens to get at least one uSTX.
;; It's possible to Stack tokens but not receive uSTX. For example, no miners may have mined in this reward cycle.
;; As another example, you may have Stacked so few that you'd be entitled to less than 1 uSTX.
(define-read-only (get-entitled-stacking-reward (stacker-id principal) (target-reward-cycle uint) (cur-block-height uint))
(let (
(stacked-this-cycle
(get amount-token
(default-to { amount-token: u0 }
(map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle }))))
(total-tokens-this-cycle
(default-to { total-ustx: u0, total-tokens: u0 }
(map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })))
)
(match (get-reward-cycle cur-block-height)
cur-reward-cycle
(if (or (<= cur-reward-cycle target-reward-cycle) (is-eq u0 (get total-tokens total-tokens-this-cycle)))
;; either this reward cycle hasn't finished yet, or the Stacker contributed nothing
u0
;; (total-ustx * this-stackers-tokens) / total-tokens-stacked
(/ (* (get total-ustx total-tokens-this-cycle) stacked-this-cycle)
(get total-tokens total-tokens-this-cycle))
)
;; before first reward cycle
u0
))
)
;; Mark a miner as having mined in a given Stacks block and committed the given uSTX.
(define-private (set-tokens-mined (miner-id principal) (stacks-bh uint) (commit-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default stacks-bh))
(user-rec (get-user-rec-or-default miner-id))
(rc (unwrap! (get-reward-cycle stacks-bh)
(err ERR-STACKING-NOT-AVAILABLE)))
(tokens-mined (match (map-get? tokens-per-cycle { reward-cycle: rc })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(map-set miners
{ stacks-block-height: stacks-bh }
{
miners: (unwrap-panic (as-max-len? (append (get miners miner-rec) { miner: miner-id, amount-ustx: commit-ustx }) u32)),
claimed: false
}
)
(map-set tokens-per-cycle
{ reward-cycle: rc }
{ total-ustx: (+ commit-ustx (get total-ustx tokens-mined)), total-tokens: (get total-tokens tokens-mined) }
)
(map-set pending-mines-per-user
{ user: miner-id }
{
blocks: (unwrap-panic (as-max-len? (append (get blocks user-rec) stacks-bh) u10000)),
}
)
(ok true)
))
)
;; Get the reward cycle for a given Stacks block height
(define-read-only (get-reward-cycle (stacks-bh uint))
(let (
(first-stack-block (var-get first-stacking-block))
(rc-len (var-get reward-cycle-length))
)
(if (>= stacks-bh first-stack-block)
(some (/ (- stacks-bh first-stack-block) rc-len))
none
))
)
;; Get the first Stacks block height for a given reward cycle.
(define-read-only (get-first-block-height-in-reward-cycle (reward-cycle uint))
(+ (var-get first-stacking-block) (* (var-get reward-cycle-length) reward-cycle)))
;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine
;; which one may claim the token batch for the given block height.
(define-read-only (get-random-uint-at-block (stacks-block uint))
(let (
(vrf-lower-uint-opt
(match (get-block-info? vrf-seed stacks-block)
vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed)))
none))
)
vrf-lower-uint-opt)
)
;; Inner fold function for Stacking tokens. Populates the stacked-per-cycle and tokens-per-cycle tables for each
;; reward cycle the Stacker is Stacking in.
(define-private (stack-tokens-closure (reward-cycle-idx uint) (stacker { id: principal, amt: uint, first: uint, last: uint }))
(let (
(stacker-id (get id stacker))
(amount-token (get amt stacker))
(first-reward-cycle (get first stacker))
(last-reward-cycle (get last stacker))
(target-reward-cycle (+ first-reward-cycle reward-cycle-idx))
(stacked-already (match (map-get? stacked-per-cycle { owner: stacker-id, reward-cycle: target-reward-cycle })
rec (get amount-token rec)
u0))
(tokens-this-cycle (match (map-get? tokens-per-cycle { reward-cycle: target-reward-cycle })
rec rec
{ total-ustx: u0, total-tokens: u0 }))
)
(begin
(if (and (>= target-reward-cycle first-reward-cycle) (< target-reward-cycle last-reward-cycle))
(begin
(map-set stacked-per-cycle
{ owner: stacker-id, reward-cycle: target-reward-cycle }
{ amount-token: (+ amount-token stacked-already) })
(map-set tokens-per-cycle
{ reward-cycle: target-reward-cycle }
{ total-ustx: (get total-ustx tokens-this-cycle), total-tokens: (+ amount-token (get total-tokens tokens-this-cycle)) })
true)
false)
{ id: stacker-id, amt: amount-token, first: first-reward-cycle, last: last-reward-cycle }
))
)
;; Stack the contract's tokens. Stacking will begin at the next reward cycle following
;; the reward cycle in which start-stacks-ht resides.
;; This method takes possession of the Stacker's tokens until the given number of reward cycles
;; has passed.
(define-public (stack-tokens (amount-tokens uint) (start-stacks-ht uint) (lock-period uint))
(let (
(start-reward-cycle (+ u1 (unwrap! (get-reward-cycle start-stacks-ht) (err ERR-STACKING-NOT-AVAILABLE))))
)
(begin
(print u11)
(try! (can-stack-tokens tx-sender amount-tokens block-height start-stacks-ht lock-period))
(unwrap! (ft-transfer? stackables amount-tokens tx-sender (as-contract tx-sender))
(err ERR-INSUFFICIENT-BALANCE))
(fold stack-tokens-closure REWARD-CYCLE-INDEXES
{ id: tx-sender, amt: amount-tokens, first: start-reward-cycle, last: (+ start-reward-cycle lock-period) })
(ok true)
))
)
;; Mine tokens. The miner commits uSTX into this contract (which Stackers can claim later with claim-stacking-reward),
;; and in doing so, enters their candidacy to be able to claim the block reward (via claim-token-reward). The miner must
;; wait for a token maturity window in order to obtain the tokens. Once that window passes, they can get the tokens.
;; This ensures that no one knows the VRF seed that will be used to pick the winner.
(define-public (mine-tokens (amount-ustx uint))
(let (
(miner-rec (get-block-miner-rec-or-default block-height))
)
(begin
(try! (can-mine-tokens tx-sender block-height amount-ustx miner-rec))
(try! (set-tokens-mined tx-sender block-height amount-ustx))
(unwrap-panic (stx-transfer? amount-ustx tx-sender (as-contract tx-sender)))
(ok true)
))
)
;; Claim the block reward. This mints and transfers out a miner's tokens if it is indeed the block winner for
;; the given Stacks block. The VRF seed will be sampled at the target mined stacks block height _plus_ the
;; maturity window, and if the miner (i.e. the caller of this function) both mined in the target Stacks block
;; and was later selected by the VRF as the winner, they will receive that block's token batch.
;; Note that this method actually mints the contract's tokens -- they do not exist until the miner calls
;; this method.
(define-public (claim-token-reward (mined-stacks-block-ht uint))
(let (
(random-sample (unwrap! (get-random-uint-at-block (+ mined-stacks-block-ht (var-get token-reward-maturity)))
(err ERR-IMMATURE-TOKEN-REWARD)))
(miners-rec (unwrap! (map-get? miners { stacks-block-height: mined-stacks-block-ht })
(err ERR-NO-WINNER)))
)
(begin
(try! (can-claim-tokens tx-sender mined-stacks-block-ht random-sample miners-rec block-height))
(try! (set-tokens-claimed mined-stacks-block-ht))
(unwrap-panic (mint-coinbase tx-sender mined-stacks-block-ht))
(ok true)
))
)
;; Claim a Stacking reward. Once a reward cycle passes, a Stacker can call this method to obtain any
;; uSTX that were committed to the contract during that reward cycle (proportional to how many tokens
;; they locked up).
(define-public (claim-stacking-reward (target-reward-cycle uint))
(let (
(entitled-ustx (get-entitled-stacking-reward tx-sender target-reward-cycle block-height))
(stacker-id tx-sender)
)
(begin
(asserts! (> entitled-ustx u0)
(err ERR-NOTHING-TO-REDEEM))
;; can't claim again
(map-set stacked-per-cycle
{ owner: tx-sender, reward-cycle: target-reward-cycle }
{ amount-token: u0 })
(unwrap-panic
(as-contract
(stx-transfer? entitled-ustx tx-sender stacker-id)))
(ok true)
))
)
;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;;
;; Data variables specific to the deployed token contract
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; Track who deployed the token and whether it has been initialized
(define-data-var contract-owner principal tx-sender)
(define-data-var is-initialized bool false)
(define-public (transfer (amount uint) (from principal) (to principal))
(begin
(asserts! (is-eq from tx-sender)
(err ERR-UNAUTHORIZED))
(ft-transfer? stackables amount from to)
)
)
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance stackables owner)))
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
;; Returns the total number of tokens that currently exist
(define-read-only (get-total-supply)
(ok (ft-get-supply stackables)))
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; Setter for the URI - only the owner can set it
(define-public (set-token-uri (updated-uri (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-uri", updated-uri: updated-uri })
(ok (var-set uri updated-uri))))
;; Variable for UwebsiteRI storage
(define-data-var website (string-utf8 256) u"")
;; Public getter for the website
(define-read-only (get-token-website)
(ok (some (var-get website))))
;; Setter for the website - only the owner can set it
(define-public (set-token-website (updated-website (string-utf8 256)))
(begin
(asserts! (is-eq (var-get contract-owner) tx-sender) (err PERMISSION_DENIED_ERROR))
;; Print the action for any off chain watchers
(print { action: "set-token-website", updated-website: updated-website })
(ok (var-set website updated-website))))
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256))
(website-to-set (string-utf8 256)) (initial-mint-amount uint) (first-stacking-block-to-set uint) (reward-cycle-lengh-to-set uint) (token-reward-maturity-to-set uint) (coinbase-reward-to-set uint))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
(var-set website website-to-set)
(var-set contract-owner tx-sender)
;; (try! (ft-mint? stackables initial-mint-amount tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-owner-amount initial-mint-amount) tx-sender))
(unwrap-panic (ft-mint? stackables (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-fee-v0c get-stackswap-amount initial-mint-amount) STACKSWAP_ACCOUNT))
;; (asserts! (is-eq (len REWARD-CYCLE-INDEXES) MAX-REWARD-CYCLES) (err "Invalid max reward cycles"))
(var-set first-stacking-block first-stacking-block-to-set)
(var-set reward-cycle-length reward-cycle-lengh-to-set)
(var-set token-reward-maturity token-reward-maturity-to-set)
(var-set coinbase-reward coinbase-reward-to-set)
(ok u0)
))
;; Variable for approve
(define-data-var approved bool false)
;; Public getter for the approve
(define-read-only (get-is-approved)
(ok (some (var-get approved))))
(define-public (approve (is-approved bool))
(begin
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err PERMISSION_DENIED_ERROR))
(ok (var-set approved is-approved))
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-poxl-token (as-contract tx-sender))
|
0x5c483b2a2fbb8b93f15ff6d34b9afe02885e5475ab657bf0ac0f2afe73b33c6f
| 25,725 |
success
|
liquidity-tokensoft-v1-token-v0a14
|
;; ;; we implement the sip-010 + a mint function liquidity-token-soft-trait
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.liquidity-token-trait-v0a.liquidity-token-trait)
(impl-trait 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.initializable-trait-v0a.initializable-liquidity-token-trait)
;; ;; we can use an ft-token here, so use it!
(define-fungible-token tokensoft-token)
(define-constant no-acccess-err u40)
;; Error returned for permission denied - stolen from http 403
(define-constant PERMISSION_DENIED_ERROR u403)
;; Track who deployed the token and whether it has been initialized
(define-data-var deployer-principal principal tx-sender)
(define-data-var is-initialized bool false)
(define-data-var token-name (string-ascii 32) "")
(define-data-var token-symbol (string-ascii 32) "")
(define-data-var token-decimals uint u0)
;; implement all functions required by sip-010
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(ft-transfer? tokensoft-token amount tx-sender recipient)
)
)
;; Returns the token name
(define-read-only (get-name)
(ok (var-get token-name)))
;; Returns the symbol or "ticker" for this token
(define-read-only (get-symbol)
(ok (var-get token-symbol)))
;; Returns the number of decimals used
(define-read-only (get-decimals)
(ok (var-get token-decimals)))
(define-read-only (get-balance-of (owner principal))
(ok (ft-get-balance tokensoft-token owner))
)
(define-read-only (get-total-supply)
(ok (ft-get-supply tokensoft-token))
)
;; Variable for URI storage
(define-data-var uri (string-utf8 256) u"")
;; Public getter for the URI
(define-read-only (get-token-uri)
(ok (some (var-get uri))))
;; one stop function to gather all the data relevant to the liquidity token in one call
(define-read-only (get-data (owner principal))
(ok {
name: (unwrap-panic (get-name)),
symbol: (unwrap-panic (get-symbol)),
decimals: (unwrap-panic (get-decimals)),
uri: (unwrap-panic (get-token-uri)),
supply: (unwrap-panic (get-total-supply)),
balance: (unwrap-panic (get-balance-of owner))
})
)
;; the extra mint method used by stackswap when adding liquidity
;; can only be used by STACKSWAP main contract
(define-public (mint (recipient principal) (amount uint))
(begin
(print "token-liquidity.mint")
(print (some contract-caller))
;; (print (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-mint? tokensoft-token amount recipient)
)
)
;; the extra burn method used by STACKSWAP when removing liquidity
;; can only be used by STACKSWAP main contract
(define-public (burn (recipient principal) (amount uint))
(begin
(print "token-liquidity.burn")
(print contract-caller)
(print amount)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "swap"))) (err no-acccess-err))
(ft-burn? tokensoft-token amount recipient)
)
)
(define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32)) (decimals-to-set uint) (uri-to-set (string-utf8 256)))
(begin
(print "token-liquidity.init")
(print contract-caller)
(asserts! (is-eq contract-caller (unwrap-panic (contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-dao-v0e get-qualified-name-by-name "one-step-mint"))) (err no-acccess-err))
(asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR))
(var-set is-initialized true) ;; Set to true so that this can't be called again
(var-set token-name name-to-set)
(var-set token-symbol symbol-to-set)
(var-set token-decimals decimals-to-set)
(var-set uri uri-to-set)
;; (map-set roles { role: MINTER_ROLE, account: initial-owner } { allowed: true })
(ok u0)
)
)
(contract-call? 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275.stackswap-one-step-mint-v0d add-liquidity-token (as-contract tx-sender))
|
0x9322718c1bf59037bc3b0d62fb004d42294ebba77f5d969ecdc20de7312a2dbc
| 25,726 |
success
|
is-block-winner
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2EMP9PXNNNEZ51FCBDA0X7HYFWGDEXR8T84MMK6)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x20c3bcc08b79ebe1720fd5d190bbb82eba0afcea24ce269ead9614d33e12dbd9
| 25,866 |
success
|
is-block-winnerclaim-mining-reward
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP2EMP9PXNNNEZ51FCBDA0X7HYFWGDEXR8T84MMK6)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0x6abf1123daaead291dde7a625c7f63a543ff03b43d24ab7ec93f0aadfea38717
| 25,870 |
success
|
blockedu-v1
|
;; BlockEdu Contract v1
;; ;; ;; ;; ;; Errors ;; ;; ;; ;; ;;
(define-constant FAILED-TO-MINT-ERROR (err u1))
(define-constant NOT-OWNER-ERROR (err u2))
(define-constant NOT-AUTH-UNI (err u3))
(define-constant NOT-STUDENT (err u4))
(define-constant CALLER-DOES-NOT-OWN-TOKEN (err u5))
(define-constant ACCESS-DENIED (err u6))
;; ;; ;; ;; ;; Constants ;; ;; ;; ;; ;;
(define-constant contract-owner tx-sender)
;; ;; ;; ;; ;; Data Maps and Vars ;; ;; ;; ;; ;;
;; Number of tokens a particular university owns
(define-map tokens-count { owner: principal } { count: int })
;; Data map to store information about the degree: university-name, student-name, degree
(define-map degree-data
{token-id: uint}
{data-hash: (buff 32), token-uri: (string-ascii 64)}
)
;; Authorised Universities pricipals
(define-map auth-universities { uni-principal: principal } { data-hash: (buff 32), token-uri: (string-ascii 64) })
;; Authorised Students pricipals
(define-map auth-students { student-principal: principal } { data-hash: (buff 32), token-uri: (string-ascii 64) })
;; Authorised Employers pricipals
(define-map auth-employers { employer-principal: principal, student-principal: principal } {access-allowed: bool})
;; NFT Variable
(define-non-fungible-token blockedu-token uint)
;; The counter for the issued NFTs
(define-data-var curr-token-index uint u100)
;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;
;; ;; ;; ;; ;; Public Functions ;; ;; ;; ;; ;;
;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;
;; Allow deployer to populate auth-universities map with verified universies
(define-public (add-auth-university (new-uni principal) (data-hash (buff 32)) (token-uri (string-ascii 64)) )
(begin
(asserts! (is-eq contract-owner tx-sender) NOT-OWNER-ERROR)
(ok (map-set auth-universities { uni-principal: new-uni } { data-hash: data-hash, token-uri: token-uri }))
)
)
;; Allow authenticated universities to populate auth-students map with it's students
(define-public (add-auth-student (new-student principal) (data-hash (buff 32)) (token-uri (string-ascii 64)) )
(begin
(asserts! (is-principal-university tx-sender) NOT-AUTH-UNI)
(ok (map-set auth-students { student-principal: new-student } { data-hash: data-hash, token-uri: token-uri }))
)
)
;; Allow students to add authenticated employers who can see their degree data
(define-public (add-auth-employer (employer-principal principal))
(begin
;; Only the student can add new map elements
(asserts! (is-principal-student tx-sender) NOT-STUDENT)
(ok (map-set auth-employers {employer-principal: employer-principal, student-principal: tx-sender} {access-allowed: true}))
)
)
;; Allow students to remove authenticated employers who can no longer see their degree data
(define-public (remove-auth-employer (employer-principal principal))
(begin
;; Only the student can remove map elements
(asserts! (is-principal-student tx-sender) NOT-STUDENT)
;; Only the student can remove it's respective employers whom he has given to permission earlier
(asserts! (is-eq (get access-allowed (map-get? auth-employers {employer-principal: employer-principal, student-principal: tx-sender})) (some true) ) ACCESS-DENIED)
(ok (map-delete auth-employers {employer-principal: employer-principal, student-principal: tx-sender}))
)
)
;; Mint new tokens
(define-public (mint!
(owner principal)
(data-hash (buff 32))
(token-uri (string-ascii 64)))
(let ((token-id (+ (var-get curr-token-index) u1)))
;; Access Rights Implementation
;; - only verified universities can mint tokens
(asserts! (is-principal-university tx-sender) NOT-AUTH-UNI)
;; - only students whose wallets exist can have tokens minted for them
(asserts! (is-principal-student owner) NOT-STUDENT)
;; Check if the token is successfully minted
(asserts! (register-token owner token-id) FAILED-TO-MINT-ERROR)
(map-set degree-data
{token-id: token-id}
{data-hash: data-hash, token-uri: token-uri}
)
(var-set curr-token-index token-id)
(ok token-id)
)
)
;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;
;; ;; ;; ;; ;; Read only Functions ;; ;; ;; ;; ;;
;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;
;; Gets the owner of the specified token ID.
(define-read-only (owner-of? (token-id uint))
(nft-get-owner? blockedu-token token-id)
)
(define-read-only (get-auth-universities (uni-principal principal))
(map-get? auth-universities (tuple (uni-principal uni-principal)))
)
(define-read-only (get-auth-students (student-principal principal))
(map-get? auth-students (tuple (student-principal student-principal)))
)
(define-read-only (get-auth-employers (employer-principal principal) (student-principal principal))
(map-get? auth-employers (tuple (employer-principal employer-principal) (student-principal student-principal)))
)
;; Get map information
(define-read-only (get-info (token-id uint))
(begin
(asserts!
(or
;; Anyone one of the below conditions should be true:
;; 1- The caller can either be a employer who have access to
;; this degree data can see the information of this NFT OR
;; 2- Only student (degree owners) can see the information of their tokens
(is-eq (owner-of? token-id) (some tx-sender))
(is-eq
(get access-allowed
(map-get? auth-employers
{employer-principal: tx-sender, student-principal: (default-to tx-sender (owner-of? token-id))}))
(some true)
)
)
ACCESS-DENIED
)
;; Return the NFT information
(ok (map-get? degree-data (tuple (token-id token-id))))
)
)
;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;
;; ;; ;; ;; ;; Private Functions ;; ;; ;; ;; ;;
;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;
;; function to see whether a principal is in the auth-universities map
(define-private (is-principal-university (incoming-principal principal))
(is-some (map-get? auth-universities (tuple (uni-principal incoming-principal))))
)
;; function to see whether a principal is in the auth-students map
(define-private (is-principal-student (incoming-principal principal))
(is-some (map-get? auth-students (tuple (student-principal incoming-principal))))
)
;; Gets the amount of tokens owned by the specified address.
(define-private (balance-of (account principal))
(default-to 0
(get count
(map-get? tokens-count (tuple (owner account))))))
;; Checks whether the owner owns the token or not
(define-private (is-owner (actor principal) (token-id uint))
(is-eq actor
(unwrap! (nft-get-owner? blockedu-token token-id) false)
)
)
;; Register token
(define-private (register-token (new-owner principal) (token-id uint))
(begin
(unwrap! (nft-mint? blockedu-token token-id new-owner) false)
(map-set tokens-count
{owner: new-owner}
{count: (+ 1 (balance-of new-owner))}
)
)
)
|
0xfd142a25f4b403013a6fa437e69adf9561e012d7f39ca04fcf2ac327385544eb
| 25,961 |
success
|
claim-mining-reward
|
;; hello-world contract
(define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
(define-constant recipient 'SP3Y8SK4AKCCPAEWGA7KETX5SBZTXBQBEPC8W8T66)
(define-fungible-token novel-token-19)
(begin (ft-mint? novel-token-19 u12 sender))
(begin (ft-transfer? novel-token-19 u2 sender recipient))
(define-non-fungible-token hello-nft uint)
(begin (nft-mint? hello-nft u1 sender))
(begin (nft-mint? hello-nft u2 sender))
(begin (nft-transfer? hello-nft u1 sender recipient))
(define-public (test-emit-event)
(begin
(print "Event! Hello world")
(ok u1)))
(begin (test-emit-event))
(define-public (test-event-types)
(begin
(unwrap-panic (ft-mint? novel-token-19 u3 recipient))
(unwrap-panic (nft-mint? hello-nft u2 recipient))
(unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR))
(unwrap-panic (stx-burn? u20 tx-sender))
(ok u1)))
(define-map store {key: (buff 32)} {value: (buff 32)})
(define-public (get-value (key (buff 32)))
(begin
(match (map-get? store {key: key})
entry (ok (get value entry))
(err 0))))
(define-public (set-value (key (buff 32)) (value (buff 32)))
(begin
(map-set store {key: key} {value: value})
(ok u1)))
|
0xf4a623d0c2bf56835b8b08fb0e0f7e388e134cc943cb5b44fd7a4fc0c003506a
| 25,973 |
success
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.