contract_name
stringlengths
1
40
code_body
stringlengths
0
100k
txid
stringlengths
66
66
block_height
int32
0
165k
status
stringclasses
3 values
can-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)))
0xf2fd251b765a527e255cc4d1831381910cfd507313ba7b657403efda8b990d61
25,981
success
supposed-ivory-quokka
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP3BMCWYQ7WYKKTEMMEKS465X3VSJ9PWHD3NYQRTC) (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)))
0x668ceab93c285d8c5d96deb6c5e7d08b54d8c7f8826015ee00548f511997739a
25,995
success
inclined-indigo-dove
(define-fungible-token main) (define-fungible-token vote) (define-data-var owner principal tx-sender) (define-map mainTokenHolders {address: principal} {amount: uint}) (begin (ft-mint? main u1000000 tx-sender)) (begin (ft-mint? vote u1000000 tx-sender)) ;; Task 1 (define-public (transfer-token (address principal) (amount uint)) (if (is-eq tx-sender (var-get owner)) (match (ft-transfer? main amount tx-sender address) success (begin (map-insert mainTokenHolders { address: address } {amount: amount}) (ok success)) error (err error) ) (err u500) ) ) ;; Task 2 (define-public (issue-vote-tokens (address principal)) (begin (let ((amount (default-to u0 (get amount (map-get? mainTokenHolders {address: address}))))) (unwrap! (ft-transfer? vote amount tx-sender address) (err 8)) (ok 1) ) ) ) (define-public (check-balance (address principal)) (begin (ok (ft-get-balance main address)) ) )
0x3ef59164d5fa419415579344e35a0ed26682e34033997689dc04e179bbfba3d6
25,998
success
delightful-chocolate-hawk
;; 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)))
0xc83ad7893752aaff1362283a258a6f377b2113131ffad95d03667656d4616443
26,080
success
pale-black-beetle
;; 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)))
0x569eaf2717005bd9997aa94db0f488d29e83ad71babe9dcb6bac94d6633c475a
26,216
success
postal-statio-m
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP1FVA6CZBSGR02W37PCX8X0HK3FKR6CZ77QSJZED) (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)))
0xc8724b13dc52b5ab64d1bea70850d249bff28a9195785dcad6ec73b3fb467e05
26,224
success
appropriate-coral-woodpecker
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP4M6Y8JVFX0J3ZXHPPSRE0X33MSWWT9RFNF6JGJ) (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)))
0x8bf7884e7890f1f87d0255e81ace0e7b5c4a1dfc69f2ef5a76ffef48967795c5
26,233
success
activate-token
;; 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)))
0x43b35b857059b6cb877d744df881839b7ebc6e607d2a9de857ec8ef72850bbda
26,310
success
free-punks-v0
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) (define-non-fungible-token free-punks uint) ;; constants (define-constant PUNK-IMAGE-HASH u"345a94125abb0a209a57943ffe043d101e810dbf52d08c892b4718613c867798") (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-ALL-MINTED u101) (define-constant ERR-COOLDOWN u102) (define-constant CONTRACT-OWNER tx-sender) ;; variables (define-data-var punk-counter uint u0) (define-data-var token-uri (string-ascii 256) "") (define-data-var token-punk-uri (string-ascii 256) "") (define-map punks { id: uint } { minted: bool }) (define-map punks-by-owner { owner: principal } { ids: (list 1000 uint) }) (define-data-var removing-punk-id uint u0) ;; public functions (define-public (mint (punk-id uint)) (let ( (count (var-get punk-counter)) (random-punk-id (get-random)) ) (asserts! (<= count u10000) (err ERR-ALL-MINTED)) (if (> (var-get punk-counter) u9500) (begin ;; added this cause the random number generator fails too often to get a random number that is not used yet ;; only the last 500 punks can be minted like this (mint-with-id punk-id) ) (begin (if (is-some (map-get? punks {id: random-punk-id})) (begin (let ( (random-punk-id-2 (get-random)) ) (if (is-some (map-get? punks {id: random-punk-id-2})) (begin (let ( (random-punk-id-3 (get-random)) ) (if (is-some (map-get? punks {id: random-punk-id-3})) (err ERR-COOLDOWN) (mint-with-id random-punk-id-3) ) ) ) (mint-with-id random-punk-id-2) ) ) ) (mint-with-id random-punk-id) ) ) ) ) ) (define-private (mint-with-id (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (try! (nft-mint? free-punks random-punk-id tx-sender)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u1000)) } ) (ok random-punk-id) ) ) (define-read-only (get-punks-entry-by-owner (owner principal)) (default-to { ids: (list u0) } (map-get? punks-by-owner { owner: owner }) ) ) (define-public (get-punks-by-owner (owner principal)) (ok (get ids (get-punks-entry-by-owner owner))) ) (define-public (burn (index uint)) (if (is-owner index tx-sender) (match (nft-burn? free-punks index tx-sender) success (ok true) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-public (transfer (index uint) (owner principal) (recipient principal)) (if (and (is-owner index owner) (is-owner index tx-sender)) (match (nft-transfer? free-punks index owner recipient) success (let ((punk-ids (unwrap-panic (get-punks-by-owner recipient)))) (map-set punks-by-owner { owner: recipient } { ids: (unwrap-panic (as-max-len? (append punk-ids index) u1000)) } ) (try! (remove-punk owner index)) (ok true) ) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-private (remove-punk (owner principal) (punk-id uint)) (if true (let ((punk-ids (unwrap-panic (get-punks-by-owner owner)))) (var-set removing-punk-id punk-id) (map-set punks-by-owner { owner: owner } { ids: (unwrap-panic (as-max-len? (filter remove-transferred-punk punk-ids) u1000)) } ) (ok true) ) (err u0) ) ) (define-private (remove-transferred-punk (punk-id uint)) (if (is-eq punk-id (var-get removing-punk-id)) false true ) ) (define-read-only (get-last-token-id) (ok (var-get punk-counter)) ) (define-public (set-token-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-punk-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-punk-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-read-only (get-token-uri (id uint)) (if (not (is-eq id u0)) (ok (some (var-get token-punk-uri))) (ok (some (var-get token-uri))) ) ) (define-public (get-owner (index uint)) (ok (nft-get-owner? free-punks index)) ) (define-read-only (stx-balance) (stx-get-balance (as-contract tx-sender)) ) (define-read-only (stx-balance-of (address principal)) (stx-get-balance address) ) (define-public (transfer-stx (address principal) (amount uint)) (if (is-eq tx-sender CONTRACT-OWNER) (as-contract (stx-transfer? amount (as-contract tx-sender) address)) (err ERR-NOT-AUTHORIZED) ) ) ;; private functions (define-private (is-owner (index uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? free-punks index) false)) ) (define-private (get-random) (let ( (number (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u1 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number})) (begin (let ( (number-2 (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u10 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number-2})) (begin (let ( (number-3 (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u30 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number-3})) (begin (let ( (number-4 (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u40 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number-4})) (begin (let ( (number-5 (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u50 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number-5})) number-5 number-5 ) ) ) number-4 ) ) ) number-3 ) ) ) number-2 ) ) ) number ) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; random number generator stuff ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map BUFF_TO_UINT (buff 1) uint ) ;; 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-private (get-random-uint-at-block (stacksBlock uint)) (match (get-block-info? vrf-seed stacksBlock) vrfSeed (some (get-random-from-vrf-seed vrfSeed u16)) none ) ) ;; Read the on-chain VRF and turn the lower [bytes] 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-private (get-random-uint-at-block-prec (stacksBlock uint) (bytes uint)) (match (get-block-info? vrf-seed stacksBlock) vrfSeed (some (get-random-from-vrf-seed vrfSeed bytes)) none ) ) ;; Turn lower [bytes] bytes into uint. (define-private (get-random-from-vrf-seed (vrfSeed (buff 32)) (bytes uint)) (+ (if (>= bytes u16) (convert-to-le (element-at vrfSeed u16) u15) u0) (if (>= bytes u15) (convert-to-le (element-at vrfSeed u17) u14) u0) (if (>= bytes u14) (convert-to-le (element-at vrfSeed u18) u13) u0) (if (>= bytes u13) (convert-to-le (element-at vrfSeed u19) u12) u0) (if (>= bytes u12) (convert-to-le (element-at vrfSeed u20) u11) u0) (if (>= bytes u11) (convert-to-le (element-at vrfSeed u21) u10) u0) (if (>= bytes u10) (convert-to-le (element-at vrfSeed u22) u9) u0) (if (>= bytes u9) (convert-to-le (element-at vrfSeed u23) u8) u0) (if (>= bytes u8) (convert-to-le (element-at vrfSeed u24) u7) u0) (if (>= bytes u7) (convert-to-le (element-at vrfSeed u25) u6) u0) (if (>= bytes u6) (convert-to-le (element-at vrfSeed u26) u5) u0) (if (>= bytes u5) (convert-to-le (element-at vrfSeed u27) u4) u0) (if (>= bytes u4) (convert-to-le (element-at vrfSeed u28) u3) u0) (if (>= bytes u3) (convert-to-le (element-at vrfSeed u29) u2) u0) (if (>= bytes u2) (convert-to-le (element-at vrfSeed u30) u1) u0) (if (>= bytes u1) (convert-to-le (element-at vrfSeed u31) u0) u0) ) ) (define-private (convert-to-le (byte (optional (buff 1))) (pos uint)) (* (unwrap-panic (map-get? BUFF_TO_UINT (unwrap-panic byte))) (pow u2 (* u8 pos)) ) ) ;; initialize (fold fill-buff-to-uint (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 ) u0) (var-set token-punk-uri "https://freestxpunks.surge.sh/assets/punks.json") (var-set token-uri "https://freestxpunks.surge.sh/assets/punks.json") (define-private (fill-buff-to-uint (byte (buff 1)) (val uint)) (begin (map-insert BUFF_TO_UINT byte val) (+ val u1) ) )
0xc68d03331af54894214e06e47e30e28d549a90dfbec6ed1d71773910d1dd5090
26,320
success
tokensoft-v2-token-v0c0bt78h2w
;; 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))
0x54ea64dbd2d43674f714e514e43e12953d3c677f54df055b2e7ca32684627496
26,353
success
tokensoft-v2-token-v0cje5oi7df
;; 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))
0x0d69971647b163ba76c962aefffc959326f3dc7468d4fa06ec96c9d03b15ada4
26,353
success
tokensoft-v2-token-v0cyi1w8qnl
;; 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))
0xc007aa0862227fae38b07b4c6242d45065aa7c657185b600ebebee3d9080bba0
26,353
success
tokensoft-v2-token-v0csuspdmak
;; 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))
0xc4bc373cedd6d34bdbc3b921edc7b51f1034581f9a83870fe0f5c859d4664242
26,353
success
sleepy-brown-walrus
;; 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)))
0x8aeb85b322f94d5ea7c596acd234f7e0dc1b1b65492557ab6f7df2bc0d146eb2
26,359
success
inc-blue-xerinae
;; 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)))
0x20bf6091eea6d52a1b4d9ce3ca995abb6e7b7bcaba1af849eaeb53d96bfeded1
26,359
success
tokensoft-v2-token-v0cy0b9rwpx
;; 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))
0x9d76590673fe0bc2d6e03dac5917bbd120609f82a273f00894130146e320c602
26,361
success
poxlsoft-token-v0ayylls90x
;; 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))
0x06651e99c82daf77f83faac220d5904dd1fc33f2eab9afa9128a5a839bbb41fd
26,361
success
poxlsoft-token-v0a3jh4qxba
;; 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))
0xa6e7e185fa6ecd99643fcc8fe61e7848845e7b22dbd9562eaf22242b99ef1b56
26,361
success
poxlsoft-token-v0af6dmkcdj
;; 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))
0xd5f8c22f5acb157dafa84911aa4cd3deb4d5cfd70664266e03ecd6bb31a5f4a8
26,361
success
poxlsoft-token-v0amoconf91
;; 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))
0xdcf96715e97bc9ca7ad9fc34f1ba02029c06826953ee142fa9511654efd8c63f
26,361
success
poxlsoft-token-v0aa629sc2o
;; 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))
0xee53f6652956b343aa4924d24f2b4f359f38566f2d86fcde2a4a70d957b80353
26,361
success
liquidity-token-v0c6bgnfy66
;; ;; 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))
0x8d213a379ad33d6c9817fc6ae0bec326977e94f4e4aa562eb29c802da126dbcd
26,361
success
liquidity-token-v0cuynuh7bt
;; ;; 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))
0x60febce744371ef7297a8323d158ecaec5a905ccc36e55c74afc9a5a40cc32c2
26,361
success
liquidity-token-v0c5fp10qxm
;; ;; 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))
0x834238c4605e806432eb9ba71f51e8fba9b52eb5fc57b98fb1e85684bad6a5c5
26,361
success
liquidity-token-v0c6jcbz1e0
;; ;; 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))
0x9f100c0504c9ecc778ff8b0ce95fe34c9d090dcbfed28be36cbfee7775f24836
26,361
success
liquidity-token-v0cqa0hcqf2
;; ;; 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))
0x57d5bc234f571cdeec68f672a3afc3f481a64784cc400b1028500181a26c9af1
26,361
success
liquidity-token-v0cpxnz018e
;; ;; 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))
0xdfd17db83a56ad7dcd2652dbb74f41581443d5a682e4466215b369dab566c153
26,361
success
liquidity-token-v0crgyk54ja
;; ;; 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))
0x76de8faf64c0e80d69758dacfcb10b4341cfa234e1d73b4f85d118d901be726a
26,361
success
tokensoft-v2-token-v0cy0b9rwpx
;; 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))
0x9d76590673fe0bc2d6e03dac5917bbd120609f82a273f00894130146e320c602
26,362
success
poxlsoft-token-v0ayylls90x
;; 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))
0x06651e99c82daf77f83faac220d5904dd1fc33f2eab9afa9128a5a839bbb41fd
26,362
success
poxlsoft-token-v0a3jh4qxba
;; 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))
0xa6e7e185fa6ecd99643fcc8fe61e7848845e7b22dbd9562eaf22242b99ef1b56
26,362
success
poxlsoft-token-v0af6dmkcdj
;; 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))
0xd5f8c22f5acb157dafa84911aa4cd3deb4d5cfd70664266e03ecd6bb31a5f4a8
26,362
success
poxlsoft-token-v0amoconf91
;; 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))
0xdcf96715e97bc9ca7ad9fc34f1ba02029c06826953ee142fa9511654efd8c63f
26,362
success
poxlsoft-token-v0aa629sc2o
;; 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))
0xee53f6652956b343aa4924d24f2b4f359f38566f2d86fcde2a4a70d957b80353
26,362
success
liquidity-token-v0c6bgnfy66
;; ;; 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))
0x8d213a379ad33d6c9817fc6ae0bec326977e94f4e4aa562eb29c802da126dbcd
26,362
success
liquidity-token-v0cuynuh7bt
;; ;; 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))
0x60febce744371ef7297a8323d158ecaec5a905ccc36e55c74afc9a5a40cc32c2
26,362
success
liquidity-token-v0c5fp10qxm
;; ;; 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))
0x834238c4605e806432eb9ba71f51e8fba9b52eb5fc57b98fb1e85684bad6a5c5
26,362
success
liquidity-token-v0c6jcbz1e0
;; ;; 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))
0x9f100c0504c9ecc778ff8b0ce95fe34c9d090dcbfed28be36cbfee7775f24836
26,362
success
liquidity-token-v0cqa0hcqf2
;; ;; 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))
0x57d5bc234f571cdeec68f672a3afc3f481a64784cc400b1028500181a26c9af1
26,362
success
liquidity-token-v0cpxnz018e
;; ;; 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))
0xdfd17db83a56ad7dcd2652dbb74f41581443d5a682e4466215b369dab566c153
26,362
success
liquidity-token-v0crgyk54ja
;; ;; 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))
0x76de8faf64c0e80d69758dacfcb10b4341cfa234e1d73b4f85d118d901be726a
26,362
success
sip-010-trait
(define-trait sip-010-trait ( ;; Transfer from the caller to a new principal (transfer (uint principal principal (optional (buff 34))) (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 (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)) ) )
0xc60fd329accb412ddd96fd35f2c8a81fdef99aa61e6e83e5964e7ea07d666b77
26,434
success
fari-token-spec
;; Copyright (c) 2021 by The Bitfari Foundation. ;; This file is part of Bitfari. ;; Bitfari is free software. You may redistribute or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License or ;; (at your option) any later version. ;; Bitfari is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY, including without the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with Bitfari. If not, see <http://www.gnu.org/licenses/>. ;; This file contains the code to implement the Fari Token as specified in https://bitfari.org/token/ ;; The token allows for discounted advertising, network governance and deep-discount shopping. ;; Part shopping club token, part loyalty card and part advertising token, Bitfari implements a novel idea: ;; all customers are marketers, discounts contribute to loyalty, and, all token holders should have a say in the ;; destiny of the network. ;; The following token code is to be considered immutable with the exception of any required emergency adjustments. ;; --------------------------------------------------------------------------------------------------------------------- ;; implement the `ft-trait` trait defined in the `ft-trait` contract ;; trait in testnet ;;(impl-trait 'ST3FYGS9F88Y5FW2DT2Q5C7FVX99Y9HREGCXH5T9D.ft-trait.sip-010-trait) ;; trait in mainnet (impl-trait 'SP213KNHB5QD308TEESY1ZMX1BP8EZDPG4JWD0MEA.ft-trait.sip-010-trait) ;; limit supply to 100M Faris (define-constant MAX_MINT u10000000000000000) ;; max six decimal places (define-constant DECIMAL_PLACES u8) ;; errors (define-constant ERR_NO_VOID_MINT u1000) (define-constant ERR_NO_AUTH_MINT u1002) (define-constant ERR_NO_MORE_MINT u1004) (define-constant ERR_INVALID_TRANSFER u1008) ;; name the token (define-fungible-token fari) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (begin (ok (ft-get-balance fari owner)))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply fari))) ;; returns the token name (define-read-only (get-name) (ok "Fari")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FARI")) ;; the number of decimals used (define-read-only (get-decimals) (ok DECIMAL_PLACES)) ;; get the token uri (define-public (get-token-uri) (ok (some u"https://bitfari.org/token/"))) ;; transfer tokens to a recipient, check for non-zero, valid transfers (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (> (ft-get-balance fari tx-sender) u0) (begin (if (is-eq tx-sender sender) (begin (try! (ft-transfer? fari amount sender recipient)) (print memo) (ok true) ) (err ERR_INVALID_TRANSFER))) (err u0))) ;; execute mining after sanitization (define-private (execute-mint (amount uint) (account principal)) (if (< MAX_MINT (+ (ft-get-supply fari) amount)) (err ERR_NO_MORE_MINT) (begin (try! (ft-mint? fari amount account)) (ok true)))) ;; mint new tokens, check minted amount is valid (define-private (validate-mint (amount uint) (account principal)) (if (<= amount u0) (err ERR_NO_VOID_MINT) (execute-mint amount account))) ;; validate minting auth, amount, supply and then execute (define-public (mint (amount uint) (account principal)) (if ;; auth minter for testnet ;;(is-eq tx-sender 'ST3FYGS9F88Y5FW2DT2Q5C7FVX99Y9HREGCXH5T9D) ;; auth minter for mainnet (is-eq tx-sender 'SP213KNHB5QD308TEESY1ZMX1BP8EZDPG4JWD0MEA) (validate-mint amount account) (err ERR_NO_AUTH_MINT))) ;; initialize the foundation treasury ;; mint dev fund, investor fund, marketing fund, advisor fund, and grant fund ;; 80M tokens to be minted to the community ;; find the token allocations and distribution schedule at https://bitfari.org/token/ ;; testnet mint - to be distributed over 3 years ;;(mint u2000000000000000 'ST22CGGASNT176J764WXGD5ZKDK2ZEQ6XNRC660DX) ;; mainnet mint - to be distributed over 3 years (mint u2000000000000000 'SP213KNHB5QD308TEESY1ZMX1BP8EZDPG4JWD0MEA)
0xd1108d19a78d9ea727fbc5a36a924e1cedd0de34888d6fe82e7678899134ae08
26,440
abort_by_response
fari-token-mn
;; Copyright (c) 2021 by The Bitfari Foundation. ;; This file is part of Bitfari. ;; Bitfari is free software. You may redistribute or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License or ;; (at your option) any later version. ;; Bitfari is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY, including without the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with Bitfari. If not, see <http://www.gnu.org/licenses/>. ;; This file contains the code to implement the Fari Token as specified in https://bitfari.org/token/ ;; The token allows for discounted advertising, network governance and deep-discount shopping. ;; Part shopping club token, part loyalty card and part advertising token, Bitfari implements a novel idea: ;; all customers are marketers, discounts contribute to loyalty, and, all token holders should have a say in the ;; destiny of the network. ;; The following token code is to be considered immutable with the exception of any required emergency adjustments. ;; --------------------------------------------------------------------------------------------------------------------- ;; implement the `ft-trait` trait defined in the `ft-trait` contract ;; trait in testnet ;;(impl-trait 'ST3FYGS9F88Y5FW2DT2Q5C7FVX99Y9HREGCXH5T9D.ft-trait.sip-010-trait) ;; trait in mainnet (impl-trait 'SP213KNHB5QD308TEESY1ZMX1BP8EZDPG4JWD0MEA.sip-010-trait.sip-010-trait) ;; limit supply to 100M Faris (define-constant MAX_MINT u10000000000000000) ;; max six decimal places (define-constant DECIMAL_PLACES u8) ;; errors (define-constant ERR_NO_VOID_MINT u1000) (define-constant ERR_NO_AUTH_MINT u1002) (define-constant ERR_NO_MORE_MINT u1004) (define-constant ERR_INVALID_TRANSFER u1008) ;; name the token (define-fungible-token fari) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (begin (ok (ft-get-balance fari owner)))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply fari))) ;; returns the token name (define-read-only (get-name) (ok "Fari")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FARI")) ;; the number of decimals used (define-read-only (get-decimals) (ok DECIMAL_PLACES)) ;; get the token uri (define-public (get-token-uri) (ok (some u"https://bitfari.org/token/"))) ;; transfer tokens to a recipient, check for non-zero, valid transfers (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (> (ft-get-balance fari tx-sender) u0) (begin (if (is-eq tx-sender sender) (begin (try! (ft-transfer? fari amount sender recipient)) (print memo) (ok true) ) (err ERR_INVALID_TRANSFER))) (err u0))) ;; execute mining after sanitization (define-private (execute-mint (amount uint) (account principal)) (if (< MAX_MINT (+ (ft-get-supply fari) amount)) (err ERR_NO_MORE_MINT) (begin (try! (ft-mint? fari amount account)) (ok true)))) ;; mint new tokens, check minted amount is valid (define-private (validate-mint (amount uint) (account principal)) (if (<= amount u0) (err ERR_NO_VOID_MINT) (execute-mint amount account))) ;; validate minting auth, amount, supply and then execute (define-public (mint (amount uint) (account principal)) (if ;; auth minter for testnet ;;(is-eq tx-sender 'ST3FYGS9F88Y5FW2DT2Q5C7FVX99Y9HREGCXH5T9D) ;; auth minter for mainnet (is-eq tx-sender 'SP213KNHB5QD308TEESY1ZMX1BP8EZDPG4JWD0MEA) (validate-mint amount account) (err ERR_NO_AUTH_MINT))) ;; initialize the foundation treasury ;; mint dev fund, investor fund, marketing fund, advisor fund, and grant fund ;; 80M tokens to be minted to the community ;; find the token allocations and distribution schedule at https://bitfari.org/token/ ;; testnet mint - to be distributed over 3 years ;;(mint u2000000000000000 'ST22CGGASNT176J764WXGD5ZKDK2ZEQ6XNRC660DX) ;; mainnet mint - to be distributed over 3 years (mint u2000000000000000 'SP213KNHB5QD308TEESY1ZMX1BP8EZDPG4JWD0MEA)
0x0eb14df269fd61ecbd0c660475c0f51b34e990e9ab40f3d134114c32112ff828
26,442
success
helpful-amaranth-orangutan
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP2D2AM91EMBT8G4NZ0WBQ4GVEFYDTRK5M1HM1XFZ) (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)))
0x31aca3eb9a512fe28322f91a653f66810e7e8872b30876b7f96e212be422ce14
26,499
success
unusual-fuchsia-hawk
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP297VG59W96DPGBT13SGD542QE1XS954X78Z75G0) (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)))
0xdbef7a7c306685511ac37ad1b31bef43c1fe428c3058cdef77f9230101c19247
26,505
success
gastric-rose-mule
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP17YZQB1228EK9MPHQXA8GC4G3HVWZ66X7VRPMAX) (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)))
0xa1ce35d2567ef5d5d6da52a65fb174c121d55a2e459ebad1583d3f847eb993eb
26,507
success
genetic-orange-halibut
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP297VG59W96DPGBT13SGD542QE1XS954X78Z75G0) (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)))
0x74efcb24a2580331dea47e4a56fb96611e7b0481e96878bbf8dee6f8f48c6b67
26,507
success
specified-jade-canidae
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP34N5WWPHWTVJVYPE368HYDEXMZWKPVF639B3P5T) (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)))
0xc5790d934825940e6b571d43d06026355cf26e2c6f49ca60ffae87500f4c0b65
26,564
success
arrogant-emerald-frog
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP34N5WWPHWTVJVYPE368HYDEXMZWKPVF639B3P5T) (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)))
0x68ed94c01a067ab63cf6e24a82b177eed9bb47d0e61318aca9a147e2a18e4ee5
26,564
success
specified-jade-canidae
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP34N5WWPHWTVJVYPE368HYDEXMZWKPVF639B3P5T) (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)))
0xc5790d934825940e6b571d43d06026355cf26e2c6f49ca60ffae87500f4c0b65
26,565
success
arrogant-emerald-frog
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP34N5WWPHWTVJVYPE368HYDEXMZWKPVF639B3P5T) (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)))
0x68ed94c01a067ab63cf6e24a82b177eed9bb47d0e61318aca9a147e2a18e4ee5
26,565
success
stacks-punks-v2
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet (impl-trait .nft-trait.nft-trait) (define-non-fungible-token stacks-punks uint) ;; constants (define-constant PUNK-IMAGE-HASH u"345a94125abb0a209a57943ffe043d101e810dbf52d08c892b4718613c867798") (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-ALL-MINTED u101) (define-constant ERR-COOLDOWN u102) (define-constant CONTRACT-OWNER tx-sender) ;; variables (define-data-var punk-index uint u0) (define-data-var punk-counter uint u0) (define-data-var token-uri (string-ascii 256) "") (define-data-var token-punk-uri (string-ascii 256) "") (define-map punks { id: uint } { minted: bool }) (define-map punks-by-owner { owner: principal } { ids: (list 2500 uint) }) (define-data-var removing-punk-id uint u0) (define-data-var cost-per-mint uint u10000000) (define-data-var rotation uint u0) ;; public functions (define-public (claim-v1-punks) (let ( (punk-ids (unwrap-panic (contract-call? .stacks-punks get-punks-by-owner tx-sender))) ) (map migrate-v1 punk-ids) (ok true) ) ) (define-public (mint) (let ( (count (var-get punk-counter)) (rot (var-get rotation)) (index (var-get punk-index)) (next-index (+ (* rot u2000) index)) ) (asserts! (<= count u10000) (err ERR-ALL-MINTED)) (if (is-eq none (unwrap-panic (contract-call? .stacks-punks get-owner next-index))) (begin (try! (mint-with-id next-index)) (try! (set-next-rotation)) (claim-v1-punks) ) (begin (try! (mint-with-id (+ u1 next-index))) (try! (set-next-rotation)) (claim-v1-punks) ) ) ) ) (define-private (set-next-rotation) (let ( (rot (var-get rotation)) ) (if true (begin (if (< rot u4) (var-set rotation (+ u1 rot)) (begin (var-set rotation u0) (var-set punk-index (+ u1 (var-get punk-index))) ) ) (ok true) ) (err u0) ) ) ) (define-private (mint-with-id (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (match (stx-transfer? (var-get cost-per-mint) tx-sender (as-contract tx-sender)) success (begin (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) error (err error) ) ) ) (define-private (migrate-v1 (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (try! (contract-call? .stacks-punks burn random-punk-id)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) ) (define-read-only (get-punks-entry-by-owner (owner principal)) (default-to { ids: (list u0) } (map-get? punks-by-owner { owner: owner }) ) ) (define-public (get-punks-by-owner (owner principal)) (ok (get ids (get-punks-entry-by-owner owner))) ) (define-public (burn (index uint)) (if (is-owner index tx-sender) (match (nft-burn? stacks-punks index tx-sender) success (ok true) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-public (transfer (index uint) (owner principal) (recipient principal)) (if (and (is-owner index owner) (is-owner index tx-sender)) (match (nft-transfer? stacks-punks index owner recipient) success (let ((punk-ids (unwrap-panic (get-punks-by-owner recipient)))) (map-set punks-by-owner { owner: recipient } { ids: (unwrap-panic (as-max-len? (append punk-ids index) u2500)) } ) (try! (remove-punk owner index)) (ok true) ) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-private (remove-punk (owner principal) (punk-id uint)) (if true (let ((punk-ids (unwrap-panic (get-punks-by-owner owner)))) (var-set removing-punk-id punk-id) (map-set punks-by-owner { owner: owner } { ids: (unwrap-panic (as-max-len? (filter remove-transferred-punk punk-ids) u2500)) } ) (ok true) ) (err u0) ) ) (define-private (remove-transferred-punk (punk-id uint)) (if (is-eq punk-id (var-get removing-punk-id)) false true ) ) (define-read-only (get-last-token-id) (ok (var-get punk-counter)) ) (define-public (set-cost-per-mint (value uint)) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set cost-per-mint value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-punk-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-punk-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-read-only (get-token-uri (id uint)) (if (not (is-eq id u0)) (ok (some (var-get token-punk-uri))) (ok (some (var-get token-uri))) ) ) (define-public (get-owner (index uint)) (ok (nft-get-owner? stacks-punks index)) ) (define-read-only (stx-balance) (stx-get-balance (as-contract tx-sender)) ) (define-read-only (stx-balance-of (address principal)) (stx-get-balance address) ) (define-public (transfer-stx (address principal) (amount uint)) (if (is-eq tx-sender CONTRACT-OWNER) (as-contract (stx-transfer? amount (as-contract tx-sender) address)) (err ERR-NOT-AUTHORIZED) ) ) ;; private functions (define-private (is-owner (index uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? stacks-punks index) false)) ) ;; initialize (var-set token-punk-uri "https://www.stackspunks.com/assets/punks.json") (var-set token-uri "https://www.stackspunks.com/assets/punks.json")
0x20497cb04e2097f581af49239aad28e4f6e28621602381b768c896a51fc95ff9
26,644
abort_by_response
claim-stacking-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)))
0xad2eca7b3b8e7d5886764fda602618a3422babe5205ca8ca5c8b52cf4694b08a
26,650
success
exotic-coral-iguana
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP17YZQB1228EK9MPHQXA8GC4G3HVWZ66X7VRPMAX) (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)))
0xa307dbf58eebec7c73863edb6619228d7e2e78303266a0cbc5da28bf6e236b5d
26,653
success
stacks-punks-v2
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet (impl-trait .nft-trait.nft-trait) (define-non-fungible-token stacks-punks uint) ;; constants (define-constant PUNK-IMAGE-HASH u"345a94125abb0a209a57943ffe043d101e810dbf52d08c892b4718613c867798") (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-ALL-MINTED u101) (define-constant ERR-COOLDOWN u102) (define-constant CONTRACT-OWNER tx-sender) ;; variables (define-data-var punk-index uint u0) (define-data-var punk-counter uint u0) (define-data-var token-uri (string-ascii 256) "") (define-data-var token-punk-uri (string-ascii 256) "") (define-map punks { id: uint } { minted: bool }) (define-map punks-by-owner { owner: principal } { ids: (list 2500 uint) }) (define-data-var removing-punk-id uint u0) (define-data-var cost-per-mint uint u10000000) (define-data-var rotation uint u0) ;; public functions (define-public (claim-v1-punks) (let ( (punk-ids (unwrap! (contract-call? .stacks-punks get-punks-by-owner tx-sender) (err u0))) ) (map migrate-v1 punk-ids) (ok true) ) ) (define-public (mint) (let ( (count (var-get punk-counter)) (rot (var-get rotation)) (index (var-get punk-index)) (next-index (+ (* rot u2000) index)) ) (asserts! (<= count u10000) (err ERR-ALL-MINTED)) (if (is-eq none (unwrap-panic (contract-call? .stacks-punks get-owner next-index))) (begin (try! (mint-with-id next-index)) (try! (set-next-rotation)) (claim-v1-punks) ) (begin (try! (mint-with-id (+ u1 next-index))) (try! (set-next-rotation)) (claim-v1-punks) ) ) ) ) (define-private (set-next-rotation) (let ( (rot (var-get rotation)) ) (if true (begin (if (< rot u4) (var-set rotation (+ u1 rot)) (begin (var-set rotation u0) (var-set punk-index (+ u1 (var-get punk-index))) ) ) (ok true) ) (err u0) ) ) ) (define-private (mint-with-id (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (match (stx-transfer? (var-get cost-per-mint) tx-sender (as-contract tx-sender)) success (begin (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) error (err error) ) ) ) (define-private (migrate-v1 (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (try! (contract-call? .stacks-punks burn random-punk-id)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) ) (define-read-only (get-punks-entry-by-owner (owner principal)) (default-to { ids: (list ) } (map-get? punks-by-owner { owner: owner }) ) ) (define-public (get-punks-by-owner (owner principal)) (ok (get ids (get-punks-entry-by-owner owner))) ) (define-public (burn (index uint)) (if (is-owner index tx-sender) (match (nft-burn? stacks-punks index tx-sender) success (ok true) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-public (transfer (index uint) (owner principal) (recipient principal)) (if (and (is-owner index owner) (is-owner index tx-sender)) (match (nft-transfer? stacks-punks index owner recipient) success (let ((punk-ids (unwrap-panic (get-punks-by-owner recipient)))) (map-set punks-by-owner { owner: recipient } { ids: (unwrap-panic (as-max-len? (append punk-ids index) u2500)) } ) (try! (remove-punk owner index)) (ok true) ) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-private (remove-punk (owner principal) (punk-id uint)) (if true (let ((punk-ids (unwrap-panic (get-punks-by-owner owner)))) (var-set removing-punk-id punk-id) (map-set punks-by-owner { owner: owner } { ids: (unwrap-panic (as-max-len? (filter remove-transferred-punk punk-ids) u2500)) } ) (ok true) ) (err u0) ) ) (define-private (remove-transferred-punk (punk-id uint)) (if (is-eq punk-id (var-get removing-punk-id)) false true ) ) (define-read-only (get-last-token-id) (ok (var-get punk-counter)) ) (define-public (set-cost-per-mint (value uint)) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set cost-per-mint value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-punk-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-punk-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-read-only (get-token-uri (id uint)) (if (not (is-eq id u0)) (ok (some (var-get token-punk-uri))) (ok (some (var-get token-uri))) ) ) (define-public (get-owner (index uint)) (ok (nft-get-owner? stacks-punks index)) ) (define-read-only (stx-balance) (stx-get-balance (as-contract tx-sender)) ) (define-read-only (stx-balance-of (address principal)) (stx-get-balance address) ) (define-public (transfer-stx (address principal) (amount uint)) (if (is-eq tx-sender CONTRACT-OWNER) (as-contract (stx-transfer? amount (as-contract tx-sender) address)) (err ERR-NOT-AUTHORIZED) ) ) ;; private functions (define-private (is-owner (index uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? stacks-punks index) false)) ) ;; initialize (var-set token-punk-uri "https://www.stackspunks.com/assets/punks.json") (var-set token-uri "https://www.stackspunks.com/assets/punks.json")
0x7e0a5b5876524aafa5c2ea55e7aebb7ad83f776bf19f21b638a41554ac9f6016
26,653
abort_by_response
cooperative-orange-crow
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP17YZQB1228EK9MPHQXA8GC4G3HVWZ66X7VRPMAX) (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)))
0xc39d04b844679e14023e6abd55c11d41329e05e29053b01910d58c4272bb81f6
26,653
success
stacks-punks-v2
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) (define-non-fungible-token stacks-punks uint) ;; constants (define-constant PUNK-IMAGE-HASH u"345a94125abb0a209a57943ffe043d101e810dbf52d08c892b4718613c867798") (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-ALL-MINTED u101) (define-constant ERR-COOLDOWN u102) (define-constant CONTRACT-OWNER tx-sender) ;; variables (define-data-var punk-index uint u0) (define-data-var punk-counter uint u0) (define-data-var token-uri (string-ascii 256) "") (define-data-var token-punk-uri (string-ascii 256) "") (define-map punks { id: uint } { minted: bool }) (define-map punks-by-owner { owner: principal } { ids: (list 2500 uint) }) (define-data-var removing-punk-id uint u0) (define-data-var cost-per-mint uint u10000000) (define-data-var rotation uint u0) ;; public functions (define-public (claim-v1-punks) (let ( (punk-ids (unwrap! (contract-call? .stacks-punks get-punks-by-owner tx-sender) (err u0))) ) (map migrate-v1 punk-ids) (ok true) ) ) (define-public (mint) (let ( (count (var-get punk-counter)) (rot (var-get rotation)) (index (var-get punk-index)) (next-index (+ (* rot u2000) index)) ) (asserts! (<= count u10000) (err ERR-ALL-MINTED)) (if (is-eq none (unwrap-panic (contract-call? .stacks-punks get-owner next-index))) (begin (try! (mint-with-id next-index)) (try! (set-next-rotation)) (claim-v1-punks) ) (begin (try! (mint-with-id (+ u1 next-index))) (try! (set-next-rotation)) (claim-v1-punks) ) ) ) ) (define-private (set-next-rotation) (let ( (rot (var-get rotation)) ) (if true (begin (if (< rot u4) (var-set rotation (+ u1 rot)) (begin (var-set rotation u0) (var-set punk-index (+ u1 (var-get punk-index))) ) ) (ok true) ) (err u0) ) ) ) (define-private (mint-with-id (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (match (stx-transfer? (var-get cost-per-mint) tx-sender (as-contract tx-sender)) success (begin (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) error (err error) ) ) ) (define-private (migrate-v1 (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (try! (contract-call? .stacks-punks burn random-punk-id)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) ) (define-read-only (get-punks-entry-by-owner (owner principal)) (default-to { ids: (list ) } (map-get? punks-by-owner { owner: owner }) ) ) (define-public (get-punks-by-owner (owner principal)) (ok (get ids (get-punks-entry-by-owner owner))) ) (define-public (burn (index uint)) (if (is-owner index tx-sender) (match (nft-burn? stacks-punks index tx-sender) success (ok true) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-public (transfer (index uint) (owner principal) (recipient principal)) (if (and (is-owner index owner) (is-owner index tx-sender)) (match (nft-transfer? stacks-punks index owner recipient) success (let ((punk-ids (unwrap-panic (get-punks-by-owner recipient)))) (map-set punks-by-owner { owner: recipient } { ids: (unwrap-panic (as-max-len? (append punk-ids index) u2500)) } ) (try! (remove-punk owner index)) (ok true) ) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-private (remove-punk (owner principal) (punk-id uint)) (if true (let ((punk-ids (unwrap-panic (get-punks-by-owner owner)))) (var-set removing-punk-id punk-id) (map-set punks-by-owner { owner: owner } { ids: (unwrap-panic (as-max-len? (filter remove-transferred-punk punk-ids) u2500)) } ) (ok true) ) (err u0) ) ) (define-private (remove-transferred-punk (punk-id uint)) (if (is-eq punk-id (var-get removing-punk-id)) false true ) ) (define-read-only (get-last-token-id) (ok (var-get punk-counter)) ) (define-public (set-cost-per-mint (value uint)) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set cost-per-mint value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-punk-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-punk-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-read-only (get-token-uri (id uint)) (if (not (is-eq id u0)) (ok (some (var-get token-punk-uri))) (ok (some (var-get token-uri))) ) ) (define-public (get-owner (index uint)) (ok (nft-get-owner? stacks-punks index)) ) (define-read-only (stx-balance) (stx-get-balance (as-contract tx-sender)) ) (define-read-only (stx-balance-of (address principal)) (stx-get-balance address) ) (define-public (transfer-stx (address principal) (amount uint)) (if (is-eq tx-sender CONTRACT-OWNER) (as-contract (stx-transfer? amount (as-contract tx-sender) address)) (err ERR-NOT-AUTHORIZED) ) ) ;; private functions (define-private (is-owner (index uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? stacks-punks index) false)) ) ;; initialize (var-set token-punk-uri "https://www.stackspunks.com/assets/punks.json") (var-set token-uri "https://www.stackspunks.com/assets/punks.json")
0x9f9548ea2d29b8c9570e1b7dc1b2aa1d4066e281e48877a79f83c163ef95db05
26,658
success
intelligent-silver-tahr
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP17YZQB1228EK9MPHQXA8GC4G3HVWZ66X7VRPMAX) (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)))
0xfab436a2e3744ec048015e30496fcfe1dd68fc31d3f4aece30dfada8daa4096b
26,659
success
stacks-punks-v3
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet (impl-trait .nft-trait.nft-trait) (define-non-fungible-token stacks-punks uint) ;; constants (define-constant PUNK-IMAGE-HASH u"345a94125abb0a209a57943ffe043d101e810dbf52d08c892b4718613c867798") (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-ALL-MINTED u101) (define-constant ERR-COOLDOWN u102) (define-constant CONTRACT-OWNER tx-sender) ;; variables (define-data-var punk-index uint u0) (define-data-var punk-counter uint u0) (define-data-var token-uri (string-ascii 256) "") (define-data-var token-punk-uri (string-ascii 256) "") (define-map punks { id: uint } { minted: bool }) (define-map punks-by-owner { owner: principal } { ids: (list 2500 uint) }) (define-data-var removing-punk-id uint u0) (define-data-var cost-per-mint uint u10000000) (define-data-var rotation uint u0) ;; public functions (define-public (claim-v1-punks) (let ( (punk-ids (unwrap! (contract-call? .stacks-punks get-punks-by-owner tx-sender) (err u0))) ) (map migrate-v1 punk-ids) (ok true) ) ) (define-public (mint (punk-id uint)) (let ( (count (var-get punk-counter)) (rot (var-get rotation)) (index (var-get punk-index)) (next-index (+ (* rot u2000) index)) ) (asserts! (<= count u10000) (err ERR-ALL-MINTED)) (if (> (var-get punk-counter) u9500) (begin (try! (mint-with-id next-index)) (try! (set-next-rotation)) (claim-v1-punks) ) (begin (if (and (is-eq none (unwrap-panic (contract-call? .stacks-punks get-owner next-index))) (is-eq none (unwrap-panic (get-owner next-index))) ) (begin (try! (mint-with-id next-index)) (try! (set-next-rotation)) (claim-v1-punks) ) (begin (try! (set-next-rotation)) (claim-v1-punks) ) ) ) ) ) ) (define-private (set-next-rotation) (let ( (rot (var-get rotation)) ) (if true (begin (if (< rot u4) (var-set rotation (+ u1 rot)) (begin (var-set rotation u0) (var-set punk-index (+ u1 (var-get punk-index))) ) ) (ok true) ) (err u0) ) ) ) (define-private (mint-with-id (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (match (stx-transfer? (var-get cost-per-mint) tx-sender (as-contract tx-sender)) success (begin (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) error (err error) ) ) ) (define-private (migrate-v1 (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (try! (contract-call? .stacks-punks burn random-punk-id)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) ) (define-read-only (get-punks-entry-by-owner (owner principal)) (default-to { ids: (list ) } (map-get? punks-by-owner { owner: owner }) ) ) (define-public (get-punks-by-owner (owner principal)) (ok (get ids (get-punks-entry-by-owner owner))) ) (define-public (burn (index uint)) (if (is-owner index tx-sender) (match (nft-burn? stacks-punks index tx-sender) success (ok true) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-public (transfer (index uint) (owner principal) (recipient principal)) (if (and (is-owner index owner) (is-owner index tx-sender)) (match (nft-transfer? stacks-punks index owner recipient) success (let ((punk-ids (unwrap-panic (get-punks-by-owner recipient)))) (map-set punks-by-owner { owner: recipient } { ids: (unwrap-panic (as-max-len? (append punk-ids index) u2500)) } ) (try! (remove-punk owner index)) (ok true) ) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-private (remove-punk (owner principal) (punk-id uint)) (if true (let ((punk-ids (unwrap-panic (get-punks-by-owner owner)))) (var-set removing-punk-id punk-id) (map-set punks-by-owner { owner: owner } { ids: (unwrap-panic (as-max-len? (filter remove-transferred-punk punk-ids) u2500)) } ) (ok true) ) (err u0) ) ) (define-private (remove-transferred-punk (punk-id uint)) (if (is-eq punk-id (var-get removing-punk-id)) false true ) ) (define-read-only (get-last-token-id) (ok (var-get punk-counter)) ) (define-public (set-cost-per-mint (value uint)) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set cost-per-mint value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-punk-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-punk-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-read-only (get-token-uri (id uint)) (if (not (is-eq id u0)) (ok (some (var-get token-punk-uri))) (ok (some (var-get token-uri))) ) ) (define-public (get-owner (index uint)) (ok (nft-get-owner? stacks-punks index)) ) (define-read-only (stx-balance) (stx-get-balance (as-contract tx-sender)) ) (define-read-only (stx-balance-of (address principal)) (stx-get-balance address) ) (define-public (transfer-stx (address principal) (amount uint)) (if (is-eq tx-sender CONTRACT-OWNER) (as-contract (stx-transfer? amount (as-contract tx-sender) address)) (err ERR-NOT-AUTHORIZED) ) ) ;; private functions (define-private (is-owner (index uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? stacks-punks index) false)) ) ;; initialize (var-set token-punk-uri "https://www.stackspunks.com/assets/punks.json") (var-set token-uri "https://www.stackspunks.com/assets/punks.json")
0xc30756cb6c246bbd0da11c2783bec9b1079d1c4cc54f0fe863dc6755076e8ed3
26,662
abort_by_response
stacks-punks-v3
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) (define-non-fungible-token stacks-punks uint) ;; constants (define-constant PUNK-IMAGE-HASH u"345a94125abb0a209a57943ffe043d101e810dbf52d08c892b4718613c867798") (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-ALL-MINTED u101) (define-constant ERR-COOLDOWN u102) (define-constant CONTRACT-OWNER tx-sender) ;; variables (define-data-var punk-index uint u0) (define-data-var punk-counter uint u0) (define-data-var token-uri (string-ascii 256) "") (define-data-var token-punk-uri (string-ascii 256) "") (define-map punks { id: uint } { minted: bool }) (define-map punks-by-owner { owner: principal } { ids: (list 2500 uint) }) (define-data-var removing-punk-id uint u0) (define-data-var cost-per-mint uint u10000000) (define-data-var rotation uint u0) ;; public functions (define-public (claim-v1-punks) (let ( (punk-ids (unwrap! (contract-call? .stacks-punks get-punks-by-owner tx-sender) (err u0))) ) (map migrate-v1 punk-ids) (ok true) ) ) (define-public (mint (punk-id uint)) (let ( (count (var-get punk-counter)) (rot (var-get rotation)) (index (var-get punk-index)) (next-index (+ (* rot u2000) index)) ) (asserts! (<= count u10000) (err ERR-ALL-MINTED)) (if (> (var-get punk-counter) u9500) (begin (try! (mint-with-id next-index)) (try! (set-next-rotation)) (claim-v1-punks) ) (begin (if (and (is-eq none (unwrap-panic (contract-call? .stacks-punks get-owner next-index))) (is-eq none (unwrap-panic (get-owner next-index))) ) (begin (try! (mint-with-id next-index)) (try! (set-next-rotation)) (claim-v1-punks) ) (begin (try! (set-next-rotation)) (claim-v1-punks) ) ) ) ) ) ) (define-private (set-next-rotation) (let ( (rot (var-get rotation)) ) (if true (begin (if (< rot u4) (var-set rotation (+ u1 rot)) (begin (var-set rotation u0) (var-set punk-index (+ u1 (var-get punk-index))) ) ) (ok true) ) (err u0) ) ) ) (define-private (mint-with-id (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (match (stx-transfer? (var-get cost-per-mint) tx-sender (as-contract tx-sender)) success (begin (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) error (err error) ) ) ) (define-private (migrate-v1 (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (try! (contract-call? .stacks-punks burn random-punk-id)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u2500)) } ) (ok random-punk-id) ) ) (define-read-only (get-punks-entry-by-owner (owner principal)) (default-to { ids: (list ) } (map-get? punks-by-owner { owner: owner }) ) ) (define-public (get-punks-by-owner (owner principal)) (ok (get ids (get-punks-entry-by-owner owner))) ) (define-public (burn (index uint)) (if (is-owner index tx-sender) (match (nft-burn? stacks-punks index tx-sender) success (ok true) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-public (transfer (index uint) (owner principal) (recipient principal)) (if (and (is-owner index owner) (is-owner index tx-sender)) (match (nft-transfer? stacks-punks index owner recipient) success (let ((punk-ids (unwrap-panic (get-punks-by-owner recipient)))) (map-set punks-by-owner { owner: recipient } { ids: (unwrap-panic (as-max-len? (append punk-ids index) u2500)) } ) (try! (remove-punk owner index)) (ok true) ) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-private (remove-punk (owner principal) (punk-id uint)) (if true (let ((punk-ids (unwrap-panic (get-punks-by-owner owner)))) (var-set removing-punk-id punk-id) (map-set punks-by-owner { owner: owner } { ids: (unwrap-panic (as-max-len? (filter remove-transferred-punk punk-ids) u2500)) } ) (ok true) ) (err u0) ) ) (define-private (remove-transferred-punk (punk-id uint)) (if (is-eq punk-id (var-get removing-punk-id)) false true ) ) (define-read-only (get-last-token-id) (ok (var-get punk-counter)) ) (define-public (set-cost-per-mint (value uint)) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set cost-per-mint value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-punk-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-punk-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-read-only (get-token-uri (id uint)) (if (not (is-eq id u0)) (ok (some (var-get token-punk-uri))) (ok (some (var-get token-uri))) ) ) (define-public (get-owner (index uint)) (ok (nft-get-owner? stacks-punks index)) ) (define-read-only (stx-balance) (stx-get-balance (as-contract tx-sender)) ) (define-read-only (stx-balance-of (address principal)) (stx-get-balance address) ) (define-public (transfer-stx (address principal) (amount uint)) (if (is-eq tx-sender CONTRACT-OWNER) (as-contract (stx-transfer? amount (as-contract tx-sender) address)) (err ERR-NOT-AUTHORIZED) ) ) ;; private functions (define-private (is-owner (index uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? stacks-punks index) false)) ) ;; initialize (var-set token-punk-uri "https://www.stackspunks.com/assets/punks.json") (var-set token-uri "https://www.stackspunks.com/assets/punks.json")
0x9f0b6777365d4d04fbe9f6c68cbdb84e41bb8c023adca8e38ebe48c6c2d685e2
26,664
success
bitcoin-clar-v1
;; Error codes (define-constant ERR-OUT-OF-BOUNDS u1) (define-constant ERR-TOO-MANY-TXINS u2) (define-constant ERR-TOO-MANY-TXOUTS u3) (define-constant ERR-VARSLICE-TOO-LONG u4) (define-constant ERR-BAD-HEADER u5) (define-constant ERR-PROOF-TOO-SHORT u6) ;; 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 )) ;; List with 512 items, used for folding something 512 times (define-constant LIST_512 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 256 items, used for folding something 256 times (define-constant LIST_256 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 128 items, used for folding something 128 times (define-constant LIST_128 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 64 items, used for folding something 64 times (define-constant LIST_64 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 32 items, used for folding something 32 times (define-constant LIST_32 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 16 items, used for folding something 16 times (define-constant LIST_16 (list true true true true true true true true true true true true true true true true )) ;; Convert a 1-byte buff into a uint. (define-read-only (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte))) ;; Append a byte at the given index in the given data to acc. (define-read-only (inner-read-slice-1024 (ignored bool) (input { acc: (buff 1024), data: (buff 1024), index: uint })) (let ( (acc (get acc input)) (data (get data input)) (ctr (get index input)) (byte (unwrap-panic (element-at data ctr))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u1024)), data: data, index: (+ u1 ctr) }) ) ;; Read 512 bytes from data, starting at index. Return the 512-byte slice. (define-read-only (read-slice-512 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_512 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 256 bytes from data, starting at index. Return the 256-byte slice. (define-read-only (read-slice-256 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_256 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 128 bytes from data, starting at index. Return the 128-byte slice. (define-read-only (read-slice-128 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_128 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 64 bytes from data, starting at index. Return the 64-byte slice. (define-read-only (read-slice-64 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_64 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 32 bytes from data, starting at index. Return the 32-byte slice. (define-read-only (read-slice-32 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_32 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 16 bytes from data, starting at index. Return the 16-byte slice. (define-read-only (read-slice-16 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_16 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 8 bytes from data, starting at index. Return the 8-byte slice. (define-read-only (read-slice-8 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 (list true true true true true true true true) { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 4 bytes from data, starting at index. Return the 4-byte slice. (define-read-only (read-slice-4 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 (list true true true true) { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 2 bytes from data, starting at index. Return the 2-byte slice. (define-read-only (read-slice-2 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 (list true true) { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 1 byte from data, starting at index. Return the 1-byte slice. (define-read-only (read-slice-1 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 (list true) { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read a fixed-sized chunk of data from a given buffer (up to remaining bytes), starting at index, and append it to acc. ;; chunk_size must be a power of 2, up to 1024 (define-read-only (inner-read-slice (chunk_size uint) (input { acc: (buff 1024), buffer: (buff 1024), index: uint, remaining: uint })) (let ( (ctr (get index input)) (remaining (get remaining input)) ) (if (is-eq u0 remaining) ;; done reading input (let ( (acc (get acc input)) (databuff (get buffer input)) ) (if (> chunk_size remaining) ;; chunk size too big for remainder, so just skip it. input ;; we have at least chunk_size bytes to read! ;; dispatch to the right fixed-size slice reader. (if (is-eq chunk_size u512) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-512 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u256) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-256 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u128) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-128 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u64) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-64 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u32) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-32 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u16) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-16 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u8) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-8 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u4) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-4 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u2) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-2 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u1) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-1 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } { acc: acc, buffer: databuff, index: ctr, remaining: remaining } )))))))))) )) )) ) ;; Top-level function to read a slice of a given size from a given (buff 1024), starting at a given offset. ;; Returns (ok (buff 1024)) on success, and it contains "buff[offset..(offset+size)]" ;; Returns (err ERR-OUT-OF-BOUNDS) if the slice offset and/or size would copy a range of bytes outside the given buffer. (define-read-only (read-slice (data (buff 1024)) (offset uint) (size uint)) (if (or (>= offset (len data)) (> (+ offset size) (len data))) (err ERR-OUT-OF-BOUNDS) (begin (print "read slice") (print size) (ok (get acc (fold inner-read-slice (list u512 u256 u128 u64 u32 u16 u8 u4 u2 u1) { acc: 0x, buffer: data, index: offset, remaining: size })) ) ) ) ) ;; Reads the next two bytes from txbuff as a big-endian 16-bit integer, and updates the index. ;; Returns (ok { uint16: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff (define-read-only (read-uint16 (ctx { txbuff: (buff 1024), index: uint })) (let ( (data (get txbuff ctx)) (base (get index ctx)) (byte-1 (buff-to-u8 (unwrap! (element-at data base) (err ERR-OUT-OF-BOUNDS)))) (byte-2 (buff-to-u8 (unwrap! (element-at data (+ u1 base)) (err ERR-OUT-OF-BOUNDS)))) (ret (+ (* byte-2 u256) byte-1)) ) (begin (print "read uint16") (print ret) (ok { uint16: ret, ctx: { txbuff: data, index: (+ u2 base) } }) )) ) ;; Reads the next four bytes from txbuff as a big-endian 32-bit integer, and updates the index. ;; Returns (ok { uint32: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff (define-read-only (read-uint32 (ctx { txbuff: (buff 1024), index: uint })) (let ( (data (get txbuff ctx)) (base (get index ctx)) (byte-1 (buff-to-u8 (unwrap! (element-at data base) (err ERR-OUT-OF-BOUNDS)))) (byte-2 (buff-to-u8 (unwrap! (element-at data (+ u1 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-3 (buff-to-u8 (unwrap! (element-at data (+ u2 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-4 (buff-to-u8 (unwrap! (element-at data (+ u3 base)) (err ERR-OUT-OF-BOUNDS)))) (ret (+ (* byte-4 u16777216) (* byte-3 u65536) (* byte-2 u256) byte-1)) ) (begin (print "read uint32") (print ret) (ok { uint32: ret, ctx: { txbuff: data, index: (+ u4 base) } }) )) ) ;; Reads the next eight bytes from txbuff as a big-endian 64-bit integer, and updates the index. ;; Returns (ok { uint64: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff (define-read-only (read-uint64 (ctx { txbuff: (buff 1024), index: uint })) (let ( (data (get txbuff ctx)) (base (get index ctx)) (byte-1 (buff-to-u8 (unwrap! (element-at data base) (err ERR-OUT-OF-BOUNDS)))) (byte-2 (buff-to-u8 (unwrap! (element-at data (+ u1 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-3 (buff-to-u8 (unwrap! (element-at data (+ u2 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-4 (buff-to-u8 (unwrap! (element-at data (+ u3 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-5 (buff-to-u8 (unwrap! (element-at data (+ u4 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-6 (buff-to-u8 (unwrap! (element-at data (+ u5 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-7 (buff-to-u8 (unwrap! (element-at data (+ u6 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-8 (buff-to-u8 (unwrap! (element-at data (+ u7 base)) (err ERR-OUT-OF-BOUNDS)))) (ret (+ (* byte-8 u72057594037927936) (* byte-7 u281474976710656) (* byte-6 u1099511627776) (* byte-5 u4294967296) (* byte-4 u16777216) (* byte-3 u65536) (* byte-2 u256) byte-1)) ) (begin (print "read uint64") (print ret) (ok { uint64: ret, ctx: { txbuff: data, index: (+ u8 base) } }) )) ) ;; Reads the next varint from txbuff, and updates the index. ;; Returns (ok { varint: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. (define-read-only (read-varint (ctx { txbuff: (buff 1024), index: uint })) (let ( (ptr (get index ctx)) (tx (get txbuff ctx)) (byte (buff-to-u8 (unwrap! (element-at tx ptr) (err ERR-OUT-OF-BOUNDS)))) ) (if (<= byte u252) (begin (print "varint 1") (print byte) ;; given byte is the varint (ok { varint: byte, ctx: { txbuff: tx, index: (+ u1 ptr) }}) ) (if (is-eq byte u253) (let ( ;; next two bytes is the varint (parsed-u16 (try! (read-uint16 { txbuff: tx, index: (+ u1 ptr) }))) ) (begin (print "varint 2") (print (get uint16 parsed-u16)) (ok { varint: (get uint16 parsed-u16), ctx: (get ctx parsed-u16) }) )) (if (is-eq byte u254) (let ( ;; next four bytes is the varint (parsed-u32 (try! (read-uint32 { txbuff: tx, index: (+ u1 ptr) }))) ) (begin (print "varint 4") (print (get uint32 parsed-u32)) (ok { varint: (get uint32 parsed-u32), ctx: (get ctx parsed-u32) }) )) (let ( ;; next eight bytes is the varint (parsed-u64 (try! (read-uint64 { txbuff: tx, index: (+ u1 ptr) }))) ) (begin (print "varint 8") (print (get uint64 parsed-u64)) (ok { varint: (get uint64 parsed-u64), ctx: (get ctx parsed-u64) }) )) ) ) )) ) ;; Reads a varint-prefixed byte slice from txbuff, and updates the index to point to the byte after the varint and slice. ;; Returns (ok { varslice: (buff 1024), ctx: { txbuff: (buff 1024), index: uint } }) on success, where varslice has the length of the varint prefix. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. (define-read-only (read-varslice (old-ctx { txbuff: (buff 1024), index: uint })) (let ( (parsed (try! (read-varint old-ctx))) (slice-len (get varint parsed)) (ctx (get ctx parsed)) (slice (try! (read-slice (get txbuff ctx) (get index ctx) slice-len))) ) (ok { varslice: slice, ctx: { txbuff: (get txbuff ctx), index: (+ (len slice) (get index ctx)) } })) ) ;; Generate a permutation of a given 32-byte buffer, appending the element at target-index to hash-output. ;; The target-index decides which index in hash-input gets appended to hash-output. (define-read-only (inner-buff32-permutation (target-index uint) (state { hash-input: (buff 32), hash-output: (buff 32) })) { hash-input: (get hash-input state), hash-output: (unwrap-panic (as-max-len? (concat (get hash-output state) (unwrap-panic (as-max-len? (unwrap-panic (element-at (get hash-input state) target-index)) u32))) u32)) } ) ;; Reverse the byte order of a 32-byte buffer. Returns the (buff 32). (define-read-only (reverse-buff32 (input (buff 32))) (get hash-output (fold inner-buff32-permutation (list u31 u30 u29 u28 u27 u26 u25 u24 u23 u22 u21 u20 u19 u18 u17 u16 u15 u14 u13 u12 u11 u10 u9 u8 u7 u6 u5 u4 u3 u2 u1 u0) { hash-input: input, hash-output: 0x })) ) ;; Reads a big-endian hash -- consume the next 32 bytes, and reverse them. ;; Returns (ok { hashslice: (buff 32), ctx: { txbuff: (buff 1024), index: uint } }) on success, and updates the index. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. (define-read-only (read-hashslice (old-ctx { txbuff: (buff 1024), index: uint })) (let ( (hash-le (unwrap-panic (as-max-len? (try! (read-slice (get txbuff old-ctx) (get index old-ctx) u32)) u32))) ) (ok { hashslice: (reverse-buff32 hash-le), ctx: { txbuff: (get txbuff old-ctx), index: (+ u32 (get index old-ctx)) } })) ) ;; Inner fold method to read the next tx input from txbuff. ;; The index in ctx will be updated to point to the next tx input if all goes well (or to the start of the outputs) ;; Returns (ok { ... }) on success. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptSig that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXINS) if there are more than eight inputs to read. (define-read-only (read-next-txin (ignored bool) (state-res (response { ctx: { txbuff: (buff 1024), index: uint }, remaining: uint, txins: (list 8 { outpoint: { hash: (buff 32), index: uint }, scriptSig: (buff 256), ;; just big enough to hold a 2-of-3 multisig script sequence: uint }) } uint))) (match state-res state (if (< u0 (get remaining state)) (let ( (remaining (get remaining state)) (ctx (get ctx state)) (parsed-hash (try! (read-hashslice ctx))) (parsed-index (try! (read-uint32 (get ctx parsed-hash)))) (parsed-scriptSig (try! (read-varslice (get ctx parsed-index)))) (parsed-sequence (try! (read-uint32 (get ctx parsed-scriptSig)))) (new-ctx (get ctx parsed-sequence)) ) (ok { ctx: new-ctx, remaining: (- remaining u1), txins: (unwrap! (as-max-len? (append (get txins state) { outpoint: { hash: (get hashslice parsed-hash), index: (get uint32 parsed-index) }, scriptSig: (unwrap! (as-max-len? (get varslice parsed-scriptSig) u256) (err ERR-VARSLICE-TOO-LONG)), sequence: (get uint32 parsed-sequence) }) u8) (err ERR-TOO-MANY-TXINS)) })) (ok state) ) error (err error) ) ) ;; Read a transaction's inputs. ;; Returns (ok { txins: (list { ... }), remaining: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success, and updates the index in ctx to point to the start of the tx outputs. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptSig that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXINS) if there are more than eight inputs to read. (define-read-only (read-txins (ctx { txbuff: (buff 1024), index: uint })) (let ( (parsed-num-txins (try! (read-varint ctx))) (num-txins (get varint parsed-num-txins)) (new-ctx (get ctx parsed-num-txins)) ) (if (> num-txins u8) (err ERR-TOO-MANY-TXINS) (fold read-next-txin (list true true true true true true true true) (ok { ctx: new-ctx, remaining: num-txins, txins: (list ) })) )) ) ;; Read the next transaction output, and update the index in ctx to point to the next output. ;; Returns (ok { ... }) on success ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptPubKey that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXOUTS) if there are more than eight outputs to read. (define-read-only (read-next-txout (ignored bool) (state-res (response { ctx: { txbuff: (buff 1024), index: uint }, remaining: uint, txouts: (list 8 { value: uint, scriptPubKey: (buff 128) }) } uint))) (match state-res state (if (< u0 (get remaining state)) (let ( (remaining (get remaining state)) (parsed-value (try! (read-uint64 (get ctx state)))) (parsed-script (try! (read-varslice (get ctx parsed-value)))) (new-ctx (get ctx parsed-script)) ) (ok { ctx: new-ctx, remaining: (- remaining u1), txouts: (unwrap! (as-max-len? (append (get txouts state) { value: (get uint64 parsed-value), scriptPubKey: (unwrap! (as-max-len? (get varslice parsed-script) u128) (err ERR-VARSLICE-TOO-LONG)) }) u8) (err ERR-TOO-MANY-TXOUTS)) })) (ok state) ) error (err error) ) ) ;; Read all transaction outputs in a transaction. Update the index to point to the first byte after the outputs, if all goes well. ;; Returns (ok { txouts: (list { ... }), remaining: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success, and updates the index in ctx to point to the start of the tx outputs. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptPubKey that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXOUTS) if there are more than eight outputs to read. (define-read-only (read-txouts (ctx { txbuff: (buff 1024), index: uint })) (let ( (parsed-num-txouts (try! (read-varint ctx))) (num-txouts (get varint parsed-num-txouts)) (new-ctx (get ctx parsed-num-txouts)) ) (if (> num-txouts u8) (err ERR-TOO-MANY-TXOUTS) (fold read-next-txout (list true true true true true true true true) (ok { ctx: new-ctx, remaining: num-txouts, txouts: (list ) })) )) ) ;; Parse a Bitcoin transaction, with up to 8 inputs and 8 outputs, with scriptSigs of up to 256 bytes each, and with scriptPubKeys up to 128 bytes. ;; Returns a tuple structured as follows on success: ;; (ok { ;; version: uint, ;; tx version ;; ins: (list 8 ;; { ;; outpoint: { ;; pointer to the utxo this input consumes ;; hash: (buff 32), ;; index: uint ;; }, ;; scriptSig: (buff 256), ;; spending condition script ;; sequence: uint ;; }), ;; outs: (list 8 ;; { ;; value: uint, ;; satoshis sent ;; scriptPubKey: (buff 128) ;; parse this to get an address ;; }), ;; locktime: uint ;; }) ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptPubKey or scriptSig that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXOUTS) if there are more than eight inputs to read. ;; Returns (err ERR-TOO-MANY-TXINS) if there are more than eight outputs to read. (define-read-only (parse-tx (tx (buff 1024))) (let ( (ctx { txbuff: tx, index: u0 }) (parsed-version (try! (read-uint32 ctx))) (parsed-txins (try! (read-txins (get ctx parsed-version)))) (parsed-txouts (try! (read-txouts (get ctx parsed-txins)))) (parsed-locktime (try! (read-uint32 (get ctx parsed-txouts)))) ) (ok { version: (get uint32 parsed-version), ins: (get txins parsed-txins), outs: (get txouts parsed-txouts), locktime: (get uint32 parsed-locktime) })) ) ;; Parse a Bitcoin block header. ;; Returns a tuple structured as folowed on success: ;; (ok { ;; version: uint, ;; block version, ;; parent: (buff 32), ;; parent block hash, ;; merkle-root: (buff 32), ;; merkle root for all this block's transactions ;; timestamp: uint, ;; UNIX epoch timestamp of this block, in seconds ;; nbits: uint, ;; compact block difficulty representation ;; nonce: uint ;; PoW solution ;; }) ;; Returns (err ERR-BAD-HEADER) if the header buffer isn't actually 80 bytes long. (define-read-only (parse-block-header (headerbuff (buff 80))) (let ( (ctx { txbuff: (unwrap! (as-max-len? headerbuff u1024) (err ERR-BAD-HEADER)), index: u0 }) ;; none of these should fail, since they're all fixed-length fields whose lengths sum to 80 (parsed-version (unwrap-panic (read-uint32 ctx))) (parsed-parent-hash (unwrap-panic (read-hashslice (get ctx parsed-version)))) (parsed-merkle-root (unwrap-panic (read-hashslice (get ctx parsed-parent-hash)))) (parsed-timestamp (unwrap-panic (read-uint32 (get ctx parsed-merkle-root)))) (parsed-nbits (unwrap-panic (read-uint32 (get ctx parsed-timestamp)))) (parsed-nonce (unwrap-panic (read-uint32 (get ctx parsed-nbits)))) ) (ok { version: (get uint32 parsed-version), parent: (get hashslice parsed-parent-hash), merkle-root: (get hashslice parsed-merkle-root), timestamp: (get uint32 parsed-timestamp), nbits: (get uint32 parsed-nbits), nonce: (get uint32 parsed-nonce) })) ) (define-read-only (get-bc-h-hash (bh uint)) (get-block-info? burnchain-header-hash bh)) ;; Verify that a block header hashes to a burnchain header hash at a given height. ;; Returns true if so; false if not. (define-read-only (verify-block-header (headerbuff (buff 80)) (expected-block-height uint)) (match (get-bc-h-hash expected-block-height) bhh (is-eq bhh (reverse-buff32 (sha256 (sha256 headerbuff)))) false )) ;; Get the txid of a transaction, but big-endian. ;; This is the reverse of what you see on block explorers. (define-read-only (get-reversed-txid (tx (buff 1024))) (sha256 (sha256 tx))) ;; Get the txid of a transaction. ;; This is what you see on block explorers. (define-read-only (get-txid (tx (buff 1024))) (reverse-buff32 (get-reversed-txid tx)) ) ;; Determine if the ith bit in a uint is set to 1 (define-read-only (is-bit-set (val uint) (bit uint)) (is-eq (mod (/ val (pow u2 bit)) u2) u1) ) ;; Verify the next step of a Merkle proof. ;; This hashes cur-hash against the ctr-th hash in proof-hashes, and uses that as the next cur-hash. ;; The path is a bitfield describing the walk from the txid up to the merkle root: ;; * if the ith bit is 0, then cur-hash is hashed before the next proof-hash (cur-hash is "left"). ;; * if the ith bit is 1, then the next proof-hash is hashed before cur-hash (cur-hash is "right"). ;; The proof verifies if cur-hash is equal to root-hash, and we're out of proof-hashes to check. (define-read-only (inner-merkle-proof-verify (ctr uint) (state { path: uint, root-hash: (buff 32), proof-hashes: (list 12 (buff 32)), tree-depth: uint, cur-hash: (buff 32), verified: bool })) (if (get verified state) state (if (>= ctr (get tree-depth state)) (begin (print "ctr exceeds proof length or tree depth") (print ctr) (print (get tree-depth state)) (print (len (get proof-hashes state))) (merge state { verified: false }) ) (let ( (path (get path state)) (is-left (is-bit-set path ctr)) (proof-hashes (get proof-hashes state)) (cur-hash (get cur-hash state)) (root-hash (get root-hash state)) (h1 (if is-left (unwrap-panic (element-at proof-hashes ctr)) cur-hash)) (h2 (if is-left cur-hash (unwrap-panic (element-at proof-hashes ctr)))) (next-hash (sha256 (sha256 (concat h1 h2)))) (is-verified (and (is-eq (+ u1 ctr) (len proof-hashes)) (is-eq next-hash root-hash))) ) (begin (print "cur-hash") (print cur-hash) (print "next-hash") (print h1) (print h2) (print next-hash) (merge state { cur-hash: next-hash, verified: is-verified }) )) ) ) ) ;; Verify a Merkle proof, given the _reversed_ txid of a transaction, the merkle root of its block, and a proof consisting of: ;; * The index in the block where the transaction can be found (starting from 0), ;; * The list of hashes that link the txid to the merkle root, ;; * The depth of the block's merkle tree (required because Bitcoin does not identify merkle tree nodes as being leaves or intermediates). ;; The _reversed_ txid is required because that's the order (big-endian) processes them in. ;; The tx-index is required because it tells us the left/right traversals we'd make if we were walking down the tree from root to transaction, ;; and is thus used to deduce the order in which to hash the intermediate hashes with one another to link the txid to the merkle root. ;; Returns (ok true) if the proof is valid. ;; Returns (ok false) if the proof is invalid. ;; Returns (err ERR-PROOF-TOO-SHORT) if the proof's hashes aren't long enough to link the txid to the merkle root. (define-read-only (verify-merkle-proof (reversed-txid (buff 32)) (merkle-root (buff 32)) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (if (> (get tree-depth proof) (len (get hashes proof))) (err ERR-PROOF-TOO-SHORT) (ok (get verified (fold inner-merkle-proof-verify (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11) { path: (+ (pow u2 (get tree-depth proof)) (get tx-index proof)), root-hash: merkle-root, proof-hashes: (get hashes proof), cur-hash: reversed-txid, tree-depth: (get tree-depth proof), verified: false })) ) ) ) ;; Top-level verification code to determine whether or not a Bitcoin transaction was mined in a prior Bitcoin block. ;; It takes the block header and block height, the transaction, and a merkle proof, and determines that: ;; * the block header corresponds to the block that was mined at the given Bitcoin height ;; * the transaction's merkle proof links it to the block header's merkle root. ;; The proof is a list of sibling merkle tree nodes that allow us to calculate the parent node from two children nodes in each merkle tree level, ;; the depth of the block's merkle tree, and the index in the block in which the given transaction can be found (starting from 0). ;; The first element in hashes must be the given transaction's sibling transaction's ID. This and the given transaction's txid are hashed to ;; calculate the parent hash in the merkle tree, which is then hashed with the *next* hash in the proof, and so on and so forth, until the final ;; hash can be compared against the block header's merkle root field. The tx-index tells us in which order to hash each pair of siblings. ;; Note that the proof hashes -- including the sibling txid -- must be _big-endian_ hashes, because this is how Bitcoin generates them. ;; This is the reverse of what you'd see in a block explorer! ;; Returns (ok true) if the proof checks out. ;; Returns (ok false) if not. ;; Returns (err ERR-PROOF-TOO-SHORT) if the proof doesn't contain enough intermediate hash nodes in the merkle tree. (define-read-only (was-tx-mined-compact (block { header: (buff 80), height: uint }) (tx (buff 1024)) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (if (verify-block-header (get header block) (get height block)) (verify-merkle-proof (get-reversed-txid tx) (reverse-buff32 (get merkle-root (try! (parse-block-header (get header block))))) proof) (ok false) ) ) (define-read-only (concat-header (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint })) (concat (concat (concat (concat (concat (get version block) (get parent block)) (get merkle-root block)) (get timestamp block)) (get nbits block)) (get nonce block)) ) (define-read-only (concat-var (buffer (buff 256))) (concat (unwrap-panic (element-at BUFF_TO_BYTE (len buffer))) buffer)) (define-read-only (concat-in (in {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}) (result (buff 1024))) (unwrap-panic (as-max-len? (concat (concat (concat (concat result (get hash (get outpoint in))) (get index (get outpoint in))) (concat-var (get scriptSig in))) (get sequence in)) u1024 ))) (define-read-only (concat-ins (ins (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}))) (unwrap-panic (as-max-len? (concat (unwrap-panic (element-at BUFF_TO_BYTE (len ins))) (fold concat-in ins 0x)) u1024))) (define-read-only (concat-out (out {value: (buff 8), scriptPubKey: (buff 128)}) (result (buff 1024))) (unwrap-panic (as-max-len? (concat (concat result (get value out)) (concat-var (get scriptPubKey out))) u1024))) (define-read-only (concat-outs (outs (list 8 {value: (buff 8), scriptPubKey: (buff 128)}))) (unwrap-panic (as-max-len? (concat (unwrap-panic (element-at BUFF_TO_BYTE (len outs))) (fold concat-out outs 0x)) u1024))) (define-read-only (concat-tx (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (unwrap-panic (as-max-len? (concat (concat (concat (get version tx) (concat-ins (get ins tx))) (concat-outs (get outs tx))) (get locktime tx)) u1024))) (define-read-only (was-tx-mined (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx (buff 1024)) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (if (verify-block-header (concat-header block) (get height block)) (verify-merkle-proof (get-reversed-txid tx) (get merkle-root block) proof) (err u1) ) )
0x27b8f3b48e4aa216136cda17bae6580cceaa9cfdff4a636d613f81708e14c89f
26,694
success
smart-blue-dingo
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP24PN7DKF24NABSP0E945JC16TZG4CT98PJ2D0YN) (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)))
0x507f99fcb936577167d08f44017e79a6e7f69bd53b522cf0f55d168b5834cb17
26,707
success
smart-blue-dingo
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP24PN7DKF24NABSP0E945JC16TZG4CT98PJ2D0YN) (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)))
0x507f99fcb936577167d08f44017e79a6e7f69bd53b522cf0f55d168b5834cb17
26,708
success
hilarious-white-wasp
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP2141M2N9PSB9JH27PQJW31A0N4XF5640HM0F62T) (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)))
0x31f2fbfbcc22464fe0fef6c6e1555f2baf04b91ef72304c55f7060ddd6e28bc3
26,739
success
hello-world
;; hello-world contract (define-public (say-hi) (ok "hello nsm5102") )
0x90b44cdfb7d7467ee2c78941c7a739304b04a729bafc64d0902f9321114a77c9
26,838
success
stacks-punks-bulk-transfer-v3
;; transfer up to 10 punks you own to a specified address (define-public (bulk-transfer (punk-ids (list 10 uint)) (address principal)) (let ( (result (fold transfer punk-ids { address: address, count: u0 })) ) (ok result) ) ) (define-private (transfer (punk-id uint) (data (tuple (address principal) (count uint)))) (begin (asserts! (is-ok (contract-call? .stacks-punks-v3 transfer punk-id tx-sender (get address data))) { address: (get address data), count: (get count data) } ) { address: (get address data), count: (+ u1 (get count data)) } ) )
0x85b3e420755c606b71f0875ba6d9767c8aa23af5a7bd091025024c4487d96847
26,950
success
fpwr-v04
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token wrapped-rewards) (define-constant admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-data-var reward-admin principal 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-map rewards-by-height uint {wrew: uint, ustx: uint}) (define-data-var last-price (tuple (amount uint) (height uint) (timestamp uint)) {amount: u2813, height: u23481, timestamp: u1627462508}) ;; total submitted btc rewards (define-read-only (get-rewards-by-height (height uint)) (ok (default-to {wrew: u0, ustx: u0} (map-get? rewards-by-height height)))) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (ok (ft-get-balance wrapped-rewards owner))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply wrapped-rewards))) ;; returns the token name (define-read-only (get-name) (ok "Friedger Pool wrapped rewards")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FPWR")) ;; the number of decimals used (define-read-only (get-decimals) (ok u8)) ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (is-eq tx-sender sender) (begin (try! (ft-transfer? wrapped-rewards amount sender recipient)) (print memo) (ok true) ) (err u4))) (define-public (get-token-uri) (ok (some u"https://pool.friedger.de/wrapped-rewards.json"))) ;; ;; mint via admin ;; (define-private (map-update-rewards-by-height (height uint) (wrew-value uint) (ustx-value uint)) (let ((rewards (map-get? rewards-by-height height))) (let ((wrew (default-to u0 (get wrew rewards))) (ustx (default-to u0 (get ustx rewards)))) (map-set rewards-by-height height {wrew: (+ wrew-value wrew), ustx: (+ ustx-value ustx)})))) ;; admin can submit a reward amount in sats at a stx block height and mint wrapped rewards (define-public (mint (height uint) (value uint)) (if (is-eq tx-sender admin) (begin (asserts! (> value u0) ERR_TX_IGNORED) (let ((ustx (/ (* value u1000000) (get-price height)))) (asserts! (map-update-rewards-by-height height value ustx) ERR_NATIVE_FAILURE) (match (ft-mint? wrapped-rewards value (var-get reward-admin)) success (ok {value: value, ustx: ustx}) error (err (* error u1000))))) (err u403))) (define-public (update-reward-admin (new-admin principal)) (if (is-eq tx-sender (var-get reward-admin)) (ok (var-set reward-admin new-admin)) (err u403))) ;; price BTC/STX (define-private (oracle-get-price) (contract-call? 'SPZ0RAC1EFTH949T4W2SYY6YBHJRMAF4ECT5A7DD.oracle-v1 get-price "artifix-binance" "STX-BTC") ) (define-private (update (price (tuple (amount uint) (height uint) (timestamp uint))) (height uint)) (if (> height (get height (var-get last-price))) (var-set last-price price) false)) (define-private (get-price-at-height (height uint)) (match (get-block-info? id-header-hash height) hash (match (at-block hash (oracle-get-price)) price (begin (update price height) (some (get amount price))) none) none)) (define-private (get-price (height uint)) (match (get-price-at-height height) price price (match (get-price-at-height (- height u1)) price-1 price-1 (match (get-price-at-height (- height u2)) price-2 price-2 (get amount (print (var-get last-price))))))) (define-public (get-price-stx-btc (height uint)) (ok (get-price height))) (define-constant ERR_TX_IGNORED (err u6)) (define-constant ERR_NATIVE_FAILURE (err u99))
0x66d0a9b78f420932e311bae62cab389b8f4a6faa3764801acd2f3c4760a29035
26,967
success
fpwr-v04-depot
(define-constant admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-map balances principal uint) (define-data-var total-rewards uint u0) (define-data-var total-claimed uint u0) (define-read-only (get-balance (user principal)) (ok (map-get? balances user))) (define-read-only (get-depot-info) {tokens: (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v04 get-balance (as-contract tx-sender)), total-rewards: (var-get total-rewards), total-claimed: (var-get total-claimed)}) (define-public (claim) (let ((user tx-sender) (amount (default-to u0 (map-get? balances user)))) (var-set total-claimed (+ amount (var-get total-claimed))) (if (> amount u0) (begin (map-set balances user u0) (as-contract (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v04 transfer amount tx-sender user none)) ) (err u100)))) (define-private (add-reward (details {user: principal, amount: uint})) (let ((user-amount (default-to u0 (map-get? balances (get user details)))) (reward-amount (get amount details))) (var-set total-rewards (+ reward-amount (var-get total-rewards))) (map-set balances (get user details) (+ reward-amount user-amount)))) (define-public (add-rewards (rewards (list 200 {user: principal, amount: uint}))) (if (is-eq tx-sender admin) (ok (map add-reward rewards)) (err u403))) (define-public (update-reward-admin (new-admin principal)) (if (is-eq tx-sender admin) (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v04 update-reward-admin new-admin) (err u403)))
0xd2b2f252311142e242d6639d0b2bae31918a12f8b13db09cd281a1106185740d
26,967
success
stacks-punks-bulk-mint-v3
;; mint up to 20 punks (define-public (bulk-mint (punk-ids (list 20 uint))) (let ( (result (fold mint punk-ids { count: u0 })) ) (ok result) ) ) (define-private (mint (punk-id uint) (data (tuple (count uint)))) (begin (asserts! (is-ok (contract-call? .stacks-punks-v3 mint punk-id)) { count: (get count data) } ) { count: (+ u1 (get count data)) } ) )
0x02a88769bbe8fff52e2f96504750987bd7dc9bdc1a71c238e43d8fcaafa27e76
27,006
success
stacks-punks-bulk-mint-v3
;; mint up to 20 punks (define-public (bulk-mint (punk-ids (list 20 uint))) (let ( (result (fold mint punk-ids { count: u0 })) ) (ok result) ) ) (define-private (mint (punk-id uint) (data (tuple (count uint)))) (begin (asserts! (is-ok (contract-call? .stacks-punks-v3 mint punk-id)) { count: (get count data) } ) { count: (+ u1 (get count data)) } ) )
0x02a88769bbe8fff52e2f96504750987bd7dc9bdc1a71c238e43d8fcaafa27e76
27,006
success
claim-stacking-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)))
0x318f83d5881b0b4cd25dc04da93d4d014bc5d58f7a200d627f7b23c16357ffd0
27,009
success
pb1
;; Reference: https://www.nhlottery.com/About-Us/Games-Rules#a-powerball ;; Terms: ;; bv(Ball Value) = (white1 + 100 * white2 + (100**2)*white3 + (100**3)*white4 + (100**4)*white5 + (100**5)*red). white1-white5 is in ascending order. ;; pbv(Power-play Ball Value) = BV + (100**6)*(powerball?1:0) ;; wv(Win Value) = PBV + (100**7)*win_stx ;; p = player (define-constant ERR_DEPOSIT_COUNT_INVALID 1001) (define-constant ERR_BALANCE_NOT_ENOUGH 1002) (define-constant ERR_TRANSFER_STX 1003) (define-constant ERR_NO_AUTHORITY 1004) (define-constant ERR_CANNOT_CANCEL_INVEST_NOW 1005) (define-constant ERR_NO_BET 1006) (define-constant ERR_WAIT_TO_DRAW 1007) (define-constant ERR_INVALID_STATE 1008) (define-constant ERR_INVALID_PRICE 1009) (define-constant ERR_NOT_ALLOW_WITHDRAW 1010) (define-constant ERR_INVALID_REWARD 1011) (define-constant ERR_CANNOT_MANUAL_SKIP_DRAW_NOW 1012) ;; (define-constant OWNER tx-sender) (define-constant LIST_5 (list u1 u2 u3 u4 u5)) (define-constant LIST_1_10 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10)) (define-constant LIST_11_20 (list u11 u12 u13 u14 u15 u16 u17 u18 u19 u20)) (define-constant LIST_20 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20)) (define-constant LIST_69 (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)) (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 BUNCH_NUM_LIST (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 )) (define-constant BUNCH_CAPACITY (len BUNCH_NUM_LIST)) ;; (define-constant STATE_FUNDING u1) (define-constant STATE_BET u2) (define-constant STATE_WAIT_DRAW u3) (define-constant STATE_DRAW u4) (define-constant BLOCKS_PER_ROUND u288) ;; 2 day (define-constant DRAW_CD_BLOCKS u6) ;; 1 hour (MUST >= 4). after round end, wait 1 hour to draw (define-constant DEFAULT_JACKPOT_FACTOR u20000000) ;; $2 => $40000000 (define-constant MIN_AWARD_POOL_FACTOR u22500000) ;; $2 => $45000000, if award pool is less than this, wait the owner to deposit (define-constant POWER_PLAY_10x_THRESHOLD_FACTOR u75000000) ;; $2 => $150000000, when jackpot>75000000*price, no 10x power play (define-constant AL (list u0 u0 u0 u35 u500 u5000000 u20 u20 u35 u500 u250000)) ;; index=(red-same?1:0)*6 + white-same-count. value=multiply-factor. award = price * multiply-factor / 10 (define-constant BET_COUNT_THRERSHOLD_TO_WITHDRAW u1000) ;; if bet-count<=500, owner can withdraw after round finishes if want (define-constant UPDATE_PRICE_COST u1000000) (define-constant MANUAL_SKIP_DRAW_BLOCKS u288) ;; if not draw finish after 2 days since round-end, probably due to encounter error during draw. The owner can manually skip draw, otherwise the contract will die. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-data-var m_state uint u0) (define-data-var m_round uint u0) (define-data-var m_start_at uint u0) (define-data-var m_end_at uint u0) (define-data-var m_price uint u0) (define-data-var m_jackpot uint u0) (define-data-var m_start_balance uint u0) ;; contract balance when round start (define-data-var m_draw_caller_reward uint u0) ;; when someone call step_draw, reward it to cover its fee (define-data-var m_new_price uint u0) ;; if not equal to 0, will change bet price after draw finish (define-data-var m_need_withdrawal bool false) ;; if true, owner will withdraw all the remain balance after draw finish (define-data-var m_omit_times uint u0) ;; loop logic use only ;;;; bet related ;; round => total_bet_count (define-map map_total_bet_count uint uint ) ;; round => bet_bunch_count (define-map map_bet_bunch_count uint uint ) ;; (round, bet_bunch_index) => (player, pbv) (define-map map_bet_bunch { round: uint, index: uint } (list 200 { p: principal, pbv: uint } ) ) ;;;; player related ;; (round, player) => player_bet_bunch_count (define-map map_player_bet_bunch_count { round: uint, p: principal } uint ) ;; (round, player, player_bunch_index) => pbv list (define-map map_player_bet_bunch { round: uint, p: principal, index: uint } (list 200 uint) ) ;;;; draw related (define-data-var m_draw_ball_value uint u0) ;; jackpot BV (define-data-var m_draw_white uint u0) (define-data-var m_draw_power_play uint u0) (define-data-var m_draw_bunch_index uint u0) (define-data-var m_draw_jackpot_reward_index uint u0) (define-data-var m_draw_jackpot_ave_award uint u0) ;; if more than 1 player win the jackpot, they share it. Each one get the jackpot-average-award. ;; white-ball => is this ball already in award pool (define-map map_white uint bool) ;; round => win count (not include jackpot) (define-map map_draw_win_count uint uint ) ;; (round, index) => (player, wv) (define-map map_draw_win { round: uint, index: uint } { p: principal, wv: uint } ) ;; round => jackpot count (define-map map_draw_jackpot_count uint uint ) ;; (round, index) => jackpot-player (define-map map_draw_jackpot { round: uint, index: uint } principal ) ;; round => round summary (define-map map_history_summary uint { start_at: uint, end_at: uint, price: uint, bet_count: uint, win_count: uint, jackpot_count: uint, ball_value: uint, power_play: uint, draw_bunch_index: uint, success: bool, ;; whether the round is drawn successfully } ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; bet related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; each element is PBV (define-public (bet (bet_list (list 200 uint))) (let ( (round (var-get m_round)) (bet_item_list (map build_bet_item bet_list)) (bet_count (len bet_item_list)) (power_play_count (fold is_power_play bet_list u0)) (bet_cost (/ (* (+ bet_count bet_count power_play_count) (var-get m_price)) u2)) (bet_bunch_count (default-to u0 (map-get? map_bet_bunch_count round))) (bet_bunch_index (if (> bet_bunch_count u0) bet_bunch_count u1)) (bet_bunch (default-to (list) (map-get? map_bet_bunch { round: round, index: bet_bunch_index }))) (bunch_len (len bet_bunch)) (new_total_bet_count (+ (default-to u0 (map-get? map_total_bet_count round)) bet_count)) (player_bet_bunch_count (default-to u0 (map-get? map_player_bet_bunch_count { p: tx-sender, round: round }))) (player_bet_bunch_index (if (> player_bet_bunch_count u0) player_bet_bunch_count u1)) (player_bet_bunch (default-to (list) (map-get? map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index }))) (player_bet_bunch_len (len player_bet_bunch)) ) (asserts! (is-eq (var-get m_state) STATE_BET) (err ERR_INVALID_STATE)) (asserts! (> bet_count u0) (err ERR_NO_BET)) (asserts! (>= (stx-get-balance tx-sender) bet_cost) (err ERR_BALANCE_NOT_ENOUGH)) ;; deduct (unwrap! (stx-transfer? bet_cost tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX)) (map-set map_total_bet_count round new_total_bet_count) (and (is-eq new_total_bet_count bet_count) (var-set m_start_at block-height) (map-set map_bet_bunch_count round u1) ) ;; note bet related data (if (<= (+ bunch_len bet_count) BUNCH_CAPACITY) (map-set map_bet_bunch { round: round, index: bet_bunch_index } (map get_bet_item_element (concat bet_bunch bet_item_list) BUNCH_NUM_LIST)) (let ( (next_bet_bunch_index (+ bet_bunch_index u1)) ) (map-set map_bet_bunch_count round next_bet_bunch_index) (if (is-eq bunch_len BUNCH_CAPACITY) (map-set map_bet_bunch { round: round, index: next_bet_bunch_index } bet_item_list) (begin (map-set map_bet_bunch { round: round, index: bet_bunch_index } (map get_bet_item_element (concat bet_bunch bet_item_list) BUNCH_NUM_LIST)) (var-set m_omit_times (- BUNCH_CAPACITY bunch_len)) (map-set map_bet_bunch { round: round, index: next_bet_bunch_index } (filter sub_bet_item_list_loop bet_item_list)) ) ) ) ) ;; note player related data (and (is-eq player_bet_bunch_count u0) (map-set map_player_bet_bunch_count { p: tx-sender, round: round } u1) ) (if (<= (+ player_bet_bunch_len bet_count) BUNCH_CAPACITY) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index } (map get_bet_value_element (concat player_bet_bunch bet_list) BUNCH_NUM_LIST) ) (let ( (new_player_bet_bunch_count (+ player_bet_bunch_count u1)) ) (map-set map_player_bet_bunch_count { p: tx-sender, round: round } new_player_bet_bunch_count) (if (is-eq player_bet_bunch_len BUNCH_CAPACITY) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: new_player_bet_bunch_count } bet_list) (begin (map-set map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index } (map get_bet_value_element (concat player_bet_bunch bet_list) BUNCH_NUM_LIST) ) (var-set m_omit_times (- BUNCH_CAPACITY player_bet_bunch_len)) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: new_player_bet_bunch_count } (filter sub_bet_value_list_loop bet_list) ) ) ) ) ) ;; if has started 2 day, enter wait-draw state (and (>= (- block-height (var-get m_start_at)) BLOCKS_PER_ROUND) (var-set m_state STATE_WAIT_DRAW) (var-set m_end_at block-height) ) ;; (ok bet_count) ) ) (define-private (is_power_play (pbv uint) (count uint)) (if (>= pbv u1000000000000) (+ count u1) count) ) (define-private (build_bet_item (pbv uint)) { p: tx-sender, pbv: pbv } ) (define-private (get_bet_item_element (element { p: principal, pbv: uint }) (index uint)) element ) (define-private (sub_bet_item_list_loop (element { p: principal, pbv: uint })) (let ( (omit_times (var-get m_omit_times)) ) (if (> omit_times u0) (and (var-set m_omit_times (- omit_times u1)) false ) true ) ) ) (define-private (get_bet_value_element (element uint) (index uint)) element ) (define-private (sub_bet_value_list_loop (element uint)) (let ( (omit_times (var-get m_omit_times)) ) (if (> omit_times u0) (and (var-set m_omit_times (- omit_times u1)) false ) true ) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; bet related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; draw related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (step_draw) (if (is-eq (var-get m_state) STATE_DRAW) (ok (handle_step_draw)) (if (is-eq (var-get m_state) STATE_WAIT_DRAW) (if (> block-height (+ (var-get m_end_at) DRAW_CD_BLOCKS)) (let ( (round (var-get m_round)) ) (var-set m_state STATE_DRAW) ;; safely reset (var-set m_omit_times u0) (var-set m_draw_ball_value (calc_draw_ball_value)) (var-set m_draw_power_play (calc_power_play)) (var-set m_draw_bunch_index u1) (var-set m_draw_jackpot_reward_index u1) (var-set m_draw_jackpot_ave_award u0) (map-set map_draw_win_count round u0) (map-set map_draw_jackpot_count round u0) (ok (handle_step_draw)) ) (err ERR_WAIT_TO_DRAW) ) (err ERR_INVALID_STATE) ) ) ) (define-private (calc_power_play) (let ((rand_num (get-random-by-block-height (+ (var-get m_end_at) u4))) (can_10x (<= (var-get m_jackpot) (* (var-get m_price) POWER_PLAY_10x_THRESHOLD_FACTOR))) (target_num (+ (mod (/ rand_num u10) (if can_10x u43 u42)) u1))) (if (<= target_num u24) u2 (if (<= target_num u37) u3 (if (<= target_num u40) u4 (if (<= target_num u42) u5 u10)))))) (define-private (calc_draw_ball_value) (let ( (end_at (var-get m_end_at)) (rand_1 (get-random-by-block-height (+ end_at u1))) (rand_2 (get-random-by-block-height (+ end_at u2))) (rand_3 (get-random-by-block-height (+ end_at u3))) (w1 (+ (mod (/ rand_1 u100) u69) u1)) (t1 (map-set map_white w1 true)) (w2 (if (begin (var-set m_draw_white u0) (fold G1 LIST_1_10 rand_2) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w3 (if (begin (var-set m_draw_white u0) (fold G1 LIST_1_10 rand_3) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w4 (if (begin (var-set m_draw_white u0) (fold G1 LIST_11_20 rand_1) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w5 (if (begin (var-set m_draw_white u0) (fold G1 LIST_11_20 rand_2) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) ;; (wl (list w1 w2 w3 w4 w5)) ;; bubble sort white balls (res_1 (fold S wl { i: u0, m: u70, mi: u0, ov: u0 })) ;; i(index), m(min_value), mi(min_index), ov(omit_value) (res_2 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_1) (pow u10 (get mi res_1)))})) (res_3 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_2) (pow u10 (get mi res_2)))})) (res_4 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_3) (pow u10 (get mi res_3)))})) (res_5 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_4) (pow u10 (get mi res_4)))})) (red (+ (mod (/ rand_3 u1000000) u26) u1)) ) (filter R LIST_69) (+ (get m res_1) (* (get m res_2) u100) (* (get m res_3) u10000) (* (get m res_4) u1000000) (* (get m res_5) u100000000) (* red u10000000000) ) ) ) (define-private (G1 (i uint) (r uint)) (if (is-eq r u0) u0 (let ((d (/ r (pow u10 i))) (w (+ (mod d u69) u1))) (if (and (is-none (map-get? map_white w)) (> d u0)) (begin (map-set map_white w true) (var-set m_draw_white w) u0) r)))) (define-private (G2 (w uint) (b bool)) (if (and b (is-none (map-get? map_white w))) (begin (map-set map_white w true) (var-set m_draw_white w) false) b)) (define-private (R (i uint)) (map-delete map_white i)) ;; bubble sort white balls loop. i(index), m(min_value), mi(min_index), ov(omit_value) (define-private (S (num uint) (user_data { i: uint, m: uint, mi: uint, ov: uint })) (let ( (i (get i user_data)) (ov (get ov user_data)) ) (if (and (is-eq (mod (/ ov (pow u10 i)) u10) u0) (< num (get m user_data))) (merge user_data { i: (+ i u1), m: num, mi: i, }) (merge user_data { i: (+ i u1) }) ) ) ) (define-private (handle_step_draw) (let ( (round (var-get m_round)) (bet_bunch_count (unwrap-panic (map-get? map_bet_bunch_count round))) (draw_bunch_index (var-get m_draw_bunch_index)) (contract_balance (stx-get-balance (as-contract tx-sender))) (draw_caller_reward (var-get m_draw_caller_reward)) (caller tx-sender) (comb_value (+ (* (var-get m_price) u1000000000000) (var-get m_draw_ball_value))) ) ;; reward the caller to cover its fee cost (and (>= contract_balance draw_caller_reward) (unwrap! (as-contract (stx-transfer? draw_caller_reward tx-sender caller)) false) ) ;; draw 3 bunch (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: draw_bunch_index })) (+ (* contract_balance u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u1) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ;; DON't use contract_balance, not real-time ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u2) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u3) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u4) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (var-set m_draw_bunch_index (+ draw_bunch_index u5)) ;; Distribute jackpot award when draw finish as the jackpot may be shared by more than 1 players. ;; The contract balance may be unenough at this moment (very rare, reward the remain balance to it/them). (if (and (>= (var-get m_draw_bunch_index) bet_bunch_count) (is-eq (len (default-to (list) (map-get? map_bet_bunch { round: round, index: (var-get m_draw_bunch_index)}))) u0)) (if (> (default-to u0 (map-get? map_draw_jackpot_count round)) u0) (step_reward_jackpot round) (draw_end true) ) true ) ) ) ;; D(draw_one_loop), b(bet_info), d(draw_bv), a(award_mul), r(round), ba(balance), u(user_value)=contract_balance * 10000000000000000000 + bet_price * 1000000000000 + draw_ball_value. Contract balance may be unenough during draw process, will not reward the player in this very rare situation. (define-private (D (b { p: principal, pbv: uint }) (u uint)) (let ((pbv (get pbv b)) (d (mod u u1000000000000))) (match (element-at AL (+ (if (is-eq (/ d u10000000000) (mod (/ pbv u10000000000) u100)) u6 u0) (mod (/ (fold L 0x000000000000000000 (+ (* pbv u1000000000000000) (* d u1000))) u100) u10))) a ;; red*6 + white-same-count (if (> a u0) (let ((r (var-get m_round)) (n (+ (default-to u0 (map-get? map_draw_win_count r)) u1)) (ba (/ u u10000000000000000000)) (award (/ (* a (/ (mod u u10000000000000000000) u1000000000000) (if (> pbv u1000000000000) (if (is-eq a u5000000) u2 (var-get m_draw_power_play)) u1)) u10))) (if (>= ba award) (begin (unwrap! (as-contract (stx-transfer? award tx-sender (get p b))) u) (map-set map_draw_win_count r n) (map-set map_draw_win {round: r, index: n} {p: (get p b), wv: (+ (get pbv b) (* award u100000000000000)) }) (- u (* award u10000000000000000000))) u)) u) ;; jackpot (let ((r (var-get m_round)) (n (+ (default-to u0 (map-get? map_draw_jackpot_count r)) u1))) (map-set map_draw_jackpot_count r n) (map-set map_draw_jackpot { round: r, index: n } (get p b) ) u)))) ;; loop to check how many same white balls the player bet with draw result. u(user_value) = pbv*1000000000000000 + draw_bv*1000 + same_count*100 + pbv_index*10 + draw_bv_index (define-private (L (e (buff 1)) (u uint)) (if (and (< (mod u u10) u5) (< (mod u u100) u50)) (let ((p (mod (/ (/ u u1000000000000000) (pow u100 (mod (/ u u10) u10))) u100)) (d (mod (/ (mod (/ u u1000) u1000000000000) (pow u100 (mod u u10))) u100))) (if (< p d) (+ u u10) (if (> p d) (+ u u1) (+ u u111)))) u)) (define-private (step_reward_jackpot (round uint)) (let ( (jackpot_index (var-get m_draw_jackpot_reward_index)) (jackpot_count (unwrap-panic (map-get? map_draw_jackpot_count round))) ) (if (is-eq jackpot_index u1) (let ( (remain_for_caller (* (var-get m_draw_caller_reward) u1000)) (contract_balance (stx-get-balance (as-contract tx-sender))) (max_award (if (> contract_balance remain_for_caller) (- contract_balance remain_for_caller) u0)) (origin_jackpot (var-get m_jackpot)) (jackpot_reward (if (>= max_award origin_jackpot) origin_jackpot max_award)) ) (var-set m_draw_jackpot_ave_award (/ jackpot_reward jackpot_count)) (fold reward_jackpot_loop LIST_20 u1) (if (<= jackpot_count u20) (draw_end true) (var-set m_draw_jackpot_reward_index u21) ;; too many jackpot players, reward them in next step_draw call. ) ) ;; jackpot players > 20, nearly impossible, but we still need deal this situation (begin (fold reward_jackpot_loop BUNCH_NUM_LIST jackpot_index) (fold reward_jackpot_loop BUNCH_NUM_LIST (+ jackpot_index BUNCH_CAPACITY)) (fold reward_jackpot_loop BUNCH_NUM_LIST (+ jackpot_index BUNCH_CAPACITY BUNCH_CAPACITY)) (if (>= (+ jackpot_index (* BUNCH_CAPACITY u3)) jackpot_count) (draw_end true) (var-set m_draw_jackpot_reward_index (+ jackpot_index (* BUNCH_CAPACITY u3))) ) ) ) ) ) (define-private (reward_jackpot_loop (element uint) (index uint)) (let ( (ave_award (var-get m_draw_jackpot_ave_award)) ) (match (map-get? map_draw_jackpot { round: (var-get m_round), index: index }) player (begin (and (> ave_award u0) (unwrap! (as-contract (stx-transfer? ave_award tx-sender player)) index) ) (+ index u1) ) index ) ) ) (define-private (draw_end (success bool)) (let ( (round (var-get m_round)) (start_balance (var-get m_start_balance)) (cur_balance (stx-get-balance (as-contract tx-sender))) ) (map-set map_history_summary round { start_at: (default-to u0 (get-block-info? time (var-get m_start_at))), end_at: (default-to u0 (get-block-info? time (var-get m_end_at))), price: (var-get m_new_price), bet_count: (default-to u0 (map-get? map_total_bet_count round)), win_count: (default-to u0 (map-get? map_draw_win_count round)), jackpot_count: (default-to u0 (map-get? map_draw_jackpot_count round)), ball_value: (var-get m_draw_ball_value), power_play: (var-get m_draw_power_play), draw_bunch_index: (var-get m_draw_bunch_index), success: success, } ) (if success (print "draw end successfully") (print "draw end due to some error") ) ;; whether need withdraw (and (var-get m_need_withdrawal) (var-set m_need_withdrawal false) (handle_withdraw) ) ;; whether need update price (and (> (var-get m_new_price) u0) (var-set m_jackpot (/ (* (var-get m_jackpot) (var-get m_new_price)) (var-get m_price))) (var-set m_price (var-get m_new_price)) (var-set m_new_price u0) ) ;; if (balance >= MIN_AWARD_POOL) then ;; if success then ;; if has-profit then ;; reward owner, increase jackpot ;; else ;; if jackpot-is-rewarded then ;; reset-jackpot ;; else ;; jackpot = min(jackpot, balance) ;; end ;; end ;; else ;; jackpot = min(jackpot, balance) ;; end ;; start-next-round ;; else ;; start-next-round, wait owner to deposit (if (>= cur_balance (* (var-get m_price) MIN_AWARD_POOL_FACTOR)) (begin (if success (if (> cur_balance start_balance) ;; has-profit (begin (unwrap! (as-contract (stx-transfer? (/ (* (- cur_balance start_balance) u50) u100) tx-sender OWNER)) false) (var-set m_jackpot (+ (var-get m_jackpot) (/ (* (- cur_balance start_balance) u20) u100))) ) (if (> (unwrap-panic (map-get? map_draw_jackpot_count round)) u0) (var-set m_jackpot (* (var-get m_price) DEFAULT_JACKPOT_FACTOR)) (and (< cur_balance (var-get m_jackpot)) (var-set m_jackpot cur_balance)) ) ) ;; not success (and (< cur_balance (var-get m_jackpot)) (var-set m_jackpot cur_balance)) ) ;; start next round for bet (var-set m_state STATE_BET) (var-set m_round (+ round u1)) (var-set m_start_balance (stx-get-balance (as-contract tx-sender))) ) ;; balance unenough (begin (var-set m_state STATE_FUNDING) (var-set m_round (+ round u1)) ) ) ;; clean up (var-set m_start_at u0) (var-set m_end_at u0) (var-set m_omit_times u0) (reset_draw_members) ) ) (define-private (reset_draw_members) (begin (var-set m_draw_white u0) (var-set m_draw_power_play u0) (var-set m_draw_ball_value u0) (var-set m_draw_bunch_index u0) (var-set m_draw_jackpot_reward_index u1) (var-set m_draw_jackpot_ave_award u0) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; draw related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; deposit/withdraw related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (deposit (deposit_count uint)) (let ( (min_award_pool (* (var-get m_price) MIN_AWARD_POOL_FACTOR)) (cur_balance (stx-get-balance (as-contract tx-sender))) (min_need_depot (if (>= cur_balance min_award_pool) u0 (- min_award_pool cur_balance))) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (is-eq (var-get m_state) STATE_FUNDING) (err ERR_INVALID_STATE)) (asserts! (and (> deposit_count u0) (>= deposit_count min_need_depot)) (err ERR_DEPOSIT_COUNT_INVALID)) (asserts! (>= (stx-get-balance tx-sender) deposit_count) (err ERR_BALANCE_NOT_ENOUGH)) ;; (unwrap! (stx-transfer? deposit_count tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX)) ;; safely reset (var-set m_state STATE_BET) (var-set m_start_at u0) (var-set m_end_at u0) (var-set m_start_balance (stx-get-balance (as-contract tx-sender))) (var-set m_omit_times u0) (reset_draw_members) ;; (ok true) ) ) (define-public (withdraw) (let ( (round (var-get m_round)) (state (var-get m_state)) (last_round_bets (default-to u0 (map-get? map_total_bet_count (- round u1)))) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) ;; if last round has enough bets, not allow withdraw (asserts! (<= last_round_bets BET_COUNT_THRERSHOLD_TO_WITHDRAW) (err ERR_NOT_ALLOW_WITHDRAW)) (print (var-get m_state)) (if (is-eq state STATE_FUNDING) (ok (handle_withdraw)) (if (and (is-eq state STATE_BET) (is-eq (default-to u0 (map-get? map_total_bet_count round)) u0)) (begin (handle_withdraw) (var-set m_state STATE_FUNDING) (ok true) ) (ok (var-set m_need_withdrawal true)) ;; wait for draw finish ) ) ) ) (define-private (handle_withdraw) (let ( (balance (stx-get-balance (as-contract tx-sender))) ) (and (> balance u0) (unwrap! (as-contract (stx-transfer? balance tx-sender OWNER)) false)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; deposit/withdraw related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; misc begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (update_price (price uint)) (let ( (state (var-get m_state)) (round (var-get m_round)) (last_round_bets (default-to u0 (map-get? map_total_bet_count (- round u1)))) (cur_price (var-get m_price)) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (<= price u2000000) (err ERR_INVALID_PRICE)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_NOT_ENOUGH)) (if (is-eq (var-get m_state) STATE_FUNDING) (and (var-set m_jackpot (/ (* (var-get m_jackpot) price) (var-get m_price))) (var-set m_price price) ) (var-set m_new_price price) ) (ok true) ) ) (define-public (update_draw_caller_reward (reward uint)) (begin (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (and (< reward u100000)) (err ERR_INVALID_REWARD)) (var-set m_draw_caller_reward reward) (ok true) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; misc end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; random generator begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; reference: 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-random-by-block-height (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? burnchain-header-hash block-index)))) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; random generator end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; web use begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-read-only (get_summary (player_opt (optional principal))) (let ( (round (var-get m_round)) (start_at (var-get m_start_at)) (end_at (var-get m_end_at)) (player_bet_bunch_count (if (is-some player_opt) (default-to u0 (map-get? map_player_bet_bunch_count { round: round, p: (unwrap-panic player_opt)})) u0 ) ) (player_latest_bet_bunch (if (is-some player_opt) (map-get? map_player_bet_bunch { round: round, p: (unwrap-panic player_opt), index: player_bet_bunch_count }) none ) ) ) { state: (var-get m_state), round: round, ;; start_at: start_at, start_time: (default-to u0 (get-block-info? time start_at)), end_at: end_at, end_time: (default-to u0 (get-block-info? time end_at)), price: (var-get m_price), jackpot: (var-get m_jackpot), total_bet_count: (default-to u0 (map-get? map_total_bet_count round)), bet_bunch_count: (default-to u0 (map-get? map_bet_bunch_count round)), player_bet_bunch_count: player_bet_bunch_count, player_latest_bet_bunch: player_latest_bet_bunch, block_height: block-height, draw_cd: DRAW_CD_BLOCKS, draw_caller_reward: (var-get m_draw_caller_reward), draw_power_play: (var-get m_draw_power_play), draw_ball_value: (var-get m_draw_ball_value), draw_bunch_index: (var-get m_draw_bunch_index), balance: (stx-get-balance (as-contract tx-sender)), } ) ) (define-read-only (get_history_summary (round uint)) (map-get? map_history_summary round) ) (define-read-only (get_bet_bunch_count (round uint)) (map-get? map_bet_bunch_count round) ) (define-read-only (get_bet_bunch (round uint) (index uint)) (map-get? map_bet_bunch { round: round, index: index }) ) (define-read-only (get_win_count (round uint)) (map-get? map_draw_win_count round) ) (define-read-only (get_win_list (key_list (list 25 { round: uint, index: uint }))) (map get_win_item key_list) ) (define-read-only (get_win_item (key { round: uint, index: uint })) (map-get? map_draw_win key) ) (define-read-only (get_jackpot_count (round uint)) (map-get? map_draw_jackpot_count round) ) (define-read-only (get_jackpot_list (key_list (list 25 { round: uint, index: uint }))) (map get_jackpot_item key_list) ) (define-read-only (get_jackpot_item (key { round: uint, index: uint })) (map-get? map_draw_jackpot key) ) (define-read-only (get_player_bet_bunch_count (round uint) (player principal)) (default-to u0 (map-get? map_player_bet_bunch_count { round: round, p: player })) ) (define-read-only (get_player_bet_bunch (round uint) (player principal) (index uint)) (map-get? map_player_bet_bunch { round: round, p: player, index: index }) ) (define-public (manual_skip_draw) (begin (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (or (is-eq (var-get m_state) STATE_WAIT_DRAW) (is-eq (var-get m_state) STATE_DRAW)) (err ERR_INVALID_STATE)) (asserts! (> block-height (+ (var-get m_end_at) MANUAL_SKIP_DRAW_BLOCKS)) (err ERR_CANNOT_MANUAL_SKIP_DRAW_NOW)) (ok (draw_end false)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; web use end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; initialize related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (initialize) (begin (print "initialize") (var-set m_state STATE_FUNDING) (var-set m_round u1) (var-set m_price u2) (var-set m_draw_caller_reward u500) (var-set m_jackpot (* (var-get m_price) DEFAULT_JACKPOT_FACTOR)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; initialize related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (initialize) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; test begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (test_change_begin (bh uint)) (begin (var-set m_start_at (- (var-get m_start_at) bh)) (ok true) ) ) (define-public (test_change_end (bh uint)) (begin (var-set m_end_at (- (var-get m_end_at) bh)) (ok true) ) ) (define-public (force_withdraw) (let ( (round (var-get m_round)) (state (var-get m_state)) (last_round_bets (default-to u0 (map-get? map_total_bet_count (- round u1)))) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (ok (handle_withdraw)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; test end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0x0b87286aae4806279c503e3e20c96eaa46a9b53192f35476b29a75aa21ec5948
27,065
success
powerball
;; Reference: https://www.nhlottery.com/About-Us/Games-Rules#a-powerball ;; Terms: ;; bv(Ball Value) = (white1 + 100 * white2 + (100**2)*white3 + (100**3)*white4 + (100**4)*white5 + (100**5)*red). white1-white5 is in ascending order. ;; pbv(Power-play Ball Value) = BV + (100**6)*(powerball?1:0) ;; wv(Win Value) = PBV + (100**7)*win_stx ;; p = player (define-constant ERR_DEPOSIT_COUNT_INVALID 1001) (define-constant ERR_BALANCE_NOT_ENOUGH 1002) (define-constant ERR_TRANSFER_STX 1003) (define-constant ERR_NO_AUTHORITY 1004) (define-constant ERR_CANNOT_CANCEL_INVEST_NOW 1005) (define-constant ERR_NO_BET 1006) (define-constant ERR_WAIT_TO_DRAW 1007) (define-constant ERR_INVALID_STATE 1008) (define-constant ERR_INVALID_PRICE 1009) (define-constant ERR_NOT_ALLOW_WITHDRAW 1010) (define-constant ERR_INVALID_REWARD 1011) (define-constant ERR_CANNOT_MANUAL_SKIP_DRAW_NOW 1012) ;; (define-constant OWNER tx-sender) (define-constant LIST_5 (list u1 u2 u3 u4 u5)) (define-constant LIST_1_10 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10)) (define-constant LIST_11_20 (list u11 u12 u13 u14 u15 u16 u17 u18 u19 u20)) (define-constant LIST_20 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20)) (define-constant LIST_69 (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)) (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 BUNCH_NUM_LIST (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 )) (define-constant BUNCH_CAPACITY (len BUNCH_NUM_LIST)) ;; (define-constant STATE_FUNDING u1) (define-constant STATE_BET u2) (define-constant STATE_WAIT_DRAW u3) (define-constant STATE_DRAW u4) (define-constant BLOCKS_PER_ROUND u288) ;; 2 day (define-constant DRAW_CD_BLOCKS u6) ;; 1 hour (MUST >= 4). after round end, wait 1 hour to draw (define-constant DEFAULT_JACKPOT_FACTOR u20000000) ;; $2 => $40000000 (define-constant MIN_AWARD_POOL_FACTOR u22500000) ;; $2 => $45000000, if award pool is less than this, wait the owner to deposit (define-constant POWER_PLAY_10x_THRESHOLD_FACTOR u75000000) ;; $2 => $150000000, when jackpot>75000000*price, no 10x power play (define-constant AL (list u0 u0 u0 u35 u500 u5000000 u20 u20 u35 u500 u250000)) ;; index=(red-same?1:0)*6 + white-same-count. value=multiply-factor. award = price * multiply-factor / 10 (define-constant BET_COUNT_THRERSHOLD_TO_WITHDRAW u1000) ;; if bet-count<=500, owner can withdraw after round finishes if want (define-constant UPDATE_PRICE_COST u1000000) (define-constant MANUAL_SKIP_DRAW_BLOCKS u288) ;; if not draw finish after 2 days since round-end, probably due to encounter error during draw. The owner can manually skip draw, otherwise the contract will die. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-data-var m_state uint u0) (define-data-var m_round uint u0) (define-data-var m_start_at uint u0) (define-data-var m_end_at uint u0) (define-data-var m_price uint u0) (define-data-var m_jackpot uint u0) (define-data-var m_start_balance uint u0) ;; contract balance when round start (define-data-var m_draw_caller_reward uint u0) ;; when someone call step_draw, reward it to cover its fee (define-data-var m_new_price uint u0) ;; if not equal to 0, will change bet price after draw finish (define-data-var m_need_withdrawal bool false) ;; if true, owner will withdraw all the remain balance after draw finish (define-data-var m_omit_times uint u0) ;; loop logic use only ;;;; bet related ;; round => total_bet_count (define-map map_total_bet_count uint uint ) ;; round => bet_bunch_count (define-map map_bet_bunch_count uint uint ) ;; (round, bet_bunch_index) => (player, pbv) (define-map map_bet_bunch { round: uint, index: uint } (list 200 { p: principal, pbv: uint } ) ) ;;;; player related ;; (round, player) => player_bet_bunch_count (define-map map_player_bet_bunch_count { round: uint, p: principal } uint ) ;; (round, player, player_bunch_index) => pbv list (define-map map_player_bet_bunch { round: uint, p: principal, index: uint } (list 200 uint) ) ;;;; draw related (define-data-var m_draw_ball_value uint u0) ;; jackpot BV (define-data-var m_draw_white uint u0) (define-data-var m_draw_power_play uint u0) (define-data-var m_draw_bunch_index uint u0) (define-data-var m_draw_jackpot_reward_index uint u0) (define-data-var m_draw_jackpot_ave_award uint u0) ;; if more than 1 player win the jackpot, they share it. Each one get the jackpot-average-award. ;; white-ball => is this ball already in award pool (define-map map_white uint bool) ;; round => win count (not include jackpot) (define-map map_draw_win_count uint uint ) ;; (round, index) => (player, wv) (define-map map_draw_win { round: uint, index: uint } { p: principal, wv: uint } ) ;; round => jackpot count (define-map map_draw_jackpot_count uint uint ) ;; (round, index) => jackpot-player (define-map map_draw_jackpot { round: uint, index: uint } principal ) ;; round => round summary (define-map map_history_summary uint { start_at: uint, end_at: uint, price: uint, bet_count: uint, win_count: uint, jackpot_count: uint, ball_value: uint, power_play: uint, draw_bunch_index: uint, success: bool, ;; whether the round is drawn successfully } ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; bet related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; each element is PBV (define-public (bet (bet_list (list 200 uint))) (let ( (round (var-get m_round)) (bet_item_list (map build_bet_item bet_list)) (bet_count (len bet_item_list)) (power_play_count (fold is_power_play bet_list u0)) (bet_cost (/ (* (+ bet_count bet_count power_play_count) (var-get m_price)) u2)) (bet_bunch_count (default-to u0 (map-get? map_bet_bunch_count round))) (bet_bunch_index (if (> bet_bunch_count u0) bet_bunch_count u1)) (bet_bunch (default-to (list) (map-get? map_bet_bunch { round: round, index: bet_bunch_index }))) (bunch_len (len bet_bunch)) (new_total_bet_count (+ (default-to u0 (map-get? map_total_bet_count round)) bet_count)) (player_bet_bunch_count (default-to u0 (map-get? map_player_bet_bunch_count { p: tx-sender, round: round }))) (player_bet_bunch_index (if (> player_bet_bunch_count u0) player_bet_bunch_count u1)) (player_bet_bunch (default-to (list) (map-get? map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index }))) (player_bet_bunch_len (len player_bet_bunch)) ) (asserts! (is-eq (var-get m_state) STATE_BET) (err ERR_INVALID_STATE)) (asserts! (> bet_count u0) (err ERR_NO_BET)) (asserts! (>= (stx-get-balance tx-sender) bet_cost) (err ERR_BALANCE_NOT_ENOUGH)) ;; deduct (unwrap! (stx-transfer? bet_cost tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX)) (map-set map_total_bet_count round new_total_bet_count) (and (is-eq new_total_bet_count bet_count) (var-set m_start_at block-height) (map-set map_bet_bunch_count round u1) ) ;; note bet related data (if (<= (+ bunch_len bet_count) BUNCH_CAPACITY) (map-set map_bet_bunch { round: round, index: bet_bunch_index } (map get_bet_item_element (concat bet_bunch bet_item_list) BUNCH_NUM_LIST)) (let ( (next_bet_bunch_index (+ bet_bunch_index u1)) ) (map-set map_bet_bunch_count round next_bet_bunch_index) (if (is-eq bunch_len BUNCH_CAPACITY) (map-set map_bet_bunch { round: round, index: next_bet_bunch_index } bet_item_list) (begin (map-set map_bet_bunch { round: round, index: bet_bunch_index } (map get_bet_item_element (concat bet_bunch bet_item_list) BUNCH_NUM_LIST)) (var-set m_omit_times (- BUNCH_CAPACITY bunch_len)) (map-set map_bet_bunch { round: round, index: next_bet_bunch_index } (filter sub_bet_item_list_loop bet_item_list)) ) ) ) ) ;; note player related data (and (is-eq player_bet_bunch_count u0) (map-set map_player_bet_bunch_count { p: tx-sender, round: round } u1) ) (if (<= (+ player_bet_bunch_len bet_count) BUNCH_CAPACITY) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index } (map get_bet_value_element (concat player_bet_bunch bet_list) BUNCH_NUM_LIST) ) (let ( (new_player_bet_bunch_count (+ player_bet_bunch_count u1)) ) (map-set map_player_bet_bunch_count { p: tx-sender, round: round } new_player_bet_bunch_count) (if (is-eq player_bet_bunch_len BUNCH_CAPACITY) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: new_player_bet_bunch_count } bet_list) (begin (map-set map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index } (map get_bet_value_element (concat player_bet_bunch bet_list) BUNCH_NUM_LIST) ) (var-set m_omit_times (- BUNCH_CAPACITY player_bet_bunch_len)) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: new_player_bet_bunch_count } (filter sub_bet_value_list_loop bet_list) ) ) ) ) ) ;; if has started 2 day, enter wait-draw state (and (>= (- block-height (var-get m_start_at)) BLOCKS_PER_ROUND) (var-set m_state STATE_WAIT_DRAW) (var-set m_end_at block-height) ) ;; (ok bet_count) ) ) (define-private (is_power_play (pbv uint) (count uint)) (if (>= pbv u1000000000000) (+ count u1) count) ) (define-private (build_bet_item (pbv uint)) { p: tx-sender, pbv: pbv } ) (define-private (get_bet_item_element (element { p: principal, pbv: uint }) (index uint)) element ) (define-private (sub_bet_item_list_loop (element { p: principal, pbv: uint })) (let ( (omit_times (var-get m_omit_times)) ) (if (> omit_times u0) (and (var-set m_omit_times (- omit_times u1)) false ) true ) ) ) (define-private (get_bet_value_element (element uint) (index uint)) element ) (define-private (sub_bet_value_list_loop (element uint)) (let ( (omit_times (var-get m_omit_times)) ) (if (> omit_times u0) (and (var-set m_omit_times (- omit_times u1)) false ) true ) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; bet related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; draw related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (step_draw) (if (is-eq (var-get m_state) STATE_DRAW) (ok (handle_step_draw)) (if (is-eq (var-get m_state) STATE_WAIT_DRAW) (if (> block-height (+ (var-get m_end_at) DRAW_CD_BLOCKS)) (let ( (round (var-get m_round)) ) (var-set m_state STATE_DRAW) ;; safely reset (var-set m_omit_times u0) (var-set m_draw_ball_value (calc_draw_ball_value)) (var-set m_draw_power_play (calc_power_play)) (var-set m_draw_bunch_index u1) (var-set m_draw_jackpot_reward_index u1) (var-set m_draw_jackpot_ave_award u0) (map-set map_draw_win_count round u0) (map-set map_draw_jackpot_count round u0) (ok (handle_step_draw)) ) (err ERR_WAIT_TO_DRAW) ) (err ERR_INVALID_STATE) ) ) ) (define-private (calc_power_play) (let ((rand_num (get-random-by-block-height (+ (var-get m_end_at) u4))) (can_10x (<= (var-get m_jackpot) (* (var-get m_price) POWER_PLAY_10x_THRESHOLD_FACTOR))) (target_num (+ (mod (/ rand_num u10) (if can_10x u43 u42)) u1))) (if (<= target_num u24) u2 (if (<= target_num u37) u3 (if (<= target_num u40) u4 (if (<= target_num u42) u5 u10)))))) (define-private (calc_draw_ball_value) (let ( (end_at (var-get m_end_at)) (rand_1 (get-random-by-block-height (+ end_at u1))) (rand_2 (get-random-by-block-height (+ end_at u2))) (rand_3 (get-random-by-block-height (+ end_at u3))) (w1 (+ (mod (/ rand_1 u100) u69) u1)) (t1 (map-set map_white w1 true)) (w2 (if (begin (var-set m_draw_white u0) (fold G1 LIST_1_10 rand_2) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w3 (if (begin (var-set m_draw_white u0) (fold G1 LIST_1_10 rand_3) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w4 (if (begin (var-set m_draw_white u0) (fold G1 LIST_11_20 rand_1) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w5 (if (begin (var-set m_draw_white u0) (fold G1 LIST_11_20 rand_2) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) ;; (wl (list w1 w2 w3 w4 w5)) ;; bubble sort white balls (res_1 (fold S wl { i: u0, m: u70, mi: u0, ov: u0 })) ;; i(index), m(min_value), mi(min_index), ov(omit_value) (res_2 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_1) (pow u10 (get mi res_1)))})) (res_3 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_2) (pow u10 (get mi res_2)))})) (res_4 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_3) (pow u10 (get mi res_3)))})) (res_5 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_4) (pow u10 (get mi res_4)))})) (red (+ (mod (/ rand_3 u1000000) u26) u1)) ) (filter R LIST_69) (+ (get m res_1) (* (get m res_2) u100) (* (get m res_3) u10000) (* (get m res_4) u1000000) (* (get m res_5) u100000000) (* red u10000000000) ) ) ) (define-private (G1 (i uint) (r uint)) (if (is-eq r u0) u0 (let ((d (/ r (pow u10 i))) (w (+ (mod d u69) u1))) (if (and (is-none (map-get? map_white w)) (> d u0)) (begin (map-set map_white w true) (var-set m_draw_white w) u0) r)))) (define-private (G2 (w uint) (b bool)) (if (and b (is-none (map-get? map_white w))) (begin (map-set map_white w true) (var-set m_draw_white w) false) b)) (define-private (R (i uint)) (map-delete map_white i)) ;; bubble sort white balls loop. i(index), m(min_value), mi(min_index), ov(omit_value) (define-private (S (num uint) (user_data { i: uint, m: uint, mi: uint, ov: uint })) (let ( (i (get i user_data)) (ov (get ov user_data)) ) (if (and (is-eq (mod (/ ov (pow u10 i)) u10) u0) (< num (get m user_data))) (merge user_data { i: (+ i u1), m: num, mi: i, }) (merge user_data { i: (+ i u1) }) ) ) ) (define-private (handle_step_draw) (let ( (round (var-get m_round)) (bet_bunch_count (unwrap-panic (map-get? map_bet_bunch_count round))) (draw_bunch_index (var-get m_draw_bunch_index)) (contract_balance (stx-get-balance (as-contract tx-sender))) (draw_caller_reward (var-get m_draw_caller_reward)) (caller tx-sender) (comb_value (+ (* (var-get m_price) u1000000000000) (var-get m_draw_ball_value))) ) ;; reward the caller to cover its fee cost (and (>= contract_balance draw_caller_reward) (unwrap! (as-contract (stx-transfer? draw_caller_reward tx-sender caller)) false) ) ;; draw 3 bunch (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: draw_bunch_index })) (+ (* contract_balance u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u1) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ;; DON't use contract_balance, not real-time ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u2) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u3) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u4) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (var-set m_draw_bunch_index (+ draw_bunch_index u5)) ;; Distribute jackpot award when draw finish as the jackpot may be shared by more than 1 players. ;; The contract balance may be unenough at this moment (very rare, reward the remain balance to it/them). (if (and (>= (var-get m_draw_bunch_index) bet_bunch_count) (is-eq (len (default-to (list) (map-get? map_bet_bunch { round: round, index: (var-get m_draw_bunch_index)}))) u0)) (if (> (default-to u0 (map-get? map_draw_jackpot_count round)) u0) (step_reward_jackpot round) (draw_end true) ) true ) ) ) ;; D(draw_one_loop), b(bet_info), d(draw_bv), a(award_mul), r(round), ba(balance), u(user_value)=contract_balance * 10000000000000000000 + bet_price * 1000000000000 + draw_ball_value. Contract balance may be unenough during draw process, will not reward the player in this very rare situation. (define-private (D (b { p: principal, pbv: uint }) (u uint)) (let ((pbv (get pbv b)) (d (mod u u1000000000000))) (match (element-at AL (+ (if (is-eq (/ d u10000000000) (mod (/ pbv u10000000000) u100)) u6 u0) (mod (/ (fold L 0x000000000000000000 (+ (* pbv u1000000000000000) (* d u1000))) u100) u10))) a ;; red*6 + white-same-count (if (> a u0) (let ((r (var-get m_round)) (n (+ (default-to u0 (map-get? map_draw_win_count r)) u1)) (ba (/ u u10000000000000000000)) (award (/ (* a (/ (mod u u10000000000000000000) u1000000000000) (if (> pbv u1000000000000) (if (is-eq a u5000000) u2 (var-get m_draw_power_play)) u1)) u10))) (if (>= ba award) (begin (unwrap! (as-contract (stx-transfer? award tx-sender (get p b))) u) (map-set map_draw_win_count r n) (map-set map_draw_win {round: r, index: n} {p: (get p b), wv: (+ (get pbv b) (* award u100000000000000)) }) (- u (* award u10000000000000000000))) u)) u) ;; jackpot (let ((r (var-get m_round)) (n (+ (default-to u0 (map-get? map_draw_jackpot_count r)) u1))) (map-set map_draw_jackpot_count r n) (map-set map_draw_jackpot { round: r, index: n } (get p b) ) u)))) ;; loop to check how many same white balls the player bet with draw result. u(user_value) = pbv*1000000000000000 + draw_bv*1000 + same_count*100 + pbv_index*10 + draw_bv_index (define-private (L (e (buff 1)) (u uint)) (if (and (< (mod u u10) u5) (< (mod u u100) u50)) (let ((p (mod (/ (/ u u1000000000000000) (pow u100 (mod (/ u u10) u10))) u100)) (d (mod (/ (mod (/ u u1000) u1000000000000) (pow u100 (mod u u10))) u100))) (if (< p d) (+ u u10) (if (> p d) (+ u u1) (+ u u111)))) u)) (define-private (step_reward_jackpot (round uint)) (let ( (jackpot_index (var-get m_draw_jackpot_reward_index)) (jackpot_count (unwrap-panic (map-get? map_draw_jackpot_count round))) ) (if (is-eq jackpot_index u1) (let ( (remain_for_caller (* (var-get m_draw_caller_reward) u1000)) (contract_balance (stx-get-balance (as-contract tx-sender))) (max_award (if (> contract_balance remain_for_caller) (- contract_balance remain_for_caller) u0)) (origin_jackpot (var-get m_jackpot)) (jackpot_reward (if (>= max_award origin_jackpot) origin_jackpot max_award)) ) (var-set m_draw_jackpot_ave_award (/ jackpot_reward jackpot_count)) (fold reward_jackpot_loop LIST_20 u1) (if (<= jackpot_count u20) (draw_end true) (var-set m_draw_jackpot_reward_index u21) ;; too many jackpot players, reward them in next step_draw call. ) ) ;; jackpot players > 20, nearly impossible, but we still need deal this situation (begin (fold reward_jackpot_loop BUNCH_NUM_LIST jackpot_index) (fold reward_jackpot_loop BUNCH_NUM_LIST (+ jackpot_index BUNCH_CAPACITY)) (fold reward_jackpot_loop BUNCH_NUM_LIST (+ jackpot_index BUNCH_CAPACITY BUNCH_CAPACITY)) (if (>= (+ jackpot_index (* BUNCH_CAPACITY u3)) jackpot_count) (draw_end true) (var-set m_draw_jackpot_reward_index (+ jackpot_index (* BUNCH_CAPACITY u3))) ) ) ) ) ) (define-private (reward_jackpot_loop (element uint) (index uint)) (let ( (ave_award (var-get m_draw_jackpot_ave_award)) ) (match (map-get? map_draw_jackpot { round: (var-get m_round), index: index }) player (begin (and (> ave_award u0) (unwrap! (as-contract (stx-transfer? ave_award tx-sender player)) index) ) (+ index u1) ) index ) ) ) (define-private (draw_end (success bool)) (let ( (round (var-get m_round)) (start_balance (var-get m_start_balance)) (cur_balance (stx-get-balance (as-contract tx-sender))) ) (map-set map_history_summary round { start_at: (default-to u0 (get-block-info? time (var-get m_start_at))), end_at: (default-to u0 (get-block-info? time (var-get m_end_at))), price: (var-get m_new_price), bet_count: (default-to u0 (map-get? map_total_bet_count round)), win_count: (default-to u0 (map-get? map_draw_win_count round)), jackpot_count: (default-to u0 (map-get? map_draw_jackpot_count round)), ball_value: (var-get m_draw_ball_value), power_play: (var-get m_draw_power_play), draw_bunch_index: (var-get m_draw_bunch_index), success: success, } ) (if success (print "draw end successfully") (print "draw end due to some error") ) ;; whether need withdraw (and (var-get m_need_withdrawal) (var-set m_need_withdrawal false) (handle_withdraw) ) ;; whether need update price (and (> (var-get m_new_price) u0) (var-set m_jackpot (/ (* (var-get m_jackpot) (var-get m_new_price)) (var-get m_price))) (var-set m_price (var-get m_new_price)) (var-set m_new_price u0) ) ;; if (balance >= MIN_AWARD_POOL) then ;; if success then ;; if has-profit then ;; reward owner, increase jackpot ;; else ;; if jackpot-is-rewarded then ;; reset-jackpot ;; else ;; jackpot = min(jackpot, balance) ;; end ;; end ;; else ;; jackpot = min(jackpot, balance) ;; end ;; start-next-round ;; else ;; start-next-round, wait owner to deposit (if (>= cur_balance (* (var-get m_price) MIN_AWARD_POOL_FACTOR)) (begin (if success (if (> cur_balance start_balance) ;; has-profit (begin (unwrap! (as-contract (stx-transfer? (/ (* (- cur_balance start_balance) u50) u100) tx-sender OWNER)) false) (var-set m_jackpot (+ (var-get m_jackpot) (/ (* (- cur_balance start_balance) u20) u100))) ) (if (> (unwrap-panic (map-get? map_draw_jackpot_count round)) u0) (var-set m_jackpot (* (var-get m_price) DEFAULT_JACKPOT_FACTOR)) (and (< cur_balance (var-get m_jackpot)) (var-set m_jackpot cur_balance)) ) ) ;; not success (and (< cur_balance (var-get m_jackpot)) (var-set m_jackpot cur_balance)) ) ;; start next round for bet (var-set m_state STATE_BET) (var-set m_round (+ round u1)) (var-set m_start_balance (stx-get-balance (as-contract tx-sender))) ) ;; balance unenough (begin (var-set m_state STATE_FUNDING) (var-set m_round (+ round u1)) ) ) ;; clean up (var-set m_start_at u0) (var-set m_end_at u0) (var-set m_omit_times u0) (reset_draw_members) ) ) (define-private (reset_draw_members) (begin (var-set m_draw_white u0) (var-set m_draw_power_play u0) (var-set m_draw_ball_value u0) (var-set m_draw_bunch_index u0) (var-set m_draw_jackpot_reward_index u1) (var-set m_draw_jackpot_ave_award u0) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; draw related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; deposit/withdraw related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (deposit (deposit_count uint)) (let ( (min_award_pool (* (var-get m_price) MIN_AWARD_POOL_FACTOR)) (cur_balance (stx-get-balance (as-contract tx-sender))) (min_need_depot (if (>= cur_balance min_award_pool) u0 (- min_award_pool cur_balance))) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (is-eq (var-get m_state) STATE_FUNDING) (err ERR_INVALID_STATE)) (asserts! (and (> deposit_count u0) (>= deposit_count min_need_depot)) (err ERR_DEPOSIT_COUNT_INVALID)) (asserts! (>= (stx-get-balance tx-sender) deposit_count) (err ERR_BALANCE_NOT_ENOUGH)) ;; (unwrap! (stx-transfer? deposit_count tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX)) ;; safely reset (var-set m_state STATE_BET) (var-set m_start_at u0) (var-set m_end_at u0) (var-set m_start_balance (stx-get-balance (as-contract tx-sender))) (var-set m_omit_times u0) (reset_draw_members) ;; (ok true) ) ) (define-public (withdraw) (let ( (round (var-get m_round)) (state (var-get m_state)) (last_round_bets (default-to u0 (map-get? map_total_bet_count (- round u1)))) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) ;; if last round has enough bets, not allow withdraw (asserts! (<= last_round_bets BET_COUNT_THRERSHOLD_TO_WITHDRAW) (err ERR_NOT_ALLOW_WITHDRAW)) (print (var-get m_state)) (if (is-eq state STATE_FUNDING) (ok (handle_withdraw)) (if (and (is-eq state STATE_BET) (is-eq (default-to u0 (map-get? map_total_bet_count round)) u0)) (begin (handle_withdraw) (var-set m_state STATE_FUNDING) (ok true) ) (ok (var-set m_need_withdrawal true)) ;; wait for draw finish ) ) ) ) (define-private (handle_withdraw) (let ( (balance (stx-get-balance (as-contract tx-sender))) ) (and (> balance u0) (unwrap! (as-contract (stx-transfer? balance tx-sender OWNER)) false)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; deposit/withdraw related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; misc begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (update_price (price uint)) (let ( (state (var-get m_state)) (round (var-get m_round)) (last_round_bets (default-to u0 (map-get? map_total_bet_count (- round u1)))) (cur_price (var-get m_price)) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (<= price u2000000) (err ERR_INVALID_PRICE)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_NOT_ENOUGH)) (if (is-eq (var-get m_state) STATE_FUNDING) (and (var-set m_jackpot (/ (* (var-get m_jackpot) price) (var-get m_price))) (var-set m_price price) ) (var-set m_new_price price) ) (ok true) ) ) (define-public (update_draw_caller_reward (reward uint)) (begin (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (and (< reward u100000)) (err ERR_INVALID_REWARD)) (var-set m_draw_caller_reward reward) (ok true) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; misc end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; random generator begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; reference: 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-random-by-block-height (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? burnchain-header-hash block-index)))) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; random generator end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; web use begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-read-only (get_summary (player_opt (optional principal))) (let ( (round (var-get m_round)) (start_at (var-get m_start_at)) (end_at (var-get m_end_at)) (player_bet_bunch_count (if (is-some player_opt) (default-to u0 (map-get? map_player_bet_bunch_count { round: round, p: (unwrap-panic player_opt)})) u0 ) ) (player_latest_bet_bunch (if (is-some player_opt) (map-get? map_player_bet_bunch { round: round, p: (unwrap-panic player_opt), index: player_bet_bunch_count }) none ) ) ) { state: (var-get m_state), round: round, ;; start_at: start_at, start_time: (default-to u0 (get-block-info? time start_at)), end_at: end_at, end_time: (default-to u0 (get-block-info? time end_at)), price: (var-get m_price), jackpot: (var-get m_jackpot), total_bet_count: (default-to u0 (map-get? map_total_bet_count round)), bet_bunch_count: (default-to u0 (map-get? map_bet_bunch_count round)), player_bet_bunch_count: player_bet_bunch_count, player_latest_bet_bunch: player_latest_bet_bunch, block_height: block-height, draw_cd: DRAW_CD_BLOCKS, draw_caller_reward: (var-get m_draw_caller_reward), draw_power_play: (var-get m_draw_power_play), draw_ball_value: (var-get m_draw_ball_value), draw_bunch_index: (var-get m_draw_bunch_index), balance: (stx-get-balance (as-contract tx-sender)), } ) ) (define-read-only (get_history_summary (round uint)) (map-get? map_history_summary round) ) (define-read-only (get_bet_bunch_count (round uint)) (map-get? map_bet_bunch_count round) ) (define-read-only (get_bet_bunch (round uint) (index uint)) (map-get? map_bet_bunch { round: round, index: index }) ) (define-read-only (get_win_count (round uint)) (map-get? map_draw_win_count round) ) (define-read-only (get_win_list (key_list (list 25 { round: uint, index: uint }))) (map get_win_item key_list) ) (define-read-only (get_win_item (key { round: uint, index: uint })) (map-get? map_draw_win key) ) (define-read-only (get_jackpot_count (round uint)) (map-get? map_draw_jackpot_count round) ) (define-read-only (get_jackpot_list (key_list (list 25 { round: uint, index: uint }))) (map get_jackpot_item key_list) ) (define-read-only (get_jackpot_item (key { round: uint, index: uint })) (map-get? map_draw_jackpot key) ) (define-read-only (get_player_bet_bunch_count (round uint) (player principal)) (default-to u0 (map-get? map_player_bet_bunch_count { round: round, p: player })) ) (define-read-only (get_player_bet_bunch (round uint) (player principal) (index uint)) (map-get? map_player_bet_bunch { round: round, p: player, index: index }) ) (define-public (manual_skip_draw) (begin (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (or (is-eq (var-get m_state) STATE_WAIT_DRAW) (is-eq (var-get m_state) STATE_DRAW)) (err ERR_INVALID_STATE)) (asserts! (> block-height (+ (var-get m_end_at) MANUAL_SKIP_DRAW_BLOCKS)) (err ERR_CANNOT_MANUAL_SKIP_DRAW_NOW)) (ok (draw_end false)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; web use end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; initialize related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (initialize) (begin (print "initialize") (var-set m_state STATE_FUNDING) (var-set m_round u1) (var-set m_price u20) (var-set m_draw_caller_reward u500) (var-set m_jackpot (* (var-get m_price) DEFAULT_JACKPOT_FACTOR)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; initialize related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (initialize)
0x34ee15dda964934cd3ae5829624288e6381540910156ef62cea99aea2ad4e48a
27,096
success
powerball
;; Reference: https://www.nhlottery.com/About-Us/Games-Rules#a-powerball ;; Terms: ;; bv(Ball Value) = (white1 + 100 * white2 + (100**2)*white3 + (100**3)*white4 + (100**4)*white5 + (100**5)*red). white1-white5 is in ascending order. ;; pbv(Power-play Ball Value) = BV + (100**6)*(powerball?1:0) ;; wv(Win Value) = PBV + (100**7)*win_stx ;; p = player (define-constant ERR_DEPOSIT_COUNT_INVALID 1001) (define-constant ERR_BALANCE_NOT_ENOUGH 1002) (define-constant ERR_TRANSFER_STX 1003) (define-constant ERR_NO_AUTHORITY 1004) (define-constant ERR_CANNOT_CANCEL_INVEST_NOW 1005) (define-constant ERR_NO_BET 1006) (define-constant ERR_WAIT_TO_DRAW 1007) (define-constant ERR_INVALID_STATE 1008) (define-constant ERR_INVALID_PRICE 1009) (define-constant ERR_NOT_ALLOW_WITHDRAW 1010) (define-constant ERR_INVALID_REWARD 1011) (define-constant ERR_CANNOT_MANUAL_SKIP_DRAW_NOW 1012) ;; (define-constant OWNER tx-sender) (define-constant LIST_5 (list u1 u2 u3 u4 u5)) (define-constant LIST_1_10 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10)) (define-constant LIST_11_20 (list u11 u12 u13 u14 u15 u16 u17 u18 u19 u20)) (define-constant LIST_20 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20)) (define-constant LIST_69 (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)) (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 BUNCH_NUM_LIST (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 )) (define-constant BUNCH_CAPACITY (len BUNCH_NUM_LIST)) ;; (define-constant STATE_FUNDING u1) (define-constant STATE_BET u2) (define-constant STATE_WAIT_DRAW u3) (define-constant STATE_DRAW u4) (define-constant BLOCKS_PER_ROUND u288) ;; 2 day (define-constant DRAW_CD_BLOCKS u6) ;; 1 hour (MUST >= 4). after round end, wait 1 hour to draw (define-constant DEFAULT_JACKPOT_FACTOR u20000000) ;; $2 => $40000000 (define-constant MIN_AWARD_POOL_FACTOR u22500000) ;; $2 => $45000000, if award pool is less than this, wait the owner to deposit (define-constant POWER_PLAY_10x_THRESHOLD_FACTOR u75000000) ;; $2 => $150000000, when jackpot>75000000*price, no 10x power play (define-constant AL (list u0 u0 u0 u35 u500 u5000000 u20 u20 u35 u500 u250000)) ;; index=(red-same?1:0)*6 + white-same-count. value=multiply-factor. award = price * multiply-factor / 10 (define-constant BET_COUNT_THRERSHOLD_TO_WITHDRAW u1000) ;; if bet-count<=500, owner can withdraw after round finishes if want (define-constant UPDATE_PRICE_COST u1000000) (define-constant MANUAL_SKIP_DRAW_BLOCKS u288) ;; if not draw finish after 2 days since round-end, probably due to encounter error during draw. The owner can manually skip draw, otherwise the contract will die. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-data-var m_state uint u0) (define-data-var m_round uint u0) (define-data-var m_start_at uint u0) (define-data-var m_end_at uint u0) (define-data-var m_price uint u0) (define-data-var m_jackpot uint u0) (define-data-var m_start_balance uint u0) ;; contract balance when round start (define-data-var m_draw_caller_reward uint u0) ;; when someone call step_draw, reward it to cover its fee (define-data-var m_new_price uint u0) ;; if not equal to 0, will change bet price after draw finish (define-data-var m_need_withdrawal bool false) ;; if true, owner will withdraw all the remain balance after draw finish (define-data-var m_omit_times uint u0) ;; loop logic use only ;;;; bet related ;; round => total_bet_count (define-map map_total_bet_count uint uint ) ;; round => bet_bunch_count (define-map map_bet_bunch_count uint uint ) ;; (round, bet_bunch_index) => (player, pbv) (define-map map_bet_bunch { round: uint, index: uint } (list 200 { p: principal, pbv: uint } ) ) ;;;; player related ;; (round, player) => player_bet_bunch_count (define-map map_player_bet_bunch_count { round: uint, p: principal } uint ) ;; (round, player, player_bunch_index) => pbv list (define-map map_player_bet_bunch { round: uint, p: principal, index: uint } (list 200 uint) ) ;;;; draw related (define-data-var m_draw_ball_value uint u0) ;; jackpot BV (define-data-var m_draw_white uint u0) (define-data-var m_draw_power_play uint u0) (define-data-var m_draw_bunch_index uint u0) (define-data-var m_draw_jackpot_reward_index uint u0) (define-data-var m_draw_jackpot_ave_award uint u0) ;; if more than 1 player win the jackpot, they share it. Each one get the jackpot-average-award. ;; white-ball => is this ball already in award pool (define-map map_white uint bool) ;; round => win count (not include jackpot) (define-map map_draw_win_count uint uint ) ;; (round, index) => (player, wv) (define-map map_draw_win { round: uint, index: uint } { p: principal, wv: uint } ) ;; round => jackpot count (define-map map_draw_jackpot_count uint uint ) ;; (round, index) => jackpot-player (define-map map_draw_jackpot { round: uint, index: uint } principal ) ;; round => round summary (define-map map_history_summary uint { start_at: uint, end_at: uint, price: uint, bet_count: uint, win_count: uint, jackpot_count: uint, ball_value: uint, power_play: uint, draw_bunch_index: uint, success: bool, ;; whether the round is drawn successfully } ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; bet related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; each element is PBV (define-public (bet (bet_list (list 200 uint))) (let ( (round (var-get m_round)) (bet_item_list (map build_bet_item bet_list)) (bet_count (len bet_item_list)) (power_play_count (fold is_power_play bet_list u0)) (bet_cost (/ (* (+ bet_count bet_count power_play_count) (var-get m_price)) u2)) (bet_bunch_count (default-to u0 (map-get? map_bet_bunch_count round))) (bet_bunch_index (if (> bet_bunch_count u0) bet_bunch_count u1)) (bet_bunch (default-to (list) (map-get? map_bet_bunch { round: round, index: bet_bunch_index }))) (bunch_len (len bet_bunch)) (new_total_bet_count (+ (default-to u0 (map-get? map_total_bet_count round)) bet_count)) (player_bet_bunch_count (default-to u0 (map-get? map_player_bet_bunch_count { p: tx-sender, round: round }))) (player_bet_bunch_index (if (> player_bet_bunch_count u0) player_bet_bunch_count u1)) (player_bet_bunch (default-to (list) (map-get? map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index }))) (player_bet_bunch_len (len player_bet_bunch)) ) (asserts! (is-eq (var-get m_state) STATE_BET) (err ERR_INVALID_STATE)) (asserts! (> bet_count u0) (err ERR_NO_BET)) (asserts! (>= (stx-get-balance tx-sender) bet_cost) (err ERR_BALANCE_NOT_ENOUGH)) ;; deduct (unwrap! (stx-transfer? bet_cost tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX)) (map-set map_total_bet_count round new_total_bet_count) (and (is-eq new_total_bet_count bet_count) (var-set m_start_at block-height) (map-set map_bet_bunch_count round u1) ) ;; note bet related data (if (<= (+ bunch_len bet_count) BUNCH_CAPACITY) (map-set map_bet_bunch { round: round, index: bet_bunch_index } (map get_bet_item_element (concat bet_bunch bet_item_list) BUNCH_NUM_LIST)) (let ( (next_bet_bunch_index (+ bet_bunch_index u1)) ) (map-set map_bet_bunch_count round next_bet_bunch_index) (if (is-eq bunch_len BUNCH_CAPACITY) (map-set map_bet_bunch { round: round, index: next_bet_bunch_index } bet_item_list) (begin (map-set map_bet_bunch { round: round, index: bet_bunch_index } (map get_bet_item_element (concat bet_bunch bet_item_list) BUNCH_NUM_LIST)) (var-set m_omit_times (- BUNCH_CAPACITY bunch_len)) (map-set map_bet_bunch { round: round, index: next_bet_bunch_index } (filter sub_bet_item_list_loop bet_item_list)) ) ) ) ) ;; note player related data (and (is-eq player_bet_bunch_count u0) (map-set map_player_bet_bunch_count { p: tx-sender, round: round } u1) ) (if (<= (+ player_bet_bunch_len bet_count) BUNCH_CAPACITY) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index } (map get_bet_value_element (concat player_bet_bunch bet_list) BUNCH_NUM_LIST) ) (let ( (new_player_bet_bunch_count (+ player_bet_bunch_count u1)) ) (map-set map_player_bet_bunch_count { p: tx-sender, round: round } new_player_bet_bunch_count) (if (is-eq player_bet_bunch_len BUNCH_CAPACITY) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: new_player_bet_bunch_count } bet_list) (begin (map-set map_player_bet_bunch { p: tx-sender, round: round, index: player_bet_bunch_index } (map get_bet_value_element (concat player_bet_bunch bet_list) BUNCH_NUM_LIST) ) (var-set m_omit_times (- BUNCH_CAPACITY player_bet_bunch_len)) (map-set map_player_bet_bunch { p: tx-sender, round: round, index: new_player_bet_bunch_count } (filter sub_bet_value_list_loop bet_list) ) ) ) ) ) ;; if has started 2 day, enter wait-draw state (and (>= (- block-height (var-get m_start_at)) BLOCKS_PER_ROUND) (var-set m_state STATE_WAIT_DRAW) (var-set m_end_at block-height) ) ;; (ok bet_count) ) ) (define-private (is_power_play (pbv uint) (count uint)) (if (>= pbv u1000000000000) (+ count u1) count) ) (define-private (build_bet_item (pbv uint)) { p: tx-sender, pbv: pbv } ) (define-private (get_bet_item_element (element { p: principal, pbv: uint }) (index uint)) element ) (define-private (sub_bet_item_list_loop (element { p: principal, pbv: uint })) (let ( (omit_times (var-get m_omit_times)) ) (if (> omit_times u0) (and (var-set m_omit_times (- omit_times u1)) false ) true ) ) ) (define-private (get_bet_value_element (element uint) (index uint)) element ) (define-private (sub_bet_value_list_loop (element uint)) (let ( (omit_times (var-get m_omit_times)) ) (if (> omit_times u0) (and (var-set m_omit_times (- omit_times u1)) false ) true ) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; bet related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; draw related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (step_draw) (if (is-eq (var-get m_state) STATE_DRAW) (ok (handle_step_draw)) (if (is-eq (var-get m_state) STATE_WAIT_DRAW) (if (> block-height (+ (var-get m_end_at) DRAW_CD_BLOCKS)) (let ( (round (var-get m_round)) ) (var-set m_state STATE_DRAW) ;; safely reset (var-set m_omit_times u0) (var-set m_draw_ball_value (calc_draw_ball_value)) (var-set m_draw_power_play (calc_power_play)) (var-set m_draw_bunch_index u1) (var-set m_draw_jackpot_reward_index u1) (var-set m_draw_jackpot_ave_award u0) (map-set map_draw_win_count round u0) (map-set map_draw_jackpot_count round u0) (ok (handle_step_draw)) ) (err ERR_WAIT_TO_DRAW) ) (err ERR_INVALID_STATE) ) ) ) (define-private (calc_power_play) (let ((rand_num (get-random-by-block-height (+ (var-get m_end_at) u4))) (can_10x (<= (var-get m_jackpot) (* (var-get m_price) POWER_PLAY_10x_THRESHOLD_FACTOR))) (target_num (+ (mod (/ rand_num u10) (if can_10x u43 u42)) u1))) (if (<= target_num u24) u2 (if (<= target_num u37) u3 (if (<= target_num u40) u4 (if (<= target_num u42) u5 u10)))))) (define-private (calc_draw_ball_value) (let ( (end_at (var-get m_end_at)) (rand_1 (get-random-by-block-height (+ end_at u1))) (rand_2 (get-random-by-block-height (+ end_at u2))) (rand_3 (get-random-by-block-height (+ end_at u3))) (w1 (+ (mod (/ rand_1 u100) u69) u1)) (t1 (map-set map_white w1 true)) (w2 (if (begin (var-set m_draw_white u0) (fold G1 LIST_1_10 rand_2) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w3 (if (begin (var-set m_draw_white u0) (fold G1 LIST_1_10 rand_3) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w4 (if (begin (var-set m_draw_white u0) (fold G1 LIST_11_20 rand_1) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) (w5 (if (begin (var-set m_draw_white u0) (fold G1 LIST_11_20 rand_2) (> (var-get m_draw_white) u0)) (var-get m_draw_white) (begin (fold G2 LIST_5 true) (var-get m_draw_white)))) ;; (wl (list w1 w2 w3 w4 w5)) ;; bubble sort white balls (res_1 (fold S wl { i: u0, m: u70, mi: u0, ov: u0 })) ;; i(index), m(min_value), mi(min_index), ov(omit_value) (res_2 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_1) (pow u10 (get mi res_1)))})) (res_3 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_2) (pow u10 (get mi res_2)))})) (res_4 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_3) (pow u10 (get mi res_3)))})) (res_5 (fold S wl { i: u0, m: u70, mi: u0, ov: (+ (get ov res_4) (pow u10 (get mi res_4)))})) (red (+ (mod (/ rand_3 u1000000) u26) u1)) ) (filter R LIST_69) (+ (get m res_1) (* (get m res_2) u100) (* (get m res_3) u10000) (* (get m res_4) u1000000) (* (get m res_5) u100000000) (* red u10000000000) ) ) ) (define-private (G1 (i uint) (r uint)) (if (is-eq r u0) u0 (let ((d (/ r (pow u10 i))) (w (+ (mod d u69) u1))) (if (and (is-none (map-get? map_white w)) (> d u0)) (begin (map-set map_white w true) (var-set m_draw_white w) u0) r)))) (define-private (G2 (w uint) (b bool)) (if (and b (is-none (map-get? map_white w))) (begin (map-set map_white w true) (var-set m_draw_white w) false) b)) (define-private (R (i uint)) (map-delete map_white i)) ;; bubble sort white balls loop. i(index), m(min_value), mi(min_index), ov(omit_value) (define-private (S (num uint) (user_data { i: uint, m: uint, mi: uint, ov: uint })) (let ( (i (get i user_data)) (ov (get ov user_data)) ) (if (and (is-eq (mod (/ ov (pow u10 i)) u10) u0) (< num (get m user_data))) (merge user_data { i: (+ i u1), m: num, mi: i, }) (merge user_data { i: (+ i u1) }) ) ) ) (define-private (handle_step_draw) (let ( (round (var-get m_round)) (bet_bunch_count (unwrap-panic (map-get? map_bet_bunch_count round))) (draw_bunch_index (var-get m_draw_bunch_index)) (contract_balance (stx-get-balance (as-contract tx-sender))) (draw_caller_reward (var-get m_draw_caller_reward)) (caller tx-sender) (comb_value (+ (* (var-get m_price) u1000000000000) (var-get m_draw_ball_value))) ) ;; reward the caller to cover its fee cost (and (>= contract_balance draw_caller_reward) (unwrap! (as-contract (stx-transfer? draw_caller_reward tx-sender caller)) false) ) ;; draw 3 bunch (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: draw_bunch_index })) (+ (* contract_balance u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u1) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ;; DON't use contract_balance, not real-time ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u2) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u3) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: (+ draw_bunch_index u4) })) (+ (* (stx-get-balance (as-contract tx-sender)) u10000000000000000000) comb_value) ) (var-set m_draw_bunch_index (+ draw_bunch_index u5)) ;; Distribute jackpot award when draw finish as the jackpot may be shared by more than 1 players. ;; The contract balance may be unenough at this moment (very rare, reward the remain balance to it/them). (if (and (>= (var-get m_draw_bunch_index) bet_bunch_count) (is-eq (len (default-to (list) (map-get? map_bet_bunch { round: round, index: (var-get m_draw_bunch_index)}))) u0)) (if (> (default-to u0 (map-get? map_draw_jackpot_count round)) u0) (step_reward_jackpot round) (draw_end true) ) true ) ) ) ;; D(draw_one_loop), b(bet_info), d(draw_bv), a(award_mul), r(round), ba(balance), u(user_value)=contract_balance * 10000000000000000000 + bet_price * 1000000000000 + draw_ball_value. Contract balance may be unenough during draw process, will not reward the player in this very rare situation. (define-private (D (b { p: principal, pbv: uint }) (u uint)) (let ((pbv (get pbv b)) (d (mod u u1000000000000))) (match (element-at AL (+ (if (is-eq (/ d u10000000000) (mod (/ pbv u10000000000) u100)) u6 u0) (mod (/ (fold L 0x000000000000000000 (+ (* pbv u1000000000000000) (* d u1000))) u100) u10))) a ;; red*6 + white-same-count (if (> a u0) (let ((r (var-get m_round)) (n (+ (default-to u0 (map-get? map_draw_win_count r)) u1)) (ba (/ u u10000000000000000000)) (award (/ (* a (/ (mod u u10000000000000000000) u1000000000000) (if (> pbv u1000000000000) (if (is-eq a u5000000) u2 (var-get m_draw_power_play)) u1)) u10))) (if (>= ba award) (begin (unwrap! (as-contract (stx-transfer? award tx-sender (get p b))) u) (map-set map_draw_win_count r n) (map-set map_draw_win {round: r, index: n} {p: (get p b), wv: (+ (get pbv b) (* award u100000000000000)) }) (- u (* award u10000000000000000000))) u)) u) ;; jackpot (let ((r (var-get m_round)) (n (+ (default-to u0 (map-get? map_draw_jackpot_count r)) u1))) (map-set map_draw_jackpot_count r n) (map-set map_draw_jackpot { round: r, index: n } (get p b) ) u)))) ;; loop to check how many same white balls the player bet with draw result. u(user_value) = pbv*1000000000000000 + draw_bv*1000 + same_count*100 + pbv_index*10 + draw_bv_index (define-private (L (e (buff 1)) (u uint)) (if (and (< (mod u u10) u5) (< (mod u u100) u50)) (let ((p (mod (/ (/ u u1000000000000000) (pow u100 (mod (/ u u10) u10))) u100)) (d (mod (/ (mod (/ u u1000) u1000000000000) (pow u100 (mod u u10))) u100))) (if (< p d) (+ u u10) (if (> p d) (+ u u1) (+ u u111)))) u)) (define-private (step_reward_jackpot (round uint)) (let ( (jackpot_index (var-get m_draw_jackpot_reward_index)) (jackpot_count (unwrap-panic (map-get? map_draw_jackpot_count round))) ) (if (is-eq jackpot_index u1) (let ( (remain_for_caller (* (var-get m_draw_caller_reward) u1000)) (contract_balance (stx-get-balance (as-contract tx-sender))) (max_award (if (> contract_balance remain_for_caller) (- contract_balance remain_for_caller) u0)) (origin_jackpot (var-get m_jackpot)) (jackpot_reward (if (>= max_award origin_jackpot) origin_jackpot max_award)) ) (var-set m_draw_jackpot_ave_award (/ jackpot_reward jackpot_count)) (fold reward_jackpot_loop LIST_20 u1) (if (<= jackpot_count u20) (draw_end true) (var-set m_draw_jackpot_reward_index u21) ;; too many jackpot players, reward them in next step_draw call. ) ) ;; jackpot players > 20, nearly impossible, but we still need deal this situation (begin (fold reward_jackpot_loop BUNCH_NUM_LIST jackpot_index) (fold reward_jackpot_loop BUNCH_NUM_LIST (+ jackpot_index BUNCH_CAPACITY)) (fold reward_jackpot_loop BUNCH_NUM_LIST (+ jackpot_index BUNCH_CAPACITY BUNCH_CAPACITY)) (if (>= (+ jackpot_index (* BUNCH_CAPACITY u3)) jackpot_count) (draw_end true) (var-set m_draw_jackpot_reward_index (+ jackpot_index (* BUNCH_CAPACITY u3))) ) ) ) ) ) (define-private (reward_jackpot_loop (element uint) (index uint)) (let ( (ave_award (var-get m_draw_jackpot_ave_award)) ) (match (map-get? map_draw_jackpot { round: (var-get m_round), index: index }) player (begin (and (> ave_award u0) (unwrap! (as-contract (stx-transfer? ave_award tx-sender player)) index) ) (+ index u1) ) index ) ) ) (define-private (draw_end (success bool)) (let ( (round (var-get m_round)) (start_balance (var-get m_start_balance)) (cur_balance (stx-get-balance (as-contract tx-sender))) ) (map-set map_history_summary round { start_at: (default-to u0 (get-block-info? time (var-get m_start_at))), end_at: (default-to u0 (get-block-info? time (var-get m_end_at))), price: (var-get m_new_price), bet_count: (default-to u0 (map-get? map_total_bet_count round)), win_count: (default-to u0 (map-get? map_draw_win_count round)), jackpot_count: (default-to u0 (map-get? map_draw_jackpot_count round)), ball_value: (var-get m_draw_ball_value), power_play: (var-get m_draw_power_play), draw_bunch_index: (var-get m_draw_bunch_index), success: success, } ) (if success (print "draw end successfully") (print "draw end due to some error") ) ;; whether need withdraw (and (var-get m_need_withdrawal) (var-set m_need_withdrawal false) (handle_withdraw) ) ;; whether need update price (and (> (var-get m_new_price) u0) (var-set m_jackpot (/ (* (var-get m_jackpot) (var-get m_new_price)) (var-get m_price))) (var-set m_price (var-get m_new_price)) (var-set m_new_price u0) ) ;; if (balance >= MIN_AWARD_POOL) then ;; if success then ;; if has-profit then ;; reward owner, increase jackpot ;; else ;; if jackpot-is-rewarded then ;; reset-jackpot ;; else ;; jackpot = min(jackpot, balance) ;; end ;; end ;; else ;; jackpot = min(jackpot, balance) ;; end ;; start-next-round ;; else ;; start-next-round, wait owner to deposit (if (>= cur_balance (* (var-get m_price) MIN_AWARD_POOL_FACTOR)) (begin (if success (if (> cur_balance start_balance) ;; has-profit (begin (unwrap! (as-contract (stx-transfer? (/ (* (- cur_balance start_balance) u50) u100) tx-sender OWNER)) false) (var-set m_jackpot (+ (var-get m_jackpot) (/ (* (- cur_balance start_balance) u20) u100))) ) (if (> (unwrap-panic (map-get? map_draw_jackpot_count round)) u0) (var-set m_jackpot (* (var-get m_price) DEFAULT_JACKPOT_FACTOR)) (and (< cur_balance (var-get m_jackpot)) (var-set m_jackpot cur_balance)) ) ) ;; not success (and (< cur_balance (var-get m_jackpot)) (var-set m_jackpot cur_balance)) ) ;; start next round for bet (var-set m_state STATE_BET) (var-set m_round (+ round u1)) (var-set m_start_balance (stx-get-balance (as-contract tx-sender))) ) ;; balance unenough (begin (var-set m_state STATE_FUNDING) (var-set m_round (+ round u1)) ) ) ;; clean up (var-set m_start_at u0) (var-set m_end_at u0) (var-set m_omit_times u0) (reset_draw_members) ) ) (define-private (reset_draw_members) (begin (var-set m_draw_white u0) (var-set m_draw_power_play u0) (var-set m_draw_ball_value u0) (var-set m_draw_bunch_index u0) (var-set m_draw_jackpot_reward_index u1) (var-set m_draw_jackpot_ave_award u0) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; draw related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; deposit/withdraw related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (deposit (deposit_count uint)) (let ( (min_award_pool (* (var-get m_price) MIN_AWARD_POOL_FACTOR)) (cur_balance (stx-get-balance (as-contract tx-sender))) (min_need_depot (if (>= cur_balance min_award_pool) u0 (- min_award_pool cur_balance))) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (is-eq (var-get m_state) STATE_FUNDING) (err ERR_INVALID_STATE)) (asserts! (and (> deposit_count u0) (>= deposit_count min_need_depot)) (err ERR_DEPOSIT_COUNT_INVALID)) (asserts! (>= (stx-get-balance tx-sender) deposit_count) (err ERR_BALANCE_NOT_ENOUGH)) ;; (unwrap! (stx-transfer? deposit_count tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX)) ;; safely reset (var-set m_state STATE_BET) (var-set m_start_at u0) (var-set m_end_at u0) (var-set m_start_balance (stx-get-balance (as-contract tx-sender))) (var-set m_omit_times u0) (reset_draw_members) ;; (ok true) ) ) (define-public (withdraw) (let ( (round (var-get m_round)) (state (var-get m_state)) (last_round_bets (default-to u0 (map-get? map_total_bet_count (- round u1)))) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) ;; if last round has enough bets, not allow withdraw (asserts! (<= last_round_bets BET_COUNT_THRERSHOLD_TO_WITHDRAW) (err ERR_NOT_ALLOW_WITHDRAW)) (print (var-get m_state)) (if (is-eq state STATE_FUNDING) (ok (handle_withdraw)) (if (and (is-eq state STATE_BET) (is-eq (default-to u0 (map-get? map_total_bet_count round)) u0)) (begin (handle_withdraw) (var-set m_state STATE_FUNDING) (ok true) ) (ok (var-set m_need_withdrawal true)) ;; wait for draw finish ) ) ) ) (define-private (handle_withdraw) (let ( (balance (stx-get-balance (as-contract tx-sender))) ) (and (> balance u0) (unwrap! (as-contract (stx-transfer? balance tx-sender OWNER)) false)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; deposit/withdraw related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; misc begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (update_price (price uint)) (let ( (state (var-get m_state)) (round (var-get m_round)) (last_round_bets (default-to u0 (map-get? map_total_bet_count (- round u1)))) (cur_price (var-get m_price)) ) (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (<= price u2000000) (err ERR_INVALID_PRICE)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_NOT_ENOUGH)) (if (is-eq (var-get m_state) STATE_FUNDING) (and (var-set m_jackpot (/ (* (var-get m_jackpot) price) (var-get m_price))) (var-set m_price price) ) (var-set m_new_price price) ) (ok true) ) ) (define-public (update_draw_caller_reward (reward uint)) (begin (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (and (< reward u100000)) (err ERR_INVALID_REWARD)) (var-set m_draw_caller_reward reward) (ok true) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; misc end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; random generator begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; reference: 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-random-by-block-height (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? burnchain-header-hash block-index)))) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; random generator end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; web use begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-read-only (get_summary (player_opt (optional principal))) (let ( (round (var-get m_round)) (start_at (var-get m_start_at)) (end_at (var-get m_end_at)) (player_bet_bunch_count (if (is-some player_opt) (default-to u0 (map-get? map_player_bet_bunch_count { round: round, p: (unwrap-panic player_opt)})) u0 ) ) (player_latest_bet_bunch (if (is-some player_opt) (map-get? map_player_bet_bunch { round: round, p: (unwrap-panic player_opt), index: player_bet_bunch_count }) none ) ) ) { state: (var-get m_state), round: round, ;; start_at: start_at, start_time: (default-to u0 (get-block-info? time start_at)), end_at: end_at, end_time: (default-to u0 (get-block-info? time end_at)), price: (var-get m_price), jackpot: (var-get m_jackpot), total_bet_count: (default-to u0 (map-get? map_total_bet_count round)), bet_bunch_count: (default-to u0 (map-get? map_bet_bunch_count round)), player_bet_bunch_count: player_bet_bunch_count, player_latest_bet_bunch: player_latest_bet_bunch, block_height: block-height, draw_cd: DRAW_CD_BLOCKS, draw_caller_reward: (var-get m_draw_caller_reward), draw_power_play: (var-get m_draw_power_play), draw_ball_value: (var-get m_draw_ball_value), draw_bunch_index: (var-get m_draw_bunch_index), balance: (stx-get-balance (as-contract tx-sender)), } ) ) (define-read-only (get_history_summary (round uint)) (map-get? map_history_summary round) ) (define-read-only (get_bet_bunch_count (round uint)) (map-get? map_bet_bunch_count round) ) (define-read-only (get_bet_bunch (round uint) (index uint)) (map-get? map_bet_bunch { round: round, index: index }) ) (define-read-only (get_win_count (round uint)) (map-get? map_draw_win_count round) ) (define-read-only (get_win_list (key_list (list 25 { round: uint, index: uint }))) (map get_win_item key_list) ) (define-read-only (get_win_item (key { round: uint, index: uint })) (map-get? map_draw_win key) ) (define-read-only (get_jackpot_count (round uint)) (map-get? map_draw_jackpot_count round) ) (define-read-only (get_jackpot_list (key_list (list 25 { round: uint, index: uint }))) (map get_jackpot_item key_list) ) (define-read-only (get_jackpot_item (key { round: uint, index: uint })) (map-get? map_draw_jackpot key) ) (define-read-only (get_player_bet_bunch_count (round uint) (player principal)) (default-to u0 (map-get? map_player_bet_bunch_count { round: round, p: player })) ) (define-read-only (get_player_bet_bunch (round uint) (player principal) (index uint)) (map-get? map_player_bet_bunch { round: round, p: player, index: index }) ) (define-public (manual_skip_draw) (begin (asserts! (is-eq tx-sender OWNER) (err ERR_NO_AUTHORITY)) (asserts! (or (is-eq (var-get m_state) STATE_WAIT_DRAW) (is-eq (var-get m_state) STATE_DRAW)) (err ERR_INVALID_STATE)) (asserts! (> block-height (+ (var-get m_end_at) MANUAL_SKIP_DRAW_BLOCKS)) (err ERR_CANNOT_MANUAL_SKIP_DRAW_NOW)) (ok (draw_end false)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; web use end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; initialize related begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (initialize) (begin (print "initialize") (var-set m_state STATE_FUNDING) (var-set m_round u1) (var-set m_price u20) (var-set m_draw_caller_reward u500) (var-set m_jackpot (* (var-get m_price) DEFAULT_JACKPOT_FACTOR)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; initialize related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (initialize)
0x34ee15dda964934cd3ae5829624288e6381540910156ef62cea99aea2ad4e48a
27,096
success
poxlsoft-token-v0a2dwqat4g
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xd2d0c6a55b69cc774c10b009e91ef2bdea75e9462d8f7b3502747932b71280e3
27,151
success
poxlsoft-token-v0aocr0eg1p
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xe8e11746559a40b6f5f1e34c5520c66ca025973308baecd0de828885283e5e1a
27,152
success
poxlsoft-token-v0afh13yqld
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x6a8f25a52afdf74341b08071eb59ffe877aa5531ca01031d5748cad1f3b58817
27,152
success
poxlsoft-token-v0awb6gvsm8
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x9116efee2cff9107ac0cce0449978cddd6c70e10c8eea2bb14b657916c5ab7c7
27,152
success
poxlsoft-token-v0a4hzk3iu8
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xbfb68f4dca109ad6ff618233b3197ecd46a7f3de6fb43a29eeb61cf9e98007b3
27,152
success
poxlsoft-token-v0astfqw7cl
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x35760362d0a81057856fb3e2920258fade20642da5312a7d712d4568d63aa16a
27,155
success
poxlsoft-token-v0a6pqhckpv
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x030816300eb515d058961066c1eef5cef1e408a4037d8febceb97d28d99ed273
27,155
success
poxlsoft-token-v0aq7qrke29
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x1a1281927f15cd57469bf3cac035522a1b458b182977fcb28b7ab00fa71a93e9
27,155
success
poxlsoft-token-v0alf4vlnmr
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xca47ce7f6a5c6415af70ebd98da2b249635ae970d743e585ef4031419d9ec252
27,155
success
poxlsoft-token-v0acaguuypc
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x36a0792f852e50d2ede1b7b535a539606c47ebc2be6897bc5b2cd752a6e07e7c
27,155
success
poxlsoft-token-v0arf9mxd1y
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x2a7e65b0303a28631a81d944500d68c81f8f983f3ed1ad522c716026e9eb8dd6
27,156
success
poxlsoft-token-v0afm28z8wk
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xc93aecafbf4f471ccdee1186a918ba067b921ede8626f47bbf4007bbbde51052
27,156
success
poxlsoft-token-v0ayilt0sga
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x1ff847c324acda9ff31c886697375af4824b0bce233696d13cd5f4b05fe02455
27,156
success
poxlsoft-token-v0ai73azjqp
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xbef106dd9348470f20dca3acf087573f0f7b8fadea08295b2d08ccabedc63f58
27,156
success
poxlsoft-token-v0ameafk6fc
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xf36ab4b7cecde0aa633772691b97ea45f019568e23f301e8fc6fe27025809123
27,156
success
poxlsoft-token-v0a642kq5k7
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xf957ac2ff03e49ffd2c0ac81acf267a8031d567f643f47eb0d23fadc88570839
27,156
success
poxlsoft-token-v0agoavfi22
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x8c9a912777582a008386cc0ef8c2c6d79005e8118466267339739489d2bd9fd2
27,157
success
poxlsoft-token-v0a6d1ooxck
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x68bc923e09c276ce675e4abf73320ae936d718903c8b8ba47fbbd6d92f6aae55
27,157
success
poxlsoft-token-v0a4hdgqkwc
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x3744181221eed78822feef57452b26e4f6ca6d5d0986c6281697407c7070f956
27,157
success
poxlsoft-token-v0ax13jia4a
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x979501028a578f52664e47f5056348089c3c28a8ef9fc8ba6894ba38849dc136
27,157
success
poxlsoft-token-v0a9sb8x8a2
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x4712b13aacec735a84814113ed3a8704d03a2bf79b0c22c6f7f3f38eaa73fdd2
27,157
abort_by_response
poxlsoft-token-v0a5wrmnfsn
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xa305502595b1664714bcadb8e7c9e49139b5e9c257e08537bd9991d89b1fea25
27,166
abort_by_response
poxlsoft-token-v0a5wrmnfsn
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0xa305502595b1664714bcadb8e7c9e49139b5e9c257e08537bd9991d89b1fea25
27,166
abort_by_response
medical-azure-seahorse
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SPGDNN7DAWFY9P1TBWBTV4D2ZGZ2H53EH9QDFD6J) (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)))
0x233df56e5ef0bf9e1dc2097b422f9d1dfabf151b69e182bc29ccc7279c4f62ec
27,193
success
poxlsoft-token-v0a32cd81tf
;; 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) (define-data-var rem-item uint u0) ;; 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 } ) ;; 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 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)) ;; (try! (ft-mint? stackables (get-coinbase-amount stacks-block-ht) recipient)) (ok true) ;; (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 }) ) ;; 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)) (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) } ) (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) )) ) (define-read-only (can-claim-mining-reward (user principal) (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 user 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)) ;; (fold remove-block-per-user (get miners miners-rec) mined-stacks-block-ht) (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)) ;; (fold remove-block-per-user (get miners miners-rec) 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))
0x4f4e9e218298604e40cc943e80e8274f003baf059926c2c2bd3080e519250db4
27,212
abort_by_response