contract_name
stringlengths
1
40
code_body
stringlengths
0
100k
txid
stringlengths
66
66
block_height
int32
0
165k
status
stringclasses
3 values
poxlsoft-token-v0a2tb0dq34
;; 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))
0xdcc06a2debcae8f68599c03ca25f8d20ae82eab5e012ff60a48a6f0a05288520
27,215
abort_by_response
poxlsoft-token-v0a1mkdm60s
;; 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))
0x9a64ecb4b8a691078020d3961b365d42145219743be712192d73d7faad1532ed
27,216
abort_by_response
poxlsoft-token-v0a985nzwmj
;; 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))
0xd4a33045da14ac5e4c11d3f5ceb60f068963ca4d7092f46a51b51bd3774ea40a
27,216
abort_by_response
poxlsoft-token-v0a2tb0dq34
;; 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))
0xdcc06a2debcae8f68599c03ca25f8d20ae82eab5e012ff60a48a6f0a05288520
27,215
abort_by_response
poxlsoft-token-v0a1mkdm60s
;; 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))
0x9a64ecb4b8a691078020d3961b365d42145219743be712192d73d7faad1532ed
27,216
abort_by_response
poxlsoft-token-v0a985nzwmj
;; 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))
0xd4a33045da14ac5e4c11d3f5ceb60f068963ca4d7092f46a51b51bd3774ea40a
27,216
abort_by_response
poxlsoft-token-v0aupns3ydp
;; 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))
0x9286c306ed637231c27a20eeecb37e80dabf786811c6bec54876672f42c35ee8
27,265
abort_by_response
poxlsoft-token-v0a7vg9ttfp
;; 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))
0x718676dfcfe3ae943b42e72e6b3e83f6b7e4c85a2e95e0e8e3fd7bd70b165036
27,265
abort_by_response
stacks-punks-market
;; Buy and Sell StacksPunks ;; Takes 2.5% commission (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-BID-NOT-HIGH-ENOUGH u100) (define-constant ERR-PUNK-NOT-FOR-SALE u101) (define-constant CONTRACT-OWNER tx-sender) (define-map punks-for-sale { id: uint } { seller: (optional principal), price: uint }) (define-map punks-by-seller { seller: principal } { ids: (list 2500 uint) }) (define-map punk-bids { id: uint } { buyer: principal, offer: uint }) (define-data-var listed-punk-ids (list 4000 uint) (list )) (define-data-var removing-punk-id uint u0) (define-data-var sale-commission uint u250) ;; 250 basis points (define-read-only (get-listed-punk-ids) (ok (var-get listed-punk-ids)) ) (define-read-only (get-punk-for-sale (punk-id uint)) (default-to { seller: none, price: u99000000000000 } (map-get? punks-for-sale { id: punk-id }) ) ) (define-public (list-punk (punk-id uint) (price uint)) (let ( (punk-owner (unwrap-panic (contract-call? .stacks-punks-v3 get-owner punk-id))) (punk-ids (unwrap-panic (get-punks-by-seller tx-sender))) ) (asserts! (is-eq tx-sender (unwrap-panic punk-owner)) (err ERR-NOT-AUTHORIZED)) (map-set punks-for-sale { id: punk-id } { seller: (some tx-sender), price: price }) (map-set punks-by-seller { seller: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids punk-id) u2500)) } ) (var-set listed-punk-ids (unwrap-panic (as-max-len? (append (var-get listed-punk-ids) punk-id) u4000))) (contract-call? .stacks-punks-v3 transfer punk-id tx-sender (as-contract tx-sender)) ) ) (define-read-only (get-punks-entry-by-seller (seller principal)) (default-to { ids: (list ) } (map-get? punks-by-seller { seller: seller }) ) ) (define-public (get-punks-by-seller (seller principal)) (ok (get ids (get-punks-entry-by-seller seller))) ) (define-read-only (get-punk-bid (punk-id uint)) (default-to { buyer: CONTRACT-OWNER, offer: u0 } (map-get? punk-bids { id: punk-id }) ) ) (define-public (unlist-punk (punk-id uint)) (let ( (punk (get-punk-for-sale punk-id)) (sender tx-sender) (bid (get-punk-bid punk-id)) ) (asserts! (is-some (get seller punk)) (err ERR-PUNK-NOT-FOR-SALE)) (asserts! (is-eq sender (unwrap-panic (get seller punk))) (err ERR-NOT-AUTHORIZED)) (try! (remove-punk-listing punk-id sender)) (map-delete punks-for-sale { id: punk-id }) (if (> (get offer bid) u0) (begin (try! (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) (get buyer bid)))) (map-delete punk-bids { id: punk-id }) ) true ) (as-contract (contract-call? .stacks-punks-v3 transfer punk-id (as-contract tx-sender) sender)) ) ) (define-public (bid-punk (punk-id uint) (amount uint)) (let ( (punk (get-punk-for-sale punk-id)) (bid (get-punk-bid punk-id)) ) (asserts! (is-some (get seller punk)) (err ERR-PUNK-NOT-FOR-SALE)) (asserts! (> amount (get offer bid)) (err ERR-BID-NOT-HIGH-ENOUGH)) (match (stx-transfer? amount tx-sender (as-contract tx-sender)) success (begin (map-set punk-bids { id: punk-id } { buyer: tx-sender, offer: amount }) (ok amount) ) error (err error) ) ) ) (define-public (withdraw-bid (punk-id uint)) (let ( (punk (get-punk-for-sale punk-id)) (bid (get-punk-bid punk-id)) (sender tx-sender) ) (asserts! (is-some (get seller punk)) (err ERR-PUNK-NOT-FOR-SALE)) (asserts! (is-eq tx-sender (get buyer bid)) (err ERR-NOT-AUTHORIZED)) (match (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) sender)) success (begin (map-delete punk-bids { id: punk-id }) (ok (get offer bid)) ) error (err error) ) ) ) (define-public (accept-bid (punk-id uint)) (let ( (punk (get-punk-for-sale punk-id)) (bid (get-punk-bid punk-id)) (commission (/ (* (get offer bid) (var-get sale-commission)) u10000)) ) (asserts! (is-some (get seller punk)) (err ERR-PUNK-NOT-FOR-SALE)) (asserts! (is-eq tx-sender (unwrap-panic (get seller punk))) (err ERR-NOT-AUTHORIZED)) (try! (as-contract (stx-transfer? commission (as-contract tx-sender) CONTRACT-OWNER))) (try! (as-contract (stx-transfer? (- (get offer bid) commission) (as-contract tx-sender) (unwrap-panic (get seller punk))))) (try! (remove-punk-listing punk-id (unwrap-panic (get seller punk)))) (map-delete punks-for-sale { id: punk-id }) (try! (as-contract (contract-call? .stacks-punks-v3 transfer punk-id (as-contract tx-sender) (get buyer bid)))) (ok true) ) ) (define-public (buy-punk (punk-id uint)) (let ( (punk-owner (unwrap-panic (contract-call? .stacks-punks-v3 get-owner punk-id))) (punk (get-punk-for-sale punk-id)) (sender tx-sender) (commission (/ (* (get price punk) (var-get sale-commission)) u10000)) (bid (get-punk-bid punk-id)) ) (asserts! (not (is-eq tx-sender (unwrap-panic punk-owner))) (err ERR-NOT-AUTHORIZED)) (asserts! (is-some (get seller punk)) (err ERR-PUNK-NOT-FOR-SALE)) (try! (stx-transfer? commission sender CONTRACT-OWNER)) (if (not (is-eq sender (unwrap-panic (get seller punk)))) (try! (stx-transfer? (- (get price punk) commission) sender (unwrap-panic (get seller punk)))) true ) (if (> (get offer bid) u0) (begin (try! (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) (get buyer bid)))) (map-delete punk-bids { id: punk-id }) ) true ) (try! (remove-punk-listing punk-id (unwrap-panic (get seller punk)))) (map-delete punks-for-sale { id: punk-id }) (try! (as-contract (contract-call? .stacks-punks-v3 transfer punk-id (as-contract tx-sender) sender))) (ok true) ) ) (define-private (remove-punk-listing (punk-id uint) (sender principal)) (if true (let ( (punk-ids (unwrap-panic (get-punks-by-seller sender))) ) (var-set removing-punk-id punk-id) (var-set listed-punk-ids (unwrap-panic (as-max-len? (filter remove-punk-id (var-get listed-punk-ids)) u4000))) (map-set punks-by-seller { seller: sender } { ids: (unwrap-panic (as-max-len? (filter remove-punk-id punk-ids) u2500)) } ) (ok true) ) (err u0) ) ) (define-private (remove-punk-id (punk-id uint)) (if (is-eq punk-id (var-get removing-punk-id)) false true ) ) (define-public (admin-unlist (punk-id uint)) (let ( (punk (get-punk-for-sale punk-id)) (bid (get-punk-bid punk-id)) ) (asserts! (is-eq tx-sender CONTRACT-OWNER) (err ERR-NOT-AUTHORIZED)) (asserts! (is-some (get seller punk)) (err ERR-PUNK-NOT-FOR-SALE)) (try! (remove-punk-listing punk-id (unwrap-panic (get seller punk)))) (map-delete punks-for-sale { id: punk-id }) (if (> (get offer bid) u0) (begin (try! (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) (get buyer bid)))) (map-delete punk-bids { id: punk-id }) ) true ) (as-contract (contract-call? .stacks-punks-v3 transfer punk-id (as-contract tx-sender) (unwrap-panic (get seller punk)))) ) ) (define-public (set-sale-commission (commission uint)) (begin (asserts! (is-eq tx-sender CONTRACT-OWNER) (err ERR-NOT-AUTHORIZED)) (ok (var-set sale-commission commission)) ) )
0x34696e09e9a4a067e36b4342a1ff7ee875ec6eb3af3f4641b5198181d59992f7
27,328
success
stacks-stingers-v0
(define-constant ERR_NO_DEPOSITS_AT_THIS_BLOCK u1000) (define-constant ERR_NO_HIGH_AMOUNT_IN_DEPOSIT u1001) (define-constant ERR_NO_DEPOSIT_BY_HEIGHT u1002) (define-constant ERR_MAP_EXISTS_FOR_HEIGHT u1003) (define-constant ERR_INSUFFICIENT_BALANCE u1004) (define-constant ERR_UNABLE_TO_GET_DEPOSITS_FOR_BLOCK u1005) (define-constant ERR_UNABLE_TO_GET_LAST_HIGH_AMOUNT_FOR_BLOCK u1006) (define-constant ERR_UNABLE_TO_GET_WINNING_ADDRESS u1007) (define-constant ERR_NOTHING_TO_REDEEM u1008) (define-constant ERR_NO_WINNER_AT_THIS_BLOCK u1009) (define-fungible-token stinger) (define-data-var last-block-height uint u0) (define-map deposits { block-height: uint } (list 100 { address: principal, amount: uint, low-amount: uint, high-amount: uint, memo: (string-utf8 70)})) (define-map winners { block-height: uint } { address: principal, amount: uint}) (define-read-only (get-deposit-last-high-amount-by-height (height uint)) (let ((d (unwrap! (map-get? deposits {block-height: height }) (err ERR_NO_DEPOSITS_AT_THIS_BLOCK)))) (ok (unwrap! (get high-amount (element-at d (- (len d) u1))) (err ERR_NO_HIGH_AMOUNT_IN_DEPOSIT))))) (define-private (append-deposit (amount uint) (memo (string-utf8 70)) (height uint)) (match (get-deposits-by-height height) deps (if (map-set deposits { block-height: height} (unwrap! (as-max-len? (append deps { address: tx-sender, amount: amount, low-amount: (unwrap! (get-deposit-last-high-amount-by-height height) (err ERR_NO_DEPOSIT_BY_HEIGHT)), high-amount: (+ amount (unwrap! (get-deposit-last-high-amount-by-height height) (err ERR_NO_DEPOSIT_BY_HEIGHT))), memo: memo }) u100) (err ERR_NO_DEPOSITS_AT_THIS_BLOCK))) (ok true) (err ERR_MAP_EXISTS_FOR_HEIGHT)) error (err ERR_NO_DEPOSITS_AT_THIS_BLOCK))) (define-read-only (get-deposits-by-height (height uint)) (let ((deposits-at-height (unwrap! (map-get? deposits {block-height: height }) (err ERR_NO_DEPOSITS_AT_THIS_BLOCK)))) (ok deposits-at-height))) (define-read-only (get-winner-by-height (height uint)) (ok (unwrap! (map-get? winners {block-height: height }) (err ERR_NO_WINNER_AT_THIS_BLOCK)))) (define-public (deposit (amount uint) (memo (string-utf8 70))) (begin (match (stx-transfer? amount tx-sender (as-contract tx-sender)) deps (begin (if (map-insert deposits { block-height: block-height } (list { address: tx-sender, amount: amount, low-amount: u1, high-amount: amount, memo: memo })) (award-stinger) (append-deposit amount memo block-height))) error (err ERR_INSUFFICIENT_BALANCE)))) (define-read-only (randomize (seed uint) (max uint)) (let ((vrfSample (unwrap! (get-random-uint-at-block seed) u1)) (winningValue (mod vrfSample max))) winningValue)) (define-private (get-winning-address (entry { address: principal, amount: uint, low-amount: uint, high-amount: uint, memo: (string-utf8 70)}) (context { random-value: uint, result: (optional principal)})) (let ((random-value (get random-value context)) (result (get result context))) (if (is-some result) context (if (and (> random-value (get low-amount entry)) (< random-value (get high-amount entry))) {random-value: random-value, result: (some (get address entry))} context)))) (define-private (award-stinger) (if (> (var-get last-block-height) u0) (let ((h (var-get last-block-height)) (d (unwrap! (get-deposits-by-height h) (err ERR_UNABLE_TO_GET_DEPOSITS_FOR_BLOCK))) (lh (unwrap! (get-deposit-last-high-amount-by-height h) (err ERR_UNABLE_TO_GET_LAST_HIGH_AMOUNT_FOR_BLOCK))) (a (unwrap! (get result (fold get-winning-address d {random-value: (randomize h lh), result: none})) (err ERR_UNABLE_TO_GET_WINNING_ADDRESS)))) (match (ft-mint? stinger lh a) success (begin (if (map-insert winners { block-height: h } { address: a, amount: lh}) (ok (var-set last-block-height block-height)) (ok false))) error (ok false))) (begin (var-set last-block-height block-height) (ok false)))) (define-public (redeem-stinger (amount uint)) (let ((recipient tx-sender) (balance (ft-get-balance stinger recipient))) (if (>= balance amount) (begin (try! (as-contract (ft-burn? stinger amount recipient))) (try! (as-contract (stx-transfer? amount tx-sender recipient))) (ok true) ) (err ERR_NOTHING_TO_REDEEM)))) ;; VRF ;; 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 (stacksBlock uint)) (let ( (vrf-lower-uint-opt (match (get-block-info? vrf-seed stacksBlock) vrf-seed (some (buff-to-uint-le (lower-16-le vrf-seed))) none)) ) vrf-lower-uint-opt) ) ;; UTILITIES ;; 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)) ) ;; 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 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 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 }) ) ) ;; 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 }) )
0x31b6b181a5f0cf7e5eb5297402cd7d4d647e7a1c2c4911030b3593a170ef3c6f
27,433
success
free-punks-v0
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP1172T8E1EQ1MNW9E46MPS91X7D02S45GM1BNGX0) (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)))
0xdbc0658ac180a44cd6fb82036b5e239080576cdb3c43f0e644208bfc19e4b147
27,575
success
boomboxes-cycle-16
(impl-trait 'ST2PABAF9FTAJYNFZH93XENAJ8FVY99RRM4DF2YCW.nft-trait.nft-trait) (define-non-fungible-token b-16 uint) (define-constant dplyr tx-sender) ;; dplyr not used (define-constant accnt (as-contract tx-sender)) (define-constant px-addr {hashbytes: 0x13effebe0ea4bb45e35694f5a15bb5b96e851afb, version: 0x01}) (define-constant minimum-amount u40000000) (define-constant time-limit u699350) ;; add 4200 blocks (define-data-var last-id uint u0) (define-data-var start (optional uint) none) (define-data-var total-stacked uint u0) (define-map meta uint (tuple (stacker principal) (amount-ustx uint) (until-burn-ht (optional uint)) (stacked-ustx (optional uint)) (reward (optional uint)))) (define-map lookup principal uint) (define-private (pox-delegate-stx-and-stack (amount-ustx uint) (until-burn-ht (optional uint))) (begin (let ((ignore-result-revoke (contract-call? 'ST000000000000000000002AMW42H.pox revoke-delegate-stx)) (start-block-ht (+ burn-block-height u1)) (locking-cycles u1)) (match (contract-call? 'ST000000000000000000002AMW42H.pox delegate-stx amount-ustx accnt until-burn-ht none) success (let ((stacker tx-sender)) (match (as-contract (contract-call? 'ST000000000000000000002AMW42H.pox delegate-stack-stx stacker amount-ustx px-addr start-block-ht locking-cycles)) stack-success (ok stack-success) stack-error (print (err (to-uint stack-error))))) error (err (to-uint error)))))) (define-private (mint-and-delegatedly-stack (stacker principal) (amount-ustx uint) (until-burn-ht (optional uint))) (let ((id (+ u1 (var-get last-id)))) (asserts! (>= amount-ustx minimum-amount) err-delegate-below-minimum) (asserts! (< burn-block-height time-limit) err-delegate-too-late) (asserts! (>= (stx-get-balance tx-sender) amount-ustx) err-not-enough-funds) (var-set last-id id) (match (pox-delegate-stx-and-stack amount-ustx until-burn-ht) success-pox (match (nft-mint? b-16 id stacker) success-mint (begin (asserts! (map-insert lookup stacker id) err-map-function-failed) (asserts! (map-insert meta id {stacker: stacker, amount-ustx: amount-ustx, stacked-ustx: (some (get lock-amount success-pox)), until-burn-ht: until-burn-ht, reward: none}) err-map-function-failed) (ok {id: id, pox: success-pox})) error-minting (err-nft-mint error-minting)) error-pox (err error-pox)))) (define-public (delegate-stx (amount-ustx uint) (stacker principal) (until-burn-ht (optional uint)) (pox-addr (optional (tuple (hashbytes (buff 20)) (version (buff 1)))))) (if (and (or (is-eq stacker tx-sender) (is-eq stacker contract-caller)) (is-none (map-get? lookup stacker))) (begin (var-set total-stacked (+ (var-get total-stacked) amount-ustx)) (mint-and-delegatedly-stack stacker amount-ustx until-burn-ht)) err-delegate-invalid-stacker)) ;; function for pool admins (define-private (get-total (stack-result (response (tuple (lock-amount uint) (stacker principal) (unlock-burn-height uint)) (tuple (kind (string-ascii 32)) (code uint)))) (total uint)) (match stack-result details (+ total (get lock-amount details)) error total)) (define-private (update-meta (id uint) (stacked-ustx uint)) (match (map-get? meta id) entry (map-set meta id { stacker: (get stacker entry), amount-ustx: (get amount-ustx entry), until-burn-ht: (get until-burn-ht entry), stacked-ustx: (some stacked-ustx), reward: (get reward entry)}) false)) (define-public (stack-aggregation-commit (reward-cycle uint)) (if (> burn-block-height time-limit) (match (as-contract (contract-call? 'ST000000000000000000002AMW42H.pox stack-aggregation-commit px-addr reward-cycle)) success (ok success) error (err-pox-stack-aggregation-commit error)) err-commit-too-early)) (define-read-only (nft-details (nft-id uint)) (ok {stacked-ustx: (unwrap! (unwrap! (get stacked-ustx (map-get? meta nft-id)) err-invalid-asset-id) err-invalid-asset-id), owner: (unwrap! (nft-get-owner? b-16 nft-id) err-no-asset-owner)})) (define-read-only (nft-details-at-block (nft-id uint) (stacks-tip uint)) (match (get-block-info? id-header-hash stacks-tip) ihh (print (at-block (print ihh) (nft-details nft-id))) err-invalid-stacks-tip)) (define-private (payout-nft (nft-id uint) (ctx (tuple (reward-ustx uint) (total-ustx uint) (stx-from principal) (pay-stacks-tip uint) (result (list 750 (response bool uint)))))) (let ((reward-ustx (get reward-ustx ctx)) (total-ustx (get total-ustx ctx)) (stx-from (get stx-from ctx)) (stacks-tip (get pay-stacks-tip ctx))) (let ( (transfer-result (match (nft-details-at-block nft-id stacks-tip) entry (let ((reward-amount (/ (* reward-ustx (get stacked-ustx entry)) total-ustx))) (match (stx-transfer? reward-amount stx-from (get owner entry)) success-stx-transfer (ok true) error-stx-transfer (err-stx-transfer error-stx-transfer))) error (err error)))) {reward-ustx: reward-ustx, total-ustx: total-ustx, stx-from: stx-from, pay-stacks-tip: stacks-tip, result: (unwrap-panic (as-max-len? (append (get result ctx) transfer-result) u750))}))) (define-private (sum-stacked-ustx (nft-id uint) (total uint)) (match (map-get? meta nft-id) entry (match (get stacked-ustx entry) amount (+ total amount) total) total)) (define-read-only (get-total-stacked-ustx (nfts (list 750 uint))) (fold sum-stacked-ustx nfts u0)) (define-read-only (get-total-stacked-ustx-at-block (nfts (list 750 uint)) (stacks-tip uint)) (match (get-block-info? id-header-hash stacks-tip) ihh (at-block ihh (ok (get-total-stacked-ustx nfts))) err-invalid-stacks-tip)) (define-public (payout (reward-ustx uint) (nfts (list 750 uint)) (pay-stacks-tip uint)) (match (get-total-stacked-ustx-at-block nfts pay-stacks-tip) total-ustx (ok (fold payout-nft nfts {reward-ustx: reward-ustx, total-ustx: total-ustx, stx-from: tx-sender, pay-stacks-tip: pay-stacks-tip, result: (list)})) error (err error))) (define-read-only (get-total-stacked) (var-get total-stacked)) (define-public (allow-contract-caller (this-contract principal)) (if (is-eq tx-sender dplyr) (as-contract (contract-call? 'ST000000000000000000002AMW42H.pox allow-contract-caller this-contract none)) (err 403))) ;; NFT functions (define-public (transfer (id uint) (sender principal) (recipient principal)) (if (or (is-eq sender tx-sender) (is-eq sender contract-caller)) (match (nft-transfer? b-16 id sender recipient) success (ok success) error (err-nft-transfer error)) err-not-allowed-sender)) (define-read-only (get-owner (id uint)) (ok (nft-get-owner? b-16 id))) (define-read-only (get-owner-raw? (id uint)) (nft-get-owner? b-16 id)) (define-read-only (get-last-token-id) (ok (var-get last-id))) (define-read-only (last-token-id-raw) (var-get last-id)) (define-read-only (get-token-uri (id uint)) (ok (some "https://cloudflare-ipfs.com/ipfs/QmTYvatj7HFT3zsFCAz52DLERvr1jtuSfQsXXZbxLjW2Hz"))) ;; error handling (define-constant err-nft-not-owned (err u401)) ;; unauthorized (define-constant err-not-allowed-sender (err u403)) ;; forbidden (define-constant err-nft-not-found (err u404)) ;; not found (define-constant err-sender-equals-recipient (err u405)) ;; method not allowed (define-constant err-nft-exists (err u409)) ;; conflict (define-constant err-not-enough-funds (err u4021)) ;; payment required (define-constant err-amount-not-positive (err u4022)) ;; payment required (define-constant err-map-function-failed (err u601)) (define-constant err-invalid-asset-id (err u602)) (define-constant err-no-asset-owner (err u603)) (define-constant err-delegate-below-minimum (err u604)) (define-constant err-delegate-invalid-stacker (err u605)) (define-constant err-delegate-too-late (err u606)) (define-constant err-commit-too-early (err u607)) (define-constant err-invalid-stacks-tip (err u608)) (define-map err-strings (response uint uint) (string-ascii 32)) (map-insert err-strings err-nft-not-owned "nft-not-owned") (map-insert err-strings err-not-allowed-sender "not-allowed-sender") (map-insert err-strings err-nft-not-found "nft-not-found") (map-insert err-strings err-sender-equals-recipient "sender-equals-recipient") (map-insert err-strings err-nft-exists "nft-exists") (map-insert err-strings err-map-function-failed "map-function-failed") (map-insert err-strings err-invalid-asset-id "invalid-asset-id") (map-insert err-strings err-no-asset-owner "no-asset-owner") (map-insert err-strings err-delegate-below-minimum "delegate-below-minimum") (map-insert err-strings err-delegate-invalid-stacker "delegate-invalid-stacker") (map-insert err-strings err-delegate-too-late "delegate-too-late") (map-insert err-strings err-commit-too-early "commit-too-early") (map-insert err-strings err-invalid-stacks-tip "invalid-stacks-tip") (define-private (err-pox-stack-aggregation-commit (code int)) (err (to-uint (* 1000 code)))) (define-private (err-stx-transfer (code uint)) (if (is-eq u1 code) err-not-enough-funds (if (is-eq u2 code) err-sender-equals-recipient (if (is-eq u3 code) err-amount-not-positive (if (is-eq u4 code) err-not-allowed-sender (err code)))))) (define-private (err-nft-transfer (code uint)) (if (is-eq u1 code) err-nft-not-owned (if (is-eq u2 code) err-sender-equals-recipient (if (is-eq u3 code) err-nft-not-found (err code))))) (define-private (err-nft-mint (code uint)) (if (is-eq u1 code) err-nft-exists (err code))) (define-read-only (get-errstr (code uint)) (unwrap! (map-get? err-strings (err code)) "unknown-error"))
0xaeb2d58cf52c82970da308e0a355d0c26d2ad0a640d277f5e2a703253932039b
27,655
abort_by_response
test
;; 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_UNENOUGH 1002) (define-constant ERR_TRANSFER_STX 1003) (define-constant ERR_NO_AUTHORITY 1004) (define-constant ERR_NO_BET 1005) (define-constant ERR_WAIT_TO_DRAW 1006) (define-constant ERR_INVALID_STATE 1007) (define-constant ERR_INVALID_PRICE 1008) (define-constant ERR_NOT_ALLOW_WITHDRAW 1009) (define-constant ERR_INVALID_REWARD 1010) (define-constant ERR_CANNOT_MANUAL_SKIP_DRAW_NOW 1011) ;; (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_30 (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)) (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 BET_BLOCKS u288) ;; 2 day. duration of STATE_BET (define-constant WAIT_DRAW_BLOCKS u12) ;; 2 hours. duration of STATE_WAIT_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 MIN_DEPOSIT_FACTOR u25000000) ;; $2 => $50000000 (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<=1000, 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 encounter error during draw. Owner can manually skip draw, otherwise 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_withdraw 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! (or (is-eq (var-get m_state) STATE_BET) (and (is-eq (var-get m_state) STATE_WAIT_DRAW) (is-eq block-height (var-get m_end_at)))) (err ERR_INVALID_STATE)) (asserts! (> bet_count u0) (err ERR_NO_BET)) (asserts! (>= (stx-get-balance tx-sender) bet_cost) (err ERR_BALANCE_UNENOUGH)) ;; 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) BET_BLOCKS)) (is-eq (var-get m_state) STATE_BET) (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) WAIT_DRAW_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) u5))) (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 u2))) (rand_2 (get-random-by-block-height (+ end_at u3))) (rand_3 (get-random-by-block-height (+ end_at u4))) (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)) ) (print { title: "ttt", rand_1: rand_1, rand_2: rand_2, rand_3: rand_3, wl: wl, res: (list (get m res_1) (get m res_2) (get m res_3) (get m res_4) (get m res_5) red) }) (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)) (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 (>= (stx-get-balance (as-contract tx-sender)) draw_caller_reward) (unwrap! (as-contract (stx-transfer? draw_caller_reward tx-sender caller)) false) ) ;; draw 5 bunch (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: draw_bunch_index })) (+ (* (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 u1) })) (+ (* (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 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_withdraw) (var-set m_need_withdraw 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) u30) 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_deposit (* (var-get m_price) MIN_DEPOSIT_FACTOR)) (cur_balance (stx-get-balance (as-contract tx-sender))) (min_need_depot (if (>= cur_balance min_deposit) u0 (- min_deposit 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_UNENOUGH)) ;; (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_withdraw 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_UNENOUGH)) (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? vrf-seed 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: WAIT_DRAW_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 (direct_withdraw bool)) (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)) (if direct_withdraw (ok (unwrap! (as-contract (stx-transfer? (stx-get-balance tx-sender) tx-sender OWNER)) (err ERR_TRANSFER_STX))) (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 u100) (var-set m_draw_caller_reward u1000) (var-set m_jackpot (* (var-get m_price) DEFAULT_JACKPOT_FACTOR)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; initialize related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (initialize) (define-private (test_loop (bh uint)) (begin (var-set m_end_at (- block-height bh u10)) (calc_draw_ball_value) false ) ) (define-public (test) (begin (filter test_loop LIST_30) (ok true) ) )
0xf2912f67486ef7a006a062e92288c64c4e123414b16af55165de0a135f2b8d87
27,698
success
power-ball
;; 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_UNENOUGH 1002) (define-constant ERR_TRANSFER_STX 1003) (define-constant ERR_NO_AUTHORITY 1004) (define-constant ERR_NO_BET 1005) (define-constant ERR_WAIT_TO_DRAW 1006) (define-constant ERR_INVALID_STATE 1007) (define-constant ERR_INVALID_PRICE 1008) (define-constant ERR_NOT_ALLOW_WITHDRAW 1009) (define-constant ERR_INVALID_REWARD 1010) (define-constant ERR_CANNOT_MANUAL_SKIP_DRAW_NOW 1011) ;; (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 BET_BLOCKS u288) ;; 2 day. duration of STATE_BET (define-constant WAIT_DRAW_BLOCKS u12) ;; 2 hours. duration of STATE_WAIT_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 MIN_DEPOSIT_FACTOR u25000000) ;; $2 => $50000000 (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<=1000, 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 encounter error during draw. Owner can manually skip draw, otherwise 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_withdraw 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! (or (is-eq (var-get m_state) STATE_BET) (and (is-eq (var-get m_state) STATE_WAIT_DRAW) (is-eq block-height (var-get m_end_at)))) (err ERR_INVALID_STATE)) (asserts! (> bet_count u0) (err ERR_NO_BET)) (asserts! (>= (stx-get-balance tx-sender) bet_cost) (err ERR_BALANCE_UNENOUGH)) ;; 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) BET_BLOCKS)) (is-eq (var-get m_state) STATE_BET) (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) WAIT_DRAW_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)) (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 (>= (stx-get-balance (as-contract tx-sender)) draw_caller_reward) (unwrap! (as-contract (stx-transfer? draw_caller_reward tx-sender caller)) false) ) ;; draw 5 bunch (fold D (default-to (list) (map-get? map_bet_bunch { round: round, index: draw_bunch_index })) (+ (* (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 u1) })) (+ (* (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 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_withdraw) (var-set m_need_withdraw 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) u30) 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_deposit (* (var-get m_price) MIN_DEPOSIT_FACTOR)) (cur_balance (stx-get-balance (as-contract tx-sender))) (min_need_depot (if (>= cur_balance min_deposit) u0 (- min_deposit 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_UNENOUGH)) ;; (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_withdraw 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_UNENOUGH)) (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? vrf-seed 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: WAIT_DRAW_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 "Try Everything") (var-set m_state STATE_FUNDING) (var-set m_round u1) (var-set m_price u100) (var-set m_draw_caller_reward u1000) (var-set m_jackpot (* (var-get m_price) DEFAULT_JACKPOT_FACTOR)) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; initialize related end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (initialize)
0x7220c5d93040277299a60e8e11c9c8a169c81aa666f2ae7c5ee0f7ecb91106fb
27,717
success
boomboxes-cycle-16
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) (define-non-fungible-token b-16 uint) (define-constant dplyr tx-sender) ;; dplyr not used (define-constant accnt (as-contract tx-sender)) (define-constant px-addr {hashbytes: 0x13effebe0ea4bb45e35694f5a15bb5b96e851afb, version: 0x01}) (define-constant minimum-amount u40000000) (define-constant time-limit u699350) ;; add 4200 blocks (define-data-var last-id uint u0) (define-data-var start (optional uint) none) (define-data-var total-stacked uint u0) (define-map meta uint (tuple (stacker principal) (amount-ustx uint) (until-burn-ht (optional uint)) (stacked-ustx (optional uint)) (reward (optional uint)))) (define-map lookup principal uint) (define-private (pox-delegate-stx-and-stack (amount-ustx uint) (until-burn-ht (optional uint))) (begin (let ((ignore-result-revoke (contract-call? 'SP000000000000000000002Q6VF78.pox revoke-delegate-stx)) (start-block-ht (+ burn-block-height u1)) (locking-cycles u1)) (match (contract-call? 'SP000000000000000000002Q6VF78.pox delegate-stx amount-ustx accnt until-burn-ht none) success (let ((stacker tx-sender)) (match (as-contract (contract-call? 'SP000000000000000000002Q6VF78.pox delegate-stack-stx stacker amount-ustx px-addr start-block-ht locking-cycles)) stack-success (ok stack-success) stack-error (print (err (to-uint stack-error))))) error (err (to-uint error)))))) (define-private (mint-and-delegatedly-stack (stacker principal) (amount-ustx uint) (until-burn-ht (optional uint))) (let ((id (+ u1 (var-get last-id)))) (asserts! (>= amount-ustx minimum-amount) err-delegate-below-minimum) (asserts! (< burn-block-height time-limit) err-delegate-too-late) (asserts! (>= (stx-get-balance tx-sender) amount-ustx) err-not-enough-funds) (var-set last-id id) (match (pox-delegate-stx-and-stack amount-ustx until-burn-ht) success-pox (match (nft-mint? b-16 id stacker) success-mint (begin (asserts! (map-insert lookup stacker id) err-map-function-failed) (asserts! (map-insert meta id {stacker: stacker, amount-ustx: amount-ustx, stacked-ustx: (some (get lock-amount success-pox)), until-burn-ht: until-burn-ht, reward: none}) err-map-function-failed) (ok {id: id, pox: success-pox})) error-minting (err-nft-mint error-minting)) error-pox (err error-pox)))) (define-public (delegate-stx (amount-ustx uint) (stacker principal) (until-burn-ht (optional uint)) (pox-addr (optional (tuple (hashbytes (buff 20)) (version (buff 1)))))) (if (and (or (is-eq stacker tx-sender) (is-eq stacker contract-caller)) (is-none (map-get? lookup stacker))) (begin (var-set total-stacked (+ (var-get total-stacked) amount-ustx)) (mint-and-delegatedly-stack stacker amount-ustx until-burn-ht)) err-delegate-invalid-stacker)) ;; function for pool admins (define-private (get-total (stack-result (response (tuple (lock-amount uint) (stacker principal) (unlock-burn-height uint)) (tuple (kind (string-ascii 32)) (code uint)))) (total uint)) (match stack-result details (+ total (get lock-amount details)) error total)) (define-private (update-meta (id uint) (stacked-ustx uint)) (match (map-get? meta id) entry (map-set meta id { stacker: (get stacker entry), amount-ustx: (get amount-ustx entry), until-burn-ht: (get until-burn-ht entry), stacked-ustx: (some stacked-ustx), reward: (get reward entry)}) false)) (define-public (stack-aggregation-commit (reward-cycle uint)) (if (> burn-block-height time-limit) (match (as-contract (contract-call? 'SP000000000000000000002Q6VF78.pox stack-aggregation-commit px-addr reward-cycle)) success (ok success) error (err-pox-stack-aggregation-commit error)) err-commit-too-early)) (define-read-only (nft-details (nft-id uint)) (ok {stacked-ustx: (unwrap! (unwrap! (get stacked-ustx (map-get? meta nft-id)) err-invalid-asset-id) err-invalid-asset-id), owner: (unwrap! (nft-get-owner? b-16 nft-id) err-no-asset-owner)})) (define-read-only (nft-details-at-block (nft-id uint) (stacks-tip uint)) (match (get-block-info? id-header-hash stacks-tip) ihh (print (at-block (print ihh) (nft-details nft-id))) err-invalid-stacks-tip)) (define-private (payout-nft (nft-id uint) (ctx (tuple (reward-ustx uint) (total-ustx uint) (stx-from principal) (pay-stacks-tip uint) (result (list 750 (response bool uint)))))) (let ((reward-ustx (get reward-ustx ctx)) (total-ustx (get total-ustx ctx)) (stx-from (get stx-from ctx)) (stacks-tip (get pay-stacks-tip ctx))) (let ( (transfer-result (match (nft-details-at-block nft-id stacks-tip) entry (let ((reward-amount (/ (* reward-ustx (get stacked-ustx entry)) total-ustx))) (match (stx-transfer? reward-amount stx-from (get owner entry)) success-stx-transfer (ok true) error-stx-transfer (err-stx-transfer error-stx-transfer))) error (err error)))) {reward-ustx: reward-ustx, total-ustx: total-ustx, stx-from: stx-from, pay-stacks-tip: stacks-tip, result: (unwrap-panic (as-max-len? (append (get result ctx) transfer-result) u750))}))) (define-private (sum-stacked-ustx (nft-id uint) (total uint)) (match (map-get? meta nft-id) entry (match (get stacked-ustx entry) amount (+ total amount) total) total)) (define-read-only (get-total-stacked-ustx (nfts (list 750 uint))) (fold sum-stacked-ustx nfts u0)) (define-read-only (get-total-stacked-ustx-at-block (nfts (list 750 uint)) (stacks-tip uint)) (match (get-block-info? id-header-hash stacks-tip) ihh (at-block ihh (ok (get-total-stacked-ustx nfts))) err-invalid-stacks-tip)) (define-public (payout (reward-ustx uint) (nfts (list 750 uint)) (pay-stacks-tip uint)) (match (get-total-stacked-ustx-at-block nfts pay-stacks-tip) total-ustx (ok (fold payout-nft nfts {reward-ustx: reward-ustx, total-ustx: total-ustx, stx-from: tx-sender, pay-stacks-tip: pay-stacks-tip, result: (list)})) error (err error))) (define-read-only (get-total-stacked) (var-get total-stacked)) (define-public (allow-contract-caller (this-contract principal)) (if (is-eq tx-sender dplyr) (as-contract (contract-call? 'SP000000000000000000002Q6VF78.pox allow-contract-caller this-contract none)) (err 403))) ;; NFT functions (define-public (transfer (id uint) (sender principal) (recipient principal)) (if (or (is-eq sender tx-sender) (is-eq sender contract-caller)) (match (nft-transfer? b-16 id sender recipient) success (ok success) error (err-nft-transfer error)) err-not-allowed-sender)) (define-read-only (get-owner (id uint)) (ok (nft-get-owner? b-16 id))) (define-read-only (get-owner-raw? (id uint)) (nft-get-owner? b-16 id)) (define-read-only (get-last-token-id) (ok (var-get last-id))) (define-read-only (last-token-id-raw) (var-get last-id)) (define-read-only (get-token-uri (id uint)) (ok (some "https://cloudflare-ipfs.com/ipfs/QmTYvatj7HFT3zsFCAz52DLERvr1jtuSfQsXXZbxLjW2Hz"))) ;; error handling (define-constant err-nft-not-owned (err u401)) ;; unauthorized (define-constant err-not-allowed-sender (err u403)) ;; forbidden (define-constant err-nft-not-found (err u404)) ;; not found (define-constant err-sender-equals-recipient (err u405)) ;; method not allowed (define-constant err-nft-exists (err u409)) ;; conflict (define-constant err-not-enough-funds (err u4021)) ;; payment required (define-constant err-amount-not-positive (err u4022)) ;; payment required (define-constant err-map-function-failed (err u601)) (define-constant err-invalid-asset-id (err u602)) (define-constant err-no-asset-owner (err u603)) (define-constant err-delegate-below-minimum (err u604)) (define-constant err-delegate-invalid-stacker (err u605)) (define-constant err-delegate-too-late (err u606)) (define-constant err-commit-too-early (err u607)) (define-constant err-invalid-stacks-tip (err u608)) (define-map err-strings (response uint uint) (string-ascii 32)) (map-insert err-strings err-nft-not-owned "nft-not-owned") (map-insert err-strings err-not-allowed-sender "not-allowed-sender") (map-insert err-strings err-nft-not-found "nft-not-found") (map-insert err-strings err-sender-equals-recipient "sender-equals-recipient") (map-insert err-strings err-nft-exists "nft-exists") (map-insert err-strings err-map-function-failed "map-function-failed") (map-insert err-strings err-invalid-asset-id "invalid-asset-id") (map-insert err-strings err-no-asset-owner "no-asset-owner") (map-insert err-strings err-delegate-below-minimum "delegate-below-minimum") (map-insert err-strings err-delegate-invalid-stacker "delegate-invalid-stacker") (map-insert err-strings err-delegate-too-late "delegate-too-late") (map-insert err-strings err-commit-too-early "commit-too-early") (map-insert err-strings err-invalid-stacks-tip "invalid-stacks-tip") (define-private (err-pox-stack-aggregation-commit (code int)) (err (to-uint (* 1000 code)))) (define-private (err-stx-transfer (code uint)) (if (is-eq u1 code) err-not-enough-funds (if (is-eq u2 code) err-sender-equals-recipient (if (is-eq u3 code) err-amount-not-positive (if (is-eq u4 code) err-not-allowed-sender (err code)))))) (define-private (err-nft-transfer (code uint)) (if (is-eq u1 code) err-nft-not-owned (if (is-eq u2 code) err-sender-equals-recipient (if (is-eq u3 code) err-nft-not-found (err code))))) (define-private (err-nft-mint (code uint)) (if (is-eq u1 code) err-nft-exists (err code))) (define-read-only (get-errstr (code uint)) (unwrap! (map-get? err-strings (err code)) "unknown-error"))
0x0157b557cbdb4d9461be417f585a557f848412629f7692bc0ab2b8e526a0ad5a
27,725
success
stx-ft-swap-v1
(use-trait fungible-token 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) ;; the fee structure is defined by the calling client ;; this is to avoid duplication of the protocol just with adjusted fee structure ;; it is the responsibility of the client to adjust the post conditions accordingly (define-trait fees-trait ((get-fees (uint) (response uint uint)) (hold-fees (uint) (response bool uint)) (release-fees (uint) (response bool uint)) (pay-fees (uint) (response bool uint)))) (define-constant expiry u100) (define-map swaps uint {ustx: uint, stx-sender: principal, amount: uint, ft-sender: (optional principal), when: uint, open: bool, ft: principal, fees: principal}) (define-data-var next-id uint u0) ;; helper function to transfer stx to a principal with memo (define-private (stx-transfer-to (ustx uint) (to principal) (memo (buff 34))) (contract-call? 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.send-many-memo send-many (list {to: to, ustx: ustx, memo: memo}))) ;; create a swap between btc and fungible token (define-public (create-swap (ustx uint) (amount uint) (ft-sender (optional principal)) (ft <fungible-token>) (fees <fees-trait>)) (let ((id (var-get next-id))) (asserts! (map-insert swaps id {ustx: ustx, stx-sender: tx-sender, amount: amount, ft-sender: ft-sender, when: block-height, open: true, ft: (contract-of ft), fees: (contract-of fees)}) ERR_INVALID_ID) (var-set next-id (+ id u1)) (try! (contract-call? fees hold-fees ustx)) (match (stx-transfer-to ustx (as-contract tx-sender) 0x636174616d6172616e2073776170) success (ok id) error (err (* error u100))))) ;; any user can cancle the swap after the expiry period (define-public (cancel (id uint) (ft <fungible-token>) (fees <fees-trait>)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID)) (ustx (get ustx swap))) (asserts! (is-eq (contract-of ft) (get ft swap)) ERR_INVALID_FUNGIBLE_TOKEN) (asserts! (is-eq (contract-of fees) (get fees swap)) ERR_INVALID_FEES_TRAIT) (asserts! (< (+ (get when swap) expiry) block-height) ERR_TOO_EARLY) (asserts! (get open swap) ERR_ALREADY_DONE) (asserts! (map-set swaps id (merge swap {open: false})) ERR_NATIVE_FAILURE) (try! (contract-call? fees release-fees ustx)) (match (as-contract (stx-transfer-to ustx (get stx-sender swap) 0x72657665727420636174616d6172616e2073776170)) success (ok success) error (err (* error u100))))) ;; any user can submit a tx that contains the swap (define-public (submit-swap (id uint) (ft <fungible-token>) (fees <fees-trait>)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID)) (ustx (get ustx swap)) (stx-receiver (default-to tx-sender (get ft-sender swap)))) (asserts! (get open swap) ERR_ALREADY_DONE) (asserts! (is-eq (contract-of ft) (get ft swap)) ERR_INVALID_FUNGIBLE_TOKEN) (asserts! (is-eq (contract-of fees) (get fees swap)) ERR_INVALID_FEES_TRAIT) (asserts! (map-set swaps id (merge swap {open: false})) ERR_NATIVE_FAILURE) (asserts! (is-eq tx-sender stx-receiver) ERR_INVALID_STX_RECEIVER) (try! (contract-call? fees pay-fees ustx)) (match (contract-call? ft transfer (get amount swap) stx-receiver (get stx-sender swap) (some 0x636174616d6172616e2073776170)) success-ft (begin (asserts! success-ft ERR_NATIVE_FAILURE) (match (as-contract (stx-transfer-to (get ustx swap) stx-receiver 0x636174616d6172616e2073776170)) success-stx (ok success-stx) error-stx (err (* error-stx u100)))) error-ft (err (* error-ft u1000))))) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_INVALID_FUNGIBLE_TOKEN (err u8)) (define-constant ERR_INVALID_STX_RECEIVER (err u9)) (define-constant ERR_INVALID_FEES (err u10)) (define-constant ERR_INVALID_FEES_TRAIT (err u11)) (define-constant ERR_NATIVE_FAILURE (err u99))
0x5d1df719277fe444764382ca585b970dd4f0ee24bef9b476045b40f8ca06e07f
27,913
success
stx-ft-swap-v1-fixed-fees
(define-constant fee-receiver tx-sender) (define-constant charging-ctr SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.stx-ft-swap-v1) (define-public (get-fees (ustx uint)) (ok (/ ustx u100))) (define-public (hold-fees (ustx uint)) (begin (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (stx-transfer? (/ ustx u100) tx-sender (as-contract tx-sender)))) (define-public (release-fees (ustx uint)) (let ((user tx-sender)) (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (as-contract (stx-transfer? (/ ustx u100) tx-sender user)))) (define-public (pay-fees (ustx uint)) (let ((fee (/ ustx u100))) (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (if (> fee u0) (as-contract (stx-transfer? fee tx-sender fee-receiver)) (ok true)))) (define-constant ERR_NOT_AUTH (err u404))
0x1c950ddc0ee7665e9210d62539fe808d8e11c2331de6c3a1e631f5f8c6d44924
27,923
abort_by_response
stx-ft-swap-v1-fixed-fees
;; Implementation of fixed fees of 1% for the service ;; by the charging-ctr. On that contract can call the public functions. (define-constant fee-receiver tx-sender) (define-constant charging-ctr 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.stx-ft-swap-v1) ;; For information only. (define-public (get-fees (ustx uint)) (ok (/ ustx u100))) ;; Hold fees for the given amount in escrow. (define-public (hold-fees (ustx uint)) (begin (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (stx-transfer? (/ ustx u100) tx-sender (as-contract tx-sender)))) ;; Release fees for the given amount if swap was canceled. ;; It relies on the logic of the charging-ctr that this contract. (define-public (release-fees (ustx uint)) (let ((user tx-sender)) (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (as-contract (stx-transfer? (/ ustx u100) tx-sender user)))) ;; Pay fee for the given amount if swap was executed. (define-public (pay-fees (ustx uint)) (let ((fee (/ ustx u100))) (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (if (> fee u0) (as-contract (stx-transfer? fee tx-sender fee-receiver)) (ok true)))) (define-constant ERR_NOT_AUTH (err u404))
0x0a80d106a58b263ffc4369c6219820bb4dd5f3b4f2e24cbd2cabdffa3490bbd8
27,934
success
friedger-token-v1
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token friedger) (define-constant ctr-dplyr tx-sender) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (ok (ft-get-balance friedger owner))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply friedger))) ;; returns the token name (define-read-only (get-name) (ok "Friedger Token")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FRIE")) ;; the number of decimals used (define-read-only (get-decimals) (ok u6)) ;; 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? friedger amount sender recipient)) (print memo) (ok true) ) (err u4))) (define-public (get-token-uri) (ok (some u"https://friedger.de/stacks-frie.json"))) ;; Mint 1 micro FRIE for 1 uSTX (define-public (mint (ufriedger uint)) (begin (try! (stx-transfer? ufriedger tx-sender ctr-dplyr)) (ft-mint? friedger ufriedger tx-sender)))
0xae15214bf3317cf3ca17726f02ea7cdeb51e096febcff215c1ede36a34415375
27,956
success
empty-contract-v1
;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract ;; An empty contract, an empty contract, an empty contract, an empty contract, an empty contract
0x466cf77caf99ba2a3af37894fc7cfc1fa2b1ab3e9d92a23c3b225abacb718306
27,956
success
SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP2Z5DAY1SF1W15DZ0Z5RQP35A9Q80CWAMJ5RKF0W) (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)))
0x01815cc33eca187feb725cecc20502213161027c01fb33798abb0f602deeec02
27,966
success
can-claim-mining-reward
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP38NZD5JZTN7BBBR6T76HAW8Z25DMPAKJHNMQ1B1) (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)))
0x7cebffe571ba07cccdf853548e75323365e57e428de0ec6af6b42b958fe81fe8
28,129
success
claim-stacking-reward
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP38NZD5JZTN7BBBR6T76HAW8Z25DMPAKJHNMQ1B1) (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)))
0x10fe2f5bbb8c50e1455d5881cd38412fa33d0edb2e3c48c67fbdee4f8ebbf36f
28,129
success
register-user
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP38NZD5JZTN7BBBR6T76HAW8Z25DMPAKJHNMQ1B1) (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)))
0xe7d3979edfa69514b61cf288bf17d5e8b614a079dcb846b18708b92e0828706f
28,133
success
coloured-ivory-shrimp
coreContract as principal (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP3CKJ7KNK557K0Z6M0BM0V4APXAPDDPAZQTDS13G) (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)))
0x9d06b1c00f6cc681f3566d3fd6902cfdb9568c09f325f8c7343cf3258d7d1aef
28,189
abort_by_response
coloured-ivory-shrimp
coreContract as principal (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP3CKJ7KNK557K0Z6M0BM0V4APXAPDDPAZQTDS13G) (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)))
0x9d06b1c00f6cc681f3566d3fd6902cfdb9568c09f325f8c7343cf3258d7d1aef
28,189
abort_by_response
running-azure-ermine
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP3T2YV466V4TA8P8C25HVVAEM5YA64129AVTT9EP) (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)))
0x1b70826ba309846831fb94ed29c45dc32a4cd4cc9a1722be51eb7df0db89ebca
28,221
success
SP000000000000000000002Q6VF78
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP3T2YV466V4TA8P8C25HVVAEM5YA64129AVTT9EP) (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)))
0xb645056f05e9456b10612c80b249115af58911788eecbcf343f4e7eedfaca0cb
28,222
success
loopbomb-v1
;; Interface definitions ;; test/mocknet ;; (impl-trait 'SP3QSAJQ4EA8WXEDSRRKMZZ29NH91VZ6C5X88FGZQ.nft-approvable-trait.nft-approvable-trait) ;; (impl-trait 'SP3QSAJQ4EA8WXEDSRRKMZZ29NH91VZ6C5X88FGZQ.nft-trait.nft-trait) ;; mainnet ;; (impl-trait SP3QSAJQ4EA8WXEDSRRKMZZ29NH91VZ6C5X88FGZQ.nft-approvable-trait.nft-approvable-trait) ;; (impl-trait SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) ;; contract variables (define-data-var administrator principal 'SP3QSAJQ4EA8WXEDSRRKMZZ29NH91VZ6C5X88FGZQ) (define-data-var mint-price uint u100000) (define-data-var base-token-uri (string-ascii 256) "https://loopbomb.io/nfts") (define-data-var mint-counter uint u0) (define-data-var platform-fee uint u5) ;; constants (define-constant token-name "loopbomb") (define-constant token-symbol "LOOP") ;; Non Fungible Token, modeled after ERC-721 via nft-trait ;; Note this is a basic implementation - no support yet for setting approvals for assets ;; NFT are identified by nft-index (uint) which is tied via a reverse lookup to a real world ;; asset hash - SHA 256 32 byte value. The Asset Hash is used to tie arbitrary real world ;; data to the NFT (define-non-fungible-token my-nft uint) ;; data structures (define-map nft-approvals {nft-index: uint} {approval: principal}) (define-map nft-lookup {asset-hash: (buff 32), edition: uint} {nft-index: uint}) (define-map nft-data {nft-index: uint} {asset-hash: (buff 32), meta-data-url: (buff 200), max-editions: uint, edition: uint, edition-cost: uint, mint-block-height: uint, series-original: uint}) (define-map nft-sale-data {nft-index: uint} {sale-type: uint, increment-stx: uint, reserve-stx: uint, amount-stx: uint, bidding-end-time: uint, sale-cycle-index: uint}) (define-map nft-beneficiaries {nft-index: uint} { addresses: (list 10 principal), shares: (list 10 uint) }) (define-map nft-bid-history {nft-index: uint, bid-index: uint} {sale-cycle: uint, bidder: principal, amount: uint, app-timestamp: uint}) (define-map nft-offer-history {nft-index: uint, offer-index: uint} {sale-cycle: uint, offerer: principal, app-timestamp: uint, amount: uint, accepted: uint}) ;; counters keep track per NFT of the... ;; a) number of editions minted (1 based index) ;; b) number of offers made (0 based index) ;; c) number of bids made (0 based index) (define-map nft-offer-counter {nft-index: uint} {offer-counter: uint, sale-cycle: uint}) (define-map nft-edition-counter {nft-index: uint} {edition-counter: uint}) (define-map nft-high-bid-counter {nft-index: uint} {high-bid-counter: uint, sale-cycle: uint}) (define-constant percentage-on-secondary u10) (define-constant percentage-with-twodp u10000000000) (define-constant not-allowed (err u10)) (define-constant not-found (err u11)) (define-constant amount-not-set (err u12)) (define-constant seller-not-found (err u13)) (define-constant asset-not-registered (err u14)) (define-constant transfer-error (err u15)) (define-constant not-approved-to-sell (err u16)) (define-constant same-spender-err (err u17)) (define-constant failed-to-mint-err (err u18)) (define-constant edition-counter-error (err u19)) (define-constant edition-limit-reached (err u20)) (define-constant user-amount-different (err u21)) (define-constant failed-to-stx-transfer (err u22)) (define-constant failed-to-close-1 (err u23)) (define-constant failed-refund (err u24)) (define-constant failed-to-close-3 (err u24)) (define-constant cant-pay-mint-price (err u25)) (define-constant editions-error (err u26)) (define-constant payment-error (err u28)) (define-constant payment-address-error (err u33)) (define-constant payment-share-error (err u34)) (define-constant bidding-error (err u35)) (define-constant prevbid-bidding-error (err u36)) (define-constant not-originale (err u37)) (define-constant bidding-opening-error (err u38)) (define-constant bidding-amount-error (err u39)) (define-constant bidding-endtime-error (err u40)) (define-constant nft-not-owned-err (err u401)) ;; unauthorized (define-constant sender-equals-recipient-err (err u405)) ;; method not allowed (define-constant nft-not-found-err (err u404)) ;; not found ;; interface methods ;; from nft-trait: Last token ID, limited to uint range ;; note decrement as mint counter is the id of the next nft (define-read-only (get-last-token-id) (ok (- (var-get mint-counter) u1)) ) ;; from nft-trait: URI for metadata associated with the token (define-read-only (get-token-uri (nftIndex uint)) (ok (some (var-get base-token-uri))) ) ;; from nft-trait: Gets the owner of the 'SPecified token ID. (define-read-only (get-owner (nftIndex uint)) (ok (nft-get-owner? my-nft nftIndex)) ) ;; from nft-trait: Gets the owner of the 'SPecified token ID. (define-read-only (get-approval (nftIndex uint)) (ok (unwrap! (get approval (map-get? nft-approvals {nft-index: nftIndex})) not-found)) ) ;; sets an approval principal - allowed to call transfer on owner behalf. (define-public (set-approval-for (nftIndex uint) (approval principal)) (if (is-owner nftIndex tx-sender) (begin (map-set nft-approvals {nft-index: nftIndex} {approval: approval}) (ok true) ) nft-not-owned-err ) ) ;; Transfers tokens to a 'SPecified principal. (define-public (transfer (nftIndex uint) (owner principal) (recipient principal)) (if (and (is-owner-or-approval nftIndex owner) (is-owner-or-approval nftIndex tx-sender)) (match (nft-transfer? my-nft nftIndex owner recipient) success (ok true) error (nft-transfer-err error)) nft-not-owned-err) ) (define-private (nft-transfer-err (code uint)) (if (is-eq u1 code) nft-not-owned-err (if (is-eq u2 code) sender-equals-recipient-err (if (is-eq u3 code) nft-not-found-err (err code))))) (define-private (is-owner (nftIndex uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? my-nft nftIndex) false)) ) (define-private (is-approval (nftIndex uint) (user principal)) (is-eq user (unwrap! (get approval (map-get? nft-approvals {nft-index: nftIndex})) false)) ) (define-private (is-owner-or-approval (nftIndex uint) (user principal)) (if (is-owner nftIndex user) true (if (is-approval nftIndex user) true false) ) ) ;; public methods ;; -------------- ;; the contract administrator can change the contract administrator (define-public (transfer-administrator (new-administrator principal)) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (var-set administrator new-administrator) (ok true) ) ) ;; the contract administrator can change the transfer fee charged by the contract on sale of tokens (define-public (change-fee (new-fee uint)) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (var-set platform-fee new-fee) (ok true) ) ) ;; the contract administrator can change the base uri - where meta data for tokens in this contract ;; are located (define-public (update-base-token-uri (new-base-token-uri (string-ascii 256))) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (var-set base-token-uri new-base-token-uri) (ok true) ) ) ;; the contract administrator can change the mint price (define-public (update-mint-price (new-mint-price uint)) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (var-set mint-price new-mint-price) (ok true) ) ) ;; The administrator can transfer the balance in the contract to another address (define-public (transfer-balance (recipient principal)) (let ( (balance (stx-get-balance (as-contract tx-sender))) ) (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (unwrap! (stx-transfer? balance (as-contract tx-sender) recipient) failed-to-stx-transfer) (print {evt: "transfer-balance", recipient: recipient, balance: balance}) (ok balance) ) ) ;; adds an offer to the list of offers on an NFT (define-public (make-offer (nft-index uint) (amount uint) (app-timestamp uint)) (let ( (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nft-index})) not-allowed)) (offerCounter (default-to u0 (get offer-counter (map-get? nft-offer-counter {nft-index: nft-index})))) (saleCycleIndex (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nft-index})) amount-not-set)) ) (map-insert nft-offer-history {nft-index: nft-index, offer-index: offerCounter} {sale-cycle: saleCycleIndex, offerer: tx-sender, app-timestamp: app-timestamp, amount: amount, accepted: u0}) (map-set nft-offer-counter {nft-index: nft-index} {sale-cycle: saleCycleIndex, offer-counter: (+ offerCounter u1)}) (ok (+ offerCounter u1)) ) ) ;; accept-offer ;; marks offer as accepted and transfers ownership to the recipient (define-public (accept-offer (nft-index uint) (offer-index uint) (owner principal) (recipient principal)) (let ( (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nft-index})) not-allowed)) (offerer (unwrap! (get offerer (map-get? nft-offer-history {nft-index: nft-index, offer-index: offer-index})) not-allowed)) (app-timestamp (unwrap! (get app-timestamp (map-get? nft-offer-history {nft-index: nft-index, offer-index: offer-index})) not-allowed)) (sale-cycle (unwrap! (get sale-cycle (map-get? nft-offer-history {nft-index: nft-index, offer-index: offer-index})) not-allowed)) (amount (unwrap! (get amount (map-get? nft-offer-history {nft-index: nft-index, offer-index: offer-index})) not-allowed)) ) (asserts! (is-eq saleType u3) not-allowed) (map-set nft-offer-history {nft-index: nft-index, offer-index: offer-index} {sale-cycle: sale-cycle, offerer: offerer, app-timestamp: app-timestamp, amount: amount, accepted: u1}) (ok (transfer nft-index owner recipient)) ) ) ;; mint a new token ;; asset-hash: sha256 hash of asset file ;; max-editions: maximum number of editions allowed for this asset ;; royalties: a list of priciple/percentages to be be paid from sale price ;; ;; 1. transfer mint price to the administrator ;; 2. mint the token using built in mint function ;; 3. update the two maps - first contains the data indexed by the nft index, second ;; provides a reverse lookup based on the asset hash - this allows tokens to be located ;; from just a knowledge of the original asset. ;; Note series-original in the case of the original in series is just ;; mintCounter - for editions this provides a safety hook back to the original in cases ;; where the asset hash is unknown (ie cant be found from nft-lookup). (define-public (mint-token (asset-hash (buff 32)) (metaDataUrl (buff 200)) (maxEditions uint) (editionCost uint) (addresses (list 10 principal)) (shares (list 10 uint))) (let ( (mintCounter (var-get mint-counter)) (ahash (get asset-hash (map-get? nft-data {nft-index: (var-get mint-counter)}))) (block-time (unwrap! (get-block-info? time u0) amount-not-set)) ) (asserts! (> maxEditions u0) editions-error) (asserts! (> (stx-get-balance tx-sender) (var-get mint-price)) cant-pay-mint-price) (asserts! (is-none ahash) asset-not-registered) ;; Note: series original is really for later editions to refer back to this one - this one IS the series original (map-insert nft-data {nft-index: mintCounter} {asset-hash: asset-hash, meta-data-url: metaDataUrl, max-editions: maxEditions, edition: u1, edition-cost: editionCost, mint-block-height: block-height, series-original: mintCounter}) ;; Note editions are 1 based and <= maxEditions - the one minted here is #1 (map-insert nft-edition-counter {nft-index: mintCounter} {edition-counter: u2}) ;; By default we accept offers - sale type can be changed via the UI. (map-insert nft-sale-data { nft-index: mintCounter } { sale-cycle-index: u1, sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: (+ block-time u1814400)}) (map-insert nft-lookup {asset-hash: asset-hash, edition: u1} {nft-index: mintCounter}) ;; The payment is split between the nft-beneficiaries with share > 0 they are set per edition (map-insert nft-beneficiaries {nft-index: mintCounter} {addresses: addresses, shares: shares}) ;; finally - mint the NFT and step the counter (if (is-eq tx-sender (var-get administrator)) (print "mint-token : tx-sender is contract - skipping mint price") (begin (unwrap! (stx-transfer? (var-get mint-price) tx-sender (var-get administrator)) failed-to-stx-transfer) (print "mint-token : tx-sender paid mint price") ) ) (unwrap! (nft-mint? my-nft mintCounter tx-sender) failed-to-mint-err) (print {evt: "mint-token", nftIndex: mintCounter, owner: tx-sender, amount: (var-get mint-price)}) (var-set mint-counter (+ mintCounter u1)) (ok mintCounter) ) ) (define-public (mint-edition (nftIndex uint)) (let ( ;; before we start... check the hash corresponds to a minted asset (ahash (unwrap! (get asset-hash (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (metaDataUrl (unwrap! (get meta-data-url (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (maxEditions (unwrap! (get max-editions (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (editionCost (unwrap! (get edition-cost (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (editionCounter (unwrap! (get edition-counter (map-get? nft-edition-counter {nft-index: nftIndex})) edition-counter-error)) (thisEdition (default-to u0 (get nft-index (map-get? nft-lookup {asset-hash: ahash, edition: editionCounter})))) (block-time (unwrap! (get-block-info? time u0) amount-not-set)) (mintCounter (var-get mint-counter)) ) ;; can only mint an edition via buy now or bidding - not offers (asserts! (is-eq thisEdition u0) edition-counter-error) ;; Note - the edition index is 1 based and incremented before insertion in this method - therefore the test is '<=' here! (asserts! (<= editionCounter maxEditions) edition-limit-reached) ;; This asserts the first one has been minted already - see mint-token. (asserts! (> editionCounter u1) edition-counter-error) ;; check the buyer has enough funds.. (asserts! (> (stx-get-balance tx-sender) editionCost) cant-pay-mint-price) ;; set max editions so we know where we are in the series (map-insert nft-data {nft-index: mintCounter} {asset-hash: ahash, meta-data-url: metaDataUrl, max-editions: maxEditions, edition: editionCounter, edition-cost: editionCost, mint-block-height: block-height, series-original: nftIndex}) ;; put the nft index into the list of editions in the look up map (map-insert nft-lookup {asset-hash: ahash, edition: editionCounter} {nft-index: mintCounter}) ;; mint the NFT and update the counter for the next.. (unwrap! (nft-mint? my-nft mintCounter tx-sender) failed-to-mint-err) ;; saleType = 1 (buy now) - split out the payments according to royalties - or roll everything back. (if (> editionCost u0) (begin (unwrap! (payment-split nftIndex editionCost tx-sender) failed-to-mint-err) (print "mint-edition : payment split made")) (print "mint-edition : payment not required") ) ;; (print "mint-edition : payment managed") ;; initialise the sale data - not for sale until the owner sets it. (map-insert nft-sale-data { nft-index: mintCounter } { sale-cycle-index: u1, sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: (+ block-time u1814400)}) (print {evt: "mint-edition", nftIndex: nftIndex, owner: tx-sender, edition: editionCounter, amount: editionCost}) ;; inncrement the mint counter and edition counter ready for the next edition (map-set nft-edition-counter {nft-index: nftIndex} {edition-counter: (+ u1 editionCounter)}) (var-set mint-counter (+ mintCounter u1)) (ok mintCounter) ) ) ;; allow the owner of the series original to set the cost of minting editions ;; the cost for each edition is taken from the series original and so we need to ;; operate on the the original here - ie nftIndex is the index of thee original ;; and NOT the edition andd only the creator of the series original can change this. (define-public (set-edition-cost (nftIndex uint) (maxEditions uint) (editionCost uint)) (let ( (ahash (unwrap! (get asset-hash (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (metaDataUrl (unwrap! (get meta-data-url (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (edition (unwrap! (get edition (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (mintBlockHeight (unwrap! (get mint-block-height (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (seriesOriginal (unwrap! (get series-original (map-get? nft-data {nft-index: nftIndex})) not-allowed)) ) (asserts! (is-owner nftIndex tx-sender) nft-not-owned-err) (asserts! (is-eq nftIndex seriesOriginal) not-originale) (ok (map-set nft-data {nft-index: nftIndex} {asset-hash: ahash, meta-data-url: metaDataUrl, max-editions: maxEditions, edition: edition, edition-cost: editionCost, mint-block-height: mintBlockHeight, series-original: seriesOriginal})) ) ) ;; set-sale-data updates the sale type and purchase info for a given NFT. Only the owner can call this method ;; and doing so make the asset transferable by the recipient - on condition of meeting the conditions of sale ;; This is equivalent to the setApprovalForAll method in ERC 721 contracts. ;; Assumption being made here is that all editions have the same sale data associated (define-public (set-sale-data (nftIndex uint) (sale-type uint) (increment-stx uint) (reserve-stx uint) (amount-stx uint) (bidding-end-time uint)) (let ( ;; before we start... check the hash corresponds to a minted asset (saleCycleIndex (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (currentBidIndex (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) (currentAmount (unwrap! (get-current-bid-amount nftIndex currentBidIndex) bidding-error)) ) (asserts! (not (and (> currentAmount u0) (is-eq saleType u2))) bidding-error) (print {evt: "set-sale-data", nftIndex: nftIndex, saleType: sale-type, increment: increment-stx, reserve: reserve-stx, amount: amount-stx, biddingEndTime: bidding-end-time}) (if (is-owner nftIndex tx-sender) ;; Note - don't override the sale cyle index here as this is a public method and can be called ad hoc. Sale cycle is update at end of sale! (if (map-set nft-sale-data {nft-index: nftIndex} {sale-cycle-index: saleCycleIndex, sale-type: sale-type, increment-stx: increment-stx, reserve-stx: reserve-stx, amount-stx: amount-stx, bidding-end-time: bidding-end-time}) (ok nftIndex) not-allowed ) not-allowed ) ) ) ;; buy-now ;; pay royalties and transfer asset ownership to tx-sender. ;; Checks that: ;; a) asset is registered ;; b) on sale via buy now ;; c) amount is set ;; (define-public (buy-now (nftIndex uint) (owner principal) (recipient principal)) (let ( (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleCycleIndex (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (amount (unwrap! (get amount-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (ahash (get asset-hash (map-get? nft-data {nft-index: nftIndex}))) ) (asserts! (is-some ahash) asset-not-registered) (asserts! (is-eq saleType u1) not-approved-to-sell) (asserts! (> amount u0) amount-not-set) ;; Make the royalty payments - then zero out the sale data and register the transfer ;; (print "buy-now : Make the royalty payments") (print (unwrap! (payment-split nftIndex amount tx-sender) payment-error)) (map-set nft-sale-data { nft-index: nftIndex } { sale-cycle-index: (+ saleCycleIndex u1), sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: u0}) ;; (print "buy-now : Added internal transfer - transfering nft...") ;; finally transfer ownership to the buyer (note: via the buyers transaction!) (print {evt: "buy-now", nftIndex: nftIndex, owner: owner, recipient: recipient, amount: amount}) (nft-transfer? my-nft nftIndex owner recipient) ) ) ;; opening-bid ;; nft-index: unique index for NFT ;; The opening bid in the given sale cycle a given item. (define-public (opening-bid (nftIndex uint) (bidAmount uint) (appTimestamp uint)) (let ( (amount (unwrap! (get amount-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleCycle (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (biddingEndTime (unwrap! (get bidding-end-time (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (bidCounter (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) ) ;; Check the user bid amount is the opening price OR the current bid plus increment (asserts! (is-eq bidAmount amount) bidding-amount-error) (asserts! (> biddingEndTime appTimestamp) bidding-endtime-error) (print "place-bid : sending this much to; ") (print bidAmount) (print (as-contract tx-sender)) (print "place-bid : when") (print appTimestamp) (unwrap! (stx-transfer? bidAmount tx-sender (as-contract tx-sender)) failed-to-stx-transfer) (map-insert nft-bid-history {nft-index: nftIndex, bid-index: bidCounter} {bidder: tx-sender, amount: bidAmount, app-timestamp: appTimestamp, sale-cycle: saleCycle}) (map-set nft-high-bid-counter {nft-index: nftIndex} {high-bid-counter: (+ bidCounter u1), sale-cycle: saleCycle}) (print {evt: "opening-bid", nftIndex: nftIndex, txSender: tx-sender, appTimestamp: appTimestamp, amount: bidAmount}) (ok bidAmount) ) ) (define-private (get-current-bidder (nftIndex uint) (currentBidIndex uint)) (let ( (currentBidder (unwrap! (get bidder (map-get? nft-bid-history {nft-index: nftIndex, bid-index: (- currentBidIndex u1)})) bidding-error)) ) (ok currentBidder) ) ) (define-private (get-current-bid-amount (nftIndex uint) (currentBidIndex uint)) (if (is-eq currentBidIndex u0) (ok u0) (let ( (currentAmount (default-to u0 (get amount (map-get? nft-bid-history {nft-index: nftIndex, bid-index: (- currentBidIndex u1)})))) ) (ok currentAmount) ) ) ) ;; place-bid ;; nft-index: unique index for NFT ;; nextBidAmount: amount the user is bidding - i.e the amount display on th place bid button. (define-public (place-bid (nftIndex uint) (nextBidAmount uint) (appTimestamp uint)) (let ( (bidding-end-time (unwrap! (get bidding-end-time (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleCycle (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (amountStart (unwrap! (get amount-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (increment (unwrap! (get increment-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (reserve (unwrap! (get reserve-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (currentBidIndex (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) (currentBidder (unwrap! (get-current-bidder nftIndex currentBidIndex) bidding-error)) (currentAmount (unwrap! (get-current-bid-amount nftIndex currentBidIndex) bidding-error)) (owner (unwrap! (nft-get-owner? my-nft nftIndex) nft-not-owned-err)) ) ;; Check the user bid amount is the opening price OR the current bid plus increment (print "place-bid : assert there is an opening bid - otherwise client calls opening-bid") (print currentAmount) (asserts! (> currentAmount u0) user-amount-different) (asserts! (is-eq nextBidAmount (+ currentAmount increment)) user-amount-different) ;; if (appTimestamp > bidding-end-time) then this is either the winning or a too late bid on the NFT ;; a too late bid will have been rejected as the last bid resets the sale/bidding data on the item. ;; if its the last bid... ;; 1. Refund the currentBid to the bidder ;; 2. move currentBid to bid history ;; 3. Set the bid in nft-high-bid-counter - note 'set' so we overwrite the previous bid ;; (next-bid) we ;; 1. Refund the currentBid to the bidder ;; 2. Insert currentBid to bid history ;; 3. Set the bid in nft-high-bid-counter - note 'set' so we overwrite the previous bid (if (> appTimestamp bidding-end-time) (begin (print {evt: "place-bid-closure", nftIndex: nftIndex, appTimestamp: appTimestamp, biddingEndTime: bidding-end-time, amount: nextBidAmount, reserve: reserve}) (unwrap! (refund-bid nftIndex currentBidder currentAmount) failed-to-stx-transfer) (if (< nextBidAmount reserve) ;; if this bid is less than reserve & its the last bid then just refund previous bid (unwrap! (ok true) failed-to-stx-transfer) (begin ;; WINNING BID - is the FIRST bid after bidding close. (print "place-bid : Make the royalty payments") (unwrap! (payment-split nftIndex nextBidAmount tx-sender) payment-error) (unwrap! (record-bid nftIndex nextBidAmount currentBidIndex appTimestamp saleCycle) failed-to-stx-transfer) (map-set nft-sale-data { nft-index: nftIndex } { sale-cycle-index: (+ saleCycle u1), sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: u0}) (print "place-bid : Added internal transfer - transfering nft...") ;; finally transfer ownership to the buyer (note: via the buyers transaction!) (unwrap! (nft-transfer? my-nft nftIndex owner tx-sender) failed-to-stx-transfer) ) ) ) (begin (print {evt: "place-bid-refund", nftIndex: nftIndex, appTimestamp: appTimestamp, biddingEndTime: bidding-end-time, amount: nextBidAmount}) (unwrap! (refund-bid nftIndex currentBidder currentAmount) failed-refund) (unwrap! (next-bid nftIndex nextBidAmount currentBidIndex appTimestamp saleCycle) failed-to-stx-transfer) ) ) ;; ;; NOTE: Above code will only reconcile IF a bid comes in after 'block-time' ;; We may need a manual trigger to end bidding when this doesn't happen - unless there is a ;; to repond to future events / timeouts that I dont know about. ;; (print {evt: "place-bid", nftIndex: nftIndex, txSender: tx-sender, appTimestamp: appTimestamp, amount: nextBidAmount}) (ok true) ) ) ;; Mint subsequent editions of the NFT ;; nft-index: the index of the original NFT in this series of editions. ;; The sale data must have been set on the asset before calling this. ;; The amount is split according to the royalties. ;; The nextBidAmount is passed to avoid concurrency issues - amount on the buy/bid button must ;; equal the amount expected by the contract. ;; close-bidding ;; nft-index: index of the NFT ;; closeType: type of closure, values are; ;; 1 = buy now closure - uses the last bid (thats held in escrow) to transfer the item to the bidder and to pay royalties ;; 2 = refund closure - the last bid gets refunded and sale is closed. The item ownership does not change. ;; Note bidding can also be closed automatically - if a bid is received after the bidding end time. ;; In the context of a 'live auction' items have no end time and are closed by the 'auctioneer'. (define-public (close-bidding (nftIndex uint) (closeType uint)) (let ( (bidding-end-time (unwrap! (get bidding-end-time (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleCycleIndex (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) failed-to-close-1)) (block-time (unwrap! (get-block-info? time u0) not-allowed)) (currentBidIndex (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) (currentBidder (unwrap! (get-current-bidder nftIndex currentBidIndex) bidding-error)) (currentAmount (unwrap! (get-current-bid-amount nftIndex currentBidIndex) bidding-error)) ) (asserts! (or (is-eq closeType u1) (is-eq closeType u2)) failed-to-close-1) ;; only the owner or administrator can call close (asserts! (or (is-owner nftIndex tx-sender) (unwrap! (is-administrator) not-allowed)) not-allowed) ;; only the administrator can call close BEFORE the end time - note we use the less accurate ;; but fool proof block time here to prevent owner/client code jerry mandering the close function (asserts! (or (> block-time bidding-end-time) (unwrap! (is-administrator) failed-to-close-3)) failed-to-close-3) ;; Check for a current bid - if none then just reset the sale data to not selling (if (is-eq currentAmount u0) (map-set nft-sale-data { nft-index: nftIndex } { sale-cycle-index: (+ saleCycleIndex u1), sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: u0}) (if (is-eq closeType u1) (begin ;; buy now closure - pay and transfer ownership ;; note that the money to pay with is in the contract! (print {evt: "close-bidding", nftIndex: nftIndex, payType: "from-contract", txSender: tx-sender, currentBidder: currentBidder, currentAmount: currentAmount, currentBidIndex: currentBidIndex}) (unwrap! (payment-split nftIndex currentAmount (as-contract tx-sender)) payment-error) (unwrap! (nft-transfer? my-nft nftIndex (unwrap! (nft-get-owner? my-nft nftIndex) nft-not-owned-err) tx-sender) transfer-error) ) (begin ;; refund closure - refund the bid and reset sale data (print {evt: "close-bidding", nftIndex: nftIndex, payType: "refund", txSender: tx-sender, currentBidder: currentBidder, currentAmount: currentAmount}) (unwrap! (refund-bid nftIndex currentBidder currentAmount) failed-refund) (map-set nft-sale-data { nft-index: nftIndex } { sale-cycle-index: (+ saleCycleIndex u1), sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: u0}) ) ) ) (print {evt: "close-bidding", nftIndex: nftIndex, closeType: closeType, txSender: tx-sender, currentBidder: currentBidder, currentAmount: currentAmount}) (ok nftIndex) ) ) ;; read only methods ;; --------------- (define-read-only (get-administrator) (var-get administrator)) (define-read-only (is-administrator) (ok (is-eq (var-get administrator) tx-sender))) (define-read-only (get-base-token-uri) (var-get base-token-uri)) (define-read-only (get-mint-counter) (ok (var-get mint-counter)) ) (define-read-only (get-mint-price) (var-get mint-price)) (define-read-only (get-token-by-index (nftIndex uint)) (ok (get-all-data nftIndex)) ) (define-read-only (get-beneficiaries (nftIndex uint)) (let ( (beneficiaries (map-get? nft-beneficiaries {nft-index: nftIndex})) ) (ok beneficiaries) ) ) (define-read-only (get-offer-at-index (nftIndex uint) (offerIndex uint)) (let ( (the-offer (map-get? nft-offer-history {nft-index: nftIndex, offer-index: offerIndex})) ) (ok the-offer) ) ) (define-read-only (get-bid-at-index (nftIndex uint) (bidIndex uint)) (let ( (the-bid (map-get? nft-bid-history {nft-index: nftIndex, bid-index: bidIndex})) ) (ok the-bid) ) ) ;; Get the edition from a knowledge of the #1 edition and the specific edition number (define-read-only (get-edition-by-hash (asset-hash (buff 32)) (edition uint)) (let ( (nftIndex (unwrap! (get nft-index (map-get? nft-lookup {asset-hash: asset-hash, edition: edition})) amount-not-set)) ) (ok (get-all-data nftIndex)) ) ) (define-read-only (get-token-by-hash (asset-hash (buff 32))) (let ( (nftIndex (unwrap! (get nft-index (map-get? nft-lookup {asset-hash: asset-hash, edition: u1})) amount-not-set)) ) (ok (get-all-data nftIndex)) ) ) (define-read-only (get-contract-data) (let ( (the-administrator (var-get administrator)) (the-mint-price (var-get mint-price)) (the-base-token-uri (var-get base-token-uri)) (the-mint-counter (var-get mint-counter)) (the-platform-fee (var-get platform-fee)) (the-token-name token-name) (the-token-symbol token-symbol) ) (ok (tuple (administrator the-administrator) (mintPrice the-mint-price) (baseTokenUri the-base-token-uri) (mintCounter the-mint-counter) (platformFee the-platform-fee) (tokenName the-token-name) (tokenSymbol the-token-symbol))) ) ) (define-private (get-all-data (nftIndex uint)) (let ( (the-owner (unwrap-panic (nft-get-owner? my-nft nftIndex))) (the-token-info (map-get? nft-data {nft-index: nftIndex})) (the-sale-data (map-get? nft-sale-data {nft-index: nftIndex})) (the-beneficiary-data (map-get? nft-beneficiaries {nft-index: nftIndex})) (the-edition-counter (default-to u0 (get edition-counter (map-get? nft-edition-counter {nft-index: nftIndex})))) (the-offer-counter (default-to u0 (get offer-counter (map-get? nft-offer-counter {nft-index: nftIndex})))) (the-high-bid-counter (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) ) (ok (tuple (offerCounter the-offer-counter) (bidCounter the-high-bid-counter) (editionCounter the-edition-counter) (nftIndex nftIndex) (tokenInfo the-token-info) (saleData the-sale-data) (beneficiaryData the-beneficiary-data) (owner the-owner) ) ) ) ) (define-read-only (get-sale-data (nftIndex uint)) (match (map-get? nft-sale-data {nft-index: nftIndex}) mySaleData (ok mySaleData) not-found ) ) (define-read-only (get-token-name) (ok token-name) ) (define-read-only (get-token-symbol) (ok token-symbol) ) (define-read-only (get-balance) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (ok (stx-get-balance (as-contract tx-sender))) ) ) ;; private methods ;; --------------- (define-private (refund-bid (nftIndex uint) (currentBidder principal) (currentAmount uint)) (begin (unwrap! (as-contract (stx-transfer? currentAmount tx-sender currentBidder)) failed-to-stx-transfer) (print {evt: "refund-bid", nftIndex: nftIndex, txSender: tx-sender, currentBidder: currentBidder, currentAmount: currentAmount}) (ok true) ) ) ;; need to account for reserve-stx (define-private (record-bid (nftIndex uint) (bidAmount uint) (bidCounter uint) (appTimestamp uint) (saleCycle uint)) (begin ;; see place-bid for the payment - no need for this (unwrap! (stx-transfer? bidAmount tx-sender (as-contract tx-sender)) failed-to-stx-transfer) (map-insert nft-bid-history {nft-index: nftIndex, bid-index: bidCounter} {bidder: tx-sender, amount: bidAmount, app-timestamp: appTimestamp, sale-cycle: saleCycle}) (map-set nft-high-bid-counter {nft-index: nftIndex} {high-bid-counter: (+ bidCounter u1), sale-cycle: saleCycle}) (print {evt: "record-bid", nftIndex: nftIndex, txSender: tx-sender, bidAmount: bidAmount, bidCounter: bidCounter, appTimestamp: appTimestamp, saleCycle: saleCycle}) (ok true) ) ) (define-private (next-bid (nftIndex uint) (bidAmount uint) (bidCounter uint) (appTimestamp uint) (saleCycle uint)) (begin (unwrap! (stx-transfer? bidAmount tx-sender (as-contract tx-sender)) failed-to-stx-transfer) (map-insert nft-bid-history {nft-index: nftIndex, bid-index: bidCounter} {bidder: tx-sender, amount: bidAmount, app-timestamp: appTimestamp, sale-cycle: saleCycle}) (map-set nft-high-bid-counter {nft-index: nftIndex} {high-bid-counter: (+ bidCounter u1), sale-cycle: saleCycle}) (print {appTimestamp: appTimestamp, bidAmount: bidAmount, bidCounter: bidCounter, evt: "next-bid", nftIndex: nftIndex, saleCycle: saleCycle, txSender: tx-sender}) (ok true) ) ) ;; sends payments to each recipient listed in the royalties ;; Note this is called by mint-edition where thee nftIndex actuallt referes to the series orginal and is where the royalties are stored. (define-private (payment-split (nftIndex uint) (saleAmount uint) (payer principal)) (let ( (addresses (unwrap! (get addresses (map-get? nft-beneficiaries {nft-index: nftIndex})) failed-to-mint-err)) (shares (unwrap! (get shares (map-get? nft-beneficiaries {nft-index: nftIndex})) failed-to-mint-err)) (saleCycle (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (split u0) ;; If secondary sale (sale-cycle > 1) - the seller gets half the sale value and each royalty payment is half the original amount (scalor (if (> (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set) u1) percentage-on-secondary u1)) ) (if (is-eq scalor percentage-on-secondary) ;; If secondary sale - pay the seller then split the royalties (unwrap! (stx-transfer? (/ (* saleAmount (- u100 percentage-on-secondary)) u100) payer (unwrap! (nft-get-owner? my-nft nftIndex) payment-share-error)) transfer-error) ;; Primary sale - split the royalties true ) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u0) payment-address-error) (unwrap! (element-at shares u0) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u1) payment-address-error) (unwrap! (element-at shares u1) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u2) payment-address-error) (unwrap! (element-at shares u2) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u3) payment-address-error) (unwrap! (element-at shares u3) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u4) payment-address-error) (unwrap! (element-at shares u4) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u5) payment-address-error) (unwrap! (element-at shares u5) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u6) payment-address-error) (unwrap! (element-at shares u6) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u7) payment-address-error) (unwrap! (element-at shares u7) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u8) payment-address-error) (unwrap! (element-at shares u8) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty scalor payer saleAmount (unwrap! (element-at addresses u9) payment-address-error) (unwrap! (element-at shares u9) payment-share-error)) payment-share-error)) (print {evt: "payment-split", nftIndex: nftIndex, scalor: scalor, payer: payer, saleAmount: saleAmount, seller: (/ (* saleAmount (- u100 percentage-on-secondary)) u100), txSender: tx-sender}) (ok split) ) ) ;; unit of saleAmount is in Satoshi and the share variable is a percentage (ex for 5% it will be equal to 5) ;; also the scalor is 1 on first purchase - direct from artist and 2 for secondary sales - so the seller gets half the ;; sale value and each royalty address gets half their original amount. (define-private (pay-royalty (scalor uint) (payer principal) (saleAmount uint) (payee principal) (share uint)) (begin (if (> share u0) (let ( (split (/ (* saleAmount (/ share scalor)) percentage-with-twodp)) ) ;; ignore royalty payment if its to the buyer / tx-sender. (if (not (is-eq tx-sender payee)) (unwrap! (stx-transfer? split payer payee) transfer-error) (unwrap! (ok true) transfer-error) ) (print {evt: "pay-royalty-primary", payee: payee, payer: payer, saleAmount: saleAmount, scalor: scalor, share: share, split: split, txSender: tx-sender}) (ok split) ) (ok u0) ) ) )
0xd7a792ae201497a51469009a57ee1f6967300a722df37a36efceba35c96ae2be
28,284
success
stx-ft-swap-v1-fixed-fpwr-v04-fees
;; Implementation of fixed fees of 1% for the service ;; by the charging-ctr. Only that contract can call the public functions. ;; the ft is exchange at the price of stx to btc received from oracle (define-constant fee-receiver tx-sender) (define-constant charging-ctr 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.stx-ft-swap-v1) ;; For information only. (define-public (get-fees (ustx uint)) (ok (to-ft-fee ustx))) (define-read-only (to-ft-fee (ustx uint)) (/ (/ (* ustx (get amount (unwrap-panic (contract-call? 'SPZ0RAC1EFTH949T4W2SYY6YBHJRMAF4ECT5A7DD.oracle-v1 get-price "artifix-binance" "STX-BTC")))) u1000000) u100)) (define-private (ft-transfer-to (amount uint) (recipient principal) (memo (optional (buff 34)))) (begin (and (> amount u0) (try! (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v04 transfer amount tx-sender recipient memo))) (ok true))) ;; Hold fees for the given amount in escrow. (define-public (hold-fees (ustx uint)) (begin (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (ft-transfer-to (to-ft-fee ustx) (as-contract tx-sender) none))) ;; Release fees for the given amount if swap was canceled. ;; It relies on the logic of the charging-ctr that this contract. (define-public (release-fees (ustx uint)) (let ((user tx-sender)) (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (as-contract (ft-transfer-to (to-ft-fee ustx) user none)))) ;; Pay fee for the given amount if swap was executed. (define-public (pay-fees (ustx uint)) (begin (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (as-contract (ft-transfer-to (to-ft-fee ustx) fee-receiver none)))) (define-constant ERR_NOT_AUTH (err u404))
0x7a5bfd47745d9761945186b91610656dc44df51f168e6273666033d07c18d6f3
28,307
success
retriever-v0a
(define-public (claim-and-transfer (targetCycle uint) (dest principal)) (begin (if (is-ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-core-v1 claim-stacking-reward targetCycle)) ;; check error, or ignore (print "success") (print "error") ) (stx-transfer? (stx-get-balance tx-sender) tx-sender dest) ) )
0x467ce17625254fd9ad424f7bbaff2680899887bf2835076edc119ea07f7618c7
28,391
success
cycle-2-hodl-mia-fees
;; Implementation of proof of hodl during mia cycle 2 for any service. ;; For information only. (define-public (get-fees (ustx uint)) (ok u0)) (define-read-only (hodls-mia (user principal)) (> (unwrap! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user) false) u1000)) ;; Hold fees for the given amount in escrow. (define-public (hold-fees (ustx uint)) (begin (asserts! (and (< block-height u30797) (hodls-mia tx-sender)) ERR_NOT_AUTH) (ok true))) ;; Release fees for the given amount if swap was canceled. ;; It relies on the logic of the charging-ctr that this contract. (define-public (release-fees (ustx uint)) (ok true)) ;; Pay fee for the given amount if swap was executed. (define-public (pay-fees (ustx uint)) (ok true)) (define-constant ERR_NOT_AUTH (err u404))
0x6100858438527c9eb34bb88c6b016eb32a7af6a3abce0e6fdc2070f22daa46fd
28,417
success
stx-ft-swap-v1-fixed-frie-fees
;; Implementation of fixed fees of 1% for the service ;; by the charging-ctr. Only that contract can call the public functions. ;; the ft is exchanged 1:1 to STX (define-constant fee-receiver tx-sender) (define-constant charging-ctr 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.stx-ft-swap-v1) ;; For information only. (define-public (get-fees (ustx uint)) (ok (to-ft-fee ustx))) (define-read-only (to-ft-fee (ustx uint)) (/ ustx u100)) (define-private (ft-transfer-to (amount uint) (recipient principal) (memo (optional (buff 34)))) (begin (and (> amount u0) (try! (contract-call? 'SPN4Y5QPGQA8882ZXW90ADC2DHYXMSTN8VAR8C3X.friedger-token-v1 transfer amount tx-sender recipient memo))) (ok true))) ;; Hold fees for the given amount in escrow. (define-public (hold-fees (ustx uint)) (begin (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (ft-transfer-to (to-ft-fee ustx) (as-contract tx-sender) none))) ;; Release fees for the given amount if swap was canceled. ;; It relies on the logic of the charging-ctr that this contract. (define-public (release-fees (ustx uint)) (let ((user tx-sender)) (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (as-contract (ft-transfer-to (to-ft-fee ustx) user none)))) ;; Pay fee for the given amount if swap was executed. (define-public (pay-fees (ustx uint)) (begin (asserts! (is-eq contract-caller charging-ctr) ERR_NOT_AUTH) (as-contract (ft-transfer-to (to-ft-fee ustx) fee-receiver none)))) (define-constant ERR_NOT_AUTH (err u404))
0x36d6305088f76b4d276e83249854f4db4445dd322fccdefbceea328537e12c6f
28,455
success
claim-v0b
(define-public (claim-and-transfer (targetCycle uint) (dest principal)) (begin (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-core-v1 claim-stacking-reward targetCycle)) (print (stx-transfer? (print (stx-get-balance tx-sender)) tx-sender dest)) (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance tx-sender)) tx-sender dest none ) ) (ok true) ) )
0x94c7aac44660fe5734dbab16341141494412a93aaba55035421bdd5fdb1cf349
28,491
abort_by_response
claim-v0b
(define-public (claim-and-transfer (targetCycle uint) (dest principal)) (begin (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-core-v1 claim-stacking-reward targetCycle)) (print (stx-transfer? (print (stx-get-balance tx-sender)) tx-sender dest)) (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer (unwrap! (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance tx-sender)) (err u10)) tx-sender dest none ) ) (ok true) ) )
0x162579d926386e73ba10842377daae48d069ee66c7155d7ef1adbc08d1432fdd
28,493
abort_by_response
claim-v0b
(define-public (claim-and-transfer (targetCycle uint) (dest principal)) (begin (if (is-ok (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-core-v1 claim-stacking-reward targetCycle))) (print "ok 1") (print "error 1") ) (if (is-ok (print (stx-transfer? (print (stx-get-balance tx-sender)) tx-sender dest))) (print "ok 2") (print "error 2") ) (if (is-ok (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer (unwrap! (print (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance tx-sender)) (err u10)) tx-sender dest none ) )) (print "ok 3") (print "error 3") ) (ok true) ) )
0x0d03ee778e2500d7f829b98cb8cad425a56fae591f48c313347d685501176687
28,494
success
jose-token-v1
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token jose-token) (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-SENDER-MISMATCH u4) (define-constant CONTRACT-DEPLOYER tx-sender) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (begin (ok (ft-get-balance jose-token owner)))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply jose-token))) ;; returns the token name (define-read-only (get-name) (ok "Jose Token")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "JOSE")) ;; 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? jose-token amount sender recipient)) (print memo) (ok true) ) (err ERR-SENDER-MISMATCH))) (define-public (get-token-uri) (ok (some u"https://josesoto.com/stacks-jose.json"))) (define-public (mint (amount uint) (recipient principal)) (if (is-eq tx-sender CONTRACT-DEPLOYER) (ft-mint? jose-token amount recipient) (err ERR-NOT-AUTHORIZED)) ) (ft-mint? jose-token u1000000000000000 tx-sender)
0xc9fb9ccd6d93119318d257700532c944b972c22142bdce5c5dd953728745882e
28,730
success
Artie
;; Import ;; trait to implement ;; on mainnet (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) ;; on testnet ;;(impl-trait 'ST2PABAF9FTAJYNFZH93XENAJ8FVY99RRM4DF2YCW.nft-trait.nft-trait) ;; local ;;(impl-trait .nft-trait.nft-trait) ;; Non Fungible Token, modeled after ERC-721(this is the name that appears in wallet, global) (define-non-fungible-token OpenArtSource uint) ;; Storage (define-map tokens-spender uint principal) (define-map tokens-count principal uint) (define-map accounts-operator (tuple (operator principal) (account principal)) (tuple (is-approved bool))) (define-map tokens-meta-uri uint (string-ascii 256) ) ;; Store the last issued token ID (define-data-var last-token-id uint u0) ;; Store contract admins(simulate ethereum only_owner like usage) (define-map admins principal bool) ;; # of admin, control accidental removing all admins (define-data-var admin-count uint u0) (define-map last-token-count principal uint) (define-map tokens-owned { owner: principal, idx: uint } ;; principal and owner slot#(0...) uint ;; token id ) (define-map tokens-slot { owner: principal, token-id: uint } ;; principal and token id uint ;; slot# ) (define-constant UINT_LIST (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 )) (define-private (not-zero (a uint)) (> a u0) ) (define-private (add-base (a uint) (l (list 10 uint))) (get result (fold build-add-base l {base: a, result: (filter not-zero (list u0))})) ) (define-private (build-add-base (elem uint) (data { base: uint, result: (list 10 uint)})) (let ((base (get base data))) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) (+ elem base)) u10)) }) ) ) (define-private (block-range (upperLimit uint)) (get result (fold build-block-range UINT_LIST {upperLimit: upperLimit, result: (filter not-zero (list u0))})) ) (define-private (build-block-range (elem uint) (data { upperLimit: uint, result: (list 100 uint)})) (if (<= elem (get upperLimit data)) (merge data { result: (unwrap-panic (as-max-len? (concat (get result data) (add-base (* (- elem u1) u10) UINT_LIST)) u100)) }) data ) ) (define-private (range (upperLimit uint)) (let ((blocks (+ (/ upperLimit u10) u1))) (get result (fold build-range (block-range blocks) {upperLimit: upperLimit, result: (filter not-zero (list u0))})) ) ) (define-private (build-range (elem uint) (data { upperLimit: uint, result: (list 100 uint)})) (if (<= elem (get upperLimit data)) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) elem) u100)) }) data ) ) ;; Internals ;; Gets the amount of tokens owned by the specified address. (define-private (balance-of (account principal)) (default-to u0 (map-get? tokens-count account))) ;; Gets the approved address for a token ID, or zero if no address set (approved method in ERC721) (define-private (is-spender-approved (spender principal) (token-id uint)) (let ((approved-spender (unwrap! (map-get? tokens-spender token-id) false))) ;; return false if no specified spender (is-eq spender approved-spender))) ;; Tells whether an operator is approved by a given owner (isApprovedForAll method in ERC721) (define-private (is-operator-approved (account principal) (operator principal)) (default-to false (get is-approved (map-get? accounts-operator {operator: operator, account: account})))) (define-private (is-owner (actor principal) (token-id uint)) (is-eq actor ;; if no owner, return false (unwrap! (nft-get-owner? oas-nft token-id) false))) ;; Returns whether the given actor can transfer a given token ID. ;; To be optimized (define-private (can-transfer (actor principal) (token-id uint)) (or (is-owner actor token-id) (is-spender-approved actor token-id) (is-operator-approved (unwrap! (nft-get-owner? oas-nft token-id) false) actor))) ;; Internal - add token to owner list(for loop based lookup ) (define-private (add-token (owner principal) (token-id uint)) (let ( (newIdx (+ (default-to u0 (map-get? last-token-count owner)) u1)) ) (map-insert tokens-owned { owner: owner, idx: newIdx } token-id) (map-insert tokens-slot { owner: owner, token-id: token-id } newIdx) (map-set last-token-count owner newIdx) (ok true) ) ) ;; Internal - remove token from owner list(for loop based lookup ) (define-private (remove-token (owner principal) (token-id uint)) (let ( (idx (unwrap-panic (map-get? tokens-slot {owner: owner, token-id: token-id }))) (lastidx (unwrap-panic (map-get? last-token-count owner))) (last-owned-token-id (unwrap-panic (map-get? tokens-owned { owner: owner, idx: lastidx }))) ) (map-set tokens-owned { owner: owner, idx: idx } last-owned-token-id) (map-set tokens-slot { owner: owner, token-id: last-owned-token-id } idx) (map-delete tokens-slot { owner: owner, token-id: token-id }) (map-delete tokens-owned { owner: owner, idx: lastidx }) (map-set last-token-count owner (- lastidx u1)) (ok true) ) ) ;; Internal - Register token (define-private (mint (new-owner principal) (token-uri (string-ascii 256))) (let ((token-id (+ u1 (var-get last-token-id)))) (let ((current-balance (balance-of new-owner))) (match (nft-mint? oas-nft token-id new-owner) success (begin (var-set last-token-id token-id) (map-set tokens-count new-owner (+ u1 current-balance)) (map-set tokens-meta-uri token-id token-uri) (unwrap-panic (add-token new-owner token-id)) (ok success)) error (nft-mint-err error))))) ;; Internal - Tranfer token (define-private (transfer-token (token-id uint) (owner principal) (new-owner principal)) (let ((current-balance-owner (balance-of owner)) (current-balance-new-owner (balance-of new-owner))) (begin (map-delete tokens-spender token-id) (map-set tokens-count owner (- current-balance-owner u1)) (unwrap-panic (remove-token owner token-id)) (map-set tokens-count new-owner (+ current-balance-new-owner u1)) (unwrap-panic (add-token new-owner token-id)) (match (nft-transfer? oas-nft token-id owner new-owner) success (ok success) error (nft-transfer-err error))))) ;; Read only (define-read-only (is-admin) (or (default-to false (map-get? admins tx-sender)) (default-to false (map-get? admins contract-caller)) ) ) (define-read-only (get-token-owned-at (owner principal) (idx uint)) (unwrap-panic (map-get? tokens-owned {owner: owner, idx: idx})) ) (define-read-only (get-tokens-owned (owner principal)) (let ((token-count (default-to u0 (map-get? last-token-count owner)))) (if (> token-count u0) (get result (fold build-owner-tokens (range token-count) {owner: owner, result: (filter not-zero (list u0))})) (filter not-zero (list u0)) ) ) ) (define-private (build-owner-tokens (elem uint) (data { owner: principal, result: (list 100 uint)})) (let ((owner (get owner data))) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) (get-token-owned-at owner elem)) u100)) }) ) ) ;; Public functions ;; Claim a new NFT (define-public (claim (token-uri (string-ascii 256))) (mint tx-sender token-uri)) ;; mint a token to specific owner (define-public (mint-for (recipient principal) (token-uri (string-ascii 256))) (begin (asserts! (is-admin) not-admin-err) (mint recipient token-uri)) ) ;; mint a token to specific owner (define-public (set-admin (new-admin principal)) (begin (asserts! (is-admin) not-admin-err) (if (default-to false (map-get? admins new-admin)) (ok true) (begin (map-set admins new-admin true) (var-set admin-count (+ (var-get admin-count) u1)) (ok true) ) ) ) ) ;; mint a token to specific owner (define-public (remove-admin (old-admin principal) (allow-no-admin bool)) (begin (asserts! (is-admin) not-admin-err) (if (default-to false (map-get? admins old-admin)) false (begin (map-delete admins old-admin) (var-set admin-count (- (var-get admin-count) u1)) true ) ) (asserts! (or allow-no-admin (> (var-get admin-count) u0)) not-admin-err) (ok true) ) ) ;; Approves another address to transfer the given token ID (approve method in ERC721) ;; To be optimized (define-public (set-spender-approval (spender principal) (token-id uint)) (if (is-eq spender tx-sender) sender-equals-recipient-err (if (or (is-owner tx-sender token-id) (is-operator-approved (unwrap! (nft-get-owner? oas-nft token-id) nft-not-found-err) tx-sender)) (begin (map-set tokens-spender token-id spender) (ok token-id)) not-approved-spender-err))) ;; Sets or unsets the approval of a given operator (setApprovalForAll method in ERC721) (define-public (set-operator-approval (operator principal) (is-approved bool)) (if (is-eq operator tx-sender) sender-equals-recipient-err (begin (map-set accounts-operator {operator: operator, account: tx-sender} {is-approved: is-approved}) (ok true)))) ;; Transfers the ownership of a given token ID to another address. (define-public (transfer-from (token-id uint) (owner principal) (recipient principal)) (begin (asserts! (can-transfer tx-sender token-id) not-approved-spender-err) (asserts! (is-owner owner token-id) nft-not-owned-err) (asserts! (not (is-eq recipient owner)) sender-equals-recipient-err) (transfer-token token-id owner recipient))) ;; Transfers tokens to a specified principal. (define-public (transfer (token-id uint) (sender principal) (recipient principal)) (transfer-from token-id tx-sender recipient)) ;; Gets the owner of the specified token ID. (define-read-only (get-owner (token-id uint)) (ok (nft-get-owner? oas-nft token-id))) ;; SIP009: Get the last token ID (define-read-only (get-last-token-id) (ok (var-get last-token-id))) (define-read-only (get-token-uri (token-id uint)) ;; (ok (some "ipfs://ipfs/QmPAg1mjxcEQPPtqsLoEcauVedaeMH81WXDPvPx3VC5zUz")) (ok (map-get? tokens-meta-uri token-id)) ) ;; error handling (define-constant nft-not-owned-err (err u401)) ;; unauthorized (define-constant not-approved-spender-err (err u403)) ;; forbidden (define-constant not-admin-err (err u403)) ;; forbidden (define-constant no-admin-err (err u403)) ;; forbidden (define-constant nft-not-found-err (err u404)) ;; not found (define-constant sender-equals-recipient-err (err u405)) ;; method not allowed (define-constant nft-exists-err (err u409)) ;; conflict (define-map err-strings (response uint uint) (string-ascii 32)) (map-insert err-strings nft-not-owned-err "nft-not-owned") (map-insert err-strings not-approved-spender-err "not-approaved-spender") (map-insert err-strings not-admin-err "not-admin") (map-insert err-strings no-admin-err "no-admin") (map-insert err-strings nft-not-found-err "nft-not-found") (map-insert err-strings nft-exists-err "nft-exists") (define-private (nft-transfer-err (code uint)) (if (is-eq u1 code) nft-not-owned-err (if (is-eq u2 code) sender-equals-recipient-err (if (is-eq u3 code) nft-not-found-err (err code))))) (define-private (nft-mint-err (code uint)) (if (is-eq u1 code) nft-exists-err (err code))) (define-read-only (get-errstr (code uint)) (unwrap! (map-get? err-strings (err code)) "unknown-error")) ;; Initialize the contract (begin (map-set admins tx-sender true) (var-set admin-count u1) ;; (try! (mint tx-sender "https://www.google.com")) ;; (try! (claim "https://www.google.com")) )
0x418f66067e0ad8b7031fd5baa8850f9b38656598706f95f77e98b6156f77fa58
28,969
abort_by_response
Artie
;; Import ;; trait to implement ;; on mainnet (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) ;; on testnet ;;(impl-trait 'ST2PABAF9FTAJYNFZH93XENAJ8FVY99RRM4DF2YCW.nft-trait.nft-trait) ;; local ;;(impl-trait .nft-trait.nft-trait) ;; Non Fungible Token, modeled after ERC-721(this is the name that appears in wallet, global) (define-non-fungible-token OpenArtSource uint) ;; Storage (define-map tokens-spender uint principal) (define-map tokens-count principal uint) (define-map accounts-operator (tuple (operator principal) (account principal)) (tuple (is-approved bool))) (define-map tokens-meta-uri uint (string-ascii 256) ) ;; Store the last issued token ID (define-data-var last-token-id uint u0) ;; Store contract admins(simulate ethereum only_owner like usage) (define-map admins principal bool) ;; # of admin, control accidental removing all admins (define-data-var admin-count uint u0) (define-map last-token-count principal uint) (define-map tokens-owned { owner: principal, idx: uint } ;; principal and owner slot#(0...) uint ;; token id ) (define-map tokens-slot { owner: principal, token-id: uint } ;; principal and token id uint ;; slot# ) (define-constant UINT_LIST (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 )) (define-private (not-zero (a uint)) (> a u0) ) (define-private (add-base (a uint) (l (list 10 uint))) (get result (fold build-add-base l {base: a, result: (filter not-zero (list u0))})) ) (define-private (build-add-base (elem uint) (data { base: uint, result: (list 10 uint)})) (let ((base (get base data))) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) (+ elem base)) u10)) }) ) ) (define-private (block-range (upperLimit uint)) (get result (fold build-block-range UINT_LIST {upperLimit: upperLimit, result: (filter not-zero (list u0))})) ) (define-private (build-block-range (elem uint) (data { upperLimit: uint, result: (list 100 uint)})) (if (<= elem (get upperLimit data)) (merge data { result: (unwrap-panic (as-max-len? (concat (get result data) (add-base (* (- elem u1) u10) UINT_LIST)) u100)) }) data ) ) (define-private (range (upperLimit uint)) (let ((blocks (+ (/ upperLimit u10) u1))) (get result (fold build-range (block-range blocks) {upperLimit: upperLimit, result: (filter not-zero (list u0))})) ) ) (define-private (build-range (elem uint) (data { upperLimit: uint, result: (list 100 uint)})) (if (<= elem (get upperLimit data)) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) elem) u100)) }) data ) ) ;; Internals ;; Gets the amount of tokens owned by the specified address. (define-private (balance-of (account principal)) (default-to u0 (map-get? tokens-count account))) ;; Gets the approved address for a token ID, or zero if no address set (approved method in ERC721) (define-private (is-spender-approved (spender principal) (token-id uint)) (let ((approved-spender (unwrap! (map-get? tokens-spender token-id) false))) ;; return false if no specified spender (is-eq spender approved-spender))) ;; Tells whether an operator is approved by a given owner (isApprovedForAll method in ERC721) (define-private (is-operator-approved (account principal) (operator principal)) (default-to false (get is-approved (map-get? accounts-operator {operator: operator, account: account})))) (define-private (is-owner (actor principal) (token-id uint)) (is-eq actor ;; if no owner, return false (unwrap! (nft-get-owner? oas-nft token-id) false))) ;; Returns whether the given actor can transfer a given token ID. ;; To be optimized (define-private (can-transfer (actor principal) (token-id uint)) (or (is-owner actor token-id) (is-spender-approved actor token-id) (is-operator-approved (unwrap! (nft-get-owner? oas-nft token-id) false) actor))) ;; Internal - add token to owner list(for loop based lookup ) (define-private (add-token (owner principal) (token-id uint)) (let ( (newIdx (+ (default-to u0 (map-get? last-token-count owner)) u1)) ) (map-insert tokens-owned { owner: owner, idx: newIdx } token-id) (map-insert tokens-slot { owner: owner, token-id: token-id } newIdx) (map-set last-token-count owner newIdx) (ok true) ) ) ;; Internal - remove token from owner list(for loop based lookup ) (define-private (remove-token (owner principal) (token-id uint)) (let ( (idx (unwrap-panic (map-get? tokens-slot {owner: owner, token-id: token-id }))) (lastidx (unwrap-panic (map-get? last-token-count owner))) (last-owned-token-id (unwrap-panic (map-get? tokens-owned { owner: owner, idx: lastidx }))) ) (map-set tokens-owned { owner: owner, idx: idx } last-owned-token-id) (map-set tokens-slot { owner: owner, token-id: last-owned-token-id } idx) (map-delete tokens-slot { owner: owner, token-id: token-id }) (map-delete tokens-owned { owner: owner, idx: lastidx }) (map-set last-token-count owner (- lastidx u1)) (ok true) ) ) ;; Internal - Register token (define-private (mint (new-owner principal) (token-uri (string-ascii 256))) (let ((token-id (+ u1 (var-get last-token-id)))) (let ((current-balance (balance-of new-owner))) (match (nft-mint? oas-nft token-id new-owner) success (begin (var-set last-token-id token-id) (map-set tokens-count new-owner (+ u1 current-balance)) (map-set tokens-meta-uri token-id token-uri) (unwrap-panic (add-token new-owner token-id)) (ok success)) error (nft-mint-err error))))) ;; Internal - Tranfer token (define-private (transfer-token (token-id uint) (owner principal) (new-owner principal)) (let ((current-balance-owner (balance-of owner)) (current-balance-new-owner (balance-of new-owner))) (begin (map-delete tokens-spender token-id) (map-set tokens-count owner (- current-balance-owner u1)) (unwrap-panic (remove-token owner token-id)) (map-set tokens-count new-owner (+ current-balance-new-owner u1)) (unwrap-panic (add-token new-owner token-id)) (match (nft-transfer? oas-nft token-id owner new-owner) success (ok success) error (nft-transfer-err error))))) ;; Read only (define-read-only (is-admin) (or (default-to false (map-get? admins tx-sender)) (default-to false (map-get? admins contract-caller)) ) ) (define-read-only (get-token-owned-at (owner principal) (idx uint)) (unwrap-panic (map-get? tokens-owned {owner: owner, idx: idx})) ) (define-read-only (get-tokens-owned (owner principal)) (let ((token-count (default-to u0 (map-get? last-token-count owner)))) (if (> token-count u0) (get result (fold build-owner-tokens (range token-count) {owner: owner, result: (filter not-zero (list u0))})) (filter not-zero (list u0)) ) ) ) (define-private (build-owner-tokens (elem uint) (data { owner: principal, result: (list 100 uint)})) (let ((owner (get owner data))) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) (get-token-owned-at owner elem)) u100)) }) ) ) ;; Public functions ;; Claim a new NFT (define-public (claim (token-uri (string-ascii 256))) (mint tx-sender token-uri)) ;; mint a token to specific owner (define-public (mint-for (recipient principal) (token-uri (string-ascii 256))) (begin (asserts! (is-admin) not-admin-err) (mint recipient token-uri)) ) ;; mint a token to specific owner (define-public (set-admin (new-admin principal)) (begin (asserts! (is-admin) not-admin-err) (if (default-to false (map-get? admins new-admin)) (ok true) (begin (map-set admins new-admin true) (var-set admin-count (+ (var-get admin-count) u1)) (ok true) ) ) ) ) ;; mint a token to specific owner (define-public (remove-admin (old-admin principal) (allow-no-admin bool)) (begin (asserts! (is-admin) not-admin-err) (if (default-to false (map-get? admins old-admin)) false (begin (map-delete admins old-admin) (var-set admin-count (- (var-get admin-count) u1)) true ) ) (asserts! (or allow-no-admin (> (var-get admin-count) u0)) not-admin-err) (ok true) ) ) ;; Approves another address to transfer the given token ID (approve method in ERC721) ;; To be optimized (define-public (set-spender-approval (spender principal) (token-id uint)) (if (is-eq spender tx-sender) sender-equals-recipient-err (if (or (is-owner tx-sender token-id) (is-operator-approved (unwrap! (nft-get-owner? oas-nft token-id) nft-not-found-err) tx-sender)) (begin (map-set tokens-spender token-id spender) (ok token-id)) not-approved-spender-err))) ;; Sets or unsets the approval of a given operator (setApprovalForAll method in ERC721) (define-public (set-operator-approval (operator principal) (is-approved bool)) (if (is-eq operator tx-sender) sender-equals-recipient-err (begin (map-set accounts-operator {operator: operator, account: tx-sender} {is-approved: is-approved}) (ok true)))) ;; Transfers the ownership of a given token ID to another address. (define-public (transfer-from (token-id uint) (owner principal) (recipient principal)) (begin (asserts! (can-transfer tx-sender token-id) not-approved-spender-err) (asserts! (is-owner owner token-id) nft-not-owned-err) (asserts! (not (is-eq recipient owner)) sender-equals-recipient-err) (transfer-token token-id owner recipient))) ;; Transfers tokens to a specified principal. (define-public (transfer (token-id uint) (sender principal) (recipient principal)) (transfer-from token-id tx-sender recipient)) ;; Gets the owner of the specified token ID. (define-read-only (get-owner (token-id uint)) (ok (nft-get-owner? oas-nft token-id))) ;; SIP009: Get the last token ID (define-read-only (get-last-token-id) (ok (var-get last-token-id))) (define-read-only (get-token-uri (token-id uint)) ;; (ok (some "ipfs://ipfs/QmPAg1mjxcEQPPtqsLoEcauVedaeMH81WXDPvPx3VC5zUz")) (ok (map-get? tokens-meta-uri token-id)) ) ;; error handling (define-constant nft-not-owned-err (err u401)) ;; unauthorized (define-constant not-approved-spender-err (err u403)) ;; forbidden (define-constant not-admin-err (err u403)) ;; forbidden (define-constant no-admin-err (err u403)) ;; forbidden (define-constant nft-not-found-err (err u404)) ;; not found (define-constant sender-equals-recipient-err (err u405)) ;; method not allowed (define-constant nft-exists-err (err u409)) ;; conflict (define-map err-strings (response uint uint) (string-ascii 32)) (map-insert err-strings nft-not-owned-err "nft-not-owned") (map-insert err-strings not-approved-spender-err "not-approaved-spender") (map-insert err-strings not-admin-err "not-admin") (map-insert err-strings no-admin-err "no-admin") (map-insert err-strings nft-not-found-err "nft-not-found") (map-insert err-strings nft-exists-err "nft-exists") (define-private (nft-transfer-err (code uint)) (if (is-eq u1 code) nft-not-owned-err (if (is-eq u2 code) sender-equals-recipient-err (if (is-eq u3 code) nft-not-found-err (err code))))) (define-private (nft-mint-err (code uint)) (if (is-eq u1 code) nft-exists-err (err code))) (define-read-only (get-errstr (code uint)) (unwrap! (map-get? err-strings (err code)) "unknown-error")) ;; Initialize the contract (begin (map-set admins tx-sender true) (var-set admin-count u1) ;; (try! (mint tx-sender "https://www.google.com")) ;; (try! (claim "https://www.google.com")) )
0x418f66067e0ad8b7031fd5baa8850f9b38656598706f95f77e98b6156f77fa58
28,969
abort_by_response
Artie
;; Import ;; trait to implement ;; on mainnet (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) ;; on testnet ;;(impl-trait 'ST2PABAF9FTAJYNFZH93XENAJ8FVY99RRM4DF2YCW.nft-trait.nft-trait) ;; local ;;(impl-trait .nft-trait.nft-trait) ;; Non Fungible Token, modeled after ERC-721(this is the name that appears in wallet, global) (define-non-fungible-token OpenArtSource uint) ;; Storage (define-map tokens-spender uint principal) (define-map tokens-count principal uint) (define-map accounts-operator (tuple (operator principal) (account principal)) (tuple (is-approved bool))) (define-map tokens-meta-uri uint (string-ascii 256) ) ;; Store the last issued token ID (define-data-var last-token-id uint u0) ;; Store contract admins(simulate ethereum only_owner like usage) (define-map admins principal bool) ;; # of admin, control accidental removing all admins (define-data-var admin-count uint u0) (define-map last-token-count principal uint) (define-map tokens-owned { owner: principal, idx: uint } ;; principal and owner slot#(0...) uint ;; token id ) (define-map tokens-slot { owner: principal, token-id: uint } ;; principal and token id uint ;; slot# ) (define-constant UINT_LIST (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 )) (define-private (not-zero (a uint)) (> a u0) ) (define-private (add-base (a uint) (l (list 10 uint))) (get result (fold build-add-base l {base: a, result: (filter not-zero (list u0))})) ) (define-private (build-add-base (elem uint) (data { base: uint, result: (list 10 uint)})) (let ((base (get base data))) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) (+ elem base)) u10)) }) ) ) (define-private (block-range (upperLimit uint)) (get result (fold build-block-range UINT_LIST {upperLimit: upperLimit, result: (filter not-zero (list u0))})) ) (define-private (build-block-range (elem uint) (data { upperLimit: uint, result: (list 100 uint)})) (if (<= elem (get upperLimit data)) (merge data { result: (unwrap-panic (as-max-len? (concat (get result data) (add-base (* (- elem u1) u10) UINT_LIST)) u100)) }) data ) ) (define-private (range (upperLimit uint)) (let ((blocks (+ (/ upperLimit u10) u1))) (get result (fold build-range (block-range blocks) {upperLimit: upperLimit, result: (filter not-zero (list u0))})) ) ) (define-private (build-range (elem uint) (data { upperLimit: uint, result: (list 100 uint)})) (if (<= elem (get upperLimit data)) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) elem) u100)) }) data ) ) ;; Internals ;; Gets the amount of tokens owned by the specified address. (define-private (balance-of (account principal)) (default-to u0 (map-get? tokens-count account))) ;; Gets the approved address for a token ID, or zero if no address set (approved method in ERC721) (define-private (is-spender-approved (spender principal) (token-id uint)) (let ((approved-spender (unwrap! (map-get? tokens-spender token-id) false))) ;; return false if no specified spender (is-eq spender approved-spender))) ;; Tells whether an operator is approved by a given owner (isApprovedForAll method in ERC721) (define-private (is-operator-approved (account principal) (operator principal)) (default-to false (get is-approved (map-get? accounts-operator {operator: operator, account: account})))) (define-private (is-owner (actor principal) (token-id uint)) (is-eq actor ;; if no owner, return false (unwrap! (nft-get-owner? OpenArtSource token-id) false))) ;; Returns whether the given actor can transfer a given token ID. ;; To be optimized (define-private (can-transfer (actor principal) (token-id uint)) (or (is-owner actor token-id) (is-spender-approved actor token-id) (is-operator-approved (unwrap! (nft-get-owner? OpenArtSource token-id) false) actor))) ;; Internal - add token to owner list(for loop based lookup ) (define-private (add-token (owner principal) (token-id uint)) (let ( (newIdx (+ (default-to u0 (map-get? last-token-count owner)) u1)) ) (map-insert tokens-owned { owner: owner, idx: newIdx } token-id) (map-insert tokens-slot { owner: owner, token-id: token-id } newIdx) (map-set last-token-count owner newIdx) (ok true) ) ) ;; Internal - remove token from owner list(for loop based lookup ) (define-private (remove-token (owner principal) (token-id uint)) (let ( (idx (unwrap-panic (map-get? tokens-slot {owner: owner, token-id: token-id }))) (lastidx (unwrap-panic (map-get? last-token-count owner))) (last-owned-token-id (unwrap-panic (map-get? tokens-owned { owner: owner, idx: lastidx }))) ) (map-set tokens-owned { owner: owner, idx: idx } last-owned-token-id) (map-set tokens-slot { owner: owner, token-id: last-owned-token-id } idx) (map-delete tokens-slot { owner: owner, token-id: token-id }) (map-delete tokens-owned { owner: owner, idx: lastidx }) (map-set last-token-count owner (- lastidx u1)) (ok true) ) ) ;; Internal - Register token (define-private (mint (new-owner principal) (token-uri (string-ascii 256))) (let ((token-id (+ u1 (var-get last-token-id)))) (let ((current-balance (balance-of new-owner))) (match (nft-mint? OpenArtSource token-id new-owner) success (begin (var-set last-token-id token-id) (map-set tokens-count new-owner (+ u1 current-balance)) (map-set tokens-meta-uri token-id token-uri) (unwrap-panic (add-token new-owner token-id)) (ok success)) error (nft-mint-err error))))) ;; Internal - Tranfer token (define-private (transfer-token (token-id uint) (owner principal) (new-owner principal)) (let ((current-balance-owner (balance-of owner)) (current-balance-new-owner (balance-of new-owner))) (begin (map-delete tokens-spender token-id) (map-set tokens-count owner (- current-balance-owner u1)) (unwrap-panic (remove-token owner token-id)) (map-set tokens-count new-owner (+ current-balance-new-owner u1)) (unwrap-panic (add-token new-owner token-id)) (match (nft-transfer? OpenArtSource token-id owner new-owner) success (ok success) error (nft-transfer-err error))))) ;; Read only (define-read-only (is-admin) (or (default-to false (map-get? admins tx-sender)) (default-to false (map-get? admins contract-caller)) ) ) (define-read-only (get-token-owned-at (owner principal) (idx uint)) (unwrap-panic (map-get? tokens-owned {owner: owner, idx: idx})) ) (define-read-only (get-tokens-owned (owner principal)) (let ((token-count (default-to u0 (map-get? last-token-count owner)))) (if (> token-count u0) (get result (fold build-owner-tokens (range token-count) {owner: owner, result: (filter not-zero (list u0))})) (filter not-zero (list u0)) ) ) ) (define-private (build-owner-tokens (elem uint) (data { owner: principal, result: (list 100 uint)})) (let ((owner (get owner data))) (merge data { result: (unwrap-panic (as-max-len? (append (get result data) (get-token-owned-at owner elem)) u100)) }) ) ) ;; Public functions ;; Claim a new NFT (define-public (claim (token-uri (string-ascii 256))) (mint tx-sender token-uri)) ;; mint a token to specific owner (define-public (mint-for (recipient principal) (token-uri (string-ascii 256))) (begin (asserts! (is-admin) not-admin-err) (mint recipient token-uri)) ) ;; mint a token to specific owner (define-public (set-admin (new-admin principal)) (begin (asserts! (is-admin) not-admin-err) (if (default-to false (map-get? admins new-admin)) (ok true) (begin (map-set admins new-admin true) (var-set admin-count (+ (var-get admin-count) u1)) (ok true) ) ) ) ) ;; mint a token to specific owner (define-public (remove-admin (old-admin principal) (allow-no-admin bool)) (begin (asserts! (is-admin) not-admin-err) (if (default-to false (map-get? admins old-admin)) false (begin (map-delete admins old-admin) (var-set admin-count (- (var-get admin-count) u1)) true ) ) (asserts! (or allow-no-admin (> (var-get admin-count) u0)) not-admin-err) (ok true) ) ) ;; Approves another address to transfer the given token ID (approve method in ERC721) ;; To be optimized (define-public (set-spender-approval (spender principal) (token-id uint)) (if (is-eq spender tx-sender) sender-equals-recipient-err (if (or (is-owner tx-sender token-id) (is-operator-approved (unwrap! (nft-get-owner? OpenArtSource token-id) nft-not-found-err) tx-sender)) (begin (map-set tokens-spender token-id spender) (ok token-id)) not-approved-spender-err))) ;; Sets or unsets the approval of a given operator (setApprovalForAll method in ERC721) (define-public (set-operator-approval (operator principal) (is-approved bool)) (if (is-eq operator tx-sender) sender-equals-recipient-err (begin (map-set accounts-operator {operator: operator, account: tx-sender} {is-approved: is-approved}) (ok true)))) ;; Transfers the ownership of a given token ID to another address. (define-public (transfer-from (token-id uint) (owner principal) (recipient principal)) (begin (asserts! (can-transfer tx-sender token-id) not-approved-spender-err) (asserts! (is-owner owner token-id) nft-not-owned-err) (asserts! (not (is-eq recipient owner)) sender-equals-recipient-err) (transfer-token token-id owner recipient))) ;; Transfers tokens to a specified principal. (define-public (transfer (token-id uint) (sender principal) (recipient principal)) (transfer-from token-id tx-sender recipient)) ;; Gets the owner of the specified token ID. (define-read-only (get-owner (token-id uint)) (ok (nft-get-owner? OpenArtSource token-id))) ;; SIP009: Get the last token ID (define-read-only (get-last-token-id) (ok (var-get last-token-id))) (define-read-only (get-token-uri (token-id uint)) ;; (ok (some "ipfs://ipfs/QmPAg1mjxcEQPPtqsLoEcauVedaeMH81WXDPvPx3VC5zUz")) (ok (map-get? tokens-meta-uri token-id)) ) ;; error handling (define-constant nft-not-owned-err (err u401)) ;; unauthorized (define-constant not-approved-spender-err (err u403)) ;; forbidden (define-constant not-admin-err (err u403)) ;; forbidden (define-constant no-admin-err (err u403)) ;; forbidden (define-constant nft-not-found-err (err u404)) ;; not found (define-constant sender-equals-recipient-err (err u405)) ;; method not allowed (define-constant nft-exists-err (err u409)) ;; conflict (define-map err-strings (response uint uint) (string-ascii 32)) (map-insert err-strings nft-not-owned-err "nft-not-owned") (map-insert err-strings not-approved-spender-err "not-approaved-spender") (map-insert err-strings not-admin-err "not-admin") (map-insert err-strings no-admin-err "no-admin") (map-insert err-strings nft-not-found-err "nft-not-found") (map-insert err-strings nft-exists-err "nft-exists") (define-private (nft-transfer-err (code uint)) (if (is-eq u1 code) nft-not-owned-err (if (is-eq u2 code) sender-equals-recipient-err (if (is-eq u3 code) nft-not-found-err (err code))))) (define-private (nft-mint-err (code uint)) (if (is-eq u1 code) nft-exists-err (err code))) (define-read-only (get-errstr (code uint)) (unwrap! (map-get? err-strings (err code)) "unknown-error")) ;; Initialize the contract (begin (map-set admins tx-sender true) (var-set admin-count u1) ;; (try! (mint tx-sender "https://www.google.com")) ;; (try! (claim "https://www.google.com")) )
0xf1fd5fad5bbd48f299ff48cd56ac1f0249394e6c862ee067e18e57ab8fe7aaca
28,974
success
dutch-olive-rook
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP4R4XEEQ2HG3GYC917ZWD10J41KD9AC0CCAKJ74) (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)))
0x8b1c7dc274c2816169b1ac58ea937b20a6edd5bcf259dd985820e886ffea27f9
29,119
success
Moment
;; Use the SIP090 interface (testnet) (impl-trait 'ST2PABAF9FTAJYNFZH93XENAJ8FVY99RRM4DF2YCW.nft-trait.nft-trait) ;; Define a new NFT (define-non-fungible-token Moment uint) ;; Store the last issues token ID (define-data-var last-id uint u0) ;; Store metadata (define-map meta uint (tuple (url (string-ascii 2048)) )) ;; Mint a new NFT (define-public (mint (url (string-ascii 2048))) (create tx-sender url)) ;; SIP009: transfer token to a specified principal (define-public (transfer (token-id uint) (sender principal) (recipient principal)) (if (and (is-eq tx-sender sender)) (match (nft-transfer? Moment token-id sender recipient) success (ok success) error (err error)) (err u500))) ;; SIP009: Get the owner of the specified token ID (define-read-only (get-owner (token-id uint)) ;; Make sure to replace Moment (ok (nft-get-owner? Moment token-id))) ;; SIP009: Get the last token ID (define-read-only (get-last-token-id) (ok (var-get last-id))) ;; SIP009: Get the token URI. You can set it to any other URI (define-read-only (get-token-uri (token-id uint)) (ok (some "https://momentonft.com/moments"))) (define-read-only (get-meta? (id uint)) (map-get? meta id)) ;; Internal - Mint new NFT (define-private (create (new-owner principal) (url (string-ascii 2048))) (let ((next-id (+ u1 (var-get last-id)))) (match (nft-mint? Moment next-id new-owner) success (begin (var-set last-id next-id) (map-insert meta next-id { url: url }) (ok true)) error (err error))))
0x842367b651c2187d7b31062083e18f5c6327ab52e801fca14ec42c51da3a0ff5
29,172
abort_by_response
Moment
;; Use the SIP090 interface (mainnet) (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) ;; Define a new NFT (define-non-fungible-token Moment uint) ;; Store the last issues token ID (define-data-var last-id uint u0) ;; Store metadata (define-map meta uint (tuple (url (string-ascii 2048)) )) ;; Mint a new NFT (define-public (mint (url (string-ascii 2048))) (create tx-sender url)) ;; SIP009: transfer token to a specified principal (define-public (transfer (token-id uint) (sender principal) (recipient principal)) (if (and (is-eq tx-sender sender)) (match (nft-transfer? Moment token-id sender recipient) success (ok success) error (err error)) (err u500))) ;; SIP009: Get the owner of the specified token ID (define-read-only (get-owner (token-id uint)) ;; Make sure to replace Moment (ok (nft-get-owner? Moment token-id))) ;; SIP009: Get the last token ID (define-read-only (get-last-token-id) (ok (var-get last-id))) ;; SIP009: Get the token URI. You can set it to any other URI (define-read-only (get-token-uri (token-id uint)) (ok (some "https://momentonft.com/moments"))) (define-read-only (get-meta? (id uint)) (map-get? meta id)) ;; Internal - Mint new NFT (define-private (create (new-owner principal) (url (string-ascii 2048))) (let ((next-id (+ u1 (var-get last-id)))) (match (nft-mint? Moment next-id new-owner) success (begin (var-set last-id next-id) (map-insert meta next-id { url: url }) (ok true)) error (err error))))
0xe2bd8e6e1cf0738617a5e37bea2d7107ae3eafae1b00bf6158ee4dbc672571d3
29,175
success
activate-novel-token-19
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP38NZD5JZTN7BBBR6T76HAW8Z25DMPAKJHNMQ1B1) (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)))
0xd432161a271054b3f13d6f6d0aaeca774a5e9bbf2d67742cc8ab07f639260aab
29,257
success
phases-of-satoshi
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) (define-non-fungible-token phases-of-satoshi uint) ;; constants (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-ALL-MINTED u101) (define-constant CONTRACT-OWNER tx-sender) ;; variables (define-data-var phases-index uint u0) (define-data-var phases-counter uint u6) (define-data-var token-uri (string-ascii 256) "") (define-data-var token-phases-uri (string-ascii 256) "") (define-map phases { id: uint } { minted: bool }) (define-map phases-by-owner { owner: principal } { ids: (list 210 uint) }) (define-data-var removing-phase-id uint u0) (define-data-var cost-per-mint uint u24000000) (define-data-var creator-address principal 'SPBFZ5MRGDMEKWNQTJ57W2PA2GC0765ZFC5BY0KP) ;; STBFZ5MRGDMEKWNQTJ57W2PA2GC0765ZFC33E2WE TESTNET (define-data-var nft-ids (list 210 uint) (list u122 u142 u15 u104 u118 u117 u145 u37 u77 u210 u17 u89 u60 u68 u163 u53 u28 u100 u182 u198 u160 u78 u196 u149 u157 u137 u135 u83 u121 u141 u64 u85 u16 u40 u55 u204 u197 u33 u67 u65 u8 u183 u123 u148 u70 u175 u136 u105 u49 u131 u159 u10 u75 u91 u127 u12 u147 u20 u178 u209 u162 u180 u52 u23 u61 u171 u176 u203 u99 u139 u188 u134 u35 u79 u95 u50 u36 u140 u93 u152 u166 u71 u143 u82 u73 u144 u114 u200 u18 u48 u54 u189 u30 u164 u88 u113 u26 u184 u132 u63 u41 u38 u177 u81 u46 u186 u9 u133 u191 u84 u101 u92 u201 u47 u96 u107 u185 u154 u74 u72 u169 u90 u94 u58 u42 u206 u174 u146 u103 u106 u11 u167 u165 u31 u151 u155 u109 u80 u32 u170 u44 u205 u138 u194 u179 u172 u168 u51 u195 u187 u119 u156 u86 u126 u14 u111 u153 u39 u56 u69 u22 u19 u62 u192 u120 u116 u21 u202 u13 u108 u102 u98 u57 u207 u150 u34 u161 u124 u181 u43 u130 u158 u87 u7 u199 u125 u208 u128 u45 u66 u190 u129 u25 u115 u193 u173 u27 u59 u29 u24 u110 u97 u76 u112)) ;; public functions (define-public (mint) (begin (asserts! (<= (var-get phases-counter) u210) (err ERR-ALL-MINTED)) (try! (mint-next)) (ok true) ) ) (define-private (mint-next) (let ( (count (var-get phases-counter)) (phases-ids (unwrap-panic (get-phases-by-owner tx-sender))) (random-phases-id (unwrap-panic (element-at (var-get nft-ids) (var-get phases-index)))) ) (match (stx-transfer? (var-get cost-per-mint) tx-sender (as-contract tx-sender)) success (begin (try! (nft-mint? phases-of-satoshi random-phases-id tx-sender)) (var-set phases-counter (+ u1 count)) (var-set phases-index (+ u1 (var-get phases-index))) (map-set phases { id: random-phases-id } { minted: true }) (map-set phases-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append phases-ids random-phases-id) u210)) } ) (try! (as-contract (stx-transfer? (/ (* u8000 (var-get cost-per-mint)) u10000) (as-contract tx-sender) (var-get creator-address)))) (ok random-phases-id) ) error (err error) ) ) ) (define-read-only (get-phases-entry-by-owner (owner principal)) (default-to { ids: (list ) } (map-get? phases-by-owner { owner: owner }) ) ) (define-public (get-phases-by-owner (owner principal)) (ok (get ids (get-phases-entry-by-owner owner))) ) (define-public (burn (index uint)) (if (is-owner index tx-sender) (match (nft-burn? phases-of-satoshi 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? phases-of-satoshi index owner recipient) success (let ((phases-ids (unwrap-panic (get-phases-by-owner recipient)))) (map-set phases-by-owner { owner: recipient } { ids: (unwrap-panic (as-max-len? (append phases-ids index) u210)) } ) (try! (remove-phase owner index)) (ok true) ) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-private (remove-phase (owner principal) (phases-id uint)) (if true (let ((phases-ids (unwrap-panic (get-phases-by-owner owner)))) (var-set removing-phase-id phases-id) (map-set phases-by-owner { owner: owner } { ids: (unwrap-panic (as-max-len? (filter remove-transferred-phases phases-ids) u210)) } ) (ok true) ) (err u0) ) ) (define-private (remove-transferred-phases (phases-id uint)) (if (is-eq phases-id (var-get removing-phase-id)) false true ) ) (define-read-only (get-last-token-id) (ok (var-get phases-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-phases-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-phases-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-creator-address (address principal)) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set creator-address address)) (err ERR-NOT-AUTHORIZED) ) ) (define-read-only (get-token-uri (id uint)) (if (not (is-eq id u0)) (ok (some (var-get token-phases-uri))) (ok (some (var-get token-uri))) ) ) (define-public (get-owner (index uint)) (ok (nft-get-owner? phases-of-satoshi 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? phases-of-satoshi index) false)) ) ;; initialize (var-set token-phases-uri "https://www.stacksart.com/assets/phases_of_satoshi.json") (var-set token-uri "https://www.stacksart.com/assets/phases_of_satoshi.json") (let ( (phases-ids (unwrap-panic (get-phases-by-owner 'SP3N6EZPTSX8ZV2RGPY9NR9A8CA0QET39CY978H5E))) ) (try! (nft-mint? phases-of-satoshi u1 'SP3N6EZPTSX8ZV2RGPY9NR9A8CA0QET39CY978H5E)) (map-set phases { id: u1 } { minted: true }) (map-set phases-by-owner { owner: 'SP3N6EZPTSX8ZV2RGPY9NR9A8CA0QET39CY978H5E } { ids: (unwrap-panic (as-max-len? (append phases-ids u1) u210)) }) ) (let ( (phases-ids (unwrap-panic (get-phases-by-owner 'SP3N6EZPTSX8ZV2RGPY9NR9A8CA0QET39CY978H5E))) ) (try! (nft-mint? phases-of-satoshi u2 'SP3N6EZPTSX8ZV2RGPY9NR9A8CA0QET39CY978H5E)) (map-set phases { id: u2 } { minted: true }) (map-set phases-by-owner { owner: 'SP3N6EZPTSX8ZV2RGPY9NR9A8CA0QET39CY978H5E } { ids: (unwrap-panic (as-max-len? (append phases-ids u2) u210)) }) ) (let ( (phases-ids (unwrap-panic (get-phases-by-owner 'SP14R7S7497PS3VMH3WQ1S6NPNXR47G3RRC1G2K0G))) ) (try! (nft-mint? phases-of-satoshi u3 'SP14R7S7497PS3VMH3WQ1S6NPNXR47G3RRC1G2K0G)) (map-set phases { id: u3 } { minted: true }) (map-set phases-by-owner { owner: 'SP14R7S7497PS3VMH3WQ1S6NPNXR47G3RRC1G2K0G } { ids: (unwrap-panic (as-max-len? (append phases-ids u3) u210)) }) ) (let ( (phases-ids (unwrap-panic (get-phases-by-owner 'SP3N66VSF1HAH9BP36XEAT2JZWZ45TDJXWENGS7Y5))) ) (try! (nft-mint? phases-of-satoshi u4 'SP3N66VSF1HAH9BP36XEAT2JZWZ45TDJXWENGS7Y5)) (map-set phases { id: u4 } { minted: true }) (map-set phases-by-owner { owner: 'SP3N66VSF1HAH9BP36XEAT2JZWZ45TDJXWENGS7Y5 } { ids: (unwrap-panic (as-max-len? (append phases-ids u4) u210)) }) ) (let ( (phases-ids (unwrap-panic (get-phases-by-owner 'SP4P7ZGMPH4MGF8ZD0TFG035DNFZP4X3RMBPFKT3))) ) (try! (nft-mint? phases-of-satoshi u5 'SP4P7ZGMPH4MGF8ZD0TFG035DNFZP4X3RMBPFKT3)) (map-set phases { id: u5 } { minted: true }) (map-set phases-by-owner { owner: 'SP4P7ZGMPH4MGF8ZD0TFG035DNFZP4X3RMBPFKT3 } { ids: (unwrap-panic (as-max-len? (append phases-ids u5) u210)) }) ) (let ( (phases-ids (unwrap-panic (get-phases-by-owner 'SP3X9696B2ZKFMBHEJF1RBWMYDQ2YGXDA11GFYTE5))) ) (try! (nft-mint? phases-of-satoshi u6 'SP3X9696B2ZKFMBHEJF1RBWMYDQ2YGXDA11GFYTE5)) (map-set phases { id: u6 } { minted: true }) (map-set phases-by-owner { owner: 'SP3X9696B2ZKFMBHEJF1RBWMYDQ2YGXDA11GFYTE5 } { ids: (unwrap-panic (as-max-len? (append phases-ids u6) u210)) }) )
0x55285c76d0bc9c72c6024a10ef8f7a5338534cf65cbe1cea5635e44de2a531bd
29,299
success
claim-mining-reward
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP38NZD5JZTN7BBBR6T76HAW8Z25DMPAKJHNMQ1B1) (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)))
0x3326f442a8bd62222ecfca71bfb230a9c9287f9396b2184219c767a86cd784d2
29,504
success
test-stackswap-tt
;; ;; 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 u2240722213) ) (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) (ok u123) ) ) (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) ) )
0x64ca1bd747c718de405b883c1c152a626d4585f00cb0b9fb1ad1c0c38946038e
29,504
abort_by_response
test-stackswap-tt-2
;; ;; 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 u2240722213) ) (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) (ok true) ) ) (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) ) )
0x45329584f6694dfd6e4bfff9584cd75639981b4368ec54d6ff0c588ce4347d35
29,508
success
loopbomb-stx-v0
;; Interface definitions ;; test/mocknet ;; (impl-trait 'SP3QSAJQ4EA8WXEDSRRKMZZ29NH91VZ6C5X88FGZQ.nft-approvable-trait.nft-approvable-trait) ;; (impl-trait 'SP3QSAJQ4EA8WXEDSRRKMZZ29NH91VZ6C5X88FGZQ.nft-trait.nft-trait) ;; mainnet ;; (impl-trait SP3QSAJQ4EA8WXEDSRRKMZZ29NH91VZ6C5X88FGZQ.nft-approvable-trait.nft-approvable-trait) ;; (impl-trait SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) ;; contract variables (define-data-var administrator principal 'SP3QSAJQ4EA8WXEDSRRKMZZ29NH91VZ6C5X88FGZQ) (define-data-var mint-price uint u100000) (define-data-var base-token-uri (string-ascii 256) "https://loopbomb.io/nfts/") (define-data-var mint-counter uint u0) (define-data-var platform-fee uint u5) ;; constants (define-constant token-name "loopbomb") (define-constant token-symbol "LOOP") ;; Non Fungible Token, modeled after ERC-721 via nft-trait ;; Note this is a basic implementation - no support yet for setting approvals for assets ;; NFT are identified by nft-index (uint) which is tied via a reverse lookup to a real world ;; asset hash - SHA 256 32 byte value. The Asset Hash is used to tie arbitrary real world ;; data to the NFT (define-non-fungible-token my-nft uint) ;; data structures (define-map nft-approvals {nft-index: uint} {approval: principal}) (define-map nft-lookup {asset-hash: (buff 32), edition: uint} {nft-index: uint}) (define-map nft-data {nft-index: uint} {asset-hash: (buff 32), meta-data-url: (buff 200), max-editions: uint, edition: uint, edition-cost: uint, mint-block-height: uint, series-original: uint}) (define-map nft-sale-data {nft-index: uint} {sale-type: uint, increment-stx: uint, reserve-stx: uint, amount-stx: uint, bidding-end-time: uint, sale-cycle-index: uint}) (define-map nft-beneficiaries {nft-index: uint} { addresses: (list 10 principal), shares: (list 10 uint), secondaries: (list 10 uint) }) (define-map nft-bid-history {nft-index: uint, bid-index: uint} {sale-cycle: uint, bidder: principal, amount: uint, app-timestamp: uint}) (define-map nft-offer-history {nft-index: uint, offer-index: uint} {sale-cycle: uint, offerer: principal, app-timestamp: uint, amount: uint, accepted: uint}) ;; counters keep track per NFT of the... ;; a) number of editions minted (1 based index) ;; b) number of offers made (0 based index) ;; c) number of bids made (0 based index) (define-map nft-offer-counter {nft-index: uint} {offer-counter: uint, sale-cycle: uint}) (define-map nft-edition-counter {nft-index: uint} {edition-counter: uint}) (define-map nft-high-bid-counter {nft-index: uint} {high-bid-counter: uint, sale-cycle: uint}) (define-constant percentage-with-twodp u10000000000) (define-constant not-allowed (err u10)) (define-constant not-found (err u11)) (define-constant amount-not-set (err u12)) (define-constant seller-not-found (err u13)) (define-constant asset-not-registered (err u14)) (define-constant transfer-error (err u15)) (define-constant not-approved-to-sell (err u16)) (define-constant same-spender-err (err u17)) (define-constant failed-to-mint-err (err u18)) (define-constant edition-counter-error (err u19)) (define-constant edition-limit-reached (err u20)) (define-constant user-amount-different (err u21)) (define-constant failed-to-stx-transfer (err u22)) (define-constant failed-to-close-1 (err u23)) (define-constant failed-refund (err u24)) (define-constant failed-to-close-3 (err u24)) (define-constant cant-pay-mint-price (err u25)) (define-constant editions-error (err u26)) (define-constant payment-error (err u28)) (define-constant payment-address-error (err u33)) (define-constant payment-share-error (err u34)) (define-constant bidding-error (err u35)) (define-constant prevbid-bidding-error (err u36)) (define-constant not-originale (err u37)) (define-constant bidding-opening-error (err u38)) (define-constant bidding-amount-error (err u39)) (define-constant bidding-endtime-error (err u40)) (define-constant nft-not-owned-err (err u401)) ;; unauthorized (define-constant sender-equals-recipient-err (err u405)) ;; method not allowed (define-constant nft-not-found-err (err u404)) ;; not found ;; interface methods ;; from nft-trait: Last token ID, limited to uint range ;; note decrement as mint counter is the id of the next nft (define-read-only (get-last-token-id) (ok (- (var-get mint-counter) u1)) ) ;; from nft-trait: URI for metadata associated with the token (define-read-only (get-token-uri (nftIndex uint)) (ok (some (var-get base-token-uri))) ) ;; from nft-trait: Gets the owner of the 'SPecified token ID. (define-read-only (get-owner (nftIndex uint)) (ok (nft-get-owner? my-nft nftIndex)) ) ;; from nft-trait: Gets the owner of the 'SPecified token ID. (define-read-only (get-approval (nftIndex uint)) (ok (unwrap! (get approval (map-get? nft-approvals {nft-index: nftIndex})) not-found)) ) ;; sets an approval principal - allowed to call transfer on owner behalf. (define-public (set-approval-for (nftIndex uint) (approval principal)) (if (is-owner nftIndex tx-sender) (begin (map-set nft-approvals {nft-index: nftIndex} {approval: approval}) (ok true) ) nft-not-owned-err ) ) ;; Transfers tokens to a 'SPecified principal. (define-public (transfer (nftIndex uint) (owner principal) (recipient principal)) (if (and (is-owner-or-approval nftIndex owner) (is-owner-or-approval nftIndex tx-sender)) (match (nft-transfer? my-nft nftIndex owner recipient) success (ok true) error (nft-transfer-err error)) nft-not-owned-err) ) (define-private (nft-transfer-err (code uint)) (if (is-eq u1 code) nft-not-owned-err (if (is-eq u2 code) sender-equals-recipient-err (if (is-eq u3 code) nft-not-found-err (err code))))) (define-private (is-owner (nftIndex uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? my-nft nftIndex) false)) ) (define-private (is-approval (nftIndex uint) (user principal)) (is-eq user (unwrap! (get approval (map-get? nft-approvals {nft-index: nftIndex})) false)) ) (define-private (is-owner-or-approval (nftIndex uint) (user principal)) (if (is-owner nftIndex user) true (if (is-approval nftIndex user) true false) ) ) ;; public methods ;; -------------- ;; the contract administrator can change the contract administrator (define-public (transfer-administrator (new-administrator principal)) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (var-set administrator new-administrator) (ok true) ) ) ;; the contract administrator can change the transfer fee charged by the contract on sale of tokens (define-public (change-fee (new-fee uint)) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (var-set platform-fee new-fee) (ok true) ) ) ;; the contract administrator can change the base uri - where meta data for tokens in this contract ;; are located (define-public (update-base-token-uri (new-base-token-uri (string-ascii 256))) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (var-set base-token-uri new-base-token-uri) (ok true) ) ) ;; the contract administrator can change the mint price (define-public (update-mint-price (new-mint-price uint)) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (var-set mint-price new-mint-price) (ok true) ) ) ;; The administrator can transfer the balance in the contract to another address (define-public (transfer-balance (recipient principal)) (let ( (balance (stx-get-balance (as-contract tx-sender))) ) (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (unwrap! (stx-transfer? balance (as-contract tx-sender) recipient) failed-to-stx-transfer) (print {evt: "transfer-balance", recipient: recipient, balance: balance}) (ok balance) ) ) ;; adds an offer to the list of offers on an NFT (define-public (make-offer (nft-index uint) (amount uint) (app-timestamp uint)) (let ( (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nft-index})) not-allowed)) (offerCounter (default-to u0 (get offer-counter (map-get? nft-offer-counter {nft-index: nft-index})))) (saleCycleIndex (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nft-index})) amount-not-set)) ) (map-insert nft-offer-history {nft-index: nft-index, offer-index: offerCounter} {sale-cycle: saleCycleIndex, offerer: tx-sender, app-timestamp: app-timestamp, amount: amount, accepted: u0}) (map-set nft-offer-counter {nft-index: nft-index} {sale-cycle: saleCycleIndex, offer-counter: (+ offerCounter u1)}) (ok (+ offerCounter u1)) ) ) ;; accept-offer ;; marks offer as accepted and transfers ownership to the recipient (define-public (accept-offer (nft-index uint) (offer-index uint) (owner principal) (recipient principal)) (let ( (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nft-index})) not-allowed)) (offerer (unwrap! (get offerer (map-get? nft-offer-history {nft-index: nft-index, offer-index: offer-index})) not-allowed)) (app-timestamp (unwrap! (get app-timestamp (map-get? nft-offer-history {nft-index: nft-index, offer-index: offer-index})) not-allowed)) (sale-cycle (unwrap! (get sale-cycle (map-get? nft-offer-history {nft-index: nft-index, offer-index: offer-index})) not-allowed)) (amount (unwrap! (get amount (map-get? nft-offer-history {nft-index: nft-index, offer-index: offer-index})) not-allowed)) ) (asserts! (is-eq saleType u3) not-allowed) (map-set nft-offer-history {nft-index: nft-index, offer-index: offer-index} {sale-cycle: sale-cycle, offerer: offerer, app-timestamp: app-timestamp, amount: amount, accepted: u1}) (ok (transfer nft-index owner recipient)) ) ) ;; mint a new token ;; asset-hash: sha256 hash of asset file ;; max-editions: maximum number of editions allowed for this asset ;; royalties: a list of priciple/percentages to be be paid from sale price ;; ;; 1. transfer mint price to the administrator ;; 2. mint the token using built in mint function ;; 3. update the two maps - first contains the data indexed by the nft index, second ;; provides a reverse lookup based on the asset hash - this allows tokens to be located ;; from just a knowledge of the original asset. ;; Note series-original in the case of the original in series is just ;; mintCounter - for editions this provides a safety hook back to the original in cases ;; where the asset hash is unknown (ie cant be found from nft-lookup). (define-public (mint-token (asset-hash (buff 32)) (metaDataUrl (buff 200)) (maxEditions uint) (editionCost uint) (addresses (list 10 principal)) (shares (list 10 uint)) (secondaries (list 10 uint))) (let ( (mintCounter (var-get mint-counter)) (ahash (get asset-hash (map-get? nft-data {nft-index: (var-get mint-counter)}))) (block-time (unwrap! (get-block-info? time u0) amount-not-set)) ) (asserts! (> maxEditions u0) editions-error) (asserts! (> (stx-get-balance tx-sender) (var-get mint-price)) cant-pay-mint-price) (asserts! (is-none ahash) asset-not-registered) ;; Note: series original is really for later editions to refer back to this one - this one IS the series original (map-insert nft-data {nft-index: mintCounter} {asset-hash: asset-hash, meta-data-url: metaDataUrl, max-editions: maxEditions, edition: u1, edition-cost: editionCost, mint-block-height: block-height, series-original: mintCounter}) ;; Note editions are 1 based and <= maxEditions - the one minted here is #1 (map-insert nft-edition-counter {nft-index: mintCounter} {edition-counter: u2}) ;; By default we accept offers - sale type can be changed via the UI. (map-insert nft-sale-data { nft-index: mintCounter } { sale-cycle-index: u1, sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: (+ block-time u1814400)}) (map-insert nft-lookup {asset-hash: asset-hash, edition: u1} {nft-index: mintCounter}) ;; The payment is split between the nft-beneficiaries with share > 0 they are set per edition (map-insert nft-beneficiaries {nft-index: mintCounter} {addresses: addresses, shares: shares, secondaries: secondaries}) ;; finally - mint the NFT and step the counter (if (is-eq tx-sender (var-get administrator)) (print "mint-token : tx-sender is contract - skipping mint price") (begin (unwrap! (stx-transfer? (var-get mint-price) tx-sender (var-get administrator)) failed-to-stx-transfer) (print "mint-token : tx-sender paid mint price") ) ) (unwrap! (nft-mint? my-nft mintCounter tx-sender) failed-to-mint-err) (print {evt: "mint-token", nftIndex: mintCounter, owner: tx-sender, amount: (var-get mint-price)}) (var-set mint-counter (+ mintCounter u1)) (ok mintCounter) ) ) (define-public (mint-edition (nftIndex uint)) (let ( ;; before we start... check the hash corresponds to a minted asset (ahash (unwrap! (get asset-hash (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (metaDataUrl (unwrap! (get meta-data-url (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (maxEditions (unwrap! (get max-editions (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (editionCost (unwrap! (get edition-cost (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (editionCounter (unwrap! (get edition-counter (map-get? nft-edition-counter {nft-index: nftIndex})) edition-counter-error)) (thisEdition (default-to u0 (get nft-index (map-get? nft-lookup {asset-hash: ahash, edition: editionCounter})))) (block-time (unwrap! (get-block-info? time u0) amount-not-set)) (mintCounter (var-get mint-counter)) ) ;; can only mint an edition via buy now or bidding - not offers (asserts! (is-eq thisEdition u0) edition-counter-error) ;; Note - the edition index is 1 based and incremented before insertion in this method - therefore the test is '<=' here! (asserts! (<= editionCounter maxEditions) edition-limit-reached) ;; This asserts the first one has been minted already - see mint-token. (asserts! (> editionCounter u1) edition-counter-error) ;; check the buyer has enough funds.. (asserts! (> (stx-get-balance tx-sender) editionCost) cant-pay-mint-price) ;; set max editions so we know where we are in the series (map-insert nft-data {nft-index: mintCounter} {asset-hash: ahash, meta-data-url: metaDataUrl, max-editions: maxEditions, edition: editionCounter, edition-cost: editionCost, mint-block-height: block-height, series-original: nftIndex}) ;; put the nft index into the list of editions in the look up map (map-insert nft-lookup {asset-hash: ahash, edition: editionCounter} {nft-index: mintCounter}) ;; mint the NFT and update the counter for the next.. (unwrap! (nft-mint? my-nft mintCounter tx-sender) failed-to-mint-err) ;; saleType = 1 (buy now) - split out the payments according to royalties - or roll everything back. (if (> editionCost u0) (begin (unwrap! (payment-split nftIndex editionCost tx-sender) failed-to-mint-err) (print "mint-edition : payment split made")) (print "mint-edition : payment not required") ) ;; (print "mint-edition : payment managed") ;; initialise the sale data - not for sale until the owner sets it. (map-insert nft-sale-data { nft-index: mintCounter } { sale-cycle-index: u1, sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: (+ block-time u1814400)}) (print {evt: "mint-edition", nftIndex: nftIndex, owner: tx-sender, edition: editionCounter, amount: editionCost}) ;; inncrement the mint counter and edition counter ready for the next edition (map-set nft-edition-counter {nft-index: nftIndex} {edition-counter: (+ u1 editionCounter)}) (var-set mint-counter (+ mintCounter u1)) (ok mintCounter) ) ) ;; allow the owner of the series original to set the cost of minting editions ;; the cost for each edition is taken from the series original and so we need to ;; operate on the the original here - ie nftIndex is the index of thee original ;; and NOT the edition andd only the creator of the series original can change this. (define-public (set-edition-cost (nftIndex uint) (maxEditions uint) (editionCost uint)) (let ( (ahash (unwrap! (get asset-hash (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (metaDataUrl (unwrap! (get meta-data-url (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (edition (unwrap! (get edition (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (mintBlockHeight (unwrap! (get mint-block-height (map-get? nft-data {nft-index: nftIndex})) not-allowed)) (seriesOriginal (unwrap! (get series-original (map-get? nft-data {nft-index: nftIndex})) not-allowed)) ) (asserts! (is-owner nftIndex tx-sender) nft-not-owned-err) (asserts! (is-eq nftIndex seriesOriginal) not-originale) (ok (map-set nft-data {nft-index: nftIndex} {asset-hash: ahash, meta-data-url: metaDataUrl, max-editions: maxEditions, edition: edition, edition-cost: editionCost, mint-block-height: mintBlockHeight, series-original: seriesOriginal})) ) ) ;; set-sale-data updates the sale type and purchase info for a given NFT. Only the owner can call this method ;; and doing so make the asset transferable by the recipient - on condition of meeting the conditions of sale ;; This is equivalent to the setApprovalForAll method in ERC 721 contracts. ;; Assumption being made here is that all editions have the same sale data associated (define-public (set-sale-data (nftIndex uint) (sale-type uint) (increment-stx uint) (reserve-stx uint) (amount-stx uint) (bidding-end-time uint)) (let ( ;; before we start... check the hash corresponds to a minted asset (saleCycleIndex (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (currentBidIndex (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) (currentAmount (unwrap! (get-current-bid-amount nftIndex currentBidIndex) bidding-error)) ) (asserts! (not (and (> currentAmount u0) (is-eq saleType u2))) bidding-error) (print {evt: "set-sale-data", nftIndex: nftIndex, saleType: sale-type, increment: increment-stx, reserve: reserve-stx, amount: amount-stx, biddingEndTime: bidding-end-time}) (if (is-owner nftIndex tx-sender) ;; Note - don't override the sale cyle index here as this is a public method and can be called ad hoc. Sale cycle is update at end of sale! (if (map-set nft-sale-data {nft-index: nftIndex} {sale-cycle-index: saleCycleIndex, sale-type: sale-type, increment-stx: increment-stx, reserve-stx: reserve-stx, amount-stx: amount-stx, bidding-end-time: bidding-end-time}) (ok nftIndex) not-allowed ) not-allowed ) ) ) ;; buy-now ;; pay royalties and transfer asset ownership to tx-sender. ;; Checks that: ;; a) asset is registered ;; b) on sale via buy now ;; c) amount is set ;; (define-public (buy-now (nftIndex uint) (owner principal) (recipient principal)) (let ( (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleCycleIndex (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (amount (unwrap! (get amount-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (ahash (get asset-hash (map-get? nft-data {nft-index: nftIndex}))) ) (asserts! (is-some ahash) asset-not-registered) (asserts! (is-eq saleType u1) not-approved-to-sell) (asserts! (> amount u0) amount-not-set) ;; Make the royalty payments - then zero out the sale data and register the transfer ;; (print "buy-now : Make the royalty payments") (print (unwrap! (payment-split nftIndex amount tx-sender) payment-error)) (map-set nft-sale-data { nft-index: nftIndex } { sale-cycle-index: (+ saleCycleIndex u1), sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: u0}) ;; (print "buy-now : Added internal transfer - transfering nft...") ;; finally transfer ownership to the buyer (note: via the buyers transaction!) (print {evt: "buy-now", nftIndex: nftIndex, owner: owner, recipient: recipient, amount: amount}) (nft-transfer? my-nft nftIndex owner recipient) ) ) ;; opening-bid ;; nft-index: unique index for NFT ;; The opening bid in the given sale cycle a given item. (define-public (opening-bid (nftIndex uint) (bidAmount uint) (appTimestamp uint)) (let ( (amount (unwrap! (get amount-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleCycle (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (biddingEndTime (unwrap! (get bidding-end-time (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (bidCounter (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) ) ;; Check the user bid amount is the opening price OR the current bid plus increment (asserts! (is-eq bidAmount amount) bidding-amount-error) (asserts! (> biddingEndTime appTimestamp) bidding-endtime-error) (print "place-bid : sending this much to; ") (print bidAmount) (print (as-contract tx-sender)) (print "place-bid : when") (print appTimestamp) (unwrap! (stx-transfer? bidAmount tx-sender (as-contract tx-sender)) failed-to-stx-transfer) (map-insert nft-bid-history {nft-index: nftIndex, bid-index: bidCounter} {bidder: tx-sender, amount: bidAmount, app-timestamp: appTimestamp, sale-cycle: saleCycle}) (map-set nft-high-bid-counter {nft-index: nftIndex} {high-bid-counter: (+ bidCounter u1), sale-cycle: saleCycle}) (print {evt: "opening-bid", nftIndex: nftIndex, txSender: tx-sender, appTimestamp: appTimestamp, amount: bidAmount}) (ok bidAmount) ) ) (define-private (get-current-bidder (nftIndex uint) (currentBidIndex uint)) (let ( (currentBidder (unwrap! (get bidder (map-get? nft-bid-history {nft-index: nftIndex, bid-index: (- currentBidIndex u1)})) bidding-error)) ) (ok currentBidder) ) ) (define-private (get-current-bid-amount (nftIndex uint) (currentBidIndex uint)) (if (is-eq currentBidIndex u0) (ok u0) (let ( (currentAmount (default-to u0 (get amount (map-get? nft-bid-history {nft-index: nftIndex, bid-index: (- currentBidIndex u1)})))) ) (ok currentAmount) ) ) ) ;; place-bid ;; nft-index: unique index for NFT ;; nextBidAmount: amount the user is bidding - i.e the amount display on th place bid button. (define-public (place-bid (nftIndex uint) (nextBidAmount uint) (appTimestamp uint)) (let ( (bidding-end-time (unwrap! (get bidding-end-time (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleCycle (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (amountStart (unwrap! (get amount-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (increment (unwrap! (get increment-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (reserve (unwrap! (get reserve-stx (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (currentBidIndex (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) (currentBidder (unwrap! (get-current-bidder nftIndex currentBidIndex) bidding-error)) (currentAmount (unwrap! (get-current-bid-amount nftIndex currentBidIndex) bidding-error)) (owner (unwrap! (nft-get-owner? my-nft nftIndex) nft-not-owned-err)) ) ;; Check the user bid amount is the opening price OR the current bid plus increment (print "place-bid : assert there is an opening bid - otherwise client calls opening-bid") (print currentAmount) (asserts! (> currentAmount u0) user-amount-different) (asserts! (is-eq nextBidAmount (+ currentAmount increment)) user-amount-different) ;; if (appTimestamp > bidding-end-time) then this is either the winning or a too late bid on the NFT ;; a too late bid will have been rejected as the last bid resets the sale/bidding data on the item. ;; if its the last bid... ;; 1. Refund the currentBid to the bidder ;; 2. move currentBid to bid history ;; 3. Set the bid in nft-high-bid-counter - note 'set' so we overwrite the previous bid ;; (next-bid) we ;; 1. Refund the currentBid to the bidder ;; 2. Insert currentBid to bid history ;; 3. Set the bid in nft-high-bid-counter - note 'set' so we overwrite the previous bid (if (> appTimestamp bidding-end-time) (begin (print {evt: "place-bid-closure", nftIndex: nftIndex, appTimestamp: appTimestamp, biddingEndTime: bidding-end-time, amount: nextBidAmount, reserve: reserve}) (unwrap! (refund-bid nftIndex currentBidder currentAmount) failed-to-stx-transfer) (if (< nextBidAmount reserve) ;; if this bid is less than reserve & its the last bid then just refund previous bid (unwrap! (ok true) failed-to-stx-transfer) (begin ;; WINNING BID - is the FIRST bid after bidding close. (print "place-bid : Make the royalty payments") (unwrap! (payment-split nftIndex nextBidAmount tx-sender) payment-error) (unwrap! (record-bid nftIndex nextBidAmount currentBidIndex appTimestamp saleCycle) failed-to-stx-transfer) (map-set nft-sale-data { nft-index: nftIndex } { sale-cycle-index: (+ saleCycle u1), sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: u0}) (print "place-bid : Added internal transfer - transfering nft...") ;; finally transfer ownership to the buyer (note: via the buyers transaction!) (unwrap! (nft-transfer? my-nft nftIndex owner tx-sender) failed-to-stx-transfer) ) ) ) (begin (print {evt: "place-bid-refund", nftIndex: nftIndex, appTimestamp: appTimestamp, biddingEndTime: bidding-end-time, amount: nextBidAmount}) (unwrap! (refund-bid nftIndex currentBidder currentAmount) failed-refund) (unwrap! (next-bid nftIndex nextBidAmount currentBidIndex appTimestamp saleCycle) failed-to-stx-transfer) ) ) ;; ;; NOTE: Above code will only reconcile IF a bid comes in after 'block-time' ;; We may need a manual trigger to end bidding when this doesn't happen - unless there is a ;; to repond to future events / timeouts that I dont know about. ;; (print {evt: "place-bid", nftIndex: nftIndex, txSender: tx-sender, appTimestamp: appTimestamp, amount: nextBidAmount}) (ok true) ) ) ;; Mint subsequent editions of the NFT ;; nft-index: the index of the original NFT in this series of editions. ;; The sale data must have been set on the asset before calling this. ;; The amount is split according to the royalties. ;; The nextBidAmount is passed to avoid concurrency issues - amount on the buy/bid button must ;; equal the amount expected by the contract. ;; close-bidding ;; nft-index: index of the NFT ;; closeType: type of closure, values are; ;; 1 = buy now closure - uses the last bid (thats held in escrow) to transfer the item to the bidder and to pay royalties ;; 2 = refund closure - the last bid gets refunded and sale is closed. The item ownership does not change. ;; Note bidding can also be closed automatically - if a bid is received after the bidding end time. ;; In the context of a 'live auction' items have no end time and are closed by the 'auctioneer'. (define-public (close-bidding (nftIndex uint) (closeType uint)) (let ( (bidding-end-time (unwrap! (get bidding-end-time (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleType (unwrap! (get sale-type (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (saleCycleIndex (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) failed-to-close-1)) (block-time (unwrap! (get-block-info? time u0) not-allowed)) (currentBidIndex (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) (currentBidder (unwrap! (get-current-bidder nftIndex currentBidIndex) bidding-error)) (currentAmount (unwrap! (get-current-bid-amount nftIndex currentBidIndex) bidding-error)) ) (asserts! (or (is-eq closeType u1) (is-eq closeType u2)) failed-to-close-1) ;; only the owner or administrator can call close (asserts! (or (is-owner nftIndex tx-sender) (unwrap! (is-administrator) not-allowed)) not-allowed) ;; only the administrator can call close BEFORE the end time - note we use the less accurate ;; but fool proof block time here to prevent owner/client code jerry mandering the close function (asserts! (or (> block-time bidding-end-time) (unwrap! (is-administrator) failed-to-close-3)) failed-to-close-3) ;; Check for a current bid - if none then just reset the sale data to not selling (if (is-eq currentAmount u0) (map-set nft-sale-data { nft-index: nftIndex } { sale-cycle-index: (+ saleCycleIndex u1), sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: u0}) (if (is-eq closeType u1) (begin ;; buy now closure - pay and transfer ownership ;; note that the money to pay with is in the contract! (print {evt: "close-bidding", nftIndex: nftIndex, payType: "from-contract", txSender: tx-sender, currentBidder: currentBidder, currentAmount: currentAmount, currentBidIndex: currentBidIndex}) (unwrap! (payment-split nftIndex currentAmount (as-contract tx-sender)) payment-error) (unwrap! (nft-transfer? my-nft nftIndex (unwrap! (nft-get-owner? my-nft nftIndex) nft-not-owned-err) tx-sender) transfer-error) ) (begin ;; refund closure - refund the bid and reset sale data (print {evt: "close-bidding", nftIndex: nftIndex, payType: "refund", txSender: tx-sender, currentBidder: currentBidder, currentAmount: currentAmount}) (unwrap! (refund-bid nftIndex currentBidder currentAmount) failed-refund) (map-set nft-sale-data { nft-index: nftIndex } { sale-cycle-index: (+ saleCycleIndex u1), sale-type: u0, increment-stx: u0, reserve-stx: u0, amount-stx: u0, bidding-end-time: u0}) ) ) ) (print {evt: "close-bidding", nftIndex: nftIndex, closeType: closeType, txSender: tx-sender, currentBidder: currentBidder, currentAmount: currentAmount}) (ok nftIndex) ) ) ;; read only methods ;; --------------- (define-read-only (get-administrator) (var-get administrator)) (define-read-only (is-administrator) (ok (is-eq (var-get administrator) tx-sender))) (define-read-only (get-base-token-uri) (var-get base-token-uri)) (define-read-only (get-mint-counter) (ok (var-get mint-counter)) ) (define-read-only (get-mint-price) (var-get mint-price)) (define-read-only (get-token-by-index (nftIndex uint)) (ok (get-all-data nftIndex)) ) (define-read-only (get-beneficiaries (nftIndex uint)) (let ( (beneficiaries (map-get? nft-beneficiaries {nft-index: nftIndex})) ) (ok beneficiaries) ) ) (define-read-only (get-offer-at-index (nftIndex uint) (offerIndex uint)) (let ( (the-offer (map-get? nft-offer-history {nft-index: nftIndex, offer-index: offerIndex})) ) (ok the-offer) ) ) (define-read-only (get-bid-at-index (nftIndex uint) (bidIndex uint)) (let ( (the-bid (map-get? nft-bid-history {nft-index: nftIndex, bid-index: bidIndex})) ) (ok the-bid) ) ) ;; Get the edition from a knowledge of the #1 edition and the specific edition number (define-read-only (get-edition-by-hash (asset-hash (buff 32)) (edition uint)) (let ( (nftIndex (unwrap! (get nft-index (map-get? nft-lookup {asset-hash: asset-hash, edition: edition})) amount-not-set)) ) (ok (get-all-data nftIndex)) ) ) (define-read-only (get-token-by-hash (asset-hash (buff 32))) (let ( (nftIndex (unwrap! (get nft-index (map-get? nft-lookup {asset-hash: asset-hash, edition: u1})) amount-not-set)) ) (ok (get-all-data nftIndex)) ) ) (define-read-only (get-contract-data) (let ( (the-administrator (var-get administrator)) (the-mint-price (var-get mint-price)) (the-base-token-uri (var-get base-token-uri)) (the-mint-counter (var-get mint-counter)) (the-platform-fee (var-get platform-fee)) (the-token-name token-name) (the-token-symbol token-symbol) ) (ok (tuple (administrator the-administrator) (mintPrice the-mint-price) (baseTokenUri the-base-token-uri) (mintCounter the-mint-counter) (platformFee the-platform-fee) (tokenName the-token-name) (tokenSymbol the-token-symbol))) ) ) (define-private (get-all-data (nftIndex uint)) (let ( (the-owner (unwrap-panic (nft-get-owner? my-nft nftIndex))) (the-token-info (map-get? nft-data {nft-index: nftIndex})) (the-sale-data (map-get? nft-sale-data {nft-index: nftIndex})) (the-beneficiary-data (map-get? nft-beneficiaries {nft-index: nftIndex})) (the-edition-counter (default-to u0 (get edition-counter (map-get? nft-edition-counter {nft-index: nftIndex})))) (the-offer-counter (default-to u0 (get offer-counter (map-get? nft-offer-counter {nft-index: nftIndex})))) (the-high-bid-counter (default-to u0 (get high-bid-counter (map-get? nft-high-bid-counter {nft-index: nftIndex})))) ) (ok (tuple (offerCounter the-offer-counter) (bidCounter the-high-bid-counter) (editionCounter the-edition-counter) (nftIndex nftIndex) (tokenInfo the-token-info) (saleData the-sale-data) (beneficiaryData the-beneficiary-data) (owner the-owner) ) ) ) ) (define-read-only (get-sale-data (nftIndex uint)) (match (map-get? nft-sale-data {nft-index: nftIndex}) mySaleData (ok mySaleData) not-found ) ) (define-read-only (get-token-name) (ok token-name) ) (define-read-only (get-token-symbol) (ok token-symbol) ) (define-read-only (get-balance) (begin (asserts! (is-eq (var-get administrator) tx-sender) not-allowed) (ok (stx-get-balance (as-contract tx-sender))) ) ) ;; private methods ;; --------------- (define-private (refund-bid (nftIndex uint) (currentBidder principal) (currentAmount uint)) (begin (unwrap! (as-contract (stx-transfer? currentAmount tx-sender currentBidder)) failed-to-stx-transfer) (print {evt: "refund-bid", nftIndex: nftIndex, txSender: tx-sender, currentBidder: currentBidder, currentAmount: currentAmount}) (ok true) ) ) ;; need to account for reserve-stx (define-private (record-bid (nftIndex uint) (bidAmount uint) (bidCounter uint) (appTimestamp uint) (saleCycle uint)) (begin ;; see place-bid for the payment - no need for this (unwrap! (stx-transfer? bidAmount tx-sender (as-contract tx-sender)) failed-to-stx-transfer) (map-insert nft-bid-history {nft-index: nftIndex, bid-index: bidCounter} {bidder: tx-sender, amount: bidAmount, app-timestamp: appTimestamp, sale-cycle: saleCycle}) (map-set nft-high-bid-counter {nft-index: nftIndex} {high-bid-counter: (+ bidCounter u1), sale-cycle: saleCycle}) (print {evt: "record-bid", nftIndex: nftIndex, txSender: tx-sender, bidAmount: bidAmount, bidCounter: bidCounter, appTimestamp: appTimestamp, saleCycle: saleCycle}) (ok true) ) ) (define-private (next-bid (nftIndex uint) (bidAmount uint) (bidCounter uint) (appTimestamp uint) (saleCycle uint)) (begin (unwrap! (stx-transfer? bidAmount tx-sender (as-contract tx-sender)) failed-to-stx-transfer) (map-insert nft-bid-history {nft-index: nftIndex, bid-index: bidCounter} {bidder: tx-sender, amount: bidAmount, app-timestamp: appTimestamp, sale-cycle: saleCycle}) (map-set nft-high-bid-counter {nft-index: nftIndex} {high-bid-counter: (+ bidCounter u1), sale-cycle: saleCycle}) (print {appTimestamp: appTimestamp, bidAmount: bidAmount, bidCounter: bidCounter, evt: "next-bid", nftIndex: nftIndex, saleCycle: saleCycle, txSender: tx-sender}) (ok true) ) ) ;; sends payments to each recipient listed in the royalties ;; Note this is called by mint-edition where thee nftIndex actuallt referes to the series orginal and is where the royalties are stored. (define-private (payment-split (nftIndex uint) (saleAmount uint) (payer principal)) (let ( (addresses (unwrap! (get addresses (map-get? nft-beneficiaries {nft-index: nftIndex})) failed-to-mint-err)) (shares (unwrap! (get shares (map-get? nft-beneficiaries {nft-index: nftIndex})) failed-to-mint-err)) (secondaries (unwrap! (get secondaries (map-get? nft-beneficiaries {nft-index: nftIndex})) failed-to-mint-err)) (saleCycle (unwrap! (get sale-cycle-index (map-get? nft-sale-data {nft-index: nftIndex})) amount-not-set)) (split u0) ) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (nft-get-owner? my-nft nftIndex) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u0) (element-at secondaries u0)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u1) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u1) (element-at secondaries u1)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u2) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u2) (element-at secondaries u2)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u3) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u3) (element-at secondaries u3)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u4) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u4) (element-at secondaries u4)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u5) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u5) (element-at secondaries u5)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u6) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u6) (element-at secondaries u6)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u7) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u7) (element-at secondaries u7)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u8) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u8) (element-at secondaries u8)) payment-share-error)) payment-share-error)) (+ split (unwrap! (pay-royalty payer saleAmount (unwrap! (element-at addresses u9) payment-address-error) (unwrap! (if (is-eq saleCycle u1) (element-at shares u9) (element-at secondaries u9)) payment-share-error)) payment-share-error)) (print {evt: "payment-split", nftIndex: nftIndex, saleCycle: saleCycle, payer: payer, saleAmount: saleAmount, txSender: tx-sender}) (ok split) ) ) ;; unit of saleAmount is in Satoshi and the share variable is a percentage (ex for 5% it will be equal to 5) ;; also the scalor is 1 on first purchase - direct from artist and 2 for secondary sales - so the seller gets half the ;; sale value and each royalty address gets half their original amount. (define-private (pay-royalty (payer principal) (saleAmount uint) (payee principal) (share uint)) (begin (if (> share u0) (let ( (split (/ (* saleAmount share) percentage-with-twodp)) ) ;; ignore royalty payment if its to the buyer / tx-sender. (if (not (is-eq tx-sender payee)) (unwrap! (stx-transfer? split payer payee) transfer-error) (unwrap! (ok true) transfer-error) ) (print {evt: "pay-royalty-primary", payee: payee, payer: payer, saleAmount: saleAmount, share: share, split: split, txSender: tx-sender}) (ok split) ) (ok u0) ) ) )
0xe91e260ba2e9e4c0d22777f60a4d8bfd94cb2684dd609d838cff5089b072c140
29,574
success
test-stackswap-tt-5
;; ;; 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 u50000) ) (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) (ok true) ) ) (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) ) )
0x409a77f567ca69b971e52a2569fee719d4beb474fc348696e71d14b2aacf7a7c
29,583
success
test-stackswap-tt-6
;; ;; 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 u50000) ) (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) (ok true) ) ) (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) ) )
0xdb94e217f7aeed3c4c100ee276cf1555dacf736239f54370d000c5fd688cc6d3
29,592
success
t1
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xb4d86f87ca1142fa9fdef6c997f7a4f9b256190699224aae4b2f66b883f3247d
29,703
success
t2
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xf1c5e92773b3e04542d2551d1ab3cb065460cfa5c56b4481ffe79d9552d059b9
29,704
success
a1
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x9036bc6990145807acec2a4f2a168f094988f2ffa53da8a5f3be3e0ff2aa98c0
29,706
success
a2
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xc0af5379655d3ecd3f85e660c2341b9fa92441a590bcd497322722a52c39ebc8
29,706
success
a3
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x35208e181e0dfb5ac9c69671c42d0862b194ffc9e8f63404c0fa7599a0f803b1
29,706
success
a4
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xc9d64d8da9916cf800e6706a59bf458f98038cbc759bf86d52e1b40091568b20
29,706
success
a5
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x36fdadf559e141e4648f9b0c84bb1c2ce26874a2c4feaf5f63dba13b436268f5
29,706
success
a6
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x121b1b0e02c940dc3d63b0761de61a56a03b04f3391a298908ea5b0e82d7acf4
29,706
success
a7
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x2e36001bce40dd6f729076ccd518030e43c6e0f838b6a8b1ec422b5354a82955
29,706
success
a8
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xf075ba9e59c8ed663081b2bdbdf70f22b6faf9083be133adc011a5138556030a
29,706
success
a9
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x7fe3d348d492d200bd3fdf16947038481fa6450bee28953ab6b44007151f668d
29,706
success
a10
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x83b5bbd037764cbecb29d1add88841f0e896540a1fdba632380f6d826bb9b71d
29,706
success
a11
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xb1c13d50936575ec9894dea915f92852d1e8768a59af362f943a3b44e2b8b97a
29,706
success
a12
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x09cf111056dfca064403de032207c0ff9d6f8dd103bb6a7d70932ae49788cb76
29,706
success
a13
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x4cf674d8bb2455c59605eb20893f07e33839fe3ead233903025556f7f07578fa
29,706
success
a14
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x8c08fc2d3e5aac233c5291c339073333c86fa4087aec17985905c9cff4ae0d8a
29,706
success
a15
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x4656709496e95692c387a8c9e65159d3c63f8781d94569cfcc3b8a36e5b345f8
29,708
success
a16
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x348404f3bfbee3341d18a819d4491081d5b021e87869406bbb4c70a4d3ec36b7
29,708
success
a17
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xd40d3980f2e8c024740b242c325694cf4dbb3330abec9ec8573be590c3e3ad36
29,708
success
a18
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xeb468533e17eb68dace2efac5442d6c45eb3f6631957ac38f00365bb0c9678b0
29,708
success
a19
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x3155a959187d3cb270d3dbed005bff6a85167dee819c065936b5e611af4c4d21
29,708
success
a20
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x87391aecc438d45e8a6eac7d7406013456367c328ef3c3c3ac009e4dde19fa86
29,708
success
a21
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x8b585e31802250ca69a7d3a7df40298bc1f98285dbae31f901fd3322945a8eaf
29,708
success
a22
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x5953b8de6df399fefab9e406a198d4f9778c434bb363704cdcb203062bfc91ae
29,708
success
a23
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xbf17693dc6fae1a96c62148e2f8cb3ce9948bf8630fcc6906811708daf3faace
29,708
success
a24
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x8b10b6ab6a3ba0f8834f67e9d847ccd0014cd99ef65e34990f3fa789153b201e
29,708
success
a25
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x04f40f90ebe1a05516989532de941e02d291799116878fd16cf9a77cdf19bc3e
29,708
success
a26
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xf3e42c23164cb7a7e826a2d571e7f9061604ef64367ab2d89155559d99ac86e5
29,708
success
a27
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x34aaab485779761b77664afa20f87903302164020bc52e1c454ff49370c4ce34
29,708
success
a28
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x072a4a4544daa9e501895cec802dfcae632faa939072b87936ea2c5a0186d2ad
29,710
success
a29
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x2270cd5d744d915568f0f09af639666b7bfea13db4be5d027d7b2d3a38891aec
29,710
success
a30
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xe479557aac81a47985f821127cd580fecd5c697f8d0a38d5172ddbd5577bf689
29,710
success
a31
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x14ae1260673af2de3c5e98de41f5cf547a5e759c4788e8f63be4a2f26ad81bb1
29,710
success
a32
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x7b5b3d1c12beb24fa5b62d0f09d7b7a6d6ab74807134f09d7661a23d39a12d82
29,710
success
a33
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x98893149f34a2e39ae686bf48ac95ab7b5ceffee358da2b3763b137ad3b9cfa8
29,710
success
a34
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x3a51c7915ab880b000591c35473a1a2a19d5d1a693a37e3550f0917292e753ed
29,710
success
a35
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xb28d5ce936ddea07a770755c006a53486632781c906a042d6d667bb009178943
29,710
success
a36
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x84746ff3930f57b655ce263b6f69c2ace0a2a197ee958c0e1a22026e2b617403
29,710
success
a37
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x50249030be70dcc3dfd5705e9b448018d83fa4d71f5b54d23890d0ccb194723d
29,710
success
a38
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xbe8d5e0ee9f4d3cbe5fe7b09b6604b559c03a265da3a3547a6a74c1eb285807f
29,710
success
a39
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x291648627570f6a97e2f618951f6b3a67da6d1dbbaa3e87cc87478e6b69cd870
29,710
success
a40
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x05cb68b09b9a60c85335dd431020638ca7660f70f240be0080fb159e9b490ab6
29,710
success
a41
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x7a55d2b1e007226eded3704e055d2ba587752b30d5f6eeebb4ce95b5a3fa4091
29,714
success
a42
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x494a594b4ee881b94b4f833b67ffcb85a7c7dbe20fcf61bc3811b88ac57e82b1
29,714
success
a43
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x8bd28bc863215f4beba530ce87b81abdb4188fd92daf1846032e8772d692d5c3
29,714
success
a44
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0x20405a001e2c1b859a38a5dc79a55aeb57d5b8f62727d536b56f13ad751b9fce
29,715
success
a45
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xeba1c4ba7498d0b9e17315ebc0a624f7af6e1b2c7e0c3e2ae84c131ff9368d9f
29,716
success
a46
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xd0e0e82bf2b8eef2693ad5b499e9d51745ceedb4990cb057fc67ae1a7abd6280
29,716
success
a47
(define-constant OWNER tx-sender) (define-data-var m_master_contract (optional principal) none) (define-public (transfer (namespace (buff 20)) (name (buff 48)) (new_owner principal) (zonefile-hash (optional (buff 20)))) (let ((resolve_rsp (contract-call? 'SP000000000000000000002Q6VF78.bns name-resolve namespace name))) (asserts! (is-eq contract-caller (unwrap-panic (var-get m_master_contract))) (err 9001)) (asserts! (is-ok resolve_rsp) (err 9002)) (asserts! (is-eq (get owner (unwrap-panic resolve_rsp)) (as-contract tx-sender)) (err 9003)) (as-contract (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace name new_owner zonefile-hash))) (ok true) ) ) (define-public (set_master_contract (contract_owner principal)) (begin (asserts! (is-eq tx-sender OWNER) (err 9001)) (ok (and (is-none (var-get m_master_contract)) (var-set m_master_contract (some contract_owner)))) ) )
0xb2d3bfdb2cd7c042226b6ab51ffb5527e58cd85c819bfc62ff0bba9431155315
29,716
success