contract_name
stringlengths
1
40
code_body
stringlengths
0
100k
txid
stringlengths
66
66
block_height
int32
0
165k
status
stringclasses
3 values
green1
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token GREEN) (define-data-var supply int 100000000) ;; error codes (define-constant sender-is-recipient u120) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (begin (ok (ft-get-balance GREEN owner)))) ;; returns the total number of tokens (define-read-only (total-supply) (ok (to-uint (var-get supply))) ) ;; the token name (define-read-only (get-name) (ok "GREEN")) ;; the symbol (define-read-only (get-symbol) (ok "GRE")) ;; the number of decimals used (define-read-only (get-decimals) (ok u6)) (define-public (get-token-uri) (ok (some u"https://www.green.org/token/"))) ;; 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? GREEN amount sender recipient)) (print memo) (ok true) ) (err sender-is-recipient))) ;; fund the treasury (ft-mint? GREEN u100000000000000 'SP32XCD69XPS3GKDEXAQ29PJRDSD5AR643GNEEBXZ)
0x37635c6258dbe347697b2f2c8a5b75ceb4072e3cb047c692b46d293bf1d09ddf
21,712
abort_by_response
token
(define-constant ERR-UNAUTHORIZED u1) (define-constant ERR-ZERO u2) (define-fungible-token token) (define-data-var token-uri (optional(string-utf8 256)) none) (define-constant contract-creator tx-sender) (impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) ;; SIP-010 ;; (define-public (transfer (amount uint) (from principal) (to principal) (memo (optional (buff 34)))) (begin (asserts! (is-eq from tx-sender) (err ERR-UNAUTHORIZED)) (ft-transfer? token amount from to) ) ) (define-read-only (get-name) (ok "Token Test")) (define-read-only (get-symbol) (ok "TOKE")) (define-read-only (get-decimals) (ok u6)) (define-read-only (get-balance (user principal)) (ok (ft-get-balance token user))) (define-read-only (get-total-supply) (ok (ft-get-supply token))) (define-public (set-token-uri (value (string-utf8 256))) (if (is-eq tx-sender contract-creator) (ok (var-set token-uri (some value))) (err ERR-UNAUTHORIZED))) (define-read-only (get-token-uri) (ok (var-get token-uri))) ;; fund the treasury (ft-mint? TOKE u100000000000000 'SP32XCD69XPS3GKDEXAQ29PJRDSD5AR643GNEEBXZ)
0x58331d0b67d154c7ac211eb6acb0618160998c6919bd84da22ddf7d90dd8bc93
21,713
abort_by_response
token1
(define-constant ERR-UNAUTHORIZED u1) (define-constant ERR-ZERO u2) (define-fungible-token token) (define-data-var token-uri (optional(string-utf8 256)) none) (define-constant contract-creator tx-sender) (impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) ;; SIP-010 ;; (define-public (transfer (amount uint) (from principal) (to principal) (memo (optional (buff 34)))) (begin (asserts! (is-eq from tx-sender) (err ERR-UNAUTHORIZED)) (ft-transfer? token amount from to) ) ) (define-read-only (get-name) (ok "Token Test")) (define-read-only (get-symbol) (ok "TOKE")) (define-read-only (get-decimals) (ok u6)) (define-read-only (get-balance (user principal)) (ok (ft-get-balance token user))) (define-read-only (get-total-supply) (ok (ft-get-supply token))) (define-public (set-token-uri (value (string-utf8 256))) (if (is-eq tx-sender contract-creator) (ok (var-set token-uri (some value))) (err ERR-UNAUTHORIZED))) (define-read-only (get-token-uri) (ok (var-get token-uri))) ;; fund the treasury (ft-mint? TOKE 100000000000000 'SP32XCD69XPS3GKDEXAQ29PJRDSD5AR643GNEEBXZ)
0x7e4cf602683ff8b709c42fd5ff88514a664e1571ddbce6fc89d646397fb23b37
21,715
abort_by_response
token2
(define-constant ERR-UNAUTHORIZED u1) (define-constant ERR-ZERO u2) (define-fungible-token token) (define-data-var token-uri (optional(string-utf8 256)) none) (define-constant contract-creator tx-sender) (impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) ;; SIP-010 ;; (define-public (transfer (amount uint) (from principal) (to principal) (memo (optional (buff 34)))) (begin (asserts! (is-eq from tx-sender) (err ERR-UNAUTHORIZED)) (ft-transfer? token amount from to) ) ) (define-read-only (get-name) (ok "Token Test")) (define-read-only (get-symbol) (ok "TOKE")) (define-read-only (get-decimals) (ok u6)) (define-read-only (get-balance (user principal)) (ok (ft-get-balance token user))) (define-read-only (get-total-supply) (ok (ft-get-supply token))) (define-public (set-token-uri (value (string-utf8 256))) (if (is-eq tx-sender contract-creator) (ok (var-set token-uri (some value))) (err ERR-UNAUTHORIZED))) (define-read-only (get-token-uri) (ok (var-get token-uri))) ;; fund the treasury (ft-mint? TOKE 100000000 'SP32XCD69XPS3GKDEXAQ29PJRDSD5AR643GNEEBXZ)
0xd25d5a33791b3552988a012e705d1ddd633618a206ae0ab470c981667205710f
21,717
abort_by_response
swapr-v2a
(use-trait sip-010-token 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-010-v0a.ft-trait) (use-trait swapr-token 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-trait-v0a.swapr-trait) (define-constant contract-owner 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF) (define-constant no-liquidity-err (err u61)) ;; (define-constant transfer-failed-err (err u62)) (define-constant not-owner-err (err u63)) (define-constant no-fee-to-address-err (err u64)) (define-constant invalid-pair-err (err u65)) (define-constant no-such-position-err (err u66)) (define-constant balance-too-low-err (err u67)) (define-constant too-many-pairs-err (err u68)) (define-constant pair-already-exists-err (err u69)) (define-constant wrong-token-err (err u70)) (define-constant too-much-slippage-err (err u71)) (define-constant transfer-x-failed-err (err u72)) (define-constant transfer-y-failed-err (err u73)) (define-constant value-out-of-range-err (err u74)) (define-constant no-fee-x-err (err u75)) (define-constant no-fee-y-err (err u76)) ;; for future use, or debug (define-constant e10-err (err u20)) (define-constant e11-err (err u21)) (define-constant e12-err (err u22)) (define-map pairs-map { pair-id: uint } { token-x: principal, token-y: principal, } ) (define-map pairs-data-map { token-x: principal, token-y: principal, } { shares-total: uint, balance-x: uint, balance-y: uint, fee-balance-x: uint, fee-balance-y: uint, fee-to-address: (optional principal), swapr-token: principal, name: (string-ascii 32), } ) ;; TODO(psq): replace use of balance-x/balance-y with a call to balance-of(swapr) on the token itself, no write to do actually!!! The transfer is the write, that's cool :) ;; I don't think so anymore? ;; (define-data-var pairs-list (list 2000 uint) (list)) (define-data-var pair-count uint u0) (define-read-only (get-name (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>)) (let ( (token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)) ) (ok (get name pair)) ) ) (define-public (get-symbol (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>)) (ok (concat (unwrap-panic (as-max-len? (unwrap-panic (contract-call? token-x-trait get-symbol)) u15)) (concat "-" (unwrap-panic (as-max-len? (unwrap-panic (contract-call? token-y-trait get-symbol)) u15)) ) ) ) ) (define-read-only (get-total-supply (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>)) (let ( (token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)) ) (ok (get shares-total pair)) ) ) ;; get the total number of shares in the pool (define-read-only (get-shares (token-x principal) (token-y principal)) (ok (get shares-total (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))) ) ;; get overall balances for the pair (define-public (get-balances (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>)) (let ( (token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)) ) (ok (list (get balance-x pair) (get balance-y pair))) ) ) (define-public (get-data (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-swapr-trait <swapr-token>) (owner principal)) (let ( (token-data (unwrap-panic (contract-call? token-swapr-trait get-data owner))) (balances (unwrap-panic (get-balances token-x-trait token-y-trait))) ) (ok (merge token-data { balances: balances })) ) ) ;; since we can't use a constant to refer to contract address, here what x and y are ;; (define-constant x-token 'SP2NC4YKZWM2YMCJV851VF278H9J50ZSNM33P3JM1.my-token) ;; (define-constant y-token 'SP1QR3RAGH3GEME9WV7XB0TZCX6D5MNDQP97D35EH.my-token) (define-public (add-to-position (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-swapr-trait <swapr-token>) (x uint) (y uint)) (let ( (token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (pair (unwrap-panic (map-get? pairs-data-map { token-x: token-x, token-y: token-y }))) (contract-address (as-contract tx-sender)) (recipient-address tx-sender) (balance-x (get balance-x pair)) (balance-y (get balance-y pair)) (new-shares (if (is-eq (get shares-total pair) u0) (sqrti (* x y)) ;; burn a fraction of initial lp token to avoid attack as described in WP https://uniswap.org/whitepaper.pdf (/ (* x (get shares-total pair)) balance-x) ) ) ;; TODO(psq): need to calculate y based on x, and only transfer the correct amount ;; without this, people could game the pool by only providing x!!! not nice... (new-y (if (is-eq (get shares-total pair) u0) y (/ (* x balance-y) balance-x) ) ) (pair-updated (merge pair { shares-total: (+ new-shares (get shares-total pair)), balance-x: (+ balance-x x), balance-y: (+ balance-y new-y) })) ) ;; TODO(psq) check if x or y is 0, to calculate proper exchange rate unless shares-total is 0, which would be an error (asserts! (is-ok (contract-call? token-x-trait transfer x tx-sender contract-address)) transfer-x-failed-err) (asserts! (is-ok (contract-call? token-y-trait transfer new-y tx-sender contract-address)) transfer-y-failed-err) (map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-updated) (try! (contract-call? token-swapr-trait mint recipient-address new-shares)) (print { object: "pair", action: "liquidity-added", data: pair-updated }) (ok true) ) ) (define-read-only (get-pair-details (token-x principal) (token-y principal)) (unwrap-panic (map-get? pairs-data-map { token-x: token-x, token-y: token-y })) ) (define-read-only (get-pair-contracts (pair-id uint)) (unwrap-panic (map-get? pairs-map { pair-id: pair-id })) ) (define-read-only (get-pair-count) (ok (var-get pair-count)) ) ;; (define-read-only (get-pairs) ;; (ok (map get-pair-contracts (var-get pairs-list))) ;; ) (define-public (create-pair (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-swapr-trait <swapr-token>) (pair-name (string-ascii 32)) (x uint) (y uint)) ;; TOOD(psq): add creation checks, then create map before proceeding to add-to-position ;; check neither x,y or y,x exists` (let ( (name-x (unwrap-panic (contract-call? token-x-trait get-name))) (name-y (unwrap-panic (contract-call? token-y-trait get-name))) (token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (pair-id (+ (var-get pair-count) u1)) (pair-data { shares-total: u0, balance-x: u0, balance-y: u0, fee-balance-x: u0, fee-balance-y: u0, fee-to-address: none, swapr-token: (contract-of token-swapr-trait), name: pair-name, }) ) (asserts! (and (is-none (map-get? pairs-data-map { token-x: token-x, token-y: token-y })) (is-none (map-get? pairs-data-map { token-x: token-y, token-y: token-x })) ) pair-already-exists-err ) (map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-data) (map-set pairs-map { pair-id: pair-id } { token-x: token-x, token-y: token-y }) ;; (var-set pairs-list (unwrap! (as-max-len? (append (var-get pairs-list) pair-id) u2000) too-many-pairs-err)) (var-set pair-count pair-id) (try! (add-to-position token-x-trait token-y-trait token-swapr-trait x y)) (print { object: "pair", action: "created", data: pair-data }) (ok true) ) ) ;; ;; reduce the amount of liquidity the sender provides to the pool ;; ;; to close, use u100 (define-public (reduce-position (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (token-swapr-trait <swapr-token>) (percent uint)) (let ( (token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)) (balance-x (get balance-x pair)) (balance-y (get balance-y pair)) (shares (unwrap-panic (contract-call? token-swapr-trait get-balance-of tx-sender))) (shares-total (get shares-total pair)) (contract-address (as-contract tx-sender)) (sender tx-sender) (withdrawal (/ (* shares percent) u100)) (withdrawal-x (/ (* withdrawal balance-x) shares-total)) (withdrawal-y (/ (* withdrawal balance-y) shares-total)) (pair-updated (merge pair { shares-total: (- shares-total withdrawal), balance-x: (- (get balance-x pair) withdrawal-x), balance-y: (- (get balance-y pair) withdrawal-y) } ) ) ) (asserts! (<= percent u100) value-out-of-range-err) (asserts! (is-ok (as-contract (contract-call? token-x-trait transfer withdrawal-x contract-address sender))) transfer-x-failed-err) (asserts! (is-ok (as-contract (contract-call? token-y-trait transfer withdrawal-y contract-address sender))) transfer-y-failed-err) ;; (unwrap-panic (decrease-shares token-x token-y tx-sender withdrawal)) ;; should never fail, you know... (map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-updated) ;; TODO(psq): use burn ;; (unwrap-panic (contract-call? token-swapr-trait transfer withdrawal tx-sender contract-address)) ;; transfer back to swapr, wish there was a burn instead... (try! (contract-call? token-swapr-trait burn tx-sender withdrawal)) (print { object: "pair", action: "liquidity-removed", data: pair-updated }) (ok (list withdrawal-x withdrawal-y)) ) ) ;; exchange known dx of x-token for whatever dy of y-token based on current liquidity, returns (dx dy) ;; the swap will not happen if can't get at least min-dy back (define-public (swap-x-for-y (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (dx uint) (min-dy uint)) ;; calculate dy ;; calculate fee on dx ;; transfer ;; update balances (let ( (token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)) (balance-x (get balance-x pair)) (balance-y (get balance-y pair)) (contract-address (as-contract tx-sender)) (sender tx-sender) (dy (/ (* u997 balance-y dx) (+ (* u1000 balance-x) (* u997 dx)))) ;; overall fee is 30 bp, either all for the pool, or 25 bp for pool and 5 bp for operator (fee (/ (* u5 dx) u10000)) ;; 5 bp (pair-updated (merge pair { balance-x: (+ (get balance-x pair) dx), balance-y: (- (get balance-y pair) dy), fee-balance-x: (if (is-some (get fee-to-address pair)) ;; only collect fee when fee-to-address is set (+ fee (get fee-balance-x pair)) (get fee-balance-x pair)) } ) ) ) (asserts! (< min-dy dy) too-much-slippage-err) ;; TODO(psq): check that the amount transfered in matches the amount requested (asserts! (is-ok (contract-call? token-x-trait transfer dx sender contract-address)) transfer-x-failed-err) (asserts! (is-ok (as-contract (contract-call? token-y-trait transfer dy contract-address sender))) transfer-y-failed-err) (map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-updated) (print { object: "pair", action: "swap-x-for-y", data: pair-updated }) (ok (list dx dy)) ) ) ;; exchange known dy for whatever dx based on liquidity, returns (dx dy) ;; the swap will not happen if can't get at least min-dx back (define-public (swap-y-for-x (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>) (dy uint) (min-dx uint)) ;; calculate dx ;; calculate fee on dy ;; transfer ;; update balances (let ((token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)) (balance-x (get balance-x pair)) (balance-y (get balance-y pair)) (contract-address (as-contract tx-sender)) (sender tx-sender) ;; check formula, vs x-for-y??? (dx (/ (* u997 balance-x dy) (+ (* u1000 balance-y) (* u997 dy)))) ;; overall fee is 30 bp, either all for the pool, or 25 bp for pool and 5 bp for operator (fee (/ (* u5 dy) u10000)) ;; 5 bp (pair-updated (merge pair { balance-x: (- (get balance-x pair) dx), balance-y: (+ (get balance-y pair) dy), fee-balance-y: (if (is-some (get fee-to-address pair)) ;; only collect fee when fee-to-address is set (+ fee (get fee-balance-y pair)) (get fee-balance-y pair)) }))) (asserts! (< min-dx dx) too-much-slippage-err) ;; TODO(psq): check that the amount transfered in matches the amount requested (asserts! (is-ok (as-contract (contract-call? token-x-trait transfer dx contract-address sender))) transfer-x-failed-err) (asserts! (is-ok (contract-call? token-y-trait transfer dy sender contract-address)) transfer-y-failed-err) (map-set pairs-data-map { token-x: token-x, token-y: token-y } pair-updated) (print { object: "pair", action: "swap-y-for-x", data: pair-updated }) (ok (list dx dy)) ) ) ;; ;; activate the contract fee for swaps by setting the collection address, restricted to contract owner (define-public (set-fee-to-address (token-x principal) (token-y principal) (address principal)) (let ((pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))) (asserts! (is-eq tx-sender contract-owner) not-owner-err) (map-set pairs-data-map { token-x: token-x, token-y: token-y } { shares-total: (get shares-total pair), balance-x: (get balance-y pair), balance-y: (get balance-y pair), fee-balance-x: (get fee-balance-y pair), fee-balance-y: (get fee-balance-y pair), fee-to-address: (some address), name: (get name pair), swapr-token: (get swapr-token pair), } ) (ok true) ) ) ;; ;; clear the contract fee addres ;; ;; TODO(psq): if there are any collected fees, prevent this from happening, as the fees can no longer be collect with `collect-fees` (define-public (reset-fee-to-address (token-x principal) (token-y principal)) (let ((pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))) (asserts! (is-eq tx-sender contract-owner) not-owner-err) (map-set pairs-data-map { token-x: token-x, token-y: token-y } { shares-total: (get shares-total pair), balance-x: (get balance-x pair), balance-y: (get balance-y pair), fee-balance-x: (get fee-balance-y pair), fee-balance-y: (get fee-balance-y pair), fee-to-address: none, name: (get name pair), swapr-token: (get swapr-token pair), } ) (ok true) ) ) ;; ;; get the current address used to collect a fee (define-read-only (get-fee-to-address (token-x principal) (token-y principal)) (let ((pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))) (ok (get fee-to-address pair)) ) ) ;; ;; get the amount of fees charged on x-token and y-token exchanges that have not been collected yet (define-read-only (get-fees (token-x principal) (token-y principal)) (let ((pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err))) (ok (list (get fee-balance-x pair) (get fee-balance-y pair))) ) ) ;; ;; send the collected fees the fee-to-address (define-public (collect-fees (token-x-trait <sip-010-token>) (token-y-trait <sip-010-token>)) (let ( (token-x (contract-of token-x-trait)) (token-y (contract-of token-y-trait)) (contract-address (as-contract tx-sender)) (pair (unwrap! (map-get? pairs-data-map { token-x: token-x, token-y: token-y }) invalid-pair-err)) (address (unwrap! (get fee-to-address pair) no-fee-to-address-err)) (fee-x (get fee-balance-x pair)) (fee-y (get fee-balance-y pair)) ) (asserts! (is-eq fee-x u0) no-fee-x-err) (asserts! (is-ok (as-contract (contract-call? token-x-trait transfer fee-x contract-address address))) transfer-x-failed-err) (asserts! (is-eq fee-y u0) no-fee-y-err) (asserts! (is-ok (as-contract (contract-call? token-y-trait transfer fee-y contract-address address))) transfer-y-failed-err) (map-set pairs-data-map { token-x: token-x, token-y: token-y } { shares-total: (get shares-total pair), balance-x: (get balance-x pair), balance-y: (get balance-y pair), fee-balance-x: u0, fee-balance-y: u0, fee-to-address: (get fee-to-address pair), name: (get name pair), swapr-token: (get swapr-token pair), } ) (ok (list fee-x fee-y)) ) )
0x10acb1ee9d17ffbfb39a087708133d21d38f0344564123c7319fef624ef1b241
22,286
success
stx-token-v3a
;; wrap the native STX token into an SRC20 compatible token to be usable along other tokens (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-010-v0a.ft-trait) ;; get the token balance of owner (define-read-only (get-balance-of (owner principal)) (begin (ok (print (stx-get-balance owner))) ) ) (define-read-only (get-total-supply) (ok stx-liquid-supply) ) ;; returns the token name (define-read-only (get-name) (ok "STX") ) (define-read-only (get-symbol) (ok "STX") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u6) ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/stx.json")) ) ;; { ;; "name":"STX", ;; "description":"STX token, as a SIP-010 compatible token", ;; "image":"https://swapr.finance/tokens/stx.png", ;; "vector":"https://swapr.finance/tokens/stx.svg" ;; } ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (asserts! (is-eq tx-sender sender) (err u255)) ;; too strict? (stx-transfer? amount tx-sender recipient) ) )
0xa5a7b94044e43ec0c4d8c5155e87c7da99f033d669bc5b66ebc3d32dd72610c8
22,289
success
plaid-token-v3a
;; wrap the native STX token into an SRC20 compatible token to be usable along other tokens (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-010-v0a.ft-trait) (define-fungible-token plaid) ;; get the token balance of owner (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance plaid owner)) ) ;; returns the total number of tokens ;; TODO(psq): we don't have access yet, but once POX is available, this should be a value that ;; is available from Clarity (define-read-only (get-total-supply) (ok (ft-get-supply plaid)) ) ;; returns the token name (define-read-only (get-name) (ok "Plaid") ) (define-read-only (get-symbol) (ok "PLD") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u8) ;; because we can, and interesting for testing wallets and other clients ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/plaid.json")) ) ;; { ;; "name":"Plaid", ;; "description":"Plaid token, used as a test token", ;; "image":"https://swapr.finance/tokens/plaid.png", ;; "vector":"https://swapr.finance/tokens/plaid.svg" ;; } ;; (transfer (uint principal principal) (response bool uint)) ;; amount sender recipient ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (print "plaid.transfer") (print amount) (print tx-sender) (print recipient) (asserts! (is-eq tx-sender sender) (err u255)) ;; too strict? (print (ft-transfer? plaid amount tx-sender recipient)) ) ) ;; TODO(psq): remove for mainnet, how??? (ft-mint? plaid u100000000000000 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP) (ft-mint? plaid u100000000 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF)
0x4ba677246b81231e228eaa9f4c4ca8ad2c3b4b8b10b3c408d19ecf44faef13ac
22,291
success
plaid-stx-token-v3a
;; ;; we implement the sip-010 + a mint function (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-trait-v0a.swapr-trait) ;; ;; we can use an ft-token here, so use it! (define-fungible-token plaid-stx-token) (define-constant no-acccess-err u40) ;; implement all functions required by sip-010 (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (ft-transfer? plaid-stx-token amount tx-sender recipient) ) ) (define-read-only (get-name) (ok "Plaid STX swapr") ) (define-read-only (get-symbol) (ok "plaid-stx-swapr") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u6) ;; arbitrary, or ok? ) (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance plaid-stx-token owner)) ) (define-read-only (get-total-supply) (ok (ft-get-supply plaid-stx-token)) ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/plaid-stx-token.json")) ) ;; { ;; "name":"Plaid-STX", ;; "description":"Plaid-STX swapr token", ;; "image":"https://swapr.finance/tokens/plaid-stx.png", ;; "vector":"https://swapr.finance/tokens/plaid-stx.svg" ;; } ;; one stop function to gather all the data relevant to the swapr 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 swapr when adding liquidity ;; can only be used by swapr main contract (define-public (mint (recipient principal) (amount uint)) (begin (print "token-swapr.mint") (print contract-caller) (print amount) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-mint? plaid-stx-token amount recipient) ) ) ;; the extra burn method used by swapr when removing liquidity ;; can only be used by swapr main contract (define-public (burn (recipient principal) (amount uint)) (begin (print "token-swapr.burn") (print contract-caller) (print amount) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-burn? plaid-stx-token amount recipient) ) )
0xbd9dabcf9d88a6b2b1d469b5e813069f684b9d62ae4d97f85cb4bf77906e232f
22,293
success
sws-token-v6a
;; sws-token-01 ;; we implement the sip-010 ;; In mainnet use https://explorer.stacks.co/txid/SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-10-ft-standard ;; for testnet (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-010-v0a.ft-trait) ;;(impl-trait .sip-010.ft-trait) ;; constants ;; (define-fungible-token sangwon-token) ;; (define-constant contract-owner 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF) (define-constant contract-owner 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP) (define-constant not-owner-err (err u63)) (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance sangwon-token owner)) ) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply sangwon-token)) ) ;; returns the token name (define-read-only (get-name) (ok "Sangwon Token") ) (define-read-only (get-symbol) (ok "SWS") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u3) ;; because we can, and interesting for testing wallets and other clients ) (define-read-only (get-token-uri) (ok (some u"empty")) ) ;; data maps and vars ;; ;; private functions ;; (define-public (owner-mint (account principal) (amount uint)) (if (<= amount u0) (err u0) (begin (asserts! (is-eq tx-sender contract-owner) not-owner-err) (ft-mint? sangwon-token amount account) ) ) ) ;; public functions ;; (define-public (transfer (amount uint) (sender principal) (recipient principal)) (if (>= (ft-get-balance sangwon-token sender) amount) (begin (print "sangwon.transfer") (print amount) (print tx-sender) (print recipient) (asserts! (is-eq tx-sender sender) (err u255)) ;; too strict? (print (ft-transfer? sangwon-token amount sender recipient)) ) (err u0) ) ) (ft-mint? sangwon-token u100000000000000 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP) (ft-mint? sangwon-token u100000000000000 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE) (ft-mint? sangwon-token u100000000000000 'SP3C9VW89DFE6HY6054Q5BN0XZWDFTWV8FPTGT8JK) ;; (contract-call? .sws-token-01 get-name) ;; (contract-call? .sangwon-token owner-mint 'ST23DAB333A5CPFXNK13E5YMX1DZJ07112QA7HWHA u10) ;; (contract-call? .sangwon-token get-balance-of 'ST23DAB333A5CPFXNK13E5YMX1DZJ07112QA7HWHA)
0x6e8e7670bdb738b6a50ac8126a9c67323ba7d0b2422f032f1bfd66bec4e752e7
22,294
success
nar-token-v5a
;; sws-token-01 ;; we implement the sip-010 ;; In mainnet use https://explorer.stacks.co/txid/SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-10-ft-standard ;; for testnet (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-010-v0a.ft-trait) ;;(impl-trait .sip-010.ft-trait) ;; constants ;; (define-fungible-token narangd-token) ;; (define-constant contract-owner 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF) (define-constant contract-owner 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP) (define-constant not-owner-err (err u63)) (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance narangd-token owner)) ) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply narangd-token)) ) ;; returns the token name (define-read-only (get-name) (ok "Narangd Token") ) (define-read-only (get-symbol) (ok "NAR") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u3) ;; because we can, and interesting for testing wallets and other clients ) (define-read-only (get-token-uri) (ok (some u"empty")) ) ;; data maps and vars ;; ;; private functions ;; (define-public (owner-mint (account principal) (amount uint)) (if (<= amount u0) (err u0) (begin (asserts! (is-eq tx-sender contract-owner) not-owner-err) (ft-mint? narangd-token amount account) ) ) ) ;; public functions ;; (define-public (transfer (amount uint) (sender principal) (recipient principal)) (if (>= (ft-get-balance narangd-token sender) amount) (begin (print "narangd.transfer") (print amount) (print tx-sender) (print recipient) (asserts! (is-eq tx-sender sender) (err u255)) ;; too strict? (print (ft-transfer? narangd-token amount sender recipient)) ) (err u0) ) ) (ft-mint? narangd-token u100000000000000 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP) (ft-mint? narangd-token u100000000000000 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE) (ft-mint? narangd-token u100000000000000 'SP3C9VW89DFE6HY6054Q5BN0XZWDFTWV8FPTGT8JK)
0x9df1f84178849be0ffdcccae6814f4e2a2fcd4e2e8a96ff344433d13fdc44584
22,297
success
plaid-nar-token-v3a
;; ;; we implement the sip-010 + a mint function (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-trait-v0a.swapr-trait) ;;(impl-trait .trait-swapr.swapr-trait) ;; ;; we can use an ft-token here, so use it! (define-fungible-token sws-nar-token) (define-constant no-acccess-err u40) ;; implement all functions required by sip-010 (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (ft-transfer? sws-nar-token amount tx-sender recipient) ) ) (define-read-only (get-name) (ok "PLAID NAR swapr") ) (define-read-only (get-symbol) (ok "PLD-NAR-swapr") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u6) ;; arbitrary, or ok? ) (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance sws-nar-token owner)) ) (define-read-only (get-total-supply) (ok (ft-get-supply sws-nar-token)) ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/plaid-stx-token.json")) ) ;; { ;; "name":"Plaid-STX", ;; "description":"Plaid-STX swapr token", ;; "image":"https://swapr.finance/tokens/plaid-stx.png", ;; "vector":"https://swapr.finance/tokens/plaid-stx.svg" ;; } ;; one stop function to gather all the data relevant to the swapr 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 swapr when adding liquidity ;; can only be used by swapr main contract (define-public (mint (recipient principal) (amount uint)) (begin (print "token-swapr.mint") (print contract-caller) (print amount) ;; (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr) (err no-acccess-err)) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-mint? sws-nar-token amount recipient) ) ) ;; the extra burn method used by swapr when removing liquidity ;; can only be used by swapr main contract (define-public (burn (recipient principal) (amount uint)) (begin (print "token-swapr.burn") (print contract-caller) (print amount) ;; (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr) (err no-acccess-err)) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-burn? sws-nar-token amount recipient) ) )
0x2da43fc23d4013c941379bfcbd5c7c1541ac07640ee5563c1353f3cc969ea840
22,299
success
sws-nar-token-v3a
;; ;; we implement the sip-010 + a mint function (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-trait-v0a.swapr-trait) ;;(impl-trait .trait-swapr.swapr-trait) ;; ;; we can use an ft-token here, so use it! (define-fungible-token sws-nar-token) (define-constant no-acccess-err u40) ;; implement all functions required by sip-010 (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (ft-transfer? sws-nar-token amount tx-sender recipient) ) ) (define-read-only (get-name) (ok "SWS NAR swapr") ) (define-read-only (get-symbol) (ok "SWS-NAR-swapr") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u6) ;; arbitrary, or ok? ) (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance sws-nar-token owner)) ) (define-read-only (get-total-supply) (ok (ft-get-supply sws-nar-token)) ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/plaid-stx-token.json")) ) ;; { ;; "name":"Plaid-STX", ;; "description":"Plaid-STX swapr token", ;; "image":"https://swapr.finance/tokens/plaid-stx.png", ;; "vector":"https://swapr.finance/tokens/plaid-stx.svg" ;; } ;; one stop function to gather all the data relevant to the swapr 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 swapr when adding liquidity ;; can only be used by swapr main contract (define-public (mint (recipient principal) (amount uint)) (begin (print "token-swapr.mint") (print contract-caller) (print amount) ;; (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr) (err no-acccess-err)) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-mint? sws-nar-token amount recipient) ) ) ;; the extra burn method used by swapr when removing liquidity ;; can only be used by swapr main contract (define-public (burn (recipient principal) (amount uint)) (begin (print "token-swapr.burn") (print contract-caller) (print amount) ;; (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr) (err no-acccess-err)) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-burn? sws-nar-token amount recipient) ) )
0x268e102cdb4ec1e8b881a6546ba65591e2ab5351e46c7e33427adbbd0ea62c7a
22,412
success
thing-token-v4a
;; wrap the native STX token into an SRC20 compatible token to be usable along other tokens (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-010-v0a.ft-trait) (define-fungible-token thing) ;; get the token balance of owner (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance thing owner)) ) ;; returns the total number of tokens ;; TODO(psq): we don't have access yet, but once POX is available, this should be a value that ;; is available from Clarity (define-read-only (get-total-supply) (ok (ft-get-supply thing)) ) ;; returns the token name (define-read-only (get-name) (ok "Thing") ) (define-read-only (get-symbol) (ok "THG") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u8) ;; because we can, and interesting for testing wallets and other clients ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/thing.json")) ) ;; { ;; "name":"Thing", ;; "description":"Thing token, used as a test token", ;; "image":"https://swapr.finance/tokens/thing.png", ;; "vector":"https://swapr.finance/tokens/thing.svg" ;; } ;; (transfer (uint principal principal) (response bool uint)) ;; amount sender recipient ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (print "thing.transfer") (print amount) (print tx-sender) (print recipient) (asserts! (is-eq tx-sender sender) (err u255)) ;; too strict? (print (ft-transfer? thing amount tx-sender recipient)) ) ) ;; TODO(psq): remove for mainnet, how??? (ft-mint? thing u1000000000000 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP)
0x8f740d77a43235007f4a3dfbe69871a5f1ad26e6627d96aa7adaf15994484ac9
22,413
success
plaid-thing-token-v3a
;; ;; we implement the sip-010 + a mint function (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-trait-v0a.swapr-trait) ;; ;; we can use an ft-token here, so use it! (define-fungible-token plaid-thing-token) (define-constant no-acccess-err u40) ;; implement all functions required by sip-010 (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (ft-transfer? plaid-thing-token amount tx-sender recipient) ) ) (define-read-only (get-name) (ok "Plaid Thing swapr") ) (define-read-only (get-symbol) (ok "plaid-thing-swapr") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u6) ;; arbitrary, or ok? ) (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance plaid-thing-token owner)) ) (define-read-only (get-total-supply) (ok (ft-get-supply plaid-thing-token)) ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/plaid-thing-token.json")) ) ;; { ;; "name":"Plaid-Thing", ;; "description":"Plaid-Thing swapr token", ;; "image":"https://swapr.finance/tokens/plaid-thing.png", ;; "vector":"https://swapr.finance/tokens/plaid-thing.svg" ;; } ;; one stop function to gather all the data relevant to the swapr 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 swapr when adding liquidity ;; can only be used by swapr main contract (define-public (mint (recipient principal) (amount uint)) (begin (print "token-swapr.mint") (print contract-caller) (print amount) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-mint? plaid-thing-token amount recipient) ) ) ;; the extra burn method used by swapr when removing liquidity ;; can only be used by swapr main contract (define-public (burn (recipient principal) (amount uint)) (begin (print "token-swapr.burn") (print contract-caller) (print amount) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-burn? plaid-thing-token amount recipient) ) )
0x6ee803c882247d713107036a5e174bbc78dc3be9239b8614d5b0a30ab61c965e
22,414
success
tokensoft-token-v3a
;; Implement the `ft-trait` trait defined in the `ft-trait` contract - SIP 10 ;; This can use sugared syntax in real deployment (unit tests do not allow) (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-010-v0a.ft-trait) ;; ;; Implement the token restriction trait (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.restricted-token-trait-v0a.restricted-token-trait) ;; Error returned for permission denied - stolen from http 403 (define-constant PERMISSION_DENIED_ERROR u403) ;; Data variables specific to the deployed token contract (define-data-var token-name (string-ascii 32) "") (define-data-var token-symbol (string-ascii 32) "") (define-data-var token-decimals uint u0) ;; Track who deployed the token and whether it has been initialized (define-data-var deployer-principal principal tx-sender) (define-data-var is-initialized bool false) ;; Meta Read Only Functions for reading details about the contract - conforms to SIP 10 ;; -------------------------------------------------------------------------- ;; Defines built in support functions for tokens used in this contract ;; A second optional parameter can be added here to set an upper limit on max total-supply (define-fungible-token tokensoft-token) ;; Get the token balance of the specified owner in base units (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance tokensoft-token owner))) ;; Returns the token name (define-read-only (get-name) (ok (var-get token-name))) ;; Returns the symbol or "ticker" for this token (define-read-only (get-symbol) (ok (var-get token-symbol))) ;; Returns the number of decimals used (define-read-only (get-decimals) (ok (var-get token-decimals))) ;; Returns the total number of tokens that currently exist (define-read-only (get-total-supply) (ok (ft-get-supply tokensoft-token))) ;; Write function to transfer tokens between accounts - conforms to SIP 10 ;; -------------------------------------------------------------------------- ;; Transfers tokens to a recipient ;; The originator of the transaction (tx-sender) must be the 'sender' principal ;; Smart contracts can move tokens from their own address by calling transfer with the 'as-contract' modifier to override the tx-sender. (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (try! (detect-transfer-restriction amount sender recipient)) ;; Ensure there is no restriction (asserts! (is-eq tx-sender sender) (err u4)) ;; Ensure the originator is the sender principal (ft-transfer? tokensoft-token amount sender recipient))) ;; Transfer ;; Role Based Access Control ;; -------------------------------------------------------------------------- (define-constant OWNER_ROLE u0) ;; Can manage RBAC (define-constant MINTER_ROLE u1) ;; Can mint new tokens to any account (define-constant BURNER_ROLE u2) ;; Can burn tokens from any account (define-constant REVOKER_ROLE u3) ;; Can revoke tokens and move them to any account (define-constant BLACKLISTER_ROLE u4) ;; Can add principals to a blacklist that can prevent transfers ;; Each role will have a mapping of principal to boolean. A true "allowed" in the mapping indicates that the principal has the role. ;; Each role will have special permissions to modify or manage specific capabilities in the contract. ;; Note that adding/removing roles could be optimized by having just 1 function, but since this is sensitive functionality, it was split ;; into 2 separate functions to make it explicit. ;; See the Readme about more details on the RBAC setup. (define-map roles { role: uint, account: principal } { allowed: bool }) ;; Checks if an account has the specified role (define-read-only (has-role (role-to-check uint) (principal-to-check principal)) (default-to false (get allowed (map-get? roles {role: role-to-check, account: principal-to-check})))) ;; Add a principal to the specified role ;; Only existing principals with the OWNER_ROLE can modify roles (define-public (add-principal-to-role (role-to-add uint) (principal-to-add principal)) (begin ;; Check the contract-caller to verify they have the owner role (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR)) ;; Print the action for any off chain watchers (print { action: "add-principal-to-role", role-to-add: role-to-add, principal-to-add: principal-to-add }) (ok (map-set roles { role: role-to-add, account: principal-to-add } { allowed: true })))) ;; Remove a principal from the specified role ;; Only existing principals with the OWNER_ROLE can modify roles ;; WARN: Removing all owners will irrevocably lose all ownership permissions (define-public (remove-principal-from-role (role-to-remove uint) (principal-to-remove principal)) (begin ;; Check the contract-caller to verify they have the owner role (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR)) ;; Print the action for any off chain watchers (print { action: "remove-principal-from-role", role-to-remove: role-to-remove, principal-to-remove: principal-to-remove }) (ok (map-set roles { role: role-to-remove, account: principal-to-remove } { allowed: false })))) ;; Token URI ;; -------------------------------------------------------------------------- ;; Variable for URI storage (define-data-var uri (string-utf8 256) u"https://swapr.finance/tokens/token-soft-xbtc.json") ;; Public getter for the URI (define-read-only (get-token-uri) (ok (some (var-get uri)))) ;; Setter for the URI - only the owner can set it (define-public (set-token-uri (updated-uri (string-utf8 256))) (begin (asserts! (has-role OWNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR)) ;; Print the action for any off chain watchers (print { action: "set-token-uri", updated-uri: updated-uri }) (ok (var-set uri updated-uri)))) ;; Minting and Burning ;; -------------------------------------------------------------------------- ;; Mint tokens to the target address ;; Only existing principals with the MINTER_ROLE can mint tokens (define-public (mint-tokens (mint-amount uint) (mint-to principal) ) (begin (asserts! (has-role MINTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR)) ;; Print the action for any off chain watchers (print { action: "mint-tokens", mint-amount: mint-amount, mint-to: mint-to }) (ft-mint? tokensoft-token mint-amount mint-to))) ;; Burn tokens from the target address ;; Only existing principals with the BURNER_ROLE can mint tokens (define-public (burn-tokens (burn-amount uint) (burn-from principal) ) (begin (asserts! (has-role BURNER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR)) ;; Print the action for any off chain watchers (print { action: "burn-tokens", burn-amount: burn-amount, burn-from : burn-from }) (ft-burn? tokensoft-token burn-amount burn-from))) ;; Revoking Tokens ;; -------------------------------------------------------------------------- ;; Moves tokens from one account to another ;; Only existing principals with the REVOKER_ROLE can revoke tokens (define-public (revoke-tokens (revoke-amount uint) (revoke-from principal) (revoke-to principal) ) (begin (asserts! (has-role REVOKER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR)) ;; Print the action for any off chain watchers (print { action: "revoke-tokens", revoke-amount: revoke-amount, revoke-from: revoke-from, revoke-to: revoke-to }) (ft-transfer? tokensoft-token revoke-amount revoke-from revoke-to))) ;; Blacklisting Principals ;; -------------------------------------------------------------------------- ;; Blacklist mapping. If an account has blacklisted = true then no transfers in or out are allowed (define-map blacklist { account: principal } { blacklisted: bool }) ;; Checks if an account is blacklisted (define-read-only (is-blacklisted (principal-to-check principal)) (default-to false (get blacklisted (map-get? blacklist { account: principal-to-check })))) ;; Updates an account's blacklist status ;; Only existing principals with the BLACKLISTER_ROLE can update blacklist status (define-public (update-blacklisted (principal-to-update principal) (set-blacklisted bool)) (begin (asserts! (has-role BLACKLISTER_ROLE contract-caller) (err PERMISSION_DENIED_ERROR)) ;; Print the action for any off chain watchers (print { action: "update-blacklisted", principal-to-update: principal-to-update, set-blacklisted: set-blacklisted }) (ok (map-set blacklist { account: principal-to-update } { blacklisted: set-blacklisted })))) ;; Transfer Restrictions ;; -------------------------------------------------------------------------- (define-constant RESTRICTION_NONE u0) ;; No restriction detected (define-constant RESTRICTION_BLACKLIST u1) ;; Sender or receiver is on the blacklist ;; Checks to see if a transfer should be restricted. If so returns an error code that specifies restriction type. (define-read-only (detect-transfer-restriction (amount uint) (sender principal) (recipient principal)) (if (or (is-blacklisted sender) (is-blacklisted recipient)) (err RESTRICTION_BLACKLIST) (ok RESTRICTION_NONE))) ;; Returns the user viewable string for a specific transfer restriction (define-read-only (message-for-restriction (restriction-code uint)) (if (is-eq restriction-code RESTRICTION_NONE) (ok "No Restriction Detected") (if (is-eq restriction-code RESTRICTION_BLACKLIST) (ok "Sender or recipient is on the blacklist and prevented from transacting") (ok "Unknown Error Code")))) ;; Initialization ;; -------------------------------------------------------------------------- ;; Check to ensure that the same account that deployed the contract is initializing it ;; Only allow this funtion to be called once by checking "is-initialized" (define-public (initialize (name-to-set (string-ascii 32)) (symbol-to-set (string-ascii 32) ) (decimals-to-set uint) (initial-owner principal)) (begin (asserts! (is-eq tx-sender (var-get deployer-principal)) (err PERMISSION_DENIED_ERROR)) (asserts! (not (var-get is-initialized)) (err PERMISSION_DENIED_ERROR)) (var-set is-initialized true) ;; Set to true so that this can't be called again (var-set token-name name-to-set) (var-set token-symbol symbol-to-set) (var-set token-decimals decimals-to-set) (map-set roles { role: OWNER_ROLE, account: initial-owner } { allowed: true }) (ok true))) ;; TODO(psq): remove for mainnet, how??? (initialize "xBTC" "xBTC" u8 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE) (ft-mint? tokensoft-token u2500000000 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP) (ft-mint? tokensoft-token u1500000000 'SP3C9VW89DFE6HY6054Q5BN0XZWDFTWV8FPTGT8JK)
0x1d70a4cafe95056416f7edc56a0c1547afd9b7b8c1b198fa3e80c6800f521891
22,416
success
tokensoft-stx-token-v3a
;; ;; we implement the sip-010 + a mint function (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-trait-v0a.swapr-trait) ;; ;; we can use an ft-token here, so use it! (define-fungible-token tokensoft-stx-token) (define-constant no-acccess-err u40) ;; implement all functions required by sip-010 (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (ft-transfer? tokensoft-stx-token amount tx-sender recipient) ) ) (define-read-only (get-name) (ok "xBTC STX swapr") ) (define-read-only (get-symbol) (ok "xbtc-stx-swapr") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u6) ;; arbitrary, or ok? ) (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance tokensoft-stx-token owner)) ) (define-read-only (get-total-supply) (ok (ft-get-supply tokensoft-stx-token)) ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/tokensoft-stx-token.json")) ) ;; { ;; "name":"Plaid-Thing", ;; "description":"Plaid-Thing swapr token", ;; "image":"https://swapr.finance/tokens/plaid-thing.png", ;; "vector":"https://swapr.finance/tokens/plaid-thing.svg" ;; } ;; one stop function to gather all the data relevant to the swapr 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 swapr when adding liquidity ;; can only be used by swapr main contract (define-public (mint (recipient principal) (amount uint)) (begin (print "token-swapr.mint") (print contract-caller) (print amount) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-mint? tokensoft-stx-token amount recipient) ) ) ;; the extra burn method used by swapr when removing liquidity ;; can only be used by swapr main contract (define-public (burn (recipient principal) (amount uint)) (begin (print "token-swapr.burn") (print contract-caller) (print amount) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-burn? tokensoft-stx-token amount recipient) ) )
0x29c35e22c78d1d4a04177ec50fa31b17f118f08141bd6503f1f4f9826f31b94a
22,418
success
micro-nthng-v3a
(define-fungible-token micro-nothing) (define-data-var total-supply uint u0) (define-read-only (get-total-supply) (var-get total-supply)) (define-private (mint! (account principal) (amount uint)) (if (<= amount u0) (err u0) (begin (var-set total-supply (+ (var-get total-supply) amount)) (ft-mint? micro-nothing amount account)))) (define-public (transfer (to principal) (amount uint)) (if (> (ft-get-balance micro-nothing tx-sender) u0) (ft-transfer? micro-nothing amount tx-sender to) (err u0))) (mint! 'SP1AWFMSB3AGMFZY9JBWR9GRWR6EHBTMVA9JW4M20 u20000000000000) (mint! 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60 u20000000000000) (mint! 'SP2F2NYNDDJTAXFB62PJX351DCM4ZNEVRYJSC92CT u20000000000000) (mint! 'SP32AEEF6WW5Y0NMJ1S8SBSZDAY8R5J32NBZFPKKZ u20000000000000) (mint! 'SPT9JHCME25ZBZM9WCGP7ZN38YA82F77YM5HM08B u20000000000000) (mint! 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP u100000000000000) (mint! 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE u100000000000000)
0x5116f13b58d5f82d7f42fa0299c8633c05be5b70656a569cd8515e88f059cf2e
22,424
success
wrapped-nothing-v3a
(define-constant ERR-UNAUTHORIZED u1) (define-constant ERR-YOU-POOR u2) (define-fungible-token wrapped-nthng) (define-data-var token-uri (optional (string-utf8 256)) none) (define-constant contract-creator tx-sender) (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.sip-010-v0a.ft-trait) (define-public (donate (amount uint)) (stx-transfer? amount tx-sender contract-creator)) (define-public (wrap-nthng (amount uint)) (if (is-ok (contract-call? .micro-nthng-v3a transfer (as-contract tx-sender) amount)) (begin (ft-mint? wrapped-nthng amount tx-sender) ) (err ERR-YOU-POOR))) (define-public (unwrap (amount uint)) (if (is-ok (ft-burn? wrapped-nthng amount tx-sender)) (let ((unwrapper tx-sender)) (as-contract (contract-call? .micro-nthng-v3a transfer unwrapper amount))) (err ERR-YOU-POOR) )) ;;;;;;;;;;;;;;;;;;;;; SIP 010 ;;;;;;;;;;;;;;;;;;;;;; (define-public (transfer (amount uint) (from principal) (to principal)) (begin (asserts! (is-eq from tx-sender) (err ERR-UNAUTHORIZED)) (ft-transfer? wrapped-nthng amount from to) ) ) (define-read-only (get-name) (ok "Wrapped Nothing")) (define-read-only (get-symbol) (ok "WMNO")) (define-read-only (get-decimals) (ok u0)) (define-read-only (get-balance-of (user principal)) (ok (ft-get-balance wrapped-nthng user))) (define-read-only (get-total-supply) (ok (ft-get-supply wrapped-nthng))) (define-public (set-token-uri (value (string-utf8 256))) (if (is-eq tx-sender contract-creator) (ok (var-set token-uri (some value))) (err ERR-UNAUTHORIZED))) (define-read-only (get-token-uri) (ok (var-get token-uri))) ;; send-many\ (define-public (send-nothing (amount uint) (to principal)) (let ((transfer-ok (try! (transfer amount tx-sender to)))) (ok transfer-ok))) (define-private (send-nothing-unwrap (recipient { to: principal, amount: uint })) (send-nothing (get amount recipient) (get to recipient))) (define-private (check-err (result (response bool uint)) (prior (response bool uint))) (match prior ok-value result err-value (err err-value))) (define-public (send-many (recipients (list 200 { to: principal, amount: uint }))) (fold check-err (map send-nothing-unwrap recipients) (ok true))) (define-constant wrap-amount u50000000000000) ;; TODO(psq): doesn't work, probably because not funding the right thing, but ok because just minting will provide balance ;; (if ;; (is-ok (contract-call? .micro-nthng-v3a transfer 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP wrap-amount)) ;; (ft-mint? wrapped-nthng wrap-amount 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP) ;; (err ERR-YOU-POOR) ;; ) (ft-mint? wrapped-nthng wrap-amount 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP) (set-token-uri u"https://swapr.finance/tokens/wrapped-nothing.json")
0x6a98a2c03c1615d6e4a9fc90817bfea224464dcde0e87d7c072bcd6a78481016
22,426
success
micro-nthng-v3a
(define-fungible-token micro-nothing) (define-data-var total-supply uint u0) (define-read-only (get-total-supply) (var-get total-supply)) (define-private (mint! (account principal) (amount uint)) (if (<= amount u0) (err u0) (begin (var-set total-supply (+ (var-get total-supply) amount)) (ft-mint? micro-nothing amount account)))) (define-public (transfer (to principal) (amount uint)) (if (> (ft-get-balance micro-nothing tx-sender) u0) (ft-transfer? micro-nothing amount tx-sender to) (err u0))) (mint! 'SP1AWFMSB3AGMFZY9JBWR9GRWR6EHBTMVA9JW4M20 u20000000000000) (mint! 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60 u20000000000000) (mint! 'SP2F2NYNDDJTAXFB62PJX351DCM4ZNEVRYJSC92CT u20000000000000) (mint! 'SP32AEEF6WW5Y0NMJ1S8SBSZDAY8R5J32NBZFPKKZ u20000000000000) (mint! 'SPT9JHCME25ZBZM9WCGP7ZN38YA82F77YM5HM08B u20000000000000) (mint! 'SP3WZJAY2A398KKBT73M92PAGP5ZD2GE3JKC6KSSP u100000000000000) (mint! 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE u100000000000000)
0x5116f13b58d5f82d7f42fa0299c8633c05be5b70656a569cd8515e88f059cf2e
22,423
success
plaid-wmno-token-v4a
;; ;; we implement the sip-010 + a mint function (impl-trait 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-trait-v0a.swapr-trait) ;; ;; we can use an ft-token here, so use it! (define-fungible-token plaid-wrapped-nothing-token) (define-constant no-acccess-err u40) ;; implement all functions required by sip-010 (define-public (transfer (amount uint) (sender principal) (recipient principal)) (begin (ft-transfer? plaid-wrapped-nothing-token amount tx-sender recipient) ) ) (define-read-only (get-name) (ok "Plaid Nothing swapr") ) (define-read-only (get-symbol) (ok "plaid-wmno-swapr") ) ;; the number of decimals used (define-read-only (get-decimals) (ok u6) ;; arbitrary, or ok? ) (define-read-only (get-balance-of (owner principal)) (ok (ft-get-balance plaid-wrapped-nothing-token owner)) ) (define-read-only (get-total-supply) (ok (ft-get-supply plaid-wrapped-nothing-token)) ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/plaid-wrapped-nothing-token.json")) ) ;; { ;; "name":"Plaid-STX", ;; "description":"Plaid-STX swapr token", ;; "image":"https://swapr.finance/tokens/plaid-stx.png", ;; "vector":"https://swapr.finance/tokens/plaid-stx.svg" ;; } ;; one stop function to gather all the data relevant to the swapr 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 swapr when adding liquidity ;; can only be used by swapr main contract (define-public (mint (recipient principal) (amount uint)) (begin (print "token-swapr.mint") (print contract-caller) (print amount) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-mint? plaid-wrapped-nothing-token amount recipient) ) ) ;; the extra burn method used by swapr when removing liquidity ;; can only be used by swapr main contract (define-public (burn (recipient principal) (amount uint)) (begin (print "token-swapr.burn") (print contract-caller) (print amount) (asserts! (is-eq contract-caller 'SP23DAB333A5CPFXNK13E5YMX1DZJ07112QNZEBCF.swapr-v2a) (err no-acccess-err)) (ft-burn? plaid-wrapped-nothing-token amount recipient) ) )
0x7f19c5d053d48e9f6e3f66d0fda32df97f89fde1812576730bea37cb437ce640
22,428
success
clarity-bitcoin-lib-v1
;; Error codes (define-constant ERR-OUT-OF-BOUNDS u1) (define-constant ERR-TOO-MANY-TXINS u2) (define-constant ERR-TOO-MANY-TXOUTS u3) (define-constant ERR-VARSLICE-TOO-LONG u4) (define-constant ERR-BAD-HEADER u5) (define-constant ERR-PROOF-TOO-SHORT u6) ;; lookup table for converting 1-byte buffers to uints via index-of (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) ;; List with 512 items, used for folding something 512 times (define-constant LIST_512 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 256 items, used for folding something 256 times (define-constant LIST_256 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 128 items, used for folding something 128 times (define-constant LIST_128 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 64 items, used for folding something 64 times (define-constant LIST_64 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 32 items, used for folding something 32 times (define-constant LIST_32 (list true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true )) ;; List with 16 items, used for folding something 16 times (define-constant LIST_16 (list true true true true true true true true true true true true true true true true )) ;; Convert a 1-byte buff into a uint. (define-read-only (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte))) ;; Append a byte at the given index in the given data to acc. (define-read-only (inner-read-slice-1024 (ignored bool) (input { acc: (buff 1024), data: (buff 1024), index: uint })) (let ( (acc (get acc input)) (data (get data input)) (ctr (get index input)) (byte (unwrap-panic (element-at data ctr))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u1024)), data: data, index: (+ u1 ctr) }) ) ;; Read 512 bytes from data, starting at index. Return the 512-byte slice. (define-read-only (read-slice-512 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_512 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 256 bytes from data, starting at index. Return the 256-byte slice. (define-read-only (read-slice-256 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_256 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 128 bytes from data, starting at index. Return the 128-byte slice. (define-read-only (read-slice-128 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_128 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 64 bytes from data, starting at index. Return the 64-byte slice. (define-read-only (read-slice-64 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_64 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 32 bytes from data, starting at index. Return the 32-byte slice. (define-read-only (read-slice-32 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_32 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 16 bytes from data, starting at index. Return the 16-byte slice. (define-read-only (read-slice-16 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 LIST_16 { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 8 bytes from data, starting at index. Return the 8-byte slice. (define-read-only (read-slice-8 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 (list true true true true true true true true) { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 4 bytes from data, starting at index. Return the 4-byte slice. (define-read-only (read-slice-4 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 (list true true true true) { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 2 bytes from data, starting at index. Return the 2-byte slice. (define-read-only (read-slice-2 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 (list true true) { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read 1 byte from data, starting at index. Return the 1-byte slice. (define-read-only (read-slice-1 (input { data: (buff 1024), index: uint })) (get acc (fold inner-read-slice-1024 (list true) { acc: 0x, data: (get data input), index: (get index input) }))) ;; Read a fixed-sized chunk of data from a given buffer (up to remaining bytes), starting at index, and append it to acc. ;; chunk_size must be a power of 2, up to 1024 (define-read-only (inner-read-slice (chunk_size uint) (input { acc: (buff 1024), buffer: (buff 1024), index: uint, remaining: uint })) (let ( (ctr (get index input)) (remaining (get remaining input)) ) (if (is-eq u0 remaining) ;; done reading input (let ( (acc (get acc input)) (databuff (get buffer input)) ) (if (> chunk_size remaining) ;; chunk size too big for remainder, so just skip it. input ;; we have at least chunk_size bytes to read! ;; dispatch to the right fixed-size slice reader. (if (is-eq chunk_size u512) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-512 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u256) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-256 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u128) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-128 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u64) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-64 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u32) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-32 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u16) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-16 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u8) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-8 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u4) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-4 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u2) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-2 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } (if (is-eq chunk_size u1) { acc: (unwrap-panic (as-max-len? (concat acc (read-slice-1 { data: databuff, index: ctr })) u1024)), buffer: databuff, index: (+ chunk_size ctr), remaining: (- remaining chunk_size) } { acc: acc, buffer: databuff, index: ctr, remaining: remaining } )))))))))) )) )) ) ;; Top-level function to read a slice of a given size from a given (buff 1024), starting at a given offset. ;; Returns (ok (buff 1024)) on success, and it contains "buff[offset..(offset+size)]" ;; Returns (err ERR-OUT-OF-BOUNDS) if the slice offset and/or size would copy a range of bytes outside the given buffer. (define-read-only (read-slice (data (buff 1024)) (offset uint) (size uint)) (if (or (>= offset (len data)) (> (+ offset size) (len data))) (err ERR-OUT-OF-BOUNDS) (begin (print "read slice") (print size) (ok (get acc (fold inner-read-slice (list u512 u256 u128 u64 u32 u16 u8 u4 u2 u1) { acc: 0x, buffer: data, index: offset, remaining: size })) ) ) ) ) ;; Reads the next two bytes from txbuff as a big-endian 16-bit integer, and updates the index. ;; Returns (ok { uint16: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff (define-read-only (read-uint16 (ctx { txbuff: (buff 1024), index: uint })) (let ( (data (get txbuff ctx)) (base (get index ctx)) (byte-1 (buff-to-u8 (unwrap! (element-at data base) (err ERR-OUT-OF-BOUNDS)))) (byte-2 (buff-to-u8 (unwrap! (element-at data (+ u1 base)) (err ERR-OUT-OF-BOUNDS)))) (ret (+ (* byte-2 u256) byte-1)) ) (begin (print "read uint16") (print ret) (ok { uint16: ret, ctx: { txbuff: data, index: (+ u2 base) } }) )) ) ;; Reads the next four bytes from txbuff as a big-endian 32-bit integer, and updates the index. ;; Returns (ok { uint32: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff (define-read-only (read-uint32 (ctx { txbuff: (buff 1024), index: uint })) (let ( (data (get txbuff ctx)) (base (get index ctx)) (byte-1 (buff-to-u8 (unwrap! (element-at data base) (err ERR-OUT-OF-BOUNDS)))) (byte-2 (buff-to-u8 (unwrap! (element-at data (+ u1 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-3 (buff-to-u8 (unwrap! (element-at data (+ u2 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-4 (buff-to-u8 (unwrap! (element-at data (+ u3 base)) (err ERR-OUT-OF-BOUNDS)))) (ret (+ (* byte-4 u16777216) (* byte-3 u65536) (* byte-2 u256) byte-1)) ) (begin (print "read uint32") (print ret) (ok { uint32: ret, ctx: { txbuff: data, index: (+ u4 base) } }) )) ) ;; Reads the next eight bytes from txbuff as a big-endian 64-bit integer, and updates the index. ;; Returns (ok { uint64: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff (define-read-only (read-uint64 (ctx { txbuff: (buff 1024), index: uint })) (let ( (data (get txbuff ctx)) (base (get index ctx)) (byte-1 (buff-to-u8 (unwrap! (element-at data base) (err ERR-OUT-OF-BOUNDS)))) (byte-2 (buff-to-u8 (unwrap! (element-at data (+ u1 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-3 (buff-to-u8 (unwrap! (element-at data (+ u2 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-4 (buff-to-u8 (unwrap! (element-at data (+ u3 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-5 (buff-to-u8 (unwrap! (element-at data (+ u4 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-6 (buff-to-u8 (unwrap! (element-at data (+ u5 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-7 (buff-to-u8 (unwrap! (element-at data (+ u6 base)) (err ERR-OUT-OF-BOUNDS)))) (byte-8 (buff-to-u8 (unwrap! (element-at data (+ u7 base)) (err ERR-OUT-OF-BOUNDS)))) (ret (+ (* byte-8 u72057594037927936) (* byte-7 u281474976710656) (* byte-6 u1099511627776) (* byte-5 u4294967296) (* byte-4 u16777216) (* byte-3 u65536) (* byte-2 u256) byte-1)) ) (begin (print "read uint64") (print ret) (ok { uint64: ret, ctx: { txbuff: data, index: (+ u8 base) } }) )) ) ;; Reads the next varint from txbuff, and updates the index. ;; Returns (ok { varint: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. (define-read-only (read-varint (ctx { txbuff: (buff 1024), index: uint })) (let ( (ptr (get index ctx)) (tx (get txbuff ctx)) (byte (buff-to-u8 (unwrap! (element-at tx ptr) (err ERR-OUT-OF-BOUNDS)))) ) (if (<= byte u252) (begin (print "varint 1") (print byte) ;; given byte is the varint (ok { varint: byte, ctx: { txbuff: tx, index: (+ u1 ptr) }}) ) (if (is-eq byte u253) (let ( ;; next two bytes is the varint (parsed-u16 (try! (read-uint16 { txbuff: tx, index: (+ u1 ptr) }))) ) (begin (print "varint 2") (print (get uint16 parsed-u16)) (ok { varint: (get uint16 parsed-u16), ctx: (get ctx parsed-u16) }) )) (if (is-eq byte u254) (let ( ;; next four bytes is the varint (parsed-u32 (try! (read-uint32 { txbuff: tx, index: (+ u1 ptr) }))) ) (begin (print "varint 4") (print (get uint32 parsed-u32)) (ok { varint: (get uint32 parsed-u32), ctx: (get ctx parsed-u32) }) )) (let ( ;; next eight bytes is the varint (parsed-u64 (try! (read-uint64 { txbuff: tx, index: (+ u1 ptr) }))) ) (begin (print "varint 8") (print (get uint64 parsed-u64)) (ok { varint: (get uint64 parsed-u64), ctx: (get ctx parsed-u64) }) )) ) ) )) ) ;; Reads a varint-prefixed byte slice from txbuff, and updates the index to point to the byte after the varint and slice. ;; Returns (ok { varslice: (buff 1024), ctx: { txbuff: (buff 1024), index: uint } }) on success, where varslice has the length of the varint prefix. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. (define-read-only (read-varslice (old-ctx { txbuff: (buff 1024), index: uint })) (let ( (parsed (try! (read-varint old-ctx))) (slice-len (get varint parsed)) (ctx (get ctx parsed)) (slice (try! (read-slice (get txbuff ctx) (get index ctx) slice-len))) ) (ok { varslice: slice, ctx: { txbuff: (get txbuff ctx), index: (+ (len slice) (get index ctx)) } })) ) ;; Generate a permutation of a given 32-byte buffer, appending the element at target-index to hash-output. ;; The target-index decides which index in hash-input gets appended to hash-output. (define-read-only (inner-buff32-permutation (target-index uint) (state { hash-input: (buff 32), hash-output: (buff 32) })) { hash-input: (get hash-input state), hash-output: (unwrap-panic (as-max-len? (concat (get hash-output state) (unwrap-panic (as-max-len? (unwrap-panic (element-at (get hash-input state) target-index)) u32))) u32)) } ) ;; Reverse the byte order of a 32-byte buffer. Returns the (buff 32). (define-read-only (reverse-buff32 (input (buff 32))) (get hash-output (fold inner-buff32-permutation (list u31 u30 u29 u28 u27 u26 u25 u24 u23 u22 u21 u20 u19 u18 u17 u16 u15 u14 u13 u12 u11 u10 u9 u8 u7 u6 u5 u4 u3 u2 u1 u0) { hash-input: input, hash-output: 0x })) ) ;; Reads a big-endian hash -- consume the next 32 bytes, and reverse them. ;; Returns (ok { hashslice: (buff 32), ctx: { txbuff: (buff 1024), index: uint } }) on success, and updates the index. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. (define-read-only (read-hashslice (old-ctx { txbuff: (buff 1024), index: uint })) (let ( (hash-le (unwrap-panic (as-max-len? (try! (read-slice (get txbuff old-ctx) (get index old-ctx) u32)) u32))) ) (ok { hashslice: (reverse-buff32 hash-le), ctx: { txbuff: (get txbuff old-ctx), index: (+ u32 (get index old-ctx)) } })) ) ;; Inner fold method to read the next tx input from txbuff. ;; The index in ctx will be updated to point to the next tx input if all goes well (or to the start of the outputs) ;; Returns (ok { ... }) on success. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptSig that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXINS) if there are more than eight inputs to read. (define-read-only (read-next-txin (ignored bool) (state-res (response { ctx: { txbuff: (buff 1024), index: uint }, remaining: uint, txins: (list 8 { outpoint: { hash: (buff 32), index: uint }, scriptSig: (buff 256), ;; just big enough to hold a 2-of-3 multisig script sequence: uint }) } uint))) (match state-res state (if (< u0 (get remaining state)) (let ( (remaining (get remaining state)) (ctx (get ctx state)) (parsed-hash (try! (read-hashslice ctx))) (parsed-index (try! (read-uint32 (get ctx parsed-hash)))) (parsed-scriptSig (try! (read-varslice (get ctx parsed-index)))) (parsed-sequence (try! (read-uint32 (get ctx parsed-scriptSig)))) (new-ctx (get ctx parsed-sequence)) ) (ok { ctx: new-ctx, remaining: (- remaining u1), txins: (unwrap! (as-max-len? (append (get txins state) { outpoint: { hash: (get hashslice parsed-hash), index: (get uint32 parsed-index) }, scriptSig: (unwrap! (as-max-len? (get varslice parsed-scriptSig) u256) (err ERR-VARSLICE-TOO-LONG)), sequence: (get uint32 parsed-sequence) }) u8) (err ERR-TOO-MANY-TXINS)) })) (ok state) ) error (err error) ) ) ;; Read a transaction's inputs. ;; Returns (ok { txins: (list { ... }), remaining: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success, and updates the index in ctx to point to the start of the tx outputs. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptSig that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXINS) if there are more than eight inputs to read. (define-read-only (read-txins (ctx { txbuff: (buff 1024), index: uint })) (let ( (parsed-num-txins (try! (read-varint ctx))) (num-txins (get varint parsed-num-txins)) (new-ctx (get ctx parsed-num-txins)) ) (if (> num-txins u8) (err ERR-TOO-MANY-TXINS) (fold read-next-txin (list true true true true true true true true) (ok { ctx: new-ctx, remaining: num-txins, txins: (list ) })) )) ) ;; Read the next transaction output, and update the index in ctx to point to the next output. ;; Returns (ok { ... }) on success ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptPubKey that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXOUTS) if there are more than eight outputs to read. (define-read-only (read-next-txout (ignored bool) (state-res (response { ctx: { txbuff: (buff 1024), index: uint }, remaining: uint, txouts: (list 8 { value: uint, scriptPubKey: (buff 128) }) } uint))) (match state-res state (if (< u0 (get remaining state)) (let ( (remaining (get remaining state)) (parsed-value (try! (read-uint64 (get ctx state)))) (parsed-script (try! (read-varslice (get ctx parsed-value)))) (new-ctx (get ctx parsed-script)) ) (ok { ctx: new-ctx, remaining: (- remaining u1), txouts: (unwrap! (as-max-len? (append (get txouts state) { value: (get uint64 parsed-value), scriptPubKey: (unwrap! (as-max-len? (get varslice parsed-script) u128) (err ERR-VARSLICE-TOO-LONG)) }) u8) (err ERR-TOO-MANY-TXOUTS)) })) (ok state) ) error (err error) ) ) ;; Read all transaction outputs in a transaction. Update the index to point to the first byte after the outputs, if all goes well. ;; Returns (ok { txouts: (list { ... }), remaining: uint, ctx: { txbuff: (buff 1024), index: uint } }) on success, and updates the index in ctx to point to the start of the tx outputs. ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptPubKey that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXOUTS) if there are more than eight outputs to read. (define-read-only (read-txouts (ctx { txbuff: (buff 1024), index: uint })) (let ( (parsed-num-txouts (try! (read-varint ctx))) (num-txouts (get varint parsed-num-txouts)) (new-ctx (get ctx parsed-num-txouts)) ) (if (> num-txouts u8) (err ERR-TOO-MANY-TXOUTS) (fold read-next-txout (list true true true true true true true true) (ok { ctx: new-ctx, remaining: num-txouts, txouts: (list ) })) )) ) ;; Parse a Bitcoin transaction, with up to 8 inputs and 8 outputs, with scriptSigs of up to 256 bytes each, and with scriptPubKeys up to 128 bytes. ;; Returns a tuple structured as follows on success: ;; (ok { ;; version: uint, ;; tx version ;; ins: (list 8 ;; { ;; outpoint: { ;; pointer to the utxo this input consumes ;; hash: (buff 32), ;; index: uint ;; }, ;; scriptSig: (buff 256), ;; spending condition script ;; sequence: uint ;; }), ;; outs: (list 8 ;; { ;; value: uint, ;; satoshis sent ;; scriptPubKey: (buff 128) ;; parse this to get an address ;; }), ;; locktime: uint ;; }) ;; Returns (err ERR-OUT-OF-BOUNDS) if we read past the end of txbuff. ;; Returns (err ERR-VARSLICE-TOO-LONG) if we find a scriptPubKey or scriptSig that's too long to parse. ;; Returns (err ERR-TOO-MANY-TXOUTS) if there are more than eight inputs to read. ;; Returns (err ERR-TOO-MANY-TXINS) if there are more than eight outputs to read. (define-read-only (parse-tx (tx (buff 1024))) (let ( (ctx { txbuff: tx, index: u0 }) (parsed-version (try! (read-uint32 ctx))) (parsed-txins (try! (read-txins (get ctx parsed-version)))) (parsed-txouts (try! (read-txouts (get ctx parsed-txins)))) (parsed-locktime (try! (read-uint32 (get ctx parsed-txouts)))) ) (ok { version: (get uint32 parsed-version), ins: (get txins parsed-txins), outs: (get txouts parsed-txouts), locktime: (get uint32 parsed-locktime) })) ) ;; Parse a Bitcoin block header. ;; Returns a tuple structured as folowed on success: ;; (ok { ;; version: uint, ;; block version, ;; parent: (buff 32), ;; parent block hash, ;; merkle-root: (buff 32), ;; merkle root for all this block's transactions ;; timestamp: uint, ;; UNIX epoch timestamp of this block, in seconds ;; nbits: uint, ;; compact block difficulty representation ;; nonce: uint ;; PoW solution ;; }) ;; Returns (err ERR-BAD-HEADER) if the header buffer isn't actually 80 bytes long. (define-read-only (parse-block-header (headerbuff (buff 80))) (let ( (ctx { txbuff: (unwrap! (as-max-len? headerbuff u1024) (err ERR-BAD-HEADER)), index: u0 }) ;; none of these should fail, since they're all fixed-length fields whose lengths sum to 80 (parsed-version (unwrap-panic (read-uint32 ctx))) (parsed-parent-hash (unwrap-panic (read-hashslice (get ctx parsed-version)))) (parsed-merkle-root (unwrap-panic (read-hashslice (get ctx parsed-parent-hash)))) (parsed-timestamp (unwrap-panic (read-uint32 (get ctx parsed-merkle-root)))) (parsed-nbits (unwrap-panic (read-uint32 (get ctx parsed-timestamp)))) (parsed-nonce (unwrap-panic (read-uint32 (get ctx parsed-nbits)))) ) (ok { version: (get uint32 parsed-version), parent: (get hashslice parsed-parent-hash), merkle-root: (get hashslice parsed-merkle-root), timestamp: (get uint32 parsed-timestamp), nbits: (get uint32 parsed-nbits), nonce: (get uint32 parsed-nonce) })) ) (define-read-only (get-bc-h-hash (bh uint)) (get-block-info? burnchain-header-hash bh)) ;; Verify that a block header hashes to a burnchain header hash at a given height. ;; Returns true if so; false if not. (define-read-only (verify-block-header (headerbuff (buff 80)) (expected-block-height uint)) (match (get-bc-h-hash expected-block-height) bhh (is-eq bhh (reverse-buff32 (sha256 (sha256 headerbuff)))) false )) ;; Get the txid of a transaction, but big-endian. ;; This is the reverse of what you see on block explorers. (define-read-only (get-reversed-txid (tx (buff 1024))) (sha256 (sha256 tx))) ;; Get the txid of a transaction. ;; This is what you see on block explorers. (define-read-only (get-txid (tx (buff 1024))) (reverse-buff32 (get-reversed-txid tx)) ) ;; Determine if the ith bit in a uint is set to 1 (define-read-only (is-bit-set (val uint) (bit uint)) (is-eq (mod (/ val (pow u2 bit)) u2) u1) ) ;; Verify the next step of a Merkle proof. ;; This hashes cur-hash against the ctr-th hash in proof-hashes, and uses that as the next cur-hash. ;; The path is a bitfield describing the walk from the txid up to the merkle root: ;; * if the ith bit is 0, then cur-hash is hashed before the next proof-hash (cur-hash is "left"). ;; * if the ith bit is 1, then the next proof-hash is hashed before cur-hash (cur-hash is "right"). ;; The proof verifies if cur-hash is equal to root-hash, and we're out of proof-hashes to check. (define-read-only (inner-merkle-proof-verify (ctr uint) (state { path: uint, root-hash: (buff 32), proof-hashes: (list 12 (buff 32)), tree-depth: uint, cur-hash: (buff 32), verified: bool })) (if (get verified state) state (if (>= ctr (get tree-depth state)) (begin (print "ctr exceeds proof length or tree depth") (print ctr) (print (get tree-depth state)) (print (len (get proof-hashes state))) (merge state { verified: false }) ) (let ( (path (get path state)) (is-left (is-bit-set path ctr)) (proof-hashes (get proof-hashes state)) (cur-hash (get cur-hash state)) (root-hash (get root-hash state)) (h1 (if is-left (unwrap-panic (element-at proof-hashes ctr)) cur-hash)) (h2 (if is-left cur-hash (unwrap-panic (element-at proof-hashes ctr)))) (next-hash (sha256 (sha256 (concat h1 h2)))) (is-verified (and (is-eq (+ u1 ctr) (len proof-hashes)) (is-eq next-hash root-hash))) ) (begin (print "cur-hash") (print cur-hash) (print "next-hash") (print h1) (print h2) (print next-hash) (merge state { cur-hash: next-hash, verified: is-verified }) )) ) ) ) ;; Verify a Merkle proof, given the _reversed_ txid of a transaction, the merkle root of its block, and a proof consisting of: ;; * The index in the block where the transaction can be found (starting from 0), ;; * The list of hashes that link the txid to the merkle root, ;; * The depth of the block's merkle tree (required because Bitcoin does not identify merkle tree nodes as being leaves or intermediates). ;; The _reversed_ txid is required because that's the order (big-endian) processes them in. ;; The tx-index is required because it tells us the left/right traversals we'd make if we were walking down the tree from root to transaction, ;; and is thus used to deduce the order in which to hash the intermediate hashes with one another to link the txid to the merkle root. ;; Returns (ok true) if the proof is valid. ;; Returns (ok false) if the proof is invalid. ;; Returns (err ERR-PROOF-TOO-SHORT) if the proof's hashes aren't long enough to link the txid to the merkle root. (define-read-only (verify-merkle-proof (reversed-txid (buff 32)) (merkle-root (buff 32)) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (if (> (get tree-depth proof) (len (get hashes proof))) (err ERR-PROOF-TOO-SHORT) (ok (get verified (fold inner-merkle-proof-verify (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11) { path: (+ (pow u2 (get tree-depth proof)) (get tx-index proof)), root-hash: merkle-root, proof-hashes: (get hashes proof), cur-hash: reversed-txid, tree-depth: (get tree-depth proof), verified: false })) ) ) ) ;; Top-level verification code to determine whether or not a Bitcoin transaction was mined in a prior Bitcoin block. ;; It takes the block header and block height, the transaction, and a merkle proof, and determines that: ;; * the block header corresponds to the block that was mined at the given Bitcoin height ;; * the transaction's merkle proof links it to the block header's merkle root. ;; The proof is a list of sibling merkle tree nodes that allow us to calculate the parent node from two children nodes in each merkle tree level, ;; the depth of the block's merkle tree, and the index in the block in which the given transaction can be found (starting from 0). ;; The first element in hashes must be the given transaction's sibling transaction's ID. This and the given transaction's txid are hashed to ;; calculate the parent hash in the merkle tree, which is then hashed with the *next* hash in the proof, and so on and so forth, until the final ;; hash can be compared against the block header's merkle root field. The tx-index tells us in which order to hash each pair of siblings. ;; Note that the proof hashes -- including the sibling txid -- must be _big-endian_ hashes, because this is how Bitcoin generates them. ;; This is the reverse of what you'd see in a block explorer! ;; Returns (ok true) if the proof checks out. ;; Returns (ok false) if not. ;; Returns (err ERR-PROOF-TOO-SHORT) if the proof doesn't contain enough intermediate hash nodes in the merkle tree. (define-read-only (was-tx-mined-compact (block { header: (buff 80), height: uint }) (tx (buff 1024)) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (if (verify-block-header (get header block) (get height block)) (verify-merkle-proof (get-reversed-txid tx) (reverse-buff32 (get merkle-root (try! (parse-block-header (get header block))))) proof) (ok false) ) ) (define-read-only (concat-header (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint })) (concat (concat (concat (concat (concat (get version block) (get parent block)) (get merkle-root block)) (get timestamp block)) (get nbits block)) (get nonce block)) ) (define-read-only (concat-var (buffer (buff 256))) (concat (unwrap-panic (element-at BUFF_TO_BYTE (len buffer))) buffer)) (define-read-only (concat-in (in {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}) (result (buff 1024))) (unwrap-panic (as-max-len? (concat (concat (concat (concat result (get hash (get outpoint in))) (get index (get outpoint in))) (concat-var (get scriptSig in))) (get sequence in)) u1024 ))) (define-read-only (concat-ins (ins (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}))) (unwrap-panic (as-max-len? (concat (unwrap-panic (element-at BUFF_TO_BYTE (len ins))) (fold concat-in ins 0x)) u1024))) (define-read-only (concat-out (out {value: (buff 8), scriptPubKey: (buff 128)}) (result (buff 1024))) (unwrap-panic (as-max-len? (concat (concat result (get value out)) (concat-var (get scriptPubKey out))) u1024))) (define-read-only (concat-outs (outs (list 8 {value: (buff 8), scriptPubKey: (buff 128)}))) (unwrap-panic (as-max-len? (concat (unwrap-panic (element-at BUFF_TO_BYTE (len outs))) (fold concat-out outs 0x)) u1024))) (define-read-only (concat-tx (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (unwrap-panic (as-max-len? (concat (concat (concat (get version tx) (concat-ins (get ins tx))) (concat-outs (get outs tx))) (get locktime tx)) u1024))) (define-read-only (was-tx-mined (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx (buff 1024)) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (if (verify-block-header (concat-header block) (get height block)) (verify-merkle-proof (get-reversed-txid tx) (get merkle-root block) proof) (err u1) ) )
0x8b112f2b50c1fa864997b7496aaad1e3940700309a3fdcc6c07f1c6f8b9cfb7b
22,430
success
btc-nft-swap-v1
(use-trait non-fungible-token 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) (define-constant expiry u100) (define-map swaps uint {sats: uint, btc-receiver: (buff 40), nft-id: uint, nft-receiver: principal, nft-sender: principal, when: uint, done: uint, nft: principal}) (define-data-var next-id uint u0) (define-private (find-out (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result {pubscriptkey: (buff 40), out: (optional {scriptPubKey: (buff 128), value: uint})})) (if (is-eq (get scriptPubKey entry) (get pubscriptkey result)) (merge result {out: (some {scriptPubKey: (get scriptPubKey entry), value: (get uint32 (unwrap-panic (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 read-uint32 {txbuff: (get value entry), index: u0})))})}) result)) (define-public (get-out-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (pubscriptkey (buff 40))) (ok (fold find-out (get outs tx) {pubscriptkey: pubscriptkey, out: none}))) ;; create a swap between btc and fungible token (define-public (create-swap (sats uint) (btc-receiver (buff 40)) (nft-id uint) (nft-receiver principal) (nft <non-fungible-token>)) (let ((id (var-get next-id))) (asserts! (map-insert swaps id {sats: sats, btc-receiver: btc-receiver, nft-id: nft-id, nft-receiver: nft-receiver, nft-sender: tx-sender, when: block-height, done: u0, nft: (contract-of nft)}) ERR_INVALID_ID) (var-set next-id (+ id u1)) (match (contract-call? nft transfer nft-id tx-sender (as-contract tx-sender)) success (ok id) error (err (* error u1000))))) ;; any user can cancle the swap after the expiry period (define-public (cancel (id uint) (nft <non-fungible-token>)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID))) (asserts! (is-eq (contract-of nft) (get nft swap)) ERR_INVALID_NFT) (asserts! (< (+ (get when swap) expiry) block-height) ERR_TOO_EARLY) (asserts! (is-eq (get done swap) u0) ERR_ALREADY_DONE) (asserts! (map-set swaps id (merge swap {done: u1})) ERR_NATIVE_FAILURE) (as-contract (contract-call? nft transfer (get nft-id swap) tx-sender (get nft-sender swap))))) ;; any user can submit a tx that contains the swap (define-public (submit-swap (id uint) (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint }) (nft <non-fungible-token>)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID)) (tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (begin (asserts! result ERR_VERIFICATION_FAILED) (asserts! (is-eq (get done swap) u0) ERR_ALREADY_DONE) (match (get out (unwrap! (get-out-value tx (get btc-receiver swap)) ERR_NATIVE_FAILURE)) out (if (>= (get value out) (get sats swap)) (begin (asserts! (is-eq (contract-of nft) (get nft swap)) ERR_INVALID_NFT) (asserts! (map-set swaps id (merge swap {done: u1})) ERR_NATIVE_FAILURE) (as-contract (contract-call? nft transfer (get nft-id swap) tx-sender (get nft-receiver swap)))) ERR_TX_VALUE_TOO_SMALL) ERR_TX_NOT_FOR_RECEIVER)) error (err (* error u1000))))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_NOT_FOR_RECEIVER (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_INVALID_NFT (err u8)) (define-constant ERR_NATIVE_FAILURE (err u99))
0xd8d1c00f9b1d72ea3d4e19e62b5b3ab3c1938e1a4964eefc8ebfd4616ce41b34
22,674
success
bet
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_BETS_EACH_SIDE u400) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant ONE_ROUND_DURATION_BLOCKS u1) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u1) ;;TODO u48 ;; 8 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 100*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u1000000 u10000000 u100000000)) ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-share uint u0) (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_BETS_EACH_SIDE) (<= s-num MAX_BETS_EACH_SIDE)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (and (>= bet-v u1) (<= bet-v u50))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (and (>= bet-s u51) (<= bet-s u100))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u100 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has not draw) ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (is-eq (get rand-num last-bet-summary-tuple) u0) (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) true ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (rand-num (calc-round-rand-num bet-type round)) (is-v (< rand-num u51)) ) ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; add a log (print "draw") ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per share (var-set draw-award-per-share (/ (var-get draw-award-pool-num) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u100)) (bet-s (mod bet-value u100)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (> bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (> bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (origin-award (* (get shares bet-record-tuple) (var-get draw-award-per-share))) (award (if (> origin-award bet-price) (/ (* origin-award u98) u100) origin-award ) ) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u100) (> (mod bet-value u100) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> origin-award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod vrf-rand-num u100) u1) ) ) ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (> bet-value u100)) (bet-s-flag (> (mod bet-value u100) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (ok { round: round, bet-summary: (map-get? bet-summary-map { bet-type: bet-type, round: round }) }) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (map-get? bet-summary-map { bet-type: bet-type, round: round }) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-bet-info (bet-type uint) (player principal) (index uint)) (map-get? player-record-map { player: player, bet-type: bet-type, index: index }) ) (define-read-only (get-bet-records (key-list (list 50 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) (define-read-only (get-balance) (stx-get-balance (as-contract tx-sender)) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x379b66fafd98eadf18364fc4ec7c49b58e011d96f8236c63dfb93d9d03a0c041
23,098
success
vs
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_BETS_EACH_SIDE u400) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant ONE_ROUND_DURATION_BLOCKS u1) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u1) ;;TODO u48 ;; 8 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 100*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u10000 u20000 u30000)) ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-share uint u0) (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_BETS_EACH_SIDE) (<= s-num MAX_BETS_EACH_SIDE)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (and (>= bet-v u1) (<= bet-v u50))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (and (>= bet-s u51) (<= bet-s u100))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u100 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has not draw) ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (is-eq (get rand-num last-bet-summary-tuple) u0) (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) true ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (rand-num (calc-round-rand-num bet-type round)) (is-v (< rand-num u51)) ) ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; add a log (print "draw") ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per share (var-set draw-award-per-share (/ (var-get draw-award-pool-num) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u100)) (bet-s (mod bet-value u100)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (> bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (> bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (origin-award (* (get shares bet-record-tuple) (var-get draw-award-per-share))) (award (if (> origin-award bet-price) (/ (* origin-award u98) u100) origin-award ) ) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u100) (> (mod bet-value u100) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> origin-award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod vrf-rand-num u100) u1) ) ) ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (> bet-value u100)) (bet-s-flag (> (mod bet-value u100) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (ok { round: round, bet-summary: (map-get? bet-summary-map { bet-type: bet-type, round: round }) }) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (map-get? bet-summary-map { bet-type: bet-type, round: round }) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-bet-info (bet-type uint) (player principal) (index uint)) (map-get? player-record-map { player: player, bet-type: bet-type, index: index }) ) (define-read-only (get-bet-records (key-list (list 50 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) (define-read-only (get-balance) (stx-get-balance (as-contract tx-sender)) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x838829170562e23fff376758b01b43d9150a3687fee0bfeb82503693d03c44fa
23,102
success
btc-ft-swap
(use-trait fungible-token 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-constant expiry u100) (define-map swaps uint {sats: uint, btc-receiver: (buff 40), amount: uint, ft-receiver: (optional principal), ft-sender: principal, when: uint, done: uint, ft: principal}) (define-data-var next-id uint u0) (define-private (find-out (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result {pubscriptkey: (buff 40), out: (optional {scriptPubKey: (buff 128), value: uint})})) (if (is-eq (get scriptPubKey entry) (get pubscriptkey result)) (merge result {out: (some {scriptPubKey: (get scriptPubKey entry), value: (get uint32 (unwrap-panic (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 read-uint32 {txbuff: (get value entry), index: u0})))})}) result)) (define-public (get-out-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (pubscriptkey (buff 40))) (ok (fold find-out (get outs tx) {pubscriptkey: pubscriptkey, out: none}))) ;; create a swap between btc and fungible token (define-public (create-swap (sats uint) (btc-receiver (buff 40)) (amount uint) (ft-receiver (optional principal)) (ft <fungible-token>)) (let ((id (var-get next-id))) (asserts! (map-insert swaps id {sats: sats, btc-receiver: btc-receiver, amount: amount, ft-receiver: ft-receiver, ft-sender: tx-sender, when: block-height, done: u0, ft: (contract-of ft)}) ERR_INVALID_ID) (var-set next-id (+ id u1)) (match (contract-call? ft transfer amount tx-sender (as-contract tx-sender) (some 0x636174616d6172616e2073776170)) success (ok id) error (err (* error u1000))))) (define-public (set-ft-receiver (id uint)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID))) (if (is-none (get ft-receiver swap)) (begin (asserts! (map-set swaps id (merge swap {ft-receiver: (some tx-sender)})) ERR_NATIVE_FAILURE) (ok true)) ERR_ALREADY_DONE))) ;; any user can cancle the swap after the expiry period (define-public (cancel (id uint) (ft <fungible-token>)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID))) (asserts! (is-eq (contract-of ft) (get ft swap)) ERR_INVALID_FUNGIBLE_TOKEN) (asserts! (< (+ (get when swap) expiry) block-height) ERR_TOO_EARLY) (asserts! (is-eq (get done swap) u0) ERR_ALREADY_DONE) (asserts! (map-set swaps id (merge swap {done: u1})) ERR_NATIVE_FAILURE) (as-contract (contract-call? ft transfer (get amount swap) tx-sender (get ft-sender swap) (some 0x72657665727420636174616d6172616e2073776170))))) ;; any user can submit a tx that contains the swap (define-public (submit-swap (id uint) (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint }) (ft <fungible-token>)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID)) (tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (begin (asserts! result ERR_VERIFICATION_FAILED) (asserts! (is-eq (get done swap) u0) ERR_ALREADY_DONE) (match (get out (unwrap! (get-out-value tx (get btc-receiver swap)) ERR_NATIVE_FAILURE)) out (if (>= (get value out) (get sats swap)) (begin (asserts! (is-eq (contract-of ft) (get ft swap)) ERR_INVALID_FUNGIBLE_TOKEN) (asserts! (map-set swaps id (merge swap {done: u1})) ERR_NATIVE_FAILURE) (as-contract (contract-call? ft transfer (get amount swap) tx-sender (unwrap! (get ft-receiver swap) ERR_NO_FT_RECEIVER) (some 0x636174616d6172616e2073776170)))) ERR_TX_VALUE_TOO_SMALL) ERR_TX_NOT_FOR_RECEIVER)) error (err (* error u1000))))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_NOT_FOR_RECEIVER (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_INVALID_FUNGIBLE_TOKEN (err u8)) (define-constant ERR_NO_FT_RECEIVER (err u9)) (define-constant ERR_NATIVE_FAILURE (err u99))
0xb8b822f30a063fda8f9d44a23530a562a1ed93867d5977fbb12b1c284736edbb
23,131
success
btc-stx-swap-v1
(define-constant expiry u100) (define-map swaps uint {sats: uint, btc-receiver: (buff 40), ustx: uint, stx-receiver: (optional principal), stx-sender: principal, when: uint, done: uint}) (define-data-var next-id uint u0) (define-private (find-out (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result {pubscriptkey: (buff 40), out: (optional {scriptPubKey: (buff 128), value: uint})})) (if (is-eq (get scriptPubKey entry) (get pubscriptkey result)) (merge result {out: (some {scriptPubKey: (get scriptPubKey entry), value: (get uint32 (unwrap-panic (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 read-uint32 {txbuff: (get value entry), index: u0})))})}) result)) (define-public (get-out-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (pubscriptkey (buff 40))) (ok (fold find-out (get outs tx) {pubscriptkey: pubscriptkey, out: none}))) ;; create a swap between btc and stx (define-public (create-swap (sats uint) (btc-receiver (buff 40)) (ustx uint) (stx-receiver (optional principal))) (let ((id (var-get next-id))) (asserts! (map-insert swaps id {sats: sats, btc-receiver: btc-receiver, ustx: ustx, stx-receiver: stx-receiver, stx-sender: tx-sender, when: block-height, done: u0}) ERR_INVALID_ID) (var-set next-id (+ id u1)) (match (stx-transfer? ustx tx-sender (as-contract tx-sender)) success (ok id) error (err (* error u1000))))) (define-public (set-stx-receiver (id uint)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID))) (if (is-none (get stx-receiver swap)) (begin (asserts! (map-set swaps id (merge swap {stx-receiver: (some tx-sender)})) ERR_NATIVE_FAILURE) (ok true)) ERR_ALREADY_DONE))) ;; any user can cancle the swap after the expiry period (define-public (cancel (id uint)) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID))) (asserts! (< (+ (get when swap) expiry) block-height) ERR_TOO_EARLY) (asserts! (is-eq (get done swap) u0) ERR_ALREADY_DONE) (asserts! (map-set swaps id (merge swap {done: u1})) ERR_NATIVE_FAILURE) (as-contract (stx-transfer? (get ustx swap) tx-sender (get stx-sender swap))))) ;; any user can submit a tx that contains the swap (define-public (submit-swap (id uint) (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (let ((swap (unwrap! (map-get? swaps id) ERR_INVALID_ID)) (tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (begin (asserts! result ERR_VERIFICATION_FAILED) (asserts! (is-eq (get done swap) u0) ERR_ALREADY_DONE) (match (get out (unwrap! (get-out-value tx (get btc-receiver swap)) ERR_NATIVE_FAILURE)) out (if (>= (get value out) (get sats swap)) (begin (asserts! (map-set swaps id (merge swap {done: u1})) ERR_NATIVE_FAILURE) (as-contract (stx-transfer? (get ustx swap) tx-sender (unwrap! (get stx-receiver swap) ERR_NO_STX_RECEIVER)))) ERR_TX_VALUE_TOO_SMALL) ERR_TX_NOT_FOR_RECEIVER)) error (err (* error u1000))))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_NOT_FOR_RECEIVER (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_NO_STX_RECEIVER (err u8)) (define-constant ERR_NATIVE_FAILURE (err u99))
0xcf854186fe737896889dc93217fbb4ab6df8ed3f71a28dac6f1e690b5cc3ad52
23,138
success
vs
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_BETS_EACH_SIDE u400) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant ONE_ROUND_DURATION_BLOCKS u1) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u1) ;;TODO u48 ;; 8 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 100*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-share uint u0) (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_BETS_EACH_SIDE) (<= s-num MAX_BETS_EACH_SIDE)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (and (>= bet-v u1) (<= bet-v u50))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (and (>= bet-s u51) (<= bet-s u100))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u100 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has not draw) ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (is-eq (get rand-num last-bet-summary-tuple) u0) (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) true ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (rand-num (calc-round-rand-num bet-type round)) (is-v (< rand-num u51)) ) ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; add a log (print "draw") ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per share (var-set draw-award-per-share (/ (var-get draw-award-pool-num) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u100)) (bet-s (mod bet-value u100)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (> bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (> bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (origin-award (* (get shares bet-record-tuple) (var-get draw-award-per-share))) (award (if (> origin-award bet-price) (/ (* origin-award u98) u100) origin-award ) ) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u100) (> (mod bet-value u100) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> origin-award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod vrf-rand-num u100) u1) ) ) ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (> bet-value u100)) (bet-s-flag (> (mod bet-value u100) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (ok { round: round, bet-summary: (map-get? bet-summary-map { bet-type: bet-type, round: round }) }) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (map-get? bet-summary-map { bet-type: bet-type, round: round }) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-bet-info (bet-type uint) (player principal) (index uint)) (map-get? player-record-map { player: player, bet-type: bet-type, index: index }) ) (define-read-only (get-bet-records (key-list (list 50 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) (define-read-only (get-balance) (stx-get-balance (as-contract tx-sender)) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x617faadb3be9ba5f0f7f6dfe823b11c9a4eba55401a494c74d2f8b9ca11e6036
23,206
success
vs
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_BETS_EACH_SIDE u400) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant ONE_ROUND_DURATION_BLOCKS u1) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u1) ;;TODO u48 ;; 8 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 100*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-share uint u0) (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_BETS_EACH_SIDE) (<= s-num MAX_BETS_EACH_SIDE)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (and (>= bet-v u1) (<= bet-v u50))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (and (>= bet-s u51) (<= bet-s u100))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u100 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has not draw) ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (is-eq (get rand-num last-bet-summary-tuple) u0) (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) true ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (rand-num (calc-round-rand-num bet-type round)) (is-v (< rand-num u51)) ) ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; add a log (print "draw") ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per share (var-set draw-award-per-share (/ (var-get draw-award-pool-num) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u100)) (bet-s (mod bet-value u100)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (> bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (> bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (origin-award (* (get shares bet-record-tuple) (var-get draw-award-per-share))) (award (if (> origin-award bet-price) (/ (* origin-award u98) u100) origin-award ) ) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u100) (> (mod bet-value u100) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> origin-award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod vrf-rand-num u100) u1) ) ) ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (> bet-value u100)) (bet-s-flag (> (mod bet-value u100) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (ok { round: round, bet-summary: (map-get? bet-summary-map { bet-type: bet-type, round: round }) }) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (map-get? bet-summary-map { bet-type: bet-type, round: round }) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-bet-info (bet-type uint) (player principal) (index uint)) (map-get? player-record-map { player: player, bet-type: bet-type, index: index }) ) (define-read-only (get-bet-records (key-list (list 50 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) (define-read-only (get-balance) (stx-get-balance (as-contract tx-sender)) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x617faadb3be9ba5f0f7f6dfe823b11c9a4eba55401a494c74d2f8b9ca11e6036
23,206
success
fpwr-v0
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token wrapped-rewards) (define-constant pool-admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-constant pool-scriptpubkey 0xa91413effebe0ea4bb45e35694f5a15bb5b96e851afb87) (define-map rewards-by-tx (buff 1024) uint) (define-map rewards-by-cycle uint uint) ;; total submitted btc rewards (define-read-only (get-rewards-by-cycle (cycle uint)) (ok (default-to u0 (map-get? rewards-by-cycle cycle)))) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (ok (ft-get-balance wrapped-rewards owner))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply wrapped-rewards))) ;; returns the token name (define-read-only (get-name) (ok "Friedger Pool xBTC rewards")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FPXR")) ;; the number of decimals used (define-read-only (get-decimals) (ok u8)) ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (is-eq tx-sender sender) (begin (try! (ft-transfer? wrapped-rewards amount sender recipient)) (print memo) (ok true) ) (err u4))) (define-public (get-token-uri) (ok (some u"https://pool.friedger.de/wrapped-rewards.json"))) ;; ;; mint via btc transactions ;; ;; Backport of .pox's burn-height-to-reward-cycle (define-read-only (burn-height-to-reward-cycle (height uint)) (let ( (pox-info (unwrap-panic (contract-call? 'SP000000000000000000002Q6VF78.pox get-pox-info))) ) (/ (- height (get first-burnchain-block-height pox-info)) (get reward-cycle-length pox-info)))) (define-private (map-update-cycle-rewards (height uint) (value uint)) (let ((cycle (burn-height-to-reward-cycle height))) (map-set rewards-by-cycle cycle (+ value (default-to u0 (map-get? rewards-by-cycle cycle)))))) (define-private (add-value (out {scriptPubKey: (buff 128), value: uint}) (result uint)) (+ (get value out) result) ) ;; adds out entry to result list if the scriptPubKey matches, ;; also converts buff value to uint value (define-private (find-outs (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result (list 8 {scriptPubKey: (buff 128), value: uint}))) (if (is-eq (get scriptPubKey entry) pool-scriptpubkey) (let ((value (get uint32 (unwrap-panic (contract-call? .clarity-bitcoin read-uint32 {txbuff: (get value entry), index: u0}))))) (unwrap-panic (as-max-len? (append result {scriptPubKey: (get scriptPubKey entry), value: value}) u8)) ) result)) (define-read-only (get-outs (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold find-outs (get outs tx) (list))) (define-read-only (get-tx-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold add-value (get-outs tx) u0)) ;; any user can submit a tx that contains a reward tx (define-public (mint (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (let ((tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (let ((value (get-tx-value tx))) (asserts! result ERR_VERIFICATION_FAILED) (asserts! (> value u0) ERR_TX_IGNORED) (asserts! (map-insert rewards-by-tx tx-buff value) ERR_ALREADY_DONE) (asserts! (map-update-cycle-rewards (get height block) value) ERR_NATIVE_FAILURE) (ft-mint? wrapped-rewards value pool-admin)) error (err (* error u1000))))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_IGNORED (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_NO_STX_RECEIVER (err u8)) (define-constant ERR_NATIVE_FAILURE (err u99))
0x4acad2587aac68a3d60278d2325ec706c00d75d253fdc6cc6667f95729a7c246
23,272
abort_by_response
fpwr-v0
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token wrapped-rewards) (define-constant pool-admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-constant pool-scriptpubkey 0xa91413effebe0ea4bb45e35694f5a15bb5b96e851afb87) (define-map rewards-by-tx (buff 1024) uint) (define-map rewards-by-cycle uint uint) ;; total submitted btc rewards (define-read-only (get-rewards-by-cycle (cycle uint)) (ok (default-to u0 (map-get? rewards-by-cycle cycle)))) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (ok (ft-get-balance wrapped-rewards owner))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply wrapped-rewards))) ;; returns the token name (define-read-only (get-name) (ok "Friedger Pool xBTC rewards")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FPXR")) ;; the number of decimals used (define-read-only (get-decimals) (ok u8)) ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (is-eq tx-sender sender) (begin (try! (ft-transfer? wrapped-rewards amount sender recipient)) (print memo) (ok true) ) (err u4))) (define-public (get-token-uri) (ok (some u"https://pool.friedger.de/wrapped-rewards.json"))) ;; ;; mint via btc transactions ;; ;; Backport of .pox's burn-height-to-reward-cycle (define-read-only (burn-height-to-reward-cycle (height uint)) (let ( (pox-info (unwrap-panic (contract-call? 'SP000000000000000000002Q6VF78.pox get-pox-info))) ) (/ (- height (get first-burnchain-block-height pox-info)) (get reward-cycle-length pox-info)))) (define-private (map-update-cycle-rewards (height uint) (value uint)) (let ((cycle (burn-height-to-reward-cycle height))) (map-set rewards-by-cycle cycle (+ value (default-to u0 (map-get? rewards-by-cycle cycle)))))) (define-private (add-value (out {scriptPubKey: (buff 128), value: uint}) (result uint)) (+ (get value out) result) ) ;; adds out entry to result list if the scriptPubKey matches, ;; also converts buff value to uint value (define-private (find-outs (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result (list 8 {scriptPubKey: (buff 128), value: uint}))) (if (is-eq (get scriptPubKey entry) pool-scriptpubkey) (let ((value (get uint32 (unwrap-panic (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 read-uint32 {txbuff: (get value entry), index: u0}))))) (unwrap-panic (as-max-len? (append result {scriptPubKey: (get scriptPubKey entry), value: value}) u8)) ) result)) (define-read-only (get-outs (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold find-outs (get outs tx) (list))) (define-read-only (get-tx-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold add-value (get-outs tx) u0)) ;; any user can submit a tx that contains a reward tx (define-public (mint (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (let ((tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (let ((value (get-tx-value tx))) (asserts! result ERR_VERIFICATION_FAILED) (asserts! (> value u0) ERR_TX_IGNORED) (asserts! (map-insert rewards-by-tx tx-buff value) ERR_ALREADY_DONE) (asserts! (map-update-cycle-rewards (get height block) value) ERR_NATIVE_FAILURE) (ft-mint? wrapped-rewards value pool-admin)) error (err (* error u1000))))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_IGNORED (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_NO_STX_RECEIVER (err u8)) (define-constant ERR_NATIVE_FAILURE (err u99))
0xf2c747ae5285c3644ae85c7642aabfe2a7d64b16caedf0811b7b854257535e0c
23,311
success
hello-world
;; hello-world ;; private functions ;; (define-read-only (echo-number (val int)) (ok val)) ;; public functions ;; (define-public (say-hi) (ok "hello world"))
0xa360986cb1e72806bf5ca5b2c1f11c98f5157489c002170f542375d0eb09e540
23,314
success
counter
;; define counter variable (define-data-var counter int 0) ;; counter getter (define-read-only (get-counter) (ok (var-get counter))) ;; increment method (define-public (increment) (begin (var-set counter (+ (var-get counter) 1)) (ok (var-get counter)))) ;; decrement method (define-public (decrement) (begin (var-set counter (- (var-get counter) 1)) (ok (var-get counter))))
0x47403d00e6ea807b0635e809c250f3d5c1bbf8ce9b74e92e64949a4ae536bdd3
23,329
success
french-bean-nft
;; French Bean NFT ;; use the SIP090 interface (testnet) (impl-trait 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.nft-trait.nft-trait) ;; define a new NFT. (define-non-fungible-token FRENCH-BEANS-NFT uint) ;; Store the last issues token ID (define-data-var last-id uint u0) ;; Claim a new NFT (define-public (claim) (mint tx-sender)) ;; 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? FRENCH-BEANS-NFT 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)) (ok (nft-get-owner? FRENCH-BEANS-NFT 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 "ipfs://bafybeia4xg542ak7kigujvrudq43dsku5o5wosnecdowimbbp6xixg773y/"))) ;; Internal - Mint new NFT (define-private (mint (new-owner principal)) (let ((next-id (+ u1 (var-get last-id)))) (match (nft-mint? FRENCH-BEANS-NFT next-id new-owner) success (begin (var-set last-id next-id) (ok true)) error (err error))))
0x570da52506ef2a4ab226ffa1e08b7459582e9d859997c7d07ad94fc93320abb9
23,335
abort_by_response
french-bean-nft
;; French Bean NFT ;; use the SIP090 interface (testnet) (impl-trait 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.nft-trait.nft-trait) ;; define a new NFT. (define-non-fungible-token FRENCH-BEANS-NFT uint) ;; Store the last issues token ID (define-data-var last-id uint u0) ;; Claim a new NFT (define-public (claim) (mint tx-sender)) ;; 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? FRENCH-BEANS-NFT 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)) (ok (nft-get-owner? FRENCH-BEANS-NFT 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 "ipfs://bafybeia4xg542ak7kigujvrudq43dsku5o5wosnecdowimbbp6xixg773y/"))) ;; Internal - Mint new NFT (define-private (mint (new-owner principal)) (let ((next-id (+ u1 (var-get last-id)))) (match (nft-mint? FRENCH-BEANS-NFT next-id new-owner) success (begin (var-set last-id next-id) (ok true)) error (err error))))
0xded3e699d4cf3fdc7d33c89e598cf75e94e496f1cfddb80ac74b0e7b11499b3b
23,338
abort_by_response
french-bean-nft
;; French Bean NFT ;; use the SIP090 interface (mainet?) (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) ;; define a new NFT. (define-non-fungible-token FRENCH-BEANS-NFT uint) ;; Store the last issues token ID (define-data-var last-id uint u0) ;; Claim a new NFT (define-public (claim) (mint tx-sender)) ;; 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? FRENCH-BEANS-NFT 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)) (ok (nft-get-owner? FRENCH-BEANS-NFT 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 "ipfs://bafybeia4xg542ak7kigujvrudq43dsku5o5wosnecdowimbbp6xixg773y/"))) ;; Internal - Mint new NFT (define-private (mint (new-owner principal)) (let ((next-id (+ u1 (var-get last-id)))) (match (nft-mint? FRENCH-BEANS-NFT next-id new-owner) success (begin (var-set last-id next-id) (ok true)) error (err error))))
0x70bcd7b4f8a42d49c1917bf0debf310e845f4619a64f9032e300691294f9b799
23,338
success
fpwr-v00
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token wrapped-rewards) (define-constant pool-admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-constant pool-scriptpubkey 0xa91413effebe0ea4bb45e35694f5a15bb5b96e851afb87) (define-map rewards-by-tx (buff 1024) uint) (define-map rewards-by-height uint uint) ;; total submitted btc rewards (define-read-only (get-rewards-by-height (height uint)) (ok (default-to u0 (map-get? rewards-by-height height)))) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (ok (ft-get-balance wrapped-rewards owner))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply wrapped-rewards))) ;; returns the token name (define-read-only (get-name) (ok "Friedger Pool xBTC rewards")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FPXR")) ;; the number of decimals used (define-read-only (get-decimals) (ok u8)) ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (is-eq tx-sender sender) (begin (try! (ft-transfer? wrapped-rewards amount sender recipient)) (print memo) (ok true) ) (err u4))) (define-public (get-token-uri) (ok (some u"https://pool.friedger.de/wrapped-rewards.json"))) ;; ;; mint via btc transactions ;; (define-private (map-update-rewards-by-height (height uint) (value uint)) (map-set rewards-by-height height (+ value (default-to u0 (map-get? rewards-by-height height))))) (define-private (add-value (out {scriptPubKey: (buff 128), value: uint}) (result uint)) (+ (get value out) result) ) ;; adds out entry to result list if the scriptPubKey matches, ;; also converts buff value to uint value (define-private (find-outs (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result (list 8 {scriptPubKey: (buff 128), value: uint}))) (if (is-eq (get scriptPubKey entry) pool-scriptpubkey) (let ((value (get uint32 (unwrap-panic (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 read-uint32 {txbuff: (get value entry), index: u0}))))) (unwrap-panic (as-max-len? (append result {scriptPubKey: (get scriptPubKey entry), value: value}) u8)) ) result)) (define-read-only (get-outs (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold find-outs (get outs tx) (list))) (define-read-only (get-tx-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold add-value (get-outs tx) u0)) ;; any user can submit a tx that contains a reward tx (define-public (mint (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (let ((tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (let ((value (get-tx-value tx))) (asserts! result ERR_VERIFICATION_FAILED) (asserts! (> value u0) ERR_TX_IGNORED) (asserts! (map-insert rewards-by-tx tx-buff value) ERR_ALREADY_DONE) (asserts! (map-update-rewards-by-height (get height block) value) ERR_NATIVE_FAILURE) (ft-mint? wrapped-rewards value pool-admin)) error (err (* error u1000))))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_IGNORED (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_NO_STX_RECEIVER (err u8)) (define-constant ERR_NATIVE_FAILURE (err u99))
0x815c7095f96daf590bdfda2425689320e8e601f4376f25473458ace0869a7902
23,405
success
fpwr-v01
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token wrapped-rewards) (define-constant pool-admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-constant pool-scriptpubkey 0xa91413effebe0ea4bb45e35694f5a15bb5b96e851afb87) (define-map rewards-by-tx (buff 1024) uint) (define-map rewards-by-height uint {wrew: uint, ustx: uint}) (define-data-var last-price (tuple (amount uint) (height uint) (timestamp uint)) {amount: u2813, height: u23481, timestamp: u1627462508}) ;; total submitted btc rewards (define-read-only (get-rewards-by-height (height uint)) (ok (default-to {wrew: u0, ustx: u0} (map-get? rewards-by-height height)))) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (ok (ft-get-balance wrapped-rewards owner))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply wrapped-rewards))) ;; returns the token name (define-read-only (get-name) (ok "Friedger Pool xBTC rewards")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FPWR")) ;; the number of decimals used (define-read-only (get-decimals) (ok u8)) ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (is-eq tx-sender sender) (begin (try! (ft-transfer? wrapped-rewards amount sender recipient)) (print memo) (ok true) ) (err u4))) (define-public (get-token-uri) (ok (some u"https://pool.friedger.de/wrapped-rewards.json"))) ;; ;; mint via btc transactions ;; (define-private (map-update-rewards-by-height (height uint) (wrew-value uint) (ustx-value uint)) (let ((rewards (map-get? rewards-by-height height))) (let ((wrew (default-to u0 (get wrew rewards))) (ustx (default-to u0 (get ustx rewards)))) (map-set rewards-by-height height {wrew: (+ wrew-value wrew), ustx: (+ ustx-value ustx)})))) (define-private (add-value (out {scriptPubKey: (buff 128), value: uint}) (result uint)) (+ (get value out) result) ) ;; adds out entry to result list if the scriptPubKey matches, ;; also converts buff value to uint value (define-private (find-outs (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result (list 8 {scriptPubKey: (buff 128), value: uint}))) (if (is-eq (get scriptPubKey entry) pool-scriptpubkey) (let ((value (get uint32 (unwrap-panic (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 read-uint32 {txbuff: (get value entry), index: u0}))))) (unwrap-panic (as-max-len? (append result {scriptPubKey: (get scriptPubKey entry), value: value}) u8)) ) result)) (define-read-only (get-outs (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold find-outs (get outs tx) (list))) (define-read-only (get-tx-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold add-value (get-outs tx) u0)) ;; any user can submit a tx that contains a reward tx (define-public (mint (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (let ((tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (let ((value (get-tx-value tx)) (height (get height block))) (asserts! result ERR_VERIFICATION_FAILED) (asserts! (> value u0) ERR_TX_IGNORED) (asserts! (map-insert rewards-by-tx tx-buff value) ERR_ALREADY_DONE) (let ((ustx (* value (get-price height)))) (asserts! (map-update-rewards-by-height height value ustx) ERR_NATIVE_FAILURE) (match (ft-mint? wrapped-rewards value pool-admin) success (ok {value: value, ustx: ustx}) error (err (* error u2000))))) error (err (* error u1000))))) ;; price BTC/STX (define-private (oracle-get-price) (contract-call? 'SPZ0RAC1EFTH949T4W2SYY6YBHJRMAF4ECT5A7DD.oracle-v1 get-price "artifix-binance" "STX-BTC") ) (define-private (update (price (tuple (amount uint) (height uint) (timestamp uint))) (height uint)) (if (> (get height (var-get last-price)) height) (var-set last-price price) false)) (define-private (get-price-at-height (height uint)) (match (get-block-info? id-header-hash height) hash (match (at-block hash (oracle-get-price)) price (begin (update price height) (some (get amount price))) none) none)) (define-private (get-price (height uint)) (match (get-price-at-height height) price price (match (get-price-at-height (- height u1)) price-1 price-1 (match (get-price-at-height (- height u2)) price-2 price-2 (get amount (print (var-get last-price))))))) (define-public (get-price-stx-btc (height uint)) (ok (get-price height))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_IGNORED (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_NO_STX_RECEIVER (err u8)) (define-constant ERR_FAILED_TO_GET_PRICE (err u9)) (define-constant ERR_NATIVE_FAILURE (err u99))
0xede4fa1c28ff4a3a3f6f14f1fcbc3907aecc65fd3c6a044d81add4d120efcfbd
23,486
success
nothing-faucet-alpha
(define-constant ERR_INPUT_VERSION_NOT_SUPPORTED u0) (define-public (get-nothing) (contract-call? 'SP32AEEF6WW5Y0NMJ1S8SBSZDAY8R5J32NBZFPKKZ.wrapped-nothing-v8 transfer u100000 (as-contract tx-sender) contract-caller none ) ) (define-public (convert-to-v8 (inputVersion uint) (amount uint)) (begin (if (is-eq inputVersion u5) (begin (contract-call? 'SP32AEEF6WW5Y0NMJ1S8SBSZDAY8R5J32NBZFPKKZ.wrapped-nothing-v5 unwrap amount ) (wrap-mno-to-v8 amount) ) (if (is-eq inputVersion u6) (begin (contract-call? 'SP32AEEF6WW5Y0NMJ1S8SBSZDAY8R5J32NBZFPKKZ.wrapped-nothing-v6 unwrap amount ) (wrap-mno-to-v8 amount) ) (if (is-eq inputVersion u7) (begin (contract-call? 'SP32AEEF6WW5Y0NMJ1S8SBSZDAY8R5J32NBZFPKKZ.wrapped-nothing-v7 unwrap amount ) (wrap-mno-to-v8 amount) ) (err ERR_INPUT_VERSION_NOT_SUPPORTED) ) ) ) ) ) (define-private (wrap-mno-to-v8 (amount uint)) (contract-call? 'SP32AEEF6WW5Y0NMJ1S8SBSZDAY8R5J32NBZFPKKZ.wrapped-nothing-v8 wrap-nthng amount ) )
0x6b045eb86cf4838236aac430b0ba07c7061282b907614c1b512b730a744d75ea
23,491
abort_by_response
vvv
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-read-only (get-round-data (bet-type uint) (round uint)) { summary: (map-get? bet-summary-map { bet-type: bet-type, round: round }), records: (map get-bet-record LIST_400) } ) (define-read-only (get-bet-record (index uint)) (map-get? bet-record-map { bet-type: u1, round: u1, index: index }) ) (define-read-only (get-round-data-v2 (ll (list 400 (list 3 uint)))) (let ( (first-ele (unwrap-panic (element-at ll u0))) ) { summary: (map-get? bet-summary-map { bet-type: (default-to u0 (element-at first-ele u0)), round: (default-to u0 (element-at first-ele u1)) }), records: (map get-bet-record-v2 ll) } ) ) (define-read-only (get-bet-record-v2 (ll (list 3 uint))) (map-get? bet-record-map { bet-type: (default-to u0 (element-at ll u0)), round: (default-to u0 (element-at ll u1)), index: (default-to u0 (element-at ll u2)) }) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (set-record (index uint)) (map-set bet-record-map { bet-type: u1, round: u1, index: index } { player: OWNER, bet-value: index, shares: u0, win-num: 0 } ) ) (map-set bet-summary-map { bet-type: u1, round: u1 } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u2, v-num: u1, s-num: u1, rand-num: u0, total-shares: u0 } ) (map set-record LIST_400) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0x074c10480f869da15a69fcfb1be2426d1243b366629b6097cde06c1bbecf6ae7
23,581
success
sss
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-read-only (get-round-data (bet-type uint) (round uint)) { summary: (map-get? bet-summary-map { bet-type: bet-type, round: round }), records: (map get-bet-record LIST_400) } ) (define-read-only (get-bet-record (index uint)) (map-get? bet-record-map { bet-type: u1, round: u1, index: index }) ) (define-read-only (get-round-data-v2 (ll (list 100 (list 3 uint)))) (map get-bet-record-v2 ll) ) (define-read-only (get-bet-record-v2 (ll (list 3 uint))) (map-get? bet-record-map { bet-type: (unwrap-panic (element-at ll u0)), round: (unwrap-panic (element-at ll u1)), index: (unwrap-panic (element-at ll u2)) }) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (set-record (index uint)) (map-set bet-record-map { bet-type: u1, round: u1, index: index } { player: OWNER, bet-value: index, shares: u0, win-num: 0 } ) ) (map-set bet-summary-map { bet-type: u1, round: u1 } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u2, v-num: u1, s-num: u1, rand-num: u0, total-shares: u0 } ) (map set-record LIST_400) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0x6bf640ffdda3c3de4452db2f9806f062c35a8e7c29bb81ef433d62fae993a613
23,584
success
fpwr-v02
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token wrapped-rewards) (define-constant pool-scriptpubkey 0xa91413effebe0ea4bb45e35694f5a15bb5b96e851afb87) (define-data-var reward-admin principal 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-map rewards-by-tx (buff 1024) uint) (define-map rewards-by-height uint {wrew: uint, ustx: uint}) (define-data-var last-price (tuple (amount uint) (height uint) (timestamp uint)) {amount: u2813, height: u23481, timestamp: u1627462508}) ;; total submitted btc rewards (define-read-only (get-rewards-by-height (height uint)) (ok (default-to {wrew: u0, ustx: u0} (map-get? rewards-by-height height)))) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (ok (ft-get-balance wrapped-rewards owner))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply wrapped-rewards))) ;; returns the token name (define-read-only (get-name) (ok "Friedger Pool wrapped rewards")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FPWR")) ;; the number of decimals used (define-read-only (get-decimals) (ok u8)) ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (is-eq tx-sender sender) (begin (try! (ft-transfer? wrapped-rewards amount sender recipient)) (print memo) (ok true) ) (err u4))) (define-public (get-token-uri) (ok (some u"https://pool.friedger.de/wrapped-rewards.json"))) ;; ;; mint via btc transactions ;; (define-private (map-update-rewards-by-height (height uint) (wrew-value uint) (ustx-value uint)) (let ((rewards (map-get? rewards-by-height height))) (let ((wrew (default-to u0 (get wrew rewards))) (ustx (default-to u0 (get ustx rewards)))) (map-set rewards-by-height height {wrew: (+ wrew-value wrew), ustx: (+ ustx-value ustx)})))) (define-private (add-value (out {scriptPubKey: (buff 128), value: uint}) (result uint)) (+ (get value out) result) ) ;; adds out entry to result list if the scriptPubKey matches, ;; also converts buff value to uint value (define-private (find-outs (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result (list 8 {scriptPubKey: (buff 128), value: uint}))) (if (is-eq (get scriptPubKey entry) pool-scriptpubkey) (let ((value (get uint32 (unwrap-panic (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 read-uint32 {txbuff: (get value entry), index: u0}))))) (unwrap-panic (as-max-len? (append result {scriptPubKey: (get scriptPubKey entry), value: value}) u8)) ) result)) (define-read-only (get-outs (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold find-outs (get outs tx) (list))) (define-read-only (get-tx-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold add-value (get-outs tx) u0)) ;; any user can submit a tx that contains a reward tx (define-public (mint (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (let ((tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (let ((value (get-tx-value tx)) (height (get height block))) (asserts! result ERR_VERIFICATION_FAILED) (asserts! (> value u0) ERR_TX_IGNORED) (asserts! (map-insert rewards-by-tx tx-buff value) ERR_ALREADY_DONE) (let ((ustx (* value (get-price height)))) (asserts! (map-update-rewards-by-height height value ustx) ERR_NATIVE_FAILURE) (match (ft-mint? wrapped-rewards value (var-get reward-admin)) success (ok {value: value, ustx: ustx}) error (err (* error u2000))))) error (err (* error u1000))))) (define-public (update-reward-admin (new-admin principal)) (if (is-eq tx-sender (var-get reward-admin)) (ok (var-set reward-admin new-admin)) (err u403))) ;; price BTC/STX (define-private (oracle-get-price) (contract-call? 'SPZ0RAC1EFTH949T4W2SYY6YBHJRMAF4ECT5A7DD.oracle-v1 get-price "artifix-binance" "STX-BTC") ) (define-private (update (price (tuple (amount uint) (height uint) (timestamp uint))) (height uint)) (if (> (get height (var-get last-price)) height) (var-set last-price price) false)) (define-private (get-price-at-height (height uint)) (match (get-block-info? id-header-hash height) hash (match (at-block hash (oracle-get-price)) price (begin (update price height) (some (get amount price))) none) none)) (define-private (get-price (height uint)) (match (get-price-at-height height) price price (match (get-price-at-height (- height u1)) price-1 price-1 (match (get-price-at-height (- height u2)) price-2 price-2 (get amount (print (var-get last-price))))))) (define-public (get-price-stx-btc (height uint)) (ok (get-price height))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_IGNORED (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_NO_STX_RECEIVER (err u8)) (define-constant ERR_FAILED_TO_GET_PRICE (err u9)) (define-constant ERR_NATIVE_FAILURE (err u99))
0x106b72351229cc8fc8cc60a1ff57f660252060fcbfb041b68016b7b4c0102348
23,676
success
fpwr-v02-depot
(define-constant admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-map balances principal uint) (define-data-var total-rewards uint u0) (define-data-var total-claimed uint u0) (define-read-only (get-balance (user principal)) (ok (map-get? balances user))) (define-read-only (get-depot-info) {tokens: (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v02 get-balance (as-contract tx-sender)), total-rewards: (var-get total-rewards), total-claimed: (var-get total-claimed)}) (define-public (claim) (let ((user tx-sender) (amount (default-to u0 (map-get? balances user)))) (var-set total-claimed (+ amount (var-get total-claimed))) (if (> amount u0) (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v02 transfer amount tx-sender user none) (err u1)))) (define-private (add-reward (reward {user: principal, amount: uint})) (let ((amount (default-to u0 (map-get? balances (get user reward))))) (var-set total-rewards (+ amount (var-get total-rewards))) (map-set balances (get user reward) (+ amount (get amount reward))))) (define-public (add-rewards (rewards (list 200 {user: principal, amount: uint}))) (if (is-eq tx-sender admin) (ok (map add-reward rewards)) (err u403)))
0xe272e27f88b93cd9515c267a69bb42603887be84c4fa8292a9b0b66933c7b7f4
23,683
success
fpwr-v02-depot
(define-constant admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-map balances principal uint) (define-data-var total-rewards uint u0) (define-data-var total-claimed uint u0) (define-read-only (get-balance (user principal)) (ok (map-get? balances user))) (define-read-only (get-depot-info) {tokens: (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v02 get-balance (as-contract tx-sender)), total-rewards: (var-get total-rewards), total-claimed: (var-get total-claimed)}) (define-public (claim) (let ((user tx-sender) (amount (default-to u0 (map-get? balances user)))) (var-set total-claimed (+ amount (var-get total-claimed))) (if (> amount u0) (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v02 transfer amount tx-sender user none) (err u1)))) (define-private (add-reward (reward {user: principal, amount: uint})) (let ((amount (default-to u0 (map-get? balances (get user reward))))) (var-set total-rewards (+ amount (var-get total-rewards))) (map-set balances (get user reward) (+ amount (get amount reward))))) (define-public (add-rewards (rewards (list 200 {user: principal, amount: uint}))) (if (is-eq tx-sender admin) (ok (map add-reward rewards)) (err u403)))
0xe272e27f88b93cd9515c267a69bb42603887be84c4fa8292a9b0b66933c7b7f4
23,683
success
cc-sip010-stx
;; cc-sip010-stx ;; wrap the native STX token into an SRC20 compatible token to be usable along other tokens ;; Use https://explorer.stacks.co/txid/SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait (impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait.sip-010-trait) ;; constants (define-constant ERR_INSUFFICIENT_FUNDS u1) (define-constant ERR_PERMISSION_DENIED u9) ;; get the token balance of owner (define-read-only (balance-of (owner principal)) (begin (ok (print (stx-get-balance owner))) ) ) (define-read-only (total-supply) (ok stx-liquid-supply) ) ;; returns the token name (define-read-only (name) (ok "Stacks") ) (define-read-only (symbol) (ok "STX") ) ;; the number of decimals used (define-read-only (decimals) (ok u6) ) (define-read-only (get-token-uri) (ok (some u"https://swapr.finance/tokens/stx.json")) ) ;; { ;; "name":"STX", ;; "description":"STX token, as a SIP-010 compatible token", ;; "image":"https://swapr.finance/tokens/stx.png", ;; "vector":"https://swapr.finance/tokens/stx.svg" ;; } ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (>= (stx-get-balance sender) amount) (begin (asserts! (is-eq tx-sender sender) (err ERR_PERMISSION_DENIED)) (if (is-some memo) (print memo) none ) (stx-transfer? amount sender recipient)) (err ERR_INSUFFICIENT_FUNDS)) ) ;; ;; all the data relevant to the stx token owned (define-read-only (get-data (owner principal)) (ok { name: (unwrap-panic (name)), symbol: (unwrap-panic (symbol)), decimals: (unwrap-panic (decimals)), uri: (unwrap-panic (get-token-uri)), supply: (unwrap-panic (total-supply)), balance: (unwrap-panic (balance-of owner)) }) )
0x7549078a37a3bedf7cefa7043475e13f3eeac6916c409f72d686e2cc02ceb054
23,697
success
aaa
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map bet-roundrecords-map { bet-type: uint, round: uint } (list 400 (optional { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } )) ) (define-data-var combine-bet-type uint u1) (define-data-var combine-round uint u1) (define-read-only (get-bet-record (index uint)) (map-get? bet-record-map { bet-type: u1, round: u1, index: index }) ) (define-read-only (get-round-data-v2 (ll (list 100 (list 3 uint)))) (map get-bet-record-v2 ll) ) (define-read-only (get-bet-record-v2 (ll (list 3 uint))) (map-get? bet-record-map { bet-type: (unwrap-panic (element-at ll u0)), round: (unwrap-panic (element-at ll u1)), index: (unwrap-panic (element-at ll u2)) }) ) (define-public (combine-round-records (bet-type uint) (round uint)) (begin (var-set combine-bet-type bet-type) (var-set combine-round round) (map-set bet-roundrecords-map { bet-type: bet-type, round: round } (map get-round-record LIST_400)) (map del-round-record LIST_400) (ok true) ) ) (define-read-only (get-round-records (bet-type uint) (round uint)) (map-get? bet-roundrecords-map { bet-type: bet-type, round: round }) ) (define-private (get-round-record (index uint)) (map-get? bet-record-map { bet-type: (var-get combine-bet-type), round: (var-get combine-round), index: index }) ) (define-private (del-round-record (index uint)) (map-delete bet-record-map { bet-type: (var-get combine-bet-type), round: (var-get combine-round), index: index }) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (set-record (index uint)) (map-set bet-record-map { bet-type: u1, round: u1, index: index } { player: OWNER, bet-value: index, shares: u0, win-num: 0 } ) ) (map-set bet-summary-map { bet-type: u1, round: u1 } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u2, v-num: u1, s-num: u1, rand-num: u0, total-shares: u0 } ) (map set-record LIST_400) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0x5ee0409c978d8ab1b3511a4724ff1289c2ee6435b6f62b3172593dc3ccf94578
23,725
success
ccc
(define-constant OWNER tx-sender) (define-constant LIST_300 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly p-num: uint, ;; p number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-s: uint ;; total s of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { p: principal, b: uint, ;; 100*v+s s: uint, w: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map bet-roundrecords-map { bet-type: uint, round: uint } (list 300 (optional { p: principal, b: uint, ;; 100*v+s s: uint, w: int ;; 0 means not draw yet, >0 means win, <0 means lose } )) ) (define-data-var combine-bet-type uint u1) (define-data-var combine-round uint u1) (define-read-only (get-bet-record (index uint)) (map-get? bet-record-map { bet-type: u1, round: u1, index: index }) ) (define-read-only (get-round-data-v2 (ll (list 100 (list 3 uint)))) (map get-bet-record-v2 ll) ) (define-read-only (get-bet-record-v2 (ll (list 3 uint))) (map-get? bet-record-map { bet-type: (unwrap-panic (element-at ll u0)), round: (unwrap-panic (element-at ll u1)), index: (unwrap-panic (element-at ll u2)) }) ) (define-public (combine-round-records (bet-type uint) (round uint)) (begin (var-set combine-bet-type bet-type) (var-set combine-round round) (map-set bet-roundrecords-map { bet-type: bet-type, round: round } (map get-round-record LIST_300)) (map del-round-record LIST_300) (ok true) ) ) (define-read-only (get-round-records (bet-type uint) (round uint)) (map-get? bet-roundrecords-map { bet-type: bet-type, round: round }) ) (define-private (get-round-record (index uint)) (map-get? bet-record-map { bet-type: (var-get combine-bet-type), round: (var-get combine-round), index: index }) ) (define-private (del-round-record (index uint)) (map-delete bet-record-map { bet-type: (var-get combine-bet-type), round: (var-get combine-round), index: index }) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (set-record (index uint)) (map-set bet-record-map { bet-type: u1, round: u1, index: index } { p: OWNER, b: index, s: u0, w: 0 } ) ) (map-set bet-summary-map { bet-type: u1, round: u1 } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, p-num: u2, v-num: u1, s-num: u1, rand-num: u0, total-s: u0 } ) (map set-record LIST_300) (combine-round-records u1 u1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0xd9c6a8be3968a37aac0eb15a71c775c06edae8c93350bae889d000edaa43c091
23,730
success
eee
(define-constant OWNER tx-sender) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly p-num: uint, ;; p number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-s: uint ;; total s of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { p: principal, b: uint, ;; 100*v+s s: uint, w: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map bet-roundrecords-map { bet-type: uint, round: uint } (list 360 (optional { p: principal, b: uint, ;; 100*v+s s: uint, w: int ;; 0 means not draw yet, >0 means win, <0 means lose } )) ) (define-data-var combine-bet-type uint u1) (define-data-var combine-round uint u1) (define-read-only (get-bet-record (index uint)) (map-get? bet-record-map { bet-type: u1, round: u1, index: index }) ) (define-read-only (get-round-data-v2 (ll (list 100 (list 3 uint)))) (map get-bet-record-v2 ll) ) (define-read-only (get-bet-record-v2 (ll (list 3 uint))) (map-get? bet-record-map { bet-type: (unwrap-panic (element-at ll u0)), round: (unwrap-panic (element-at ll u1)), index: (unwrap-panic (element-at ll u2)) }) ) (define-public (combine-round-records (bet-type uint) (round uint)) (begin (var-set combine-bet-type bet-type) (var-set combine-round round) (map-set bet-roundrecords-map { bet-type: bet-type, round: round } (map get-round-record LIST_360)) (map del-round-record LIST_360) (ok true) ) ) (define-read-only (get-round-records (bet-type uint) (round uint)) (map-get? bet-roundrecords-map { bet-type: bet-type, round: round }) ) (define-private (get-round-record (index uint)) (map-get? bet-record-map { bet-type: (var-get combine-bet-type), round: (var-get combine-round), index: index }) ) (define-private (del-round-record (index uint)) (map-delete bet-record-map { bet-type: (var-get combine-bet-type), round: (var-get combine-round), index: index }) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (set-record (index uint)) (map-set bet-record-map { bet-type: u1, round: u1, index: index } { p: OWNER, b: index, s: u0, w: 0 } ) ) (map-set bet-summary-map { bet-type: u1, round: u1 } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, p-num: u2, v-num: u1, s-num: u1, rand-num: u0, total-s: u0 } ) (map set-record LIST_360) (combine-round-records u1 u1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0xe50f558935d840dae2c1be8d01424bfd06ded50d6af8a7e573bb3ee940ed8c66
23,736
success
fff
(define-constant OWNER tx-sender) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map bet-roundrecords-map { bet-type: uint, round: uint } (list 360 (optional { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } )) ) (define-data-var combine-bet-type uint u1) (define-data-var combine-round uint u1) (define-read-only (get-bet-record (index uint)) (map-get? bet-record-map { bet-type: u1, round: u1, index: index }) ) (define-read-only (get-round-data-v2 (ll (list 100 (list 3 uint)))) (map get-bet-record-v2 ll) ) (define-read-only (get-bet-record-v2 (ll (list 3 uint))) (map-get? bet-record-map { bet-type: (unwrap-panic (element-at ll u0)), round: (unwrap-panic (element-at ll u1)), index: (unwrap-panic (element-at ll u2)) }) ) (define-public (combine-round-records (bet-type uint) (round uint)) (begin (var-set combine-bet-type bet-type) (var-set combine-round round) (map-set bet-roundrecords-map { bet-type: bet-type, round: round } (map get-round-record LIST_360)) (map del-round-record LIST_360) (ok true) ) ) (define-read-only (get-round-records (bet-type uint) (round uint)) (map-get? bet-roundrecords-map { bet-type: bet-type, round: round }) ) (define-private (get-round-record (index uint)) (map-get? bet-record-map { bet-type: (var-get combine-bet-type), round: (var-get combine-round), index: index }) ) (define-private (del-round-record (index uint)) (map-delete bet-record-map { bet-type: (var-get combine-bet-type), round: (var-get combine-round), index: index }) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-private (set-record (index uint)) (map-set bet-record-map { bet-type: u1, round: u1, index: index } { player: OWNER, bet-value: index, shares: u0, win-num: 0 } ) ) (map-set bet-summary-map { bet-type: u1, round: u1 } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u2, v-num: u1, s-num: u1, rand-num: u0, total-shares: u0 } ) (map set-record LIST_360) (combine-round-records u1 u1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0x59a0bcab2e16b279303d04faf6256431898a4d5f479d999648149751f0cb1d9f
23,736
success
fpwr-v03
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token wrapped-rewards) (define-constant pool-scriptpubkey 0xa91413effebe0ea4bb45e35694f5a15bb5b96e851afb87) (define-data-var reward-admin principal 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-map rewards-by-tx (buff 1024) uint) (define-map rewards-by-height uint {wrew: uint, ustx: uint}) (define-data-var last-price (tuple (amount uint) (height uint) (timestamp uint)) {amount: u2813, height: u23481, timestamp: u1627462508}) ;; total submitted btc rewards (define-read-only (get-rewards-by-height (height uint)) (ok (default-to {wrew: u0, ustx: u0} (map-get? rewards-by-height height)))) ;; get the token balance of owner (define-read-only (get-balance (owner principal)) (ok (ft-get-balance wrapped-rewards owner))) ;; returns the total number of tokens (define-read-only (get-total-supply) (ok (ft-get-supply wrapped-rewards))) ;; returns the token name (define-read-only (get-name) (ok "Friedger Pool wrapped rewards")) ;; the symbol or "ticker" for this token (define-read-only (get-symbol) (ok "FPWR")) ;; the number of decimals used (define-read-only (get-decimals) (ok u8)) ;; Transfers tokens to a recipient (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (if (is-eq tx-sender sender) (begin (try! (ft-transfer? wrapped-rewards amount sender recipient)) (print memo) (ok true) ) (err u4))) (define-public (get-token-uri) (ok (some u"https://pool.friedger.de/wrapped-rewards.json"))) ;; ;; mint via btc transactions ;; (define-private (map-update-rewards-by-height (height uint) (wrew-value uint) (ustx-value uint)) (let ((rewards (map-get? rewards-by-height height))) (let ((wrew (default-to u0 (get wrew rewards))) (ustx (default-to u0 (get ustx rewards)))) (map-set rewards-by-height height {wrew: (+ wrew-value wrew), ustx: (+ ustx-value ustx)})))) (define-private (add-value (out {scriptPubKey: (buff 128), value: uint}) (result uint)) (+ (get value out) result) ) ;; adds out entry to result list if the scriptPubKey matches, ;; also converts buff value to uint value (define-private (find-outs (entry {scriptPubKey: (buff 128), value: (buff 8)}) (result (list 8 {scriptPubKey: (buff 128), value: uint}))) (if (is-eq (get scriptPubKey entry) pool-scriptpubkey) (let ((value (get uint32 (unwrap-panic (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 read-uint32 {txbuff: (get value entry), index: u0}))))) (unwrap-panic (as-max-len? (append result {scriptPubKey: (get scriptPubKey entry), value: value}) u8)) ) result)) (define-read-only (get-outs (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold find-outs (get outs tx) (list))) (define-read-only (get-tx-value (tx { version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)})) (fold add-value (get-outs tx) u0)) ;; any user can submit a tx that contains a reward tx (define-public (mint (block { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4), height: uint }) (tx {version: (buff 4), ins: (list 8 {outpoint: {hash: (buff 32), index: (buff 4)}, scriptSig: (buff 256), sequence: (buff 4)}), outs: (list 8 {value: (buff 8), scriptPubKey: (buff 128)}), locktime: (buff 4)}) (proof { tx-index: uint, hashes: (list 12 (buff 32)), tree-depth: uint })) (let ((tx-buff (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 concat-tx tx))) (match (contract-call? 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.clarity-bitcoin-lib-v1 was-tx-mined block tx-buff proof) result (let ((value (get-tx-value tx)) (height (get height block))) (asserts! result ERR_VERIFICATION_FAILED) (asserts! (> value u0) ERR_TX_IGNORED) (asserts! (map-insert rewards-by-tx tx-buff value) ERR_ALREADY_DONE) (let ((ustx (/ (* value u1000000) (get-price height)))) (asserts! (map-update-rewards-by-height height value ustx) ERR_NATIVE_FAILURE) (match (ft-mint? wrapped-rewards value (var-get reward-admin)) success (ok {value: value, ustx: ustx}) error (err (* error u2000))))) error (err (* error u1000))))) (define-public (update-reward-admin (new-admin principal)) (if (is-eq tx-sender (var-get reward-admin)) (ok (var-set reward-admin new-admin)) (err u403))) ;; price BTC/STX (define-private (oracle-get-price) (contract-call? 'SPZ0RAC1EFTH949T4W2SYY6YBHJRMAF4ECT5A7DD.oracle-v1 get-price "artifix-binance" "STX-BTC") ) (define-private (update (price (tuple (amount uint) (height uint) (timestamp uint))) (height uint)) (if (> height (get height (var-get last-price))) (var-set last-price price) false)) (define-private (get-price-at-height (height uint)) (match (get-block-info? id-header-hash height) hash (match (at-block hash (oracle-get-price)) price (begin (update price height) (some (get amount price))) none) none)) (define-private (get-price (height uint)) (match (get-price-at-height height) price price (match (get-price-at-height (- height u1)) price-1 price-1 (match (get-price-at-height (- height u2)) price-2 price-2 (get amount (print (var-get last-price))))))) (define-public (get-price-stx-btc (height uint)) (ok (get-price height))) (define-constant ERR_VERIFICATION_FAILED (err u1)) (define-constant ERR_FAILED_TO_PARSE_TX (err u2)) (define-constant ERR_INVALID_ID (err u3)) (define-constant ERR_TOO_EARLY (err u4)) (define-constant ERR_TX_VALUE_TOO_SMALL (err u5)) (define-constant ERR_TX_IGNORED (err u6)) (define-constant ERR_ALREADY_DONE (err u7)) (define-constant ERR_NO_STX_RECEIVER (err u8)) (define-constant ERR_FAILED_TO_GET_PRICE (err u9)) (define-constant ERR_NATIVE_FAILURE (err u99))
0x292fa88dca86a5b0e8b3bd61bc1170177e133f39ccfc0e5c0534c8b2a489b1fb
23,792
success
fpwr-v03-depot
(define-constant admin 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60) (define-map balances principal uint) (define-data-var total-rewards uint u0) (define-data-var total-claimed uint u0) (define-read-only (get-balance (user principal)) (ok (map-get? balances user))) (define-read-only (get-depot-info) {tokens: (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v03 get-balance (as-contract tx-sender)), total-rewards: (var-get total-rewards), total-claimed: (var-get total-claimed)}) (define-public (claim) (let ((user tx-sender) (amount (default-to u0 (map-get? balances user)))) (var-set total-claimed (+ amount (var-get total-claimed))) (if (> amount u0) (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v03 transfer amount tx-sender user none) (err u1)))) (define-private (add-reward (reward {user: principal, amount: uint})) (let ((amount (default-to u0 (map-get? balances (get user reward))))) (var-set total-rewards (+ amount (var-get total-rewards))) (map-set balances (get user reward) (+ amount (get amount reward))))) (define-public (add-rewards (rewards (list 200 {user: principal, amount: uint}))) (if (is-eq tx-sender admin) (ok (map add-reward rewards)) (err u403))) (define-public (update-reward-admin (new-admin principal)) (if (is-eq tx-sender admin) (contract-call? 'SP1JSH2FPE8BWNTP228YZ1AZZ0HE0064PS6RXRAY4.fpwr-v03 update-reward-admin new-admin) (err u403)))
0x87e1e3591d92f5cdfa98446cd5762f0c464c3d6efcee755b9bae3482d7dc253e
23,804
success
ssss
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 400 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-public (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0x6696b9820de8509a183924f5c4c947e45b2b89168046f92fbd6bc8bd0451dea5
23,854
success
vst
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 400 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (if (<= index u300) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) none ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0x3b86166671847ed512b004f6f20e3ad55fd0526b9d25ee84d255aa0ceac66b4f
23,856
success
zzz
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 400 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0xc049eec06eaf6fe1becc37c90fe83691a8bfa8bdafc3084e51390d567dcafd43
23,856
success
zzzzz
(define-constant OWNER tx-sender) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 360 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0x4b3a2fecff8c63e930b781e2c0abfeeecb921026eb8f4957273810d465198a9a
23,856
success
vst
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 400 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (if (<= index u300) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) none ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0x3b86166671847ed512b004f6f20e3ad55fd0526b9d25ee84d255aa0ceac66b4f
23,856
success
zzz
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 400 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0xc049eec06eaf6fe1becc37c90fe83691a8bfa8bdafc3084e51390d567dcafd43
23,856
success
zzzzz
(define-constant OWNER tx-sender) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 360 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0x4b3a2fecff8c63e930b781e2c0abfeeecb921026eb8f4957273810d465198a9a
23,856
success
vst
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 400 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (if (<= index u300) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) none ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0x3b86166671847ed512b004f6f20e3ad55fd0526b9d25ee84d255aa0ceac66b4f
23,856
success
zzz
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 400 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0xc049eec06eaf6fe1becc37c90fe83691a8bfa8bdafc3084e51390d567dcafd43
23,856
success
zzzzz
(define-constant OWNER tx-sender) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 360 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (some { p: OWNER, v: (+ 10000000000000000000000 (to-int index)) }) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0x4b3a2fecff8c63e930b781e2c0abfeeecb921026eb8f4957273810d465198a9a
23,856
success
eee
(define-constant OWNER tx-sender) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) (define-map bet-record-compress-map { bet-type: uint, round: uint} (list 400 { p: principal, v: int }) ) (define-private (gen-record (index uint)) (some { p: OWNER, v: (* -1 (+ 10000000000000000000000 (to-int index))) }) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (let ( (record-opt-list (map gen-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-record-compress-map { bet-type: u1, round: u1 } record-list) ) (define-read-only (get-mapp) (ok (map-get? bet-record-compress-map { bet-type: u1, round: u1 })) )
0x9d20526169a36fb4480419ebb1342d11ffa62012e87b10b1d4db539414428ca6
23,860
success
vvvsss
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS u400) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant ONE_ROUND_DURATION_BLOCKS u1) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u48) ;;TODO u48 ;; 8 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 400 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 100*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-mshare uint u0) ;; award per micro share ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_PLAYERS) (<= s-num MAX_PLAYERS)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (and (>= bet-v u1) (<= bet-v u50))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (and (>= bet-s u51) (<= bet-s u100))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u100 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has draw): ;; if last round has not been compressed, compress it ;; else ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (> (get rand-num last-bet-summary-tuple) u0) (match (map-get? bet-records-compress-map { bet-type: bet-type, round: last-round }) com-records true ;; already compressed (compress-records bet-type last-round) ) ;;; (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print "draw") ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per micro share (var-set draw-award-per-mshare (/ (* (var-get draw-award-pool-num) SHARES_PER_MSTX) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u100)) (bet-s (mod bet-value u100)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (> bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (> bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (/ (* (get shares bet-record-tuple) (var-get draw-award-per-mshare)) SHARES_PER_MSTX)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u100) (> (mod bet-value u100) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (> bet-value u100)) (bet-s-flag (> (mod bet-value u100) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_400) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get start-time bet-summary))) (real-start-time (if (> start-time u0) start-time (default-to u0 (get-block-info? time start-at)))) ) (ok { round: round, bet-summary: bet-summary, start-time: real-start-time }) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (map-get? bet-summary-map { bet-type: bet-type, round: round }) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-bet-info (bet-type uint) (player principal) (index uint)) (map-get? player-record-map { player: player, bet-type: bet-type, index: index }) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) (define-read-only (get-bet-record2 (bet-type uint) (round uint) (index uint)) (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) ) (define-read-only (get-balance) (stx-get-balance (as-contract tx-sender)) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0xfbc282f6505dbc2cb73da9a831a9b8a3ba1cb1d5ac14926df4ed777978d91ceb
23,875
success
vvss
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS u400) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant ONE_ROUND_DURATION_BLOCKS u24) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u3) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;;TODO u48 ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 100*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 400 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 100*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-mshare uint u0) ;; award per micro share ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_PLAYERS) (<= s-num MAX_PLAYERS)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (and (>= bet-v u1) (<= bet-v u50))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (and (>= bet-s u51) (<= bet-s u100))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u100 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has draw): ;; if last round has not been compressed, compress it ;; else ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (> (get rand-num last-bet-summary-tuple) u0) (match (map-get? bet-records-compress-map { bet-type: bet-type, round: last-round }) com-records true ;; already compressed (compress-records bet-type last-round) ) ;;; (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print "draw") ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per micro share (var-set draw-award-per-mshare (/ (* (var-get draw-award-pool-num) SHARES_PER_MSTX) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u100)) (bet-s (mod bet-value u100)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (> bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (> bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (/ (* (get shares bet-record-tuple) (var-get draw-award-per-mshare)) SHARES_PER_MSTX)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u100) (> (mod bet-value u100) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (> bet-value u100)) (bet-s-flag (> (mod bet-value u100) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_400) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get start-time bet-summary))) (real-start-time (if (> start-time u0) start-time (default-to u0 (get-block-info? time start-at)))) ) (ok { round: round, bet-summary: bet-summary, start-time: real-start-time }) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (map-get? bet-summary-map { bet-type: bet-type, round: round }) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-bet-info (bet-type uint) (player principal) (index uint)) (map-get? player-record-map { player: player, bet-type: bet-type, index: index }) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) (define-read-only (get-bet-record2 (bet-type uint) (round uint) (index uint)) (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) ) (define-read-only (get-balance) (stx-get-balance (as-contract tx-sender)) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0xd2641838a8d3a810dfb2864f3a289446ccc3640847ac3a315ef469370d792fbb
23,876
success
ddddeeee
;;TODO last time test real (multiple) (define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS u360) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant ONE_ROUND_DURATION_BLOCKS u24) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u3) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;;TODO u48 ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u1000000 u2000000 u3000000)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-mshare uint u0) ;; award per micro share ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_PLAYERS) (<= s-num MAX_PLAYERS)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has draw): ;; if last round has not been compressed, compress it ;; else ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (> (get rand-num last-bet-summary-tuple) u0) (match (map-get? bet-records-compress-map { bet-type: bet-type, round: last-round }) com-records true ;; already compressed (compress-records bet-type last-round) ) ;;; (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print "draw") ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; calc each player's shares (filter calc-shares-loop LIST_360) ;; calc each award per micro share (var-set draw-award-per-mshare (/ (* (var-get draw-award-pool-num) SHARES_PER_MSTX) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (/ (* (get shares bet-record-tuple) (var-get draw-award-per-mshare)) SHARES_PER_MSTX)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get start-time bet-summary))) (real-start-time (if (> start-time u0) start-time (default-to u0 (get-block-info? time start-at)))) ) (ok { round: round, bet-summary: bet-summary, start-time: real-start-time }) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (map-get? bet-summary-map { bet-type: bet-type, round: round }) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x3053e4aebe34f4ae786246f8fe0e24c826ebff2aed96d88fc40cc1821c25e560
23,937
success
enddd
;;TODO last time test real (multiple) (define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS u400) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant ONE_ROUND_DURATION_BLOCKS u24) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u3) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;;TODO u48 ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 400 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u1000000 u2000000 u3000000)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-mshare uint u0) ;; award per micro share ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_PLAYERS) (<= s-num MAX_PLAYERS)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has draw): ;; if last round has not been compressed, compress it ;; else ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (> (get rand-num last-bet-summary-tuple) u0) (match (map-get? bet-records-compress-map { bet-type: bet-type, round: last-round }) com-records true ;; already compressed (compress-records bet-type last-round) ) ;;; (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print "draw") ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per micro share (var-set draw-award-per-mshare (/ (* (var-get draw-award-pool-num) SHARES_PER_MSTX) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (/ (* (get shares bet-record-tuple) (var-get draw-award-per-mshare)) SHARES_PER_MSTX)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_400) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get start-time bet-summary))) (real-start-time (if (> start-time u0) start-time (default-to u0 (get-block-info? time start-at)))) ) (ok { round: round, bet-summary: bet-summary, start-time: real-start-time }) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (map-get? bet-summary-map { bet-type: bet-type, round: round }) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x046bdcf8dc1990f8139a4f74f5e3cb2f9e328dbc425a2a2dd75e9f7b621c2a3f
23,937
success
theend
;;TODO last time test real (multiple) (define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS u400) (define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10 (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant ONE_ROUND_DURATION_BLOCKS u2) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;;TODO u48 ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 400 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u1000000 u2000000 u3000000)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-mshare uint u0) ;; award per micro share ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_PLAYERS) (<= s-num MAX_PLAYERS)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has draw): ;; if last round has not been compressed, compress it ;; else ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (> (get rand-num last-bet-summary-tuple) u0) (match (map-get? bet-records-compress-map { bet-type: bet-type, round: last-round }) com-records true ;; already compressed (compress-records bet-type last-round) ) ;;; (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print "draw") ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per micro share (var-set draw-award-per-mshare (/ (* (var-get draw-award-pool-num) SHARES_PER_MSTX) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (/ (* (get shares bet-record-tuple) (var-get draw-award-per-mshare)) SHARES_PER_MSTX)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_400) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get start-time bet-summary))) (real-start-time (if (> start-time u0) start-time (default-to u0 (get-block-info? time start-at)))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get end-time bet-summary))) (real-end-time (if (> end-time u0) end-time (default-to u0 (get-block-info? time end-at)))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: real-start-time, end-time: real-end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x69b1b4d0abf51a1bd0c16467ae0eb5ca0439479adb575f3dd98ef1f60795137e
23,983
success
soend
;;TODO last time test real (multiple) (define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS u400) (define-constant DRAW_BET_NUM_THRESHOLD u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant ONE_ROUND_DURATION_BLOCKS u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;;TODO u48 ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_400 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 400 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-mshare uint u0) ;; award per micro share ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_PLAYERS) (<= s-num MAX_PLAYERS)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has draw): ;; if last round has not been compressed, compress it ;; else ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (> (get rand-num last-bet-summary-tuple) u0) (match (map-get? bet-records-compress-map { bet-type: bet-type, round: last-round }) com-records true ;; already compressed (compress-records bet-type last-round) ) ;;; (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print "draw") ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; calc each player's shares (filter calc-shares-loop LIST_400) ;; calc each award per micro share (var-set draw-award-per-mshare (/ (* (var-get draw-award-pool-num) SHARES_PER_MSTX) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_400) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (/ (* (get shares bet-record-tuple) (var-get draw-award-per-mshare)) SHARES_PER_MSTX)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_400) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_400)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_400) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get start-time bet-summary))) (real-start-time (if (> start-time u0) start-time (default-to u0 (get-block-info? time start-at)))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get end-time bet-summary))) (real-end-time (if (> end-time u0) end-time (default-to u0 (get-block-info? time end-at)))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: real-start-time, end-time: real-end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x36c65ca142a8f29efee08c4c0c5b22c3be1d84f741cd633683d42da49dd9492b
23,993
success
endddd
;;TODO last time test real (multiple) (define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS u360) (define-constant DRAW_BET_NUM_THRESHOLD u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant ONE_ROUND_DURATION_BLOCKS u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;;TODO u48 ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block start-time: uint, ;; round start time-stamp roughly end-at: uint, ;; round end block end-time: uint, ;; round end time-stamp roughly player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-mshare uint u0) ;; award per micro share ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq (get start-at bet-summary-tuple) u0)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (and (<= v-num MAX_PLAYERS) (<= s-num MAX_PLAYERS)) (err ERR_EXCEED_MAX_PLAYERS_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) (start-time (if (is-eq (get start-time bet-summary-tuple) u0) (default-to u0 (get-block-info? time start-at)) (get start-time bet-summary-tuple))) (player-num (+ (get player-num bet-summary-tuple) u1)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, start-time: start-time, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if (last-round has draw): ;; if last round has not been compressed, compress it ;; else ;; if end-time hasn't be marked, try mark it. ;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round) (let ( (last-round (- cur-round u1)) (last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round })) ) (match last-bet-summary-tuple-opt last-bet-summary-tuple (if (> (get rand-num last-bet-summary-tuple) u0) (match (map-get? bet-records-compress-map { bet-type: bet-type, round: last-round }) com-records true ;; already compressed (compress-records bet-type last-round) ) ;;; (begin ;; try mark end-time (if (and (is-eq (get end-time last-bet-summary-tuple) u0) (> block-height (get end-at last-bet-summary-tuple)) ) (map-set bet-summary-map { bet-type: bet-type, round: last-round } (merge last-bet-summary-tuple { end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple))) }) ) true ) ;; try draw (if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS)) (draw bet-type last-round) true ) ) ) true ) ) ;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round) (if (and (> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS)) (>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print "draw") ;; (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; calc each player's shares (filter calc-shares-loop LIST_360) ;; calc each award per micro share (var-set draw-award-per-mshare (/ (* (var-get draw-award-pool-num) SHARES_PER_MSTX) (var-get draw-total-shares))) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, start-time: u0, end-at: u0, end-time: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (/ (* (get shares bet-record-tuple) (var-get draw-award-per-mshare)) SHARES_PER_MSTX)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (player-num (get player-num bet-summary-tuple)) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips) }) ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get start-time bet-summary))) (real-start-time (if (> start-time u0) start-time (default-to u0 (get-block-info? time start-at)))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get end-time bet-summary))) (real-end-time (if (> end-time u0) end-time (default-to u0 (get-block-info? time end-at)))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: real-start-time, end-time: real-end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (restart-round bet-type u1) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x10833f81c0e5f98ee4d8149b948771b507cea27f33af370fc76be2cbfec670ea
23,997
success
will
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u3) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u2) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-award-per-mshare uint u0) ;; award per micro share (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) ;; calc each player's shares (filter calc-shares-loop LIST_360) ;; calc each award per micro share (if (> (var-get draw-total-shares) u0) (var-set draw-award-per-mshare (/ (* (var-get draw-award-pool-num) SHARES_PER_MSTX) (var-get draw-total-shares))) (var-set draw-award-per-mshare u0) ) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; remain is the fee (unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (/ (* (get shares bet-record-tuple) (var-get draw-award-per-mshare)) SHARES_PER_MSTX)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-award-pool-num (- (var-get draw-award-pool-num) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)), } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x9ce7ea4d1fd6388179c22260159e0ccf4f50c39bfe45da32be2488060b15b0e0
24,024
success
willend
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)), } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (if (> block-height start-at) (default-to u0 (get-block-info? time start-at)) u0)) (end-at (default-to u0 (get end-at bet-summary))) (end-time (if (> block-height end-at) (default-to u0 (get-block-info? time end-at)) u0)) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x94eb76b21e0970929cf7df5c1363b7cf6e5074b9b34d879c0900a37e39324dfa
24,063
success
willend2
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)), } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x5eb0f393e375755eda8f742e2238ed077afb75df557f768ba4636fc9d095242d
24,066
success
testupdate
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u500) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)), } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x8b14bf4473030ae3abcf6429c67583812ebe6404f3679923b232828eb906629c
24,147
success
vs
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u6) (define-constant DRAW_CD_BLOCKS u1) (define-constant CANCEL_CD_BLOCKS u72) (define-constant UPDATE_PRICE_COST u5000000) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u500000 u5000000 u50000000)) (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)), } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x778d7a7c07cfa351ab299088542d194a9b5a1508a4f08a3008ea546e0e99ca2a
24,161
success
soend3
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u500) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { ;; will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)), } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0xe28daf53d1b84318731d789fa515264229d2781f7d664152c4f9356e89b9fc33
24,274
success
soend4
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u500) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-data-var price-list (list 3 uint) (list u100 u200 u300)) ;;TODO: (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1)))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (in-price-list (list 3 uint))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (let ( (cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST)) ) (var-set price-list in-price-list) (ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST))) ) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), price-list: (var-get price-list), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { ;; will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)), } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x44587ebf2acebcdcdc31f6603a915522865eddccb08c7774273b67cfbf16ae0c
24,278
success
soend5
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u500) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map price-map uint ;; bet-type uint ;; mstx ) (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (map-get? price-map bet-type)) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (bet-type uint) (price uint)) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (force-cancel-bet-type bet-type) (map-set price-map bet-type price) (ok true) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { price: (unwrap-panic (map-get? price-map bet-type)), ;; will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)) } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x1b798e22326b359d6cb4fe3b9e31fc27a557a12cd4ca91d2840d07324edc3a0f
24,280
success
soend6
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u500) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map price-map uint ;; bet-type uint ;; mstx ) (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (map-get? price-map bet-type)) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (bet-type uint) (price uint)) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (force-cancel-bet-type bet-type) (map-set price-map bet-type price) (ok true) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { price: (unwrap-panic (map-get? price-map bet-type)), ;; will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)) } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) (map-set price-map bet-type (* u100 bet-type)) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x9a6913409f061323e8fc08af984bddcf0ed4cff5e658182e61c55e5f42263f41
24,285
success
soend7
(define-constant ERR_BET_TYPE 1001) (define-constant ERR_ROUND_NOT_RIGHT 1002) (define-constant ERR_INVALID_COST 1003) (define-constant ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT 1004) (define-constant ERR_HAS_BET_CURRENT_ROUND 1005) (define-constant ERR_INVALID_BET_VALUE 1006) (define-constant ERR_BALANCE_UNENOUGH 1007) (define-constant ERR_TRANSFER_STX_ERR 1008) (define-constant ERR_ROUND_NOT_FOUND 1009) (define-constant ERR_ROUND_NOT_CURRENT 1010) (define-constant ERR_CANCAL_IN_CD 1011) (define-constant ERR_NO_BET 1012) (define-constant ERR_ALREADY_DRAW 1013) (define-constant ERR_NO_AUTHORITY 1014) ;; (define-constant OWNER tx-sender) (define-constant BET_TYPE_LIST (list u1 u2 u3)) (define-constant MAX_PLAYERS_PER_ROUND u360) (define-constant MIN_BET_COUNT_PER_ROUND u10) (define-constant HIT_SHARES u1000000) (define-constant SHARES_PER_MSTX u1000) (define-constant MAX_BLOCKS_PER_ROUND u4) ;;TODO u36 6 hours (define-constant DRAW_CD_BLOCKS u1) ;; 0.5 hour (define-constant CANCEL_CD_BLOCKS u72) ;; 12 hours (define-constant UPDATE_PRICE_COST u500) (define-constant MAPPING_21_30_LIST (list u1 u1 u2 u3 u4 u47 u48 u49 u50 u50)) (define-constant MAPPING_71_80_LIST (list u51 u51 u52 u53 u54 u97 u98 u99 u100 u100)) (define-constant BUFF_TO_BYTE (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff )) (define-constant LIST_360 (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map bet-round-map uint ;; bet-type uint ;; round ) (define-map bet-summary-map { bet-type: uint, round: uint } { start-at: uint, ;; round start block end-at: uint, ;; round end block player-num: uint, ;; player number cur round v-num: uint, ;; bet v times (v means left side [1-50]) s-num: uint, ;; bet s times (s means right side [51-100]) rand-num: uint, ;; the result random number total-shares: uint ;; total shares of the winners } ) (define-map bet-record-map { bet-type: uint, round: uint, index: uint } ;; index start from 1 { player: principal, bet-value: uint, ;; 101*v+s shares: uint, win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) ;; compress so that per round data can be fetched by single function call (define-map bet-records-compress-map { bet-type: uint, round: uint } (list 360 { p: principal, v: int }) ;; (bet-value + shares*10000 + |win-num|*10000000000) * (win-num>0 ? 1 : -1) ) (define-map player-times-map { player: principal, bet-type: uint } uint ;; times ) (define-map player-record-map { player: principal, bet-type: uint, index: uint } ;; index start from 1 { round: uint, bet-value: uint, ;; 101*v+s win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose } ) (define-map price-map uint ;; bet-type uint ;; mstx ) (define-data-var tips (string-utf8 128) u"I WISH YOU WIN!") ;; draw logic related (define-data-var draw-bet-type uint u0) (define-data-var draw-round uint u0) (define-data-var draw-bet-price uint u0) (define-data-var draw-rand-num uint u0) (define-data-var draw-rand-is-v bool false) (define-data-var draw-total-shares uint u0) (define-data-var draw-award-pool-num uint u0) (define-data-var draw-pool-remain uint u0) (define-map will-draw-map uint ;; bet-type uint ;; round-will-draw ) ;; compress logic used (define-data-var compress-bet-type uint u0) (define-data-var compress-round uint u0) (define-map will-compress-map uint ;; bet-type uint ;; round-will-compress ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; bet-round = 0 means always current round (define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type }))) (player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times })) (bet-v-flag (and (>= bet-v u1) (<= bet-v u50))) (bet-s-flag (and (>= bet-s u51) (<= bet-s u100))) (bet-price (* (unwrap-panic (map-get? price-map bet-type)) (if (and bet-v-flag bet-s-flag) u2 u1))) (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) (player-num (+ (get player-num bet-summary-tuple) u1)) (v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0))) (s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0))) (is-first-player (is-eq player-num u1)) (start-at (if is-first-player block-height (get start-at bet-summary-tuple))) ) (asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT)) (asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND)) (asserts! (<= player-num MAX_PLAYERS_PER_ROUND) (err ERR_EXCEED_MAX_PLAYERS_PER_ROUND_COUNT)) (asserts! (or (is-eq bet-v u0) (or (and (>= bet-v u1) (<= bet-v u20)) (and (>= bet-v u31) (<= bet-v u50)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or (is-eq bet-s u0) (or (and (>= bet-s u51) (<= bet-s u70)) (and (>= bet-s u81) (<= bet-s u100)))) (err ERR_INVALID_BET_VALUE)) (asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE)) (asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST)) (asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH)) ;; deduct (unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR)) (let ( (bet-value (+ (* u101 bet-v) bet-s)) ) ;; note bet related map (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge bet-summary-tuple { start-at: start-at, player-num: player-num, v-num: v-num, s-num: s-num }) ) (map-set bet-record-map { bet-type: bet-type, round: cur-round, index: player-num } { player: tx-sender, bet-value: bet-value, shares: u0, win-num: 0 } ) ;; note player related map (map-set player-times-map { player: tx-sender, bet-type: bet-type } (+ player-times u1) ) (map-set player-record-map { player: tx-sender, bet-type: bet-type, index: (+ player-times u1) } { round: cur-round, bet-value: bet-value, win-num: 0 } ) ) ;; if ((player-num >= MAX_PLAYERS_PER_ROUND) or ;; ((has passed 6 hours since start) and (total-bets >= 10) and (small-side-count*2 > big-side-count))) then ;; (end cur round, start next round) (if (or (>= player-num MAX_PLAYERS_PER_ROUND) (and (> block-height (+ start-at MAX_BLOCKS_PER_ROUND)) (>= (+ v-num s-num) MIN_BET_COUNT_PER_ROUND) (> (* v-num u2) s-num) (> (* s-num u2) v-num) ) ) (let ( (cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }))) ;; DO NOT use bet-summary-tuple as it has been modified ) (map-set bet-summary-map { bet-type: bet-type, round: cur-round } (merge cur-bet-summary-tuple { end-at: block-height }) ) (restart-round bet-type (+ cur-round u1)) ) true ) ;; if ((has round-will-draw) and (has passed 0.5 hour since round-will-draw end-time)): ;; draw round-will-draw ;; elseif ((has round-will-compress) and (round-will-compress has draw): ;; compress round-will-compress (let ( (round-will-draw (unwrap-panic (map-get? will-draw-map bet-type))) (will-draw-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-draw }))) (round-will-compress (unwrap-panic (map-get? will-compress-map bet-type))) (will-compress-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round-will-compress }))) ) (if (and (< round-will-draw cur-round) (> block-height (+ (get end-at will-draw-bet-summary-tuple) DRAW_CD_BLOCKS))) ;; use cur-round is ok, though the real-time round may be cur-round + 1 (draw bet-type round-will-draw) (if (and (< round-will-compress cur-round) (> (get rand-num will-compress-bet-summary-tuple) u0)) (compress-records bet-type round-will-compress) true ) ) ) ;; (ok true) ) ) (define-private (draw (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) (crf-rand-num (calc-round-rand-num bet-type round)) (rand-num (process-crf-rand-num crf-rand-num)) (is-v (< rand-num u51)) ) (print { title: "draw", bet-type: bet-type, round: round, }) ;; (map-set will-draw-map bet-type (+ round u1)) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-rand-num rand-num) (var-set draw-rand-is-v is-v) (var-set draw-total-shares u0) (var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type))) (var-set draw-award-pool-num (* (var-get draw-bet-price) (if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple)) ) ) (var-set draw-pool-remain (var-get draw-award-pool-num)) ;; calc each player's shares (filter calc-shares-loop LIST_360) (map-set bet-summary-map { bet-type: bet-type, round: round } (merge bet-summary-tuple { rand-num: rand-num, total-shares: (var-get draw-total-shares) }) ) ;; distribute award (filter award-loop LIST_360) ;; as the use of integer divide, there may be still a little remain here (if (> (var-get draw-pool-remain) u0) (unwrap! (as-contract (stx-transfer? (var-get draw-pool-remain) tx-sender OWNER)) false) true ) ;; finish true ) ) (define-private (process-crf-rand-num (num uint)) (if (and (>= num u21) (<= num u30)) (unwrap-panic (element-at MAPPING_21_30_LIST (- num u21))) (if (and (>= num u71) (<= num u80)) (unwrap-panic (element-at MAPPING_71_80_LIST (- num u71))) num ) ) ) (define-private (restart-round (bet-type uint) (round uint)) (begin (print { title: "restart-round", bet-type: bet-type, round: round }) (map-set bet-round-map bet-type round) (map-set bet-summary-map { bet-type: bet-type, round: round } { start-at: u0, end-at: u0, player-num: u0, v-num: u0, s-num: u0, rand-num: u0, total-shares: u0 } ) ) ) (define-public (update-tips (in-tips (string-utf8 128))) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (ok (var-set tips in-tips)) ) ) ;;;;;;;; calc-shares-loop begin ;;;;;;;; (define-private (calc-shares-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (rand-num (var-get draw-rand-num)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v (/ bet-value u101)) (bet-s (mod bet-value u101)) (shares ( if (<= rand-num u50) (if (is-eq bet-v u0) u0 (if (>= bet-v rand-num) (/ HIT_SHARES (+ (- bet-v rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-v) u10)) ) ) (if (is-eq bet-s u0) u0 (if (>= bet-s rand-num) (/ HIT_SHARES (+ (- bet-s rand-num) u10)) (/ HIT_SHARES (+ (- rand-num bet-s) u10)) ) ) ) ) ) (var-set draw-total-shares (+ (var-get draw-total-shares) shares)) (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { shares: shares }) ) false ) false ) ) ) ;;;;;;;; calc-shares-loop end ;;;;;;;; ;;;;;;;; award-loop begin ;;;;;;;; (define-private (award-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) (total-shares (var-get draw-total-shares)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-price (var-get draw-bet-price)) (award (if (> total-shares u0) (/ (* (get shares bet-record-tuple) (var-get draw-award-pool-num)) total-shares) u0)) (player (get player bet-record-tuple)) (bet-value (get bet-value bet-record-tuple)) (is-bet-both-side (and (> bet-value u101) (> (mod bet-value u101) u0))) (win-num (if is-bet-both-side (- (to-int award) (to-int bet-price)) (if (> award u0) (to-int award) (- 0 (to-int bet-price))) ) ) (player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type }))) (player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }))) ) (and (> award u0) (unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false) (var-set draw-pool-remain (- (var-get draw-pool-remain) award)) ) ;; (map-set bet-record-map { bet-type: bet-type, round: round, index: index } (merge bet-record-tuple { win-num: win-num }) ) ;; (map-set player-record-map { player: player, bet-type: bet-type, index: player-times } (merge player-record { win-num: win-num }) ) false ) false ) ) ) ;;;;;;;; award-loop end ;;;;;;;; (define-private (calc-round-rand-num (bet-type uint) (round uint)) (let ( (end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))) (vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS))) ) (+ (mod (/ vrf-rand-num u10) u100) u1) ) ) ;;;;;;;; cancel round begin ;;;;;;;; (define-public (cancel (bet-type uint) (round uint)) (let ( (cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE))) (bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND))) ) (asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT)) (asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD)) (asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET)) (asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW)) (ok (cancel-round bet-type round)) ) ) (define-private (cancel-round (bet-type uint) (round uint)) (let ( (bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))) ) (var-set draw-bet-type bet-type) (var-set draw-round round) (var-set draw-bet-price (unwrap-panic (map-get? price-map bet-type))) ;; cancel each player (filter cancel-loop LIST_360) ;; reset current round for each bet-type (restart-round bet-type round) ) ) (define-private (cancel-loop (index uint)) (let ( (bet-type (var-get draw-bet-type)) (round (var-get draw-round)) ) (match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple (let ( (bet-value (get bet-value bet-record-tuple)) (bet-v-flag (>= bet-value u101)) (bet-s-flag (> (mod bet-value u101) u0)) (player (get player bet-record-tuple)) (player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))) ) ;; (map-delete bet-record-map { bet-type: bet-type, round: round, index: index }) ;; (if (> player-times u1) (map-set player-times-map { player: player, bet-type: bet-type } (- player-times u1) ) (map-delete player-times-map { player: player, bet-type: bet-type }) ) (map-delete player-record-map { player: player, bet-type: bet-type, index: player-times }) ;; (unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false) ;; false ) false ) ) ) ;;;;;;;; cancel round end ;;;;;;;; ;;;;;;;; update price begin ;;;;;;;; (define-public (update-price (bet-type uint) (price uint)) (begin (asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY)) (unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH)) (force-cancel-bet-type bet-type) (map-set price-map bet-type price) (ok true) ) ) (define-private (force-cancel-bet-type (bet-type uint)) (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple (if (and (> (get player-num bet-summary-tuple) u0) (> (get start-at bet-summary-tuple) u0) (is-eq (get rand-num bet-summary-tuple) u0) ) (cancel-round bet-type cur-round) true ) true ) ) ) ;;;;;;;; update price end ;;;;;;;; ;;;;;;;; compress begin ;;;;;;;; (define-private (compress-records (bet-type uint) (round uint)) (begin (print { title: "compress", bet-type: bet-type, round: round, }) (map-set will-compress-map bet-type (+ round u1)) (var-set compress-bet-type bet-type) (var-set compress-round round) (let ( (record-opt-list (map compress-record LIST_360)) (record-some-list (filter s-is-some record-opt-list)) (record-list (map s-unwrap record-some-list)) ) (map-set bet-records-compress-map { bet-type: bet-type, round: round } record-list) (map delete-record LIST_360) true ) ) ) (define-private (s-is-some (a (optional { p: principal, v: int }))) (is-some a) ) (define-private (s-unwrap (a (optional { p: principal, v: int }))) (unwrap-panic a) ) (define-private (compress-record (index uint)) (match (map-get? bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) bet-record-tuple (let ( (ori-win-num (get win-num bet-record-tuple)) (flag (if (>= ori-win-num 0) 1 -1)) (abs-win-num (if (>= ori-win-num 0) ori-win-num (* -1 ori-win-num))) ) (some { p: (get player bet-record-tuple), v: (* flag (+ (to-int (get bet-value bet-record-tuple)) (* 10000 (to-int (get shares bet-record-tuple))) (* 10000000000 abs-win-num))) } ) ) none ) ) (define-private (delete-record (index uint)) (map-delete bet-record-map { bet-type: (var-get compress-bet-type), round: (var-get compress-round), index: index }) ) ;;;;;;;; compress end ;;;;;;;; ;;;;;;;; web use begin ;;;;;;;; (define-read-only (get-summary (player-opt (optional principal))) (ok { bet-summarys: (map get-cur-bet-summary BET_TYPE_LIST), extra-summarys: (map get-extra-summary BET_TYPE_LIST), player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)), block-height: block-height, tips: (var-get tips), }) ) (define-read-only (get-extra-summary (bet-type uint)) { price: (unwrap-panic (map-get? price-map bet-type)), ;; comment as will exceed readonly function's read cost ;; will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), ;; will-compress: (unwrap-panic (map-get? will-compress-map bet-type)) } ) (define-read-only (get-will-summary (bet-type uint)) { will-draw: (unwrap-panic (map-get? will-draw-map bet-type)), will-compress: (unwrap-panic (map-get? will-compress-map bet-type)) } ) (define-read-only (get-cur-bet-summary (bet-type uint)) (let ( (round (unwrap-panic (map-get? bet-round-map bet-type))) ) (get-bet-summary bet-type round) ) ) (define-read-only (get-bet-summary (bet-type uint) (round uint)) (let ( (bet-summary (map-get? bet-summary-map { bet-type: bet-type, round: round })) (start-at (default-to u0 (get start-at bet-summary))) (start-time (default-to u0 (get-block-info? time start-at))) (end-at (default-to u0 (get end-at bet-summary))) (end-time (default-to u0 (get-block-info? time end-at))) ) { bet-type: bet-type, round: round, summary: bet-summary, start-time: start-time, end-time: end-time, } ) ) (define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal))) (match player-opt player (match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times (match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record (let ( (cur-round (unwrap-panic (map-get? bet-round-map bet-type))) ) (if (is-eq cur-round (get round player-record)) (ok player-record) (err none) ) ) (err none) ) (err none) ) (err none) ) ) (define-read-only (get-player-times (player principal) (bet-type uint)) (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })) ) (define-read-only (get-player-bet-infos (key-list (list 25 { player: principal, bet-type: uint, index: uint }))) (map get-player-bet-info key-list) ) (define-read-only (get-player-bet-info (key { player: principal, bet-type: uint, index: uint })) (map-get? player-record-map key) ) (define-read-only (get-compress-bet-records (bet-type uint) (round uint)) (map-get? bet-records-compress-map { bet-type: bet-type, round: round }) ) (define-read-only (get-bet-records (key-list (list 25 { bet-type: uint, round: uint, index: uint }))) (map get-bet-record key-list) ) (define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint })) (map-get? bet-record-map key) ) ;;;;;;;; web use end ;;;;;;;; ;;;;;;;; borrow from citycoins begin ;;;;;;;; ;; (https://github.com/citycoins/citycoin/tree/main/contracts) (define-private (buff-to-u8 (byte (buff 1))) (unwrap-panic (index-of BUFF_TO_BYTE byte)) ) (define-private (buff-to-uint-le (word (buff 16))) (get acc (fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word }) ) ) (define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) })) (let ( (acc (get acc input)) (data (get data input)) (byte (buff-to-u8 (unwrap-panic (element-at data idx)))) ) { ;; acc = byte * (2**(8 * (15 - idx))) + acc acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc), data: data }) ) (define-private (lower-16-le (input (buff 32))) (get acc (fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input }) ) ) (define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) })) (let ( (acc (get acc input)) (data (get data input)) (byte (unwrap-panic (element-at data idx))) ) { acc: (unwrap-panic (as-max-len? (concat acc byte) u16)), data: data }) ) (define-read-only (get-rand-uint-at-block (block-index uint)) (buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index)))) ) ;;;;;;;; borrow from citycoins end ;;;;;;;; ;;;;;;;; initialize begin ;;;;;;;; (define-private (init-bet-type (bet-type uint)) (begin (restart-round bet-type u1) (map-set will-draw-map bet-type u1) (map-set will-compress-map bet-type u1) (map-set price-map bet-type (* u100 bet-type)) ) ) (map init-bet-type BET_TYPE_LIST) ;;;;;;;; initialize end ;;;;;;;;
0x9d974590c7cace312eda3b1166a12ad4fc2d1a6934839ffbb4fbfc94a2b12c5c
24,287
success
stacks-punks
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) (define-non-fungible-token stacks-punks uint) ;; constants (define-constant PUNK-IMAGE-HASH u"345a94125abb0a209a57943ffe043d101e810dbf52d08c892b4718613c867798") (define-constant ERR-NOT-AUTHORIZED u401) (define-constant ERR-ALL-MINTED u101) (define-constant ERR-COOLDOWN u102) (define-constant CONTRACT-OWNER tx-sender) ;; variables (define-data-var punk-counter uint u0) (define-data-var token-uri (string-ascii 256) "") (define-data-var token-punk-uri (string-ascii 256) "") (define-map punks { id: uint } { minted: bool }) (define-map punks-by-owner { owner: principal } { ids: (list 1000 uint) }) (define-data-var removing-punk-id uint u0) (define-data-var cost-per-mint uint u100000000) ;; public functions (define-public (mint (punk-id uint)) (let ( (count (var-get punk-counter)) (random-punk-id (get-random)) ) (asserts! (<= count u10000) (err ERR-ALL-MINTED)) (if (> (var-get punk-counter) u9500) (begin ;; added this cause the random number generator fails too often to get a random number that is not used yet ;; only the last 500 punks can be minted like this (mint-with-id punk-id) ) (begin (if (is-some (map-get? punks {id: random-punk-id})) (begin (let ( (random-punk-id-2 (get-random)) ) (if (is-some (map-get? punks {id: random-punk-id-2})) (begin (let ( (random-punk-id-3 (get-random)) ) (if (is-some (map-get? punks {id: random-punk-id-3})) (err ERR-COOLDOWN) (mint-with-id random-punk-id-3) ) ) ) (mint-with-id random-punk-id-2) ) ) ) (mint-with-id random-punk-id) ) ) ) ) ) (define-private (mint-with-id (random-punk-id uint)) (let ( (count (var-get punk-counter)) (punk-ids (unwrap-panic (get-punks-by-owner tx-sender))) ) (match (stx-transfer? (var-get cost-per-mint) tx-sender (as-contract tx-sender)) success (begin (try! (nft-mint? stacks-punks random-punk-id tx-sender)) (var-set punk-counter (+ count u1)) (map-set punks { id: random-punk-id } { minted: true }) (map-set punks-by-owner { owner: tx-sender } { ids: (unwrap-panic (as-max-len? (append punk-ids random-punk-id) u1000)) } ) (ok random-punk-id) ) error (err error) ) ) ) (define-read-only (get-punks-entry-by-owner (owner principal)) (default-to { ids: (list u0) } (map-get? punks-by-owner { owner: owner }) ) ) (define-public (get-punks-by-owner (owner principal)) (ok (get ids (get-punks-entry-by-owner owner))) ) (define-public (burn (index uint)) (if (is-owner index tx-sender) (match (nft-burn? stacks-punks index tx-sender) success (ok true) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-public (transfer (index uint) (owner principal) (recipient principal)) (if (and (is-owner index owner) (is-owner index tx-sender)) (match (nft-transfer? stacks-punks index owner recipient) success (let ((punk-ids (unwrap-panic (get-punks-by-owner recipient)))) (map-set punks-by-owner { owner: recipient } { ids: (unwrap-panic (as-max-len? (append punk-ids index) u1000)) } ) (try! (remove-punk owner index)) (ok true) ) error (err error) ) (err ERR-NOT-AUTHORIZED) ) ) (define-private (remove-punk (owner principal) (punk-id uint)) (if true (let ((punk-ids (unwrap-panic (get-punks-by-owner owner)))) (var-set removing-punk-id punk-id) (map-set punks-by-owner { owner: owner } { ids: (unwrap-panic (as-max-len? (filter remove-transferred-punk punk-ids) u1000)) } ) (ok true) ) (err u0) ) ) (define-private (remove-transferred-punk (punk-id uint)) (if (is-eq punk-id (var-get removing-punk-id)) false true ) ) (define-read-only (get-last-token-id) (ok (var-get punk-counter)) ) (define-public (set-cost-per-mint (value uint)) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set cost-per-mint value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-public (set-token-punk-uri (value (string-ascii 256))) (if (is-eq tx-sender CONTRACT-OWNER) (ok (var-set token-punk-uri value)) (err ERR-NOT-AUTHORIZED) ) ) (define-read-only (get-token-uri (id uint)) (if (not (is-eq id u0)) (ok (some (var-get token-punk-uri))) (ok (some (var-get token-uri))) ) ) (define-public (get-owner (index uint)) (ok (nft-get-owner? stacks-punks index)) ) (define-read-only (stx-balance) (stx-get-balance (as-contract tx-sender)) ) (define-read-only (stx-balance-of (address principal)) (stx-get-balance address) ) (define-public (transfer-stx (address principal) (amount uint)) (if (is-eq tx-sender CONTRACT-OWNER) (as-contract (stx-transfer? amount (as-contract tx-sender) address)) (err ERR-NOT-AUTHORIZED) ) ) ;; private functions (define-private (is-owner (index uint) (user principal)) (is-eq user (unwrap! (nft-get-owner? stacks-punks index) false)) ) (define-private (get-random) (let ( (number (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u1 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number})) (begin (let ( (number-2 (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u10 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number-2})) (begin (let ( (number-3 (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u30 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number-3})) (begin (let ( (number-4 (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u40 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number-4})) (begin (let ( (number-5 (mod (unwrap-panic (get-random-uint-at-block (- block-height (+ u50 (var-get punk-counter))))) u10000)) ) (if (is-some (map-get? punks {id: number-5})) number-5 number-5 ) ) ) number-4 ) ) ) number-3 ) ) ) number-2 ) ) ) number ) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; random number generator stuff ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-map BUFF_TO_UINT (buff 1) uint ) ;; Read the on-chain VRF and turn the lower 16 bytes into a uint, in order to sample the set of miners and determine ;; which one may claim the token batch for the given block height. (define-private (get-random-uint-at-block (stacksBlock uint)) (match (get-block-info? vrf-seed stacksBlock) vrfSeed (some (get-random-from-vrf-seed vrfSeed u16)) none ) ) ;; Read the on-chain VRF and turn the lower [bytes] bytes into a uint, in order to sample the set of miners and determine ;; which one may claim the token batch for the given block height. (define-private (get-random-uint-at-block-prec (stacksBlock uint) (bytes uint)) (match (get-block-info? vrf-seed stacksBlock) vrfSeed (some (get-random-from-vrf-seed vrfSeed bytes)) none ) ) ;; Turn lower [bytes] bytes into uint. (define-private (get-random-from-vrf-seed (vrfSeed (buff 32)) (bytes uint)) (+ (if (>= bytes u16) (convert-to-le (element-at vrfSeed u16) u15) u0) (if (>= bytes u15) (convert-to-le (element-at vrfSeed u17) u14) u0) (if (>= bytes u14) (convert-to-le (element-at vrfSeed u18) u13) u0) (if (>= bytes u13) (convert-to-le (element-at vrfSeed u19) u12) u0) (if (>= bytes u12) (convert-to-le (element-at vrfSeed u20) u11) u0) (if (>= bytes u11) (convert-to-le (element-at vrfSeed u21) u10) u0) (if (>= bytes u10) (convert-to-le (element-at vrfSeed u22) u9) u0) (if (>= bytes u9) (convert-to-le (element-at vrfSeed u23) u8) u0) (if (>= bytes u8) (convert-to-le (element-at vrfSeed u24) u7) u0) (if (>= bytes u7) (convert-to-le (element-at vrfSeed u25) u6) u0) (if (>= bytes u6) (convert-to-le (element-at vrfSeed u26) u5) u0) (if (>= bytes u5) (convert-to-le (element-at vrfSeed u27) u4) u0) (if (>= bytes u4) (convert-to-le (element-at vrfSeed u28) u3) u0) (if (>= bytes u3) (convert-to-le (element-at vrfSeed u29) u2) u0) (if (>= bytes u2) (convert-to-le (element-at vrfSeed u30) u1) u0) (if (>= bytes u1) (convert-to-le (element-at vrfSeed u31) u0) u0) ) ) (define-private (convert-to-le (byte (optional (buff 1))) (pos uint)) (* (unwrap-panic (map-get? BUFF_TO_UINT (unwrap-panic byte))) (pow u2 (* u8 pos)) ) ) ;; initialize (fold fill-buff-to-uint (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf 0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf 0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf 0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf 0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff ) u0) (var-set token-punk-uri "https://www.stackspunks.com/assets/punks.json") (var-set token-uri "https://www.stackspunks.com/assets/punks.json") (define-private (fill-buff-to-uint (byte (buff 1)) (val uint)) (begin (map-insert BUFF_TO_UINT byte val) (+ val u1) ) )
0xb0288f3e7eb568e8dc48ebe602144ec83f5b2678fa168472166fa85d8c808bfa
24,299
success
citycoin-vrf
;; CITYCOIN VRF CONTRACT ;; 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 }) )
0x6e18dbc3a687270a7015ebef73a590d4f066538b41bf7cb7c8d7d891b6442964
24,334
success
citycoin-core-trait
;; CITYCOIN CORE TRAIT (define-trait citycoin-core ( (register-user ((optional (string-utf8 50))) (response bool uint) ) (mine-tokens (uint (optional (buff 34))) (response bool uint) ) (claim-mining-reward (uint) (response bool uint) ) (stack-tokens (uint uint) (response bool uint) ) (claim-stacking-reward (uint) (response bool uint) ) (set-city-wallet (principal) (response bool uint) ) (shutdown-contract (uint) (response bool uint) ) ) )
0x9751f503896ca13ba797e119d1b62d990b854bee3a63301e737fa9d3ebf8ffa6
24,334
success
citycoin-token-trait
;; CITYCOIN TOKEN TRAIT (define-trait citycoin-token ( (activate-token (principal uint) (response bool uint) ) (set-token-uri ((optional (string-utf8 256))) (response bool uint) ) (mint (uint principal) (response bool uint) ) (burn (uint principal) (response bool uint) ) (send-many ((list 200 { to: principal, amount: uint, memo: (optional (buff 34)) })) (response bool uint) ) ) )
0xf64fedb420622d0403154465c176e06ecbbed306ec2337f9fb8f7bbe6c6a8575
24,334
success
citycoin-vrf
;; CITYCOIN VRF CONTRACT ;; 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 }) )
0x6e18dbc3a687270a7015ebef73a590d4f066538b41bf7cb7c8d7d891b6442964
24,334
success
citycoin-core-trait
;; CITYCOIN CORE TRAIT (define-trait citycoin-core ( (register-user ((optional (string-utf8 50))) (response bool uint) ) (mine-tokens (uint (optional (buff 34))) (response bool uint) ) (claim-mining-reward (uint) (response bool uint) ) (stack-tokens (uint uint) (response bool uint) ) (claim-stacking-reward (uint) (response bool uint) ) (set-city-wallet (principal) (response bool uint) ) (shutdown-contract (uint) (response bool uint) ) ) )
0x9751f503896ca13ba797e119d1b62d990b854bee3a63301e737fa9d3ebf8ffa6
24,334
success
citycoin-token-trait
;; CITYCOIN TOKEN TRAIT (define-trait citycoin-token ( (activate-token (principal uint) (response bool uint) ) (set-token-uri ((optional (string-utf8 256))) (response bool uint) ) (mint (uint principal) (response bool uint) ) (burn (uint principal) (response bool uint) ) (send-many ((list 200 { to: principal, amount: uint, memo: (optional (buff 34)) })) (response bool uint) ) ) )
0xf64fedb420622d0403154465c176e06ecbbed306ec2337f9fb8f7bbe6c6a8575
24,334
success
miamicoin-auth
;; MIAMICOIN AUTH CONTRACT (define-constant CONTRACT_OWNER tx-sender) ;; TRAIT DEFINITIONS (use-trait coreTrait 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.citycoin-core-trait.citycoin-core) (use-trait tokenTrait 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.citycoin-token-trait.citycoin-token) ;; ERRORS (define-constant ERR_UNKNOWN_JOB u6000) (define-constant ERR_UNAUTHORIZED u6001) (define-constant ERR_JOB_IS_ACTIVE u6002) (define-constant ERR_JOB_IS_NOT_ACTIVE u6003) (define-constant ERR_ALREADY_VOTED_THIS_WAY u6004) (define-constant ERR_JOB_IS_EXECUTED u6005) (define-constant ERR_JOB_IS_NOT_APPROVED u6006) (define-constant ERR_ARGUMENT_ALREADY_EXISTS u6007) (define-constant ERR_NO_ACTIVE_CORE_CONTRACT u6008) (define-constant ERR_CORE_CONTRACT_NOT_FOUND u6009) (define-constant ERR_UNKNOWN_ARGUMENT u6010) ;; JOB MANAGEMENT (define-constant REQUIRED_APPROVALS u3) (define-data-var lastJobId uint u0) (define-map Jobs uint { creator: principal, name: (string-ascii 255), target: principal, approvals: uint, disapprovals: uint, isActive: bool, isExecuted: bool } ) (define-map JobApprovers { jobId: uint, approver: principal } bool ) (define-map Approvers principal bool ) (define-map ArgumentLastIdsByType { jobId: uint, argumentType: (string-ascii 25) } uint ) (define-map UIntArgumentsByName { jobId: uint, argumentName: (string-ascii 255) } { argumentId: uint, value: uint} ) (define-map UIntArgumentsById { jobId: uint, argumentId: uint } { argumentName: (string-ascii 255), value: uint } ) (define-map PrincipalArgumentsByName { jobId: uint, argumentName: (string-ascii 255) } { argumentId: uint, value: principal } ) (define-map PrincipalArgumentsById { jobId: uint, argumentId: uint } { argumentName: (string-ascii 255), value: principal } ) ;; FUNCTIONS (define-read-only (get-last-job-id) (var-get lastJobId) ) (define-public (create-job (name (string-ascii 255)) (target principal)) (let ( (newJobId (+ (var-get lastJobId) u1)) ) (asserts! (is-approver tx-sender) (err ERR_UNAUTHORIZED)) (map-set Jobs newJobId { creator: tx-sender, name: name, target: target, approvals: u0, disapprovals: u0, isActive: false, isExecuted: false } ) (var-set lastJobId newJobId) (ok newJobId) ) ) (define-read-only (get-job (jobId uint)) (map-get? Jobs jobId) ) (define-public (activate-job (jobId uint)) (let ( (job (unwrap! (get-job jobId) (err ERR_UNKNOWN_JOB))) ) (asserts! (is-eq (get creator job) tx-sender) (err ERR_UNAUTHORIZED)) (asserts! (not (get isActive job)) (err ERR_JOB_IS_ACTIVE)) (map-set Jobs jobId (merge job { isActive: true }) ) (ok true) ) ) (define-public (approve-job (jobId uint)) (let ( (job (unwrap! (get-job jobId) (err ERR_UNKNOWN_JOB))) (previousVote (map-get? JobApprovers { jobId: jobId, approver: tx-sender })) ) (asserts! (get isActive job) (err ERR_JOB_IS_NOT_ACTIVE)) (asserts! (is-approver tx-sender) (err ERR_UNAUTHORIZED)) ;; save vote (map-set JobApprovers { jobId: jobId, approver: tx-sender } true ) (match previousVote approved (begin (asserts! (not approved) (err ERR_ALREADY_VOTED_THIS_WAY)) (map-set Jobs jobId (merge job { approvals: (+ (get approvals job) u1), disapprovals: (- (get disapprovals job) u1) } ) ) ) ;; no previous vote (map-set Jobs jobId (merge job { approvals: (+ (get approvals job) u1) } ) ) ) (ok true) ) ) (define-public (disapprove-job (jobId uint)) (let ( (job (unwrap! (get-job jobId) (err ERR_UNKNOWN_JOB))) (previousVote (map-get? JobApprovers { jobId: jobId, approver: tx-sender })) ) (asserts! (get isActive job) (err ERR_JOB_IS_NOT_ACTIVE)) (asserts! (is-approver tx-sender) (err ERR_UNAUTHORIZED)) ;; save vote (map-set JobApprovers { jobId: jobId, approver: tx-sender } false ) (match previousVote approved (begin (asserts! approved (err ERR_ALREADY_VOTED_THIS_WAY)) (map-set Jobs jobId (merge job { approvals: (- (get approvals job) u1), disapprovals: (+ (get disapprovals job) u1) } ) ) ) ;; no previous vote (map-set Jobs jobId (merge job { disapprovals: (+ (get disapprovals job) u1) } ) ) ) (ok true) ) ) (define-read-only (is-job-approved (jobId uint)) (match (get-job jobId) job (>= (get approvals job) REQUIRED_APPROVALS) false ) ) (define-public (mark-job-as-executed (jobId uint)) (let ( (job (unwrap! (get-job jobId) (err ERR_UNKNOWN_JOB))) ) (asserts! (get isActive job) (err ERR_JOB_IS_NOT_ACTIVE)) (asserts! (>= (get approvals job) REQUIRED_APPROVALS) (err ERR_JOB_IS_NOT_APPROVED)) (asserts! (is-eq (get target job) contract-caller) (err ERR_UNAUTHORIZED)) (asserts! (not (get isExecuted job)) (err ERR_JOB_IS_EXECUTED)) (map-set Jobs jobId (merge job { isExecuted: true }) ) (ok true) ) ) (define-public (add-uint-argument (jobId uint) (argumentName (string-ascii 255)) (value uint)) (let ( (argumentId (generate-argument-id jobId "uint")) ) (try! (guard-add-argument jobId)) (asserts! (and (map-insert UIntArgumentsById { jobId: jobId, argumentId: argumentId } { argumentName: argumentName, value: value } ) (map-insert UIntArgumentsByName { jobId: jobId, argumentName: argumentName } { argumentId: argumentId, value: value} ) ) (err ERR_ARGUMENT_ALREADY_EXISTS) ) (ok true) ) ) (define-read-only (get-uint-argument-by-name (jobId uint) (argumentName (string-ascii 255))) (map-get? UIntArgumentsByName { jobId: jobId, argumentName: argumentName }) ) (define-read-only (get-uint-argument-by-id (jobId uint) (argumentId uint)) (map-get? UIntArgumentsById { jobId: jobId, argumentId: argumentId }) ) (define-read-only (get-uint-value-by-name (jobId uint) (argumentName (string-ascii 255))) (get value (get-uint-argument-by-name jobId argumentName)) ) (define-read-only (get-uint-value-by-id (jobId uint) (argumentId uint)) (get value (get-uint-argument-by-id jobId argumentId)) ) (define-public (add-principal-argument (jobId uint) (argumentName (string-ascii 255)) (value principal)) (let ( (argumentId (generate-argument-id jobId "principal")) ) (try! (guard-add-argument jobId)) (asserts! (and (map-insert PrincipalArgumentsById { jobId: jobId, argumentId: argumentId } { argumentName: argumentName, value: value } ) (map-insert PrincipalArgumentsByName { jobId: jobId, argumentName: argumentName } { argumentId: argumentId, value: value} ) ) (err ERR_ARGUMENT_ALREADY_EXISTS) ) (ok true) ) ) (define-read-only (get-principal-argument-by-name (jobId uint) (argumentName (string-ascii 255))) (map-get? PrincipalArgumentsByName { jobId: jobId, argumentName: argumentName }) ) (define-read-only (get-principal-argument-by-id (jobId uint) (argumentId uint)) (map-get? PrincipalArgumentsById { jobId: jobId, argumentId: argumentId }) ) (define-read-only (get-principal-value-by-name (jobId uint) (argumentName (string-ascii 255))) (get value (get-principal-argument-by-name jobId argumentName)) ) (define-read-only (get-principal-value-by-id (jobId uint) (argumentId uint)) (get value (get-principal-argument-by-id jobId argumentId)) ) ;; PRIVATE FUNCTIONS (define-read-only (is-approver (user principal)) (default-to false (map-get? Approvers user)) ) (define-private (generate-argument-id (jobId uint) (argumentType (string-ascii 25))) (let ( (argumentId (+ (default-to u0 (map-get? ArgumentLastIdsByType { jobId: jobId, argumentType: argumentType })) u1)) ) (map-set ArgumentLastIdsByType { jobId: jobId, argumentType: argumentType } argumentId ) ;; return argumentId ) ) (define-private (guard-add-argument (jobId uint)) (let ( (job (unwrap! (get-job jobId) (err ERR_UNKNOWN_JOB))) ) (asserts! (not (get isActive job)) (err ERR_JOB_IS_ACTIVE)) (asserts! (is-eq (get creator job) contract-caller) (err ERR_UNAUTHORIZED)) (ok true) ) ) ;; CONTRACT MANAGEMENT ;; initial value for active core contract ;; set to deployer address at startup to prevent ;; circular dependency of core on auth (define-data-var activeCoreContract principal CONTRACT_OWNER) (define-data-var initialized bool false) ;; core contract states (define-constant STATE_DEPLOYED u0) (define-constant STATE_ACTIVE u1) (define-constant STATE_INACTIVE u2) ;; core contract map (define-map CoreContracts principal { state: uint, startHeight: uint, endHeight: uint } ) ;; getter for active core contract (define-read-only (get-active-core-contract) (begin (asserts! (not (is-eq (var-get activeCoreContract) CONTRACT_OWNER)) (err ERR_NO_ACTIVE_CORE_CONTRACT)) (ok (var-get activeCoreContract)) ) ) ;; getter for core contract map (define-read-only (get-core-contract-info (targetContract principal)) (let ( (coreContract (unwrap! (map-get? CoreContracts targetContract) (err ERR_CORE_CONTRACT_NOT_FOUND))) ) (ok coreContract) ) ) ;; one-time function to initialize contracts after all contracts are deployed ;; - check that deployer is calling this function ;; - check this contract is not activated already (one-time use) ;; - set initial map value for core contract v1 ;; - set cityWallet in core contract ;; - set intialized true (define-public (initialize-contracts (coreContract <coreTrait>)) (let ( (coreContractAddress (contract-of coreContract)) ) (asserts! (is-eq contract-caller CONTRACT_OWNER) (err ERR_UNAUTHORIZED)) (asserts! (not (var-get initialized)) (err ERR_UNAUTHORIZED)) (map-set CoreContracts coreContractAddress { state: STATE_DEPLOYED, startHeight: u0, endHeight: u0 }) (try! (contract-call? coreContract set-city-wallet (var-get cityWallet))) (var-set initialized true) (ok true) ) ) (define-read-only (is-initialized) (var-get initialized) ) ;; function to activate core contract through registration ;; - check that target is in core contract map ;; - check that caller is core contract ;; - set active in core contract map ;; - set as activeCoreContract (define-public (activate-core-contract (targetContract principal) (stacksHeight uint)) (let ( (coreContract (unwrap! (map-get? CoreContracts targetContract) (err ERR_CORE_CONTRACT_NOT_FOUND))) ) (asserts! (is-eq contract-caller targetContract) (err ERR_UNAUTHORIZED)) (map-set CoreContracts targetContract { state: STATE_ACTIVE, startHeight: stacksHeight, endHeight: u0 }) (var-set activeCoreContract targetContract) (ok true) ) ) ;; protected function to update core contract (define-public (upgrade-core-contract (oldContract <coreTrait>) (newContract <coreTrait>)) (let ( (oldContractAddress (contract-of oldContract)) (oldContractMap (unwrap! (map-get? CoreContracts oldContractAddress) (err ERR_CORE_CONTRACT_NOT_FOUND))) (newContractAddress (contract-of newContract)) ) (asserts! (not (is-eq oldContractAddress newContractAddress)) (err ERR_UNAUTHORIZED)) (asserts! (is-authorized-city) (err ERR_UNAUTHORIZED)) (map-set CoreContracts oldContractAddress { state: STATE_INACTIVE, startHeight: (get startHeight oldContractMap), endHeight: block-height }) (map-set CoreContracts newContractAddress { state: STATE_DEPLOYED, startHeight: u0, endHeight: u0 }) (var-set activeCoreContract newContractAddress) (try! (contract-call? oldContract shutdown-contract block-height)) (try! (contract-call? newContract set-city-wallet (var-get cityWallet))) (ok true) ) ) (define-public (execute-upgrade-core-contract-job (jobId uint) (oldContract <coreTrait>) (newContract <coreTrait>)) (let ( (oldContractArg (unwrap! (get-principal-value-by-name jobId "oldContract") (err ERR_UNKNOWN_ARGUMENT))) (newContractArg (unwrap! (get-principal-value-by-name jobId "newContract") (err ERR_UNKNOWN_ARGUMENT))) (oldContractAddress (contract-of oldContract)) (oldContractMap (unwrap! (map-get? CoreContracts oldContractAddress) (err ERR_CORE_CONTRACT_NOT_FOUND))) (newContractAddress (contract-of newContract)) ) (asserts! (is-approver contract-caller) (err ERR_UNAUTHORIZED)) (asserts! (and (is-eq oldContractArg oldContractAddress) (is-eq newContractArg newContractAddress)) (err ERR_UNAUTHORIZED)) (asserts! (not (is-eq oldContractAddress newContractAddress)) (err ERR_UNAUTHORIZED)) (map-set CoreContracts oldContractAddress { state: STATE_INACTIVE, startHeight: (get startHeight oldContractMap), endHeight: block-height }) (map-set CoreContracts newContractAddress { state: STATE_DEPLOYED, startHeight: u0, endHeight: u0 }) (var-set activeCoreContract newContractAddress) (try! (contract-call? oldContract shutdown-contract block-height)) (try! (contract-call? newContract set-city-wallet (var-get cityWallet))) (as-contract (mark-job-as-executed jobId)) ) ) ;; CITY WALLET MANAGEMENT ;; initial value for city wallet (define-data-var cityWallet principal 'SM2MARAVW6BEJCD13YV2RHGYHQWT7TDDNMNRB1MVT) ;; returns city wallet principal (define-read-only (get-city-wallet) (ok (var-get cityWallet)) ) ;; protected function to update city wallet variable (define-public (set-city-wallet (targetContract <coreTrait>) (newCityWallet principal)) (let ( (coreContractAddress (contract-of targetContract)) (coreContract (unwrap! (map-get? CoreContracts coreContractAddress) (err ERR_CORE_CONTRACT_NOT_FOUND))) ) (asserts! (is-authorized-city) (err ERR_UNAUTHORIZED)) (asserts! (is-eq coreContractAddress (var-get activeCoreContract)) (err ERR_UNAUTHORIZED)) (var-set cityWallet newCityWallet) (try! (contract-call? targetContract set-city-wallet newCityWallet)) (ok true) ) ) (define-public (execute-set-city-wallet-job (jobId uint) (targetContract <coreTrait>)) (let ( (coreContractAddress (contract-of targetContract)) (coreContract (unwrap! (map-get? CoreContracts coreContractAddress) (err ERR_CORE_CONTRACT_NOT_FOUND))) (newCityWallet (unwrap! (get-principal-value-by-name jobId "newCityWallet") (err ERR_UNKNOWN_ARGUMENT))) ) (asserts! (is-approver contract-caller) (err ERR_UNAUTHORIZED)) (asserts! (is-eq coreContractAddress (var-get activeCoreContract)) (err ERR_UNAUTHORIZED)) (var-set cityWallet newCityWallet) (try! (contract-call? targetContract set-city-wallet newCityWallet)) (as-contract (mark-job-as-executed jobId)) ) ) ;; check if contract caller is city wallet (define-private (is-authorized-city) (is-eq contract-caller (var-get cityWallet)) ) ;; TOKEN MANAGEMENT (define-public (set-token-uri (targetContract <tokenTrait>) (newUri (optional (string-utf8 256)))) (begin (asserts! (is-authorized-city) (err ERR_UNAUTHORIZED)) (as-contract (try! (contract-call? targetContract set-token-uri newUri))) (ok true) ) ) ;; APPROVERS MANAGEMENT (define-public (execute-replace-approver-job (jobId uint)) (let ( (oldApprover (unwrap! (get-principal-value-by-name jobId "oldApprover") (err ERR_UNKNOWN_ARGUMENT))) (newApprover (unwrap! (get-principal-value-by-name jobId "newApprover") (err ERR_UNKNOWN_ARGUMENT))) ) (asserts! (is-approver contract-caller) (err ERR_UNAUTHORIZED)) (map-set Approvers oldApprover false) (map-set Approvers newApprover true) (as-contract (mark-job-as-executed jobId)) ) ) ;; CONTRACT INITIALIZATION (map-insert Approvers 'SP372JVX6EWE2M0XPA84MWZYRRG2M6CAC4VVC12V1 true) (map-insert Approvers 'SP2R0DQYR7XHD161SH2GK49QRP1YSV7HE9JSG7W6G true) (map-insert Approvers 'SPN4Y5QPGQA8882ZXW90ADC2DHYXMSTN8VAR8C3X true) (map-insert Approvers 'SP3YYGCGX1B62CYAH4QX7PQE63YXG7RDTXD8BQHJQ true) (map-insert Approvers 'SP7DGES13508FHRWS1FB0J3SZA326FP6QRMB6JDE true)
0x3c9303ada7f0dbf6f814722cb4a6c13752f187bde0b800bd6f84a342505e006b
24,338
success
miamicoin-token
;; MIAMICOIN TOKEN CONTRACT ;; CONTRACT OWNER (define-constant CONTRACT_OWNER tx-sender) ;; TRAIT DEFINITIONS (impl-trait 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.citycoin-token-trait.citycoin-token) (use-trait coreTrait 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.citycoin-core-trait.citycoin-core) ;; ERROR CODES (define-constant ERR_UNAUTHORIZED u2000) (define-constant ERR_TOKEN_NOT_ACTIVATED u2001) (define-constant ERR_TOKEN_ALREADY_ACTIVATED u2002) ;; SIP-010 DEFINITION (impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait) (define-fungible-token miamicoin) ;; SIP-010 FUNCTIONS (define-public (transfer (amount uint) (from principal) (to principal) (memo (optional (buff 34)))) (begin (asserts! (is-eq from tx-sender) (err ERR_UNAUTHORIZED)) (if (is-some memo) (print memo) none ) (ft-transfer? miamicoin amount from to) ) ) (define-read-only (get-name) (ok "miamicoin") ) (define-read-only (get-symbol) (ok "MIA") ) (define-read-only (get-decimals) (ok u0) ) (define-read-only (get-balance (user principal)) (ok (ft-get-balance miamicoin user)) ) (define-read-only (get-total-supply) (ok (ft-get-supply miamicoin)) ) (define-read-only (get-token-uri) (ok (var-get tokenUri)) ) ;; TOKEN CONFIGURATION ;; how many blocks until the next halving occurs (define-constant TOKEN_HALVING_BLOCKS u210000) ;; store block height at each halving, set by register-user in core contract (define-data-var coinbaseThreshold1 uint u0) (define-data-var coinbaseThreshold2 uint u0) (define-data-var coinbaseThreshold3 uint u0) (define-data-var coinbaseThreshold4 uint u0) (define-data-var coinbaseThreshold5 uint u0) ;; once activated, thresholds cannot be updated again (define-data-var tokenActivated bool false) ;; core contract states (define-constant STATE_DEPLOYED u0) (define-constant STATE_ACTIVE u1) (define-constant STATE_INACTIVE u2) ;; one-time function to activate the token (define-public (activate-token (coreContract principal) (stacksHeight uint)) (let ( (coreContractMap (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-auth get-core-contract-info coreContract))) ) (asserts! (is-eq (get state coreContractMap) STATE_ACTIVE) (err ERR_UNAUTHORIZED)) (asserts! (not (var-get tokenActivated)) (err ERR_TOKEN_ALREADY_ACTIVATED)) (var-set tokenActivated true) (var-set coinbaseThreshold1 (+ stacksHeight TOKEN_HALVING_BLOCKS)) (var-set coinbaseThreshold2 (+ stacksHeight (* u2 TOKEN_HALVING_BLOCKS))) (var-set coinbaseThreshold3 (+ stacksHeight (* u3 TOKEN_HALVING_BLOCKS))) (var-set coinbaseThreshold4 (+ stacksHeight (* u4 TOKEN_HALVING_BLOCKS))) (var-set coinbaseThreshold5 (+ stacksHeight (* u5 TOKEN_HALVING_BLOCKS))) (ok true) ) ) ;; return coinbase thresholds if token activated (define-read-only (get-coinbase-thresholds) (let ( (activated (var-get tokenActivated)) ) (asserts! activated (err ERR_TOKEN_NOT_ACTIVATED)) (ok { coinbaseThreshold1: (var-get coinbaseThreshold1), coinbaseThreshold2: (var-get coinbaseThreshold2), coinbaseThreshold3: (var-get coinbaseThreshold3), coinbaseThreshold4: (var-get coinbaseThreshold4), coinbaseThreshold5: (var-get coinbaseThreshold5) }) ) ) ;; UTILITIES (define-data-var tokenUri (optional (string-utf8 256)) (some u"https://cdn.citycoins.co/metadata/miamicoin.json")) ;; set token URI to new value, only accessible by Auth (define-public (set-token-uri (newUri (optional (string-utf8 256)))) (begin (asserts! (is-authorized-auth) (err ERR_UNAUTHORIZED)) (ok (var-set tokenUri newUri)) ) ) ;; mint new tokens, only accessible by a Core contract (define-public (mint (amount uint) (recipient principal)) (let ( (coreContract (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-auth get-core-contract-info contract-caller))) ) (ft-mint? miamicoin amount recipient) ) ) ;; burn tokens, only accessible by a Core contract (define-public (burn (amount uint) (recipient principal)) (let ( (coreContract (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-auth get-core-contract-info contract-caller))) ) (ft-burn? miamicoin amount recipient) ) ) ;; checks if caller is Auth contract (define-private (is-authorized-auth) (is-eq contract-caller 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-auth) ) ;; SEND-MANY (define-public (send-many (recipients (list 200 { to: principal, amount: uint, memo: (optional (buff 34)) }))) (fold check-err (map send-token recipients) (ok true) ) ) (define-private (check-err (result (response bool uint)) (prior (response bool uint))) (match prior ok-value result err-value (err err-value) ) ) (define-private (send-token (recipient { to: principal, amount: uint, memo: (optional (buff 34)) })) (send-token-with-memo (get amount recipient) (get to recipient) (get memo recipient)) ) (define-private (send-token-with-memo (amount uint) (to principal) (memo (optional (buff 34)))) (let ( (transferOk (try! (transfer amount tx-sender to memo))) ) (ok transferOk) ) )
0xc513b769c261233865c43f101438bd6359636ecacfe34576e6424d6c2629174e
24,339
success
miamicoin-core-v1
;; MIAMICOIN CORE CONTRACT V1 ;; GENERAL CONFIGURATION (impl-trait 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.citycoin-core-trait.citycoin-core) (define-constant CONTRACT_OWNER tx-sender) ;; ERROR CODES (define-constant ERR_UNAUTHORIZED u1000) (define-constant ERR_USER_ALREADY_REGISTERED u1001) (define-constant ERR_USER_NOT_FOUND u1002) (define-constant ERR_USER_ID_NOT_FOUND u1003) (define-constant ERR_ACTIVATION_THRESHOLD_REACHED u1004) (define-constant ERR_CONTRACT_NOT_ACTIVATED u1005) (define-constant ERR_USER_ALREADY_MINED u1006) (define-constant ERR_INSUFFICIENT_COMMITMENT u1007) (define-constant ERR_INSUFFICIENT_BALANCE u1008) (define-constant ERR_USER_DID_NOT_MINE_IN_BLOCK u1009) (define-constant ERR_CLAIMED_BEFORE_MATURITY u1010) (define-constant ERR_NO_MINERS_AT_BLOCK u1011) (define-constant ERR_REWARD_ALREADY_CLAIMED u1012) (define-constant ERR_MINER_DID_NOT_WIN u1013) (define-constant ERR_NO_VRF_SEED_FOUND u1014) (define-constant ERR_STACKING_NOT_AVAILABLE u1015) (define-constant ERR_CANNOT_STACK u1016) (define-constant ERR_REWARD_CYCLE_NOT_COMPLETED u1017) (define-constant ERR_NOTHING_TO_REDEEM u1018) (define-constant ERR_UNABLE_TO_FIND_CITY_WALLET u1019) (define-constant ERR_CLAIM_IN_WRONG_CONTRACT u1020) ;; CITY WALLET MANAGEMENT ;; initial value for city wallet, set to this contract until initialized (define-data-var cityWallet principal 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-core-v1) ;; returns set city wallet principal (define-read-only (get-city-wallet) (var-get cityWallet) ) ;; protected function to update city wallet variable (define-public (set-city-wallet (newCityWallet principal)) (begin (asserts! (is-authorized-auth) (err ERR_UNAUTHORIZED)) (ok (var-set cityWallet newCityWallet)) ) ) ;; REGISTRATION (define-data-var activationBlock uint u340282366920938463463374607431768211455) (define-data-var activationDelay uint u150) (define-data-var activationReached bool false) (define-data-var activationThreshold uint u20) (define-data-var usersNonce uint u0) ;; returns Stacks block height registration was activated at plus activationDelay (define-read-only (get-activation-block) (let ( (activated (var-get activationReached)) ) (asserts! activated (err ERR_CONTRACT_NOT_ACTIVATED)) (ok (var-get activationBlock)) ) ) ;; returns activation delay (define-read-only (get-activation-delay) (var-get activationDelay) ) ;; returns activation status as boolean (define-read-only (get-activation-status) (var-get activationReached) ) ;; returns activation threshold (define-read-only (get-activation-threshold) (var-get activationThreshold) ) ;; returns number of registered users, used for activation and tracking user IDs (define-read-only (get-registered-users-nonce) (var-get usersNonce) ) ;; store user principal by user id (define-map Users uint principal ) ;; store user id by user principal (define-map UserIds principal uint ) ;; returns (some userId) or none (define-read-only (get-user-id (user principal)) (map-get? UserIds user) ) ;; returns (some userPrincipal) or none (define-read-only (get-user (userId uint)) (map-get? Users userId) ) ;; returns user ID if it has been created, or creates and returns new ID (define-private (get-or-create-user-id (user principal)) (match (map-get? UserIds user) value value (let ( (newId (+ u1 (var-get usersNonce))) ) (map-set Users newId user) (map-set UserIds user newId) (var-set usersNonce newId) newId ) ) ) ;; registers users that signal activation of contract until threshold is met (define-public (register-user (memo (optional (string-utf8 50)))) (let ( (newId (+ u1 (var-get usersNonce))) (threshold (var-get activationThreshold)) (initialized (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-auth is-initialized)) ) (asserts! initialized (err ERR_UNAUTHORIZED)) (asserts! (is-none (map-get? UserIds tx-sender)) (err ERR_USER_ALREADY_REGISTERED)) (asserts! (<= newId threshold) (err ERR_ACTIVATION_THRESHOLD_REACHED)) (if (is-some memo) (print memo) none ) (get-or-create-user-id tx-sender) (if (is-eq newId threshold) (let ( (activationBlockVal (+ block-height (var-get activationDelay))) ) (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-auth activate-core-contract (as-contract tx-sender) activationBlockVal)) (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token activate-token (as-contract tx-sender) activationBlockVal)) (try! (set-coinbase-thresholds)) (var-set activationReached true) (var-set activationBlock activationBlockVal) (ok true) ) (ok true) ) ) ) ;; MINING CONFIGURATION ;; define split to custodied wallet address for the city (define-constant SPLIT_CITY_PCT u30) ;; how long a miner must wait before block winner can claim their minted tokens (define-data-var tokenRewardMaturity uint u100) ;; At a given Stacks block height: ;; - how many miners were there ;; - what was the total amount submitted ;; - what was the total amount submitted to the city ;; - what was the total amount submitted to Stackers ;; - was the block reward claimed (define-map MiningStatsAtBlock uint { minersCount: uint, amount: uint, amountToCity: uint, amountToStackers: uint, rewardClaimed: bool } ) ;; returns map MiningStatsAtBlock at a given Stacks block height if it exists (define-read-only (get-mining-stats-at-block (stacksHeight uint)) (map-get? MiningStatsAtBlock stacksHeight) ) ;; returns map MiningStatsAtBlock at a given Stacks block height ;; or, an empty structure (define-read-only (get-mining-stats-at-block-or-default (stacksHeight uint)) (default-to { minersCount: u0, amount: u0, amountToCity: u0, amountToStackers: u0, rewardClaimed: false } (map-get? MiningStatsAtBlock stacksHeight) ) ) ;; At a given Stacks block height and user ID: ;; - what is their ustx commitment ;; - what are the low/high values (used for VRF) (define-map MinersAtBlock { stacksHeight: uint, userId: uint } { ustx: uint, lowValue: uint, highValue: uint, winner: bool } ) ;; returns true if a given miner has already mined at a given block height (define-read-only (has-mined-at-block (stacksHeight uint) (userId uint)) (is-some (map-get? MinersAtBlock { stacksHeight: stacksHeight, userId: userId }) ) ) ;; returns map MinersAtBlock at a given Stacks block height for a user ID (define-read-only (get-miner-at-block (stacksHeight uint) (userId uint)) (map-get? MinersAtBlock { stacksHeight: stacksHeight, userId: userId }) ) ;; returns map MinersAtBlock at a given Stacks block height for a user ID ;; or, an empty structure (define-read-only (get-miner-at-block-or-default (stacksHeight uint) (userId uint)) (default-to { highValue: u0, lowValue: u0, ustx: u0, winner: false } (map-get? MinersAtBlock { stacksHeight: stacksHeight, userId: userId })) ) ;; At a given Stacks block height: ;; - what is the max highValue from MinersAtBlock (used for VRF) (define-map MinersAtBlockHighValue uint uint ) ;; returns last high value from map MinersAtBlockHighValue (define-read-only (get-last-high-value-at-block (stacksHeight uint)) (default-to u0 (map-get? MinersAtBlockHighValue stacksHeight)) ) ;; At a given Stacks block height: ;; - what is the userId of miner who won this block (define-map BlockWinnerIds uint uint ) (define-read-only (get-block-winner-id (stacksHeight uint)) (map-get? BlockWinnerIds stacksHeight) ) ;; MINING ACTIONS (define-public (mine-tokens (amountUstx uint) (memo (optional (buff 34)))) (let ( (userId (get-or-create-user-id tx-sender)) ) (try! (mine-tokens-at-block userId block-height amountUstx memo)) (ok true) ) ) (define-public (mine-many (amounts (list 200 uint))) (begin (asserts! (get-activation-status) (err ERR_CONTRACT_NOT_ACTIVATED)) (asserts! (> (len amounts) u0) (err ERR_INSUFFICIENT_COMMITMENT)) (match (fold mine-single amounts (ok { userId: (get-or-create-user-id tx-sender), toStackers: u0, toCity: u0, stacksHeight: block-height })) okReturn (begin (asserts! (>= (stx-get-balance tx-sender) (+ (get toStackers okReturn) (get toCity okReturn))) (err ERR_INSUFFICIENT_BALANCE)) (if (> (get toStackers okReturn ) u0) (try! (stx-transfer? (get toStackers okReturn ) tx-sender (as-contract tx-sender))) false ) (try! (stx-transfer? (get toCity okReturn) tx-sender (var-get cityWallet))) (ok true) ) errReturn (err errReturn) ) ) ) (define-private (mine-single (amountUstx uint) (return (response { userId: uint, toStackers: uint, toCity: uint, stacksHeight: uint } uint ))) (match return okReturn (let ( (stacksHeight (get stacksHeight okReturn)) (rewardCycle (default-to u0 (get-reward-cycle stacksHeight))) (stackingActive (stacking-active-at-cycle rewardCycle)) (toCity (if stackingActive (/ (* SPLIT_CITY_PCT amountUstx) u100) amountUstx ) ) (toStackers (- amountUstx toCity)) ) (asserts! (not (has-mined-at-block stacksHeight (get userId okReturn))) (err ERR_USER_ALREADY_MINED)) (asserts! (> amountUstx u0) (err ERR_INSUFFICIENT_COMMITMENT)) (try! (set-tokens-mined (get userId okReturn) stacksHeight amountUstx toStackers toCity)) (ok (merge okReturn { toStackers: (+ (get toStackers okReturn) toStackers), toCity: (+ (get toCity okReturn) toCity), stacksHeight: (+ stacksHeight u1) } )) ) errReturn (err errReturn) ) ) (define-private (mine-tokens-at-block (userId uint) (stacksHeight uint) (amountUstx uint) (memo (optional (buff 34)))) (let ( (rewardCycle (default-to u0 (get-reward-cycle stacksHeight))) (stackingActive (stacking-active-at-cycle rewardCycle)) (toCity (if stackingActive (/ (* SPLIT_CITY_PCT amountUstx) u100) amountUstx ) ) (toStackers (- amountUstx toCity)) ) (asserts! (get-activation-status) (err ERR_CONTRACT_NOT_ACTIVATED)) (asserts! (not (has-mined-at-block stacksHeight userId)) (err ERR_USER_ALREADY_MINED)) (asserts! (> amountUstx u0) (err ERR_INSUFFICIENT_COMMITMENT)) (asserts! (>= (stx-get-balance tx-sender) amountUstx) (err ERR_INSUFFICIENT_BALANCE)) (try! (set-tokens-mined userId stacksHeight amountUstx toStackers toCity)) (if (is-some memo) (print memo) none ) (if stackingActive (try! (stx-transfer? toStackers tx-sender (as-contract tx-sender))) false ) (try! (stx-transfer? toCity tx-sender (var-get cityWallet))) (ok true) ) ) (define-private (set-tokens-mined (userId uint) (stacksHeight uint) (amountUstx uint) (toStackers uint) (toCity uint)) (let ( (blockStats (get-mining-stats-at-block-or-default stacksHeight)) (newMinersCount (+ (get minersCount blockStats) u1)) (minerLowVal (get-last-high-value-at-block stacksHeight)) (rewardCycle (unwrap! (get-reward-cycle stacksHeight) (err ERR_STACKING_NOT_AVAILABLE))) (rewardCycleStats (get-stacking-stats-at-cycle-or-default rewardCycle)) ) (map-set MiningStatsAtBlock stacksHeight { minersCount: newMinersCount, amount: (+ (get amount blockStats) amountUstx), amountToCity: (+ (get amountToCity blockStats) toCity), amountToStackers: (+ (get amountToStackers blockStats) toStackers), rewardClaimed: false } ) (map-set MinersAtBlock { stacksHeight: stacksHeight, userId: userId } { ustx: amountUstx, lowValue: (if (> minerLowVal u0) (+ minerLowVal u1) u0), highValue: (+ minerLowVal amountUstx), winner: false } ) (map-set MinersAtBlockHighValue stacksHeight (+ minerLowVal amountUstx) ) (if (> toStackers u0) (map-set StackingStatsAtCycle rewardCycle { amountUstx: (+ (get amountUstx rewardCycleStats) toStackers), amountToken: (get amountToken rewardCycleStats) } ) false ) (ok true) ) ) ;; MINING REWARD CLAIM ACTIONS ;; calls function to claim mining reward in active logic contract (define-public (claim-mining-reward (minerBlockHeight uint)) (begin (asserts! (or (is-eq (var-get shutdownHeight) u0) (< minerBlockHeight (var-get shutdownHeight))) (err ERR_CLAIM_IN_WRONG_CONTRACT)) (try! (claim-mining-reward-at-block tx-sender block-height minerBlockHeight)) (ok true) ) ) ;; 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-private (claim-mining-reward-at-block (user principal) (stacksHeight uint) (minerBlockHeight uint)) (let ( (maturityHeight (+ (var-get tokenRewardMaturity) minerBlockHeight)) (userId (unwrap! (get-user-id user) (err ERR_USER_ID_NOT_FOUND))) (blockStats (unwrap! (get-mining-stats-at-block minerBlockHeight) (err ERR_NO_MINERS_AT_BLOCK))) (minerStats (unwrap! (get-miner-at-block minerBlockHeight userId) (err ERR_USER_DID_NOT_MINE_IN_BLOCK))) (isMature (asserts! (> stacksHeight maturityHeight) (err ERR_CLAIMED_BEFORE_MATURITY))) (vrfSample (unwrap! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.citycoin-vrf get-random-uint-at-block maturityHeight) (err ERR_NO_VRF_SEED_FOUND))) (commitTotal (get-last-high-value-at-block minerBlockHeight)) (winningValue (mod vrfSample commitTotal)) ) (asserts! (not (get rewardClaimed blockStats)) (err ERR_REWARD_ALREADY_CLAIMED)) (asserts! (and (>= winningValue (get lowValue minerStats)) (<= winningValue (get highValue minerStats))) (err ERR_MINER_DID_NOT_WIN)) (try! (set-mining-reward-claimed userId minerBlockHeight)) (ok true) ) ) (define-private (set-mining-reward-claimed (userId uint) (minerBlockHeight uint)) (let ( (blockStats (get-mining-stats-at-block-or-default minerBlockHeight)) (minerStats (get-miner-at-block-or-default minerBlockHeight userId)) (user (unwrap! (get-user userId) (err ERR_USER_NOT_FOUND))) ) (map-set MiningStatsAtBlock minerBlockHeight { minersCount: (get minersCount blockStats), amount: (get amount blockStats), amountToCity: (get amountToCity blockStats), amountToStackers: (get amountToStackers blockStats), rewardClaimed: true } ) (map-set MinersAtBlock { stacksHeight: minerBlockHeight, userId: userId } { ustx: (get ustx minerStats), lowValue: (get lowValue minerStats), highValue: (get highValue minerStats), winner: true } ) (map-set BlockWinnerIds minerBlockHeight userId ) (try! (mint-coinbase user minerBlockHeight)) (ok true) ) ) (define-read-only (is-block-winner (user principal) (minerBlockHeight uint)) (is-block-winner-and-can-claim user minerBlockHeight false) ) (define-read-only (can-claim-mining-reward (user principal) (minerBlockHeight uint)) (is-block-winner-and-can-claim user minerBlockHeight true) ) (define-private (is-block-winner-and-can-claim (user principal) (minerBlockHeight uint) (testCanClaim bool)) (let ( (userId (unwrap! (get-user-id user) false)) (blockStats (unwrap! (get-mining-stats-at-block minerBlockHeight) false)) (minerStats (unwrap! (get-miner-at-block minerBlockHeight userId) false)) (maturityHeight (+ (var-get tokenRewardMaturity) minerBlockHeight)) (vrfSample (unwrap! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.citycoin-vrf get-random-uint-at-block maturityHeight) false)) (commitTotal (get-last-high-value-at-block minerBlockHeight)) (winningValue (mod vrfSample commitTotal)) ) (if (and (>= winningValue (get lowValue minerStats)) (<= winningValue (get highValue minerStats))) (if testCanClaim (not (get rewardClaimed blockStats)) true) false ) ) ) ;; STACKING CONFIGURATION (define-constant MAX_REWARD_CYCLES u32) (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)) ;; how long a reward cycle is (define-data-var rewardCycleLength uint u2100) ;; At a given reward cycle: ;; - how many Stackers were there ;; - what is the total uSTX submitted by miners ;; - what is the total amount of tokens stacked (define-map StackingStatsAtCycle uint { amountUstx: uint, amountToken: uint } ) ;; returns the total stacked tokens and committed uSTX for a given reward cycle (define-read-only (get-stacking-stats-at-cycle (rewardCycle uint)) (map-get? StackingStatsAtCycle rewardCycle) ) ;; returns the total stacked tokens and committed uSTX for a given reward cycle ;; or, an empty structure (define-read-only (get-stacking-stats-at-cycle-or-default (rewardCycle uint)) (default-to { amountUstx: u0, amountToken: u0 } (map-get? StackingStatsAtCycle rewardCycle)) ) ;; At a given reward cycle and user ID: ;; - what is the total tokens Stacked? ;; - how many tokens should be returned? (based on Stacking period) (define-map StackerAtCycle { rewardCycle: uint, userId: uint } { amountStacked: uint, toReturn: uint } ) (define-read-only (get-stacker-at-cycle (rewardCycle uint) (userId uint)) (map-get? StackerAtCycle { rewardCycle: rewardCycle, userId: userId }) ) (define-read-only (get-stacker-at-cycle-or-default (rewardCycle uint) (userId uint)) (default-to { amountStacked: u0, toReturn: u0 } (map-get? StackerAtCycle { rewardCycle: rewardCycle, userId: userId })) ) ;; get the reward cycle for a given Stacks block height (define-read-only (get-reward-cycle (stacksHeight uint)) (let ( (firstStackingBlock (var-get activationBlock)) (rcLen (var-get rewardCycleLength)) ) (if (>= stacksHeight firstStackingBlock) (some (/ (- stacksHeight firstStackingBlock) rcLen)) none) ) ) ;; determine if stacking is active in a given cycle (define-read-only (stacking-active-at-cycle (rewardCycle uint)) (is-some (get amountToken (map-get? StackingStatsAtCycle rewardCycle)) ) ) ;; get the first Stacks block height for a given reward cycle. (define-read-only (get-first-stacks-block-in-reward-cycle (rewardCycle uint)) (+ (var-get activationBlock) (* (var-get rewardCycleLength) rewardCycle)) ) ;; getter for get-entitled-stacking-reward that specifies block height (define-read-only (get-stacking-reward (userId uint) (targetCycle uint)) (get-entitled-stacking-reward userId targetCycle block-height) ) ;; get uSTX a Stacker can claim, given reward cycle they stacked in and current block height ;; this method only returns a positive value if: ;; - the current block height is in a subsequent reward cycle ;; - the stacker actually locked up tokens in the target reward cycle ;; - the stacker locked up _enough_ tokens to get at least one uSTX ;; it is possible to Stack tokens and not receive uSTX: ;; - if no miners commit during this reward cycle ;; - the amount stacked by user is too few that you'd be entitled to less than 1 uSTX (define-private (get-entitled-stacking-reward (userId uint) (targetCycle uint) (stacksHeight uint)) (let ( (rewardCycleStats (get-stacking-stats-at-cycle-or-default targetCycle)) (stackerAtCycle (get-stacker-at-cycle-or-default targetCycle userId)) (totalUstxThisCycle (get amountUstx rewardCycleStats)) (totalStackedThisCycle (get amountToken rewardCycleStats)) (userStackedThisCycle (get amountStacked stackerAtCycle)) ) (match (get-reward-cycle stacksHeight) currentCycle (if (or (<= currentCycle targetCycle) (is-eq u0 userStackedThisCycle)) ;; this cycle hasn't finished, or Stacker contributed nothing u0 ;; (totalUstxThisCycle * userStackedThisCycle) / totalStackedThisCycle (/ (* totalUstxThisCycle userStackedThisCycle) totalStackedThisCycle) ) ;; before first reward cycle u0 ) ) ) ;; STACKING ACTIONS (define-public (stack-tokens (amountTokens uint) (lockPeriod uint)) (let ( (userId (get-or-create-user-id tx-sender)) ) (try! (stack-tokens-at-cycle tx-sender userId amountTokens block-height lockPeriod)) (ok true) ) ) (define-private (stack-tokens-at-cycle (user principal) (userId uint) (amountTokens uint) (startHeight uint) (lockPeriod uint)) (let ( (currentCycle (unwrap! (get-reward-cycle startHeight) (err ERR_STACKING_NOT_AVAILABLE))) (targetCycle (+ u1 currentCycle)) (commitment { stackerId: userId, amount: amountTokens, first: targetCycle, last: (+ targetCycle lockPeriod) }) ) (asserts! (get-activation-status) (err ERR_CONTRACT_NOT_ACTIVATED)) (asserts! (and (> lockPeriod u0) (<= lockPeriod MAX_REWARD_CYCLES)) (err ERR_CANNOT_STACK)) (asserts! (> amountTokens u0) (err ERR_CANNOT_STACK)) (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amountTokens tx-sender (as-contract tx-sender) none)) (match (fold stack-tokens-closure REWARD_CYCLE_INDEXES (ok commitment)) okValue (ok true) errValue (err errValue) ) ) ) (define-private (stack-tokens-closure (rewardCycleIdx uint) (commitmentResponse (response { stackerId: uint, amount: uint, first: uint, last: uint } uint ))) (match commitmentResponse commitment (let ( (stackerId (get stackerId commitment)) (amountToken (get amount commitment)) (firstCycle (get first commitment)) (lastCycle (get last commitment)) (targetCycle (+ firstCycle rewardCycleIdx)) (stackerAtCycle (get-stacker-at-cycle-or-default targetCycle stackerId)) (amountStacked (get amountStacked stackerAtCycle)) (toReturn (get toReturn stackerAtCycle)) ) (begin (if (and (>= targetCycle firstCycle) (< targetCycle lastCycle)) (begin (if (is-eq targetCycle (- lastCycle u1)) (set-tokens-stacked stackerId targetCycle amountToken amountToken) (set-tokens-stacked stackerId targetCycle amountToken u0) ) true ) false ) commitmentResponse ) ) errValue commitmentResponse ) ) (define-private (set-tokens-stacked (userId uint) (targetCycle uint) (amountStacked uint) (toReturn uint)) (let ( (rewardCycleStats (get-stacking-stats-at-cycle-or-default targetCycle)) (stackerAtCycle (get-stacker-at-cycle-or-default targetCycle userId)) ) (map-set StackingStatsAtCycle targetCycle { amountUstx: (get amountUstx rewardCycleStats), amountToken: (+ amountStacked (get amountToken rewardCycleStats)) } ) (map-set StackerAtCycle { rewardCycle: targetCycle, userId: userId } { amountStacked: (+ amountStacked (get amountStacked stackerAtCycle)), toReturn: (+ toReturn (get toReturn stackerAtCycle)) } ) ) ) ;; STACKING REWARD CLAIMS ;; calls function to claim stacking reward in active logic contract (define-public (claim-stacking-reward (targetCycle uint)) (begin (try! (claim-stacking-reward-at-cycle tx-sender block-height targetCycle)) (ok true) ) ) (define-private (claim-stacking-reward-at-cycle (user principal) (stacksHeight uint) (targetCycle uint)) (let ( (currentCycle (unwrap! (get-reward-cycle stacksHeight) (err ERR_STACKING_NOT_AVAILABLE))) (userId (unwrap! (get-user-id user) (err ERR_USER_ID_NOT_FOUND))) (entitledUstx (get-entitled-stacking-reward userId targetCycle stacksHeight)) (stackerAtCycle (get-stacker-at-cycle-or-default targetCycle userId)) (toReturn (get toReturn stackerAtCycle)) ) (asserts! (or (is-eq true (var-get isShutdown)) (> currentCycle targetCycle)) (err ERR_REWARD_CYCLE_NOT_COMPLETED)) (asserts! (or (> toReturn u0) (> entitledUstx u0)) (err ERR_NOTHING_TO_REDEEM)) ;; disable ability to claim again (map-set StackerAtCycle { rewardCycle: targetCycle, userId: userId } { amountStacked: u0, toReturn: u0 } ) ;; send back tokens if user was eligible (if (> toReturn u0) (try! (as-contract (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer toReturn tx-sender user none))) true ) ;; send back rewards if user was eligible (if (> entitledUstx u0) (try! (as-contract (stx-transfer? entitledUstx tx-sender user))) true ) (ok true) ) ) ;; TOKEN CONFIGURATION ;; store block height at each halving, set by register-user in core contract (define-data-var coinbaseThreshold1 uint u0) (define-data-var coinbaseThreshold2 uint u0) (define-data-var coinbaseThreshold3 uint u0) (define-data-var coinbaseThreshold4 uint u0) (define-data-var coinbaseThreshold5 uint u0) (define-private (set-coinbase-thresholds) (let ( (coinbaseAmounts (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-coinbase-thresholds))) ) (var-set coinbaseThreshold1 (get coinbaseThreshold1 coinbaseAmounts)) (var-set coinbaseThreshold2 (get coinbaseThreshold2 coinbaseAmounts)) (var-set coinbaseThreshold3 (get coinbaseThreshold3 coinbaseAmounts)) (var-set coinbaseThreshold4 (get coinbaseThreshold4 coinbaseAmounts)) (var-set coinbaseThreshold5 (get coinbaseThreshold5 coinbaseAmounts)) (ok true) ) ) ;; return coinbase thresholds if contract activated (define-read-only (get-coinbase-thresholds) (let ( (activated (var-get activationReached)) ) (asserts! activated (err ERR_CONTRACT_NOT_ACTIVATED)) (ok { coinbaseThreshold1: (var-get coinbaseThreshold1), coinbaseThreshold2: (var-get coinbaseThreshold2), coinbaseThreshold3: (var-get coinbaseThreshold3), coinbaseThreshold4: (var-get coinbaseThreshold4), coinbaseThreshold5: (var-get coinbaseThreshold5) }) ) ) ;; function for deciding how many tokens to mint, depending on when they were mined (define-read-only (get-coinbase-amount (minerBlockHeight uint)) (begin ;; if contract is not active, return 0 (asserts! (>= minerBlockHeight (var-get activationBlock)) u0) ;; if contract is active, return based on issuance schedule ;; halvings occur every 210,000 blocks for 1,050,000 Stacks blocks ;; then mining continues indefinitely with 3,125 tokens as the reward (asserts! (> minerBlockHeight (var-get coinbaseThreshold1)) (if (<= (- minerBlockHeight (var-get activationBlock)) u10000) ;; bonus reward first 10,000 blocks u250000 ;; standard reward remaining 200,000 blocks until 1st halving u100000 ) ) ;; computations based on each halving threshold (asserts! (> minerBlockHeight (var-get coinbaseThreshold2)) u50000) (asserts! (> minerBlockHeight (var-get coinbaseThreshold3)) u25000) (asserts! (> minerBlockHeight (var-get coinbaseThreshold4)) u12500) (asserts! (> minerBlockHeight (var-get coinbaseThreshold5)) u6250) ;; default value after 5th halving u3125 ) ) ;; mint new tokens for claimant who won at given Stacks block height (define-private (mint-coinbase (recipient principal) (stacksHeight uint)) (as-contract (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token mint (get-coinbase-amount stacksHeight) recipient)) ) ;; UTILITIES (define-data-var shutdownHeight uint u0) (define-data-var isShutdown bool false) ;; stop mining and stacking operations ;; in preparation for a core upgrade (define-public (shutdown-contract (stacksHeight uint)) (begin ;; only allow shutdown request from AUTH (asserts! (is-authorized-auth) (err ERR_UNAUTHORIZED)) ;; set variables to disable mining/stacking in CORE (var-set activationReached false) (var-set shutdownHeight stacksHeight) ;; set variable to allow for all stacking claims (var-set isShutdown true) (ok true) ) ) ;; check if contract caller is city wallet (define-private (is-authorized-city) (is-eq contract-caller (var-get cityWallet)) ) ;; check if contract caller is contract owner (define-private (is-authorized-owner) (is-eq contract-caller CONTRACT_OWNER) ) ;; checks if caller is Auth contract (define-private (is-authorized-auth) (is-eq contract-caller 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-auth) )
0x224eb853ab072591c382b1c917136dcdd6590df80ab646bfed432d779612258f
24,341
success
mine-many
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP36MCQHXPP0DZ2KPC1KEY6ERC8GKB6QVCAK0PQYG) (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)))
0xfd3d27c4cbcf00fbeac9f0cc717ff598469724b390d145a02a159142819929e5
24,345
success
mine-tokens
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE) (define-fungible-token novel-token-19) (begin (ft-mint? novel-token-19 u12 sender)) (begin (ft-transfer? novel-token-19 u2 sender recipient)) (define-non-fungible-token hello-nft uint) (begin (nft-mint? hello-nft u1 sender)) (begin (nft-mint? hello-nft u2 sender)) (begin (nft-transfer? hello-nft u1 sender recipient)) (define-public (test-emit-event) (begin (print "Event! Hello world") (ok u1))) (begin (test-emit-event)) (define-public (test-event-types) (begin (unwrap-panic (ft-mint? novel-token-19 u3 recipient)) (unwrap-panic (nft-mint? hello-nft u2 recipient)) (unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)) (unwrap-panic (stx-burn? u20 tx-sender)) (ok u1))) (define-map store {key: (buff 32)} {value: (buff 32)}) (define-public (get-value (key (buff 32))) (begin (match (map-get? store {key: key}) entry (ok (get value entry)) (err 0)))) (define-public (set-value (key (buff 32)) (value (buff 32))) (begin (map-set store {key: key} {value: value}) (ok u1)))
0x50a4dbe69d4b4ddd424ad0748db18462b905df4925ba23831fd4c7b2c6ec1ad3
24,389
success
mine-tokens
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE) (define-fungible-token novel-token-19) (begin (ft-mint? novel-token-19 u12 sender)) (begin (ft-transfer? novel-token-19 u2 sender recipient)) (define-non-fungible-token hello-nft uint) (begin (nft-mint? hello-nft u1 sender)) (begin (nft-mint? hello-nft u2 sender)) (begin (nft-transfer? hello-nft u1 sender recipient)) (define-public (test-emit-event) (begin (print "Event! Hello world") (ok u1))) (begin (test-emit-event)) (define-public (test-event-types) (begin (unwrap-panic (ft-mint? novel-token-19 u3 recipient)) (unwrap-panic (nft-mint? hello-nft u2 recipient)) (unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)) (unwrap-panic (stx-burn? u20 tx-sender)) (ok u1))) (define-map store {key: (buff 32)} {value: (buff 32)}) (define-public (get-value (key (buff 32))) (begin (match (map-get? store {key: key}) entry (ok (get value entry)) (err 0)))) (define-public (set-value (key (buff 32)) (value (buff 32))) (begin (map-set store {key: key} {value: value}) (ok u1)))
0x50a4dbe69d4b4ddd424ad0748db18462b905df4925ba23831fd4c7b2c6ec1ad3
24,389
success
prominent-copper-dingo
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP3EP89T5JR0WRXJDAY6MVXW68F6EA800PKGS5BWG) (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)))
0x581b567a94bf60d89efd4b10efce74c00197986113d1cc56f6dfb091f3a61d45
24,391
success
mine-many
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE) (define-fungible-token novel-token-19) (begin (ft-mint? novel-token-19 u12 sender)) (begin (ft-transfer? novel-token-19 u2 sender recipient)) (define-non-fungible-token hello-nft uint) (begin (nft-mint? hello-nft u1 sender)) (begin (nft-mint? hello-nft u2 sender)) (begin (nft-transfer? hello-nft u1 sender recipient)) (define-public (test-emit-event) (begin (print "Event! Hello world") (ok u1))) (begin (test-emit-event)) (define-public (test-event-types) (begin (unwrap-panic (ft-mint? novel-token-19 u3 recipient)) (unwrap-panic (nft-mint? hello-nft u2 recipient)) (unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)) (unwrap-panic (stx-burn? u20 tx-sender)) (ok u1))) (define-map store {key: (buff 32)} {value: (buff 32)}) (define-public (get-value (key (buff 32))) (begin (match (map-get? store {key: key}) entry (ok (get value entry)) (err 0)))) (define-public (set-value (key (buff 32)) (value (buff 32))) (begin (map-set store {key: key} {value: value}) (ok u1)))
0x64bcfd33b15fe946eb2f6054bd9e519fcb2bfc3bc9b7f2730830cd6028282ebe
24,391
success
prime-cyan-guimine-manyneafowl
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE) (define-fungible-token novel-token-19) (begin (ft-mint? novel-token-19 u12 sender)) (begin (ft-transfer? novel-token-19 u2 sender recipient)) (define-non-fungible-token hello-nft uint) (begin (nft-mint? hello-nft u1 sender)) (begin (nft-mint? hello-nft u2 sender)) (begin (nft-transfer? hello-nft u1 sender recipient)) (define-public (test-emit-event) (begin (print "Event! Hello world") (ok u1))) (begin (test-emit-event)) (define-public (test-event-types) (begin (unwrap-panic (ft-mint? novel-token-19 u3 recipient)) (unwrap-panic (nft-mint? hello-nft u2 recipient)) (unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)) (unwrap-panic (stx-burn? u20 tx-sender)) (ok u1))) (define-map store {key: (buff 32)} {value: (buff 32)}) (define-public (get-value (key (buff 32))) (begin (match (map-get? store {key: key}) entry (ok (get value entry)) (err 0)))) (define-public (set-value (key (buff 32)) (value (buff 32))) (begin (map-set store {key: key} {value: value}) (ok u1)))
0x7ced7e17b2b814aaec7006c59744d04ea9d1922ceaee36f515a2fde534b786ae
24,420
success
claim-stacking-rewardl
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE) (define-fungible-token novel-token-19) (begin (ft-mint? novel-token-19 u12 sender)) (begin (ft-transfer? novel-token-19 u2 sender recipient)) (define-non-fungible-token hello-nft uint) (begin (nft-mint? hello-nft u1 sender)) (begin (nft-mint? hello-nft u2 sender)) (begin (nft-transfer? hello-nft u1 sender recipient)) (define-public (test-emit-event) (begin (print "Event! Hello world") (ok u1))) (begin (test-emit-event)) (define-public (test-event-types) (begin (unwrap-panic (ft-mint? novel-token-19 u3 recipient)) (unwrap-panic (nft-mint? hello-nft u2 recipient)) (unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)) (unwrap-panic (stx-burn? u20 tx-sender)) (ok u1))) (define-map store {key: (buff 32)} {value: (buff 32)}) (define-public (get-value (key (buff 32))) (begin (match (map-get? store {key: key}) entry (ok (get value entry)) (err 0)))) (define-public (set-value (key (buff 32)) (value (buff 32))) (begin (map-set store {key: key} {value: value}) (ok u1)))
0x0adce52075af2db6c18d6d38dc327f836ed5307737dff929905370033d539ef5
24,420
success
purring-aquamarine-goat
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP3G2MF3HZA81PXBF6DKEGTBWTJKA8444XY6QHKYS) (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)))
0x5ddc251a316e857801618e9a3e7cc6207d8528aa6f051ceb16b222042a17c18a
24,519
success
boomboxes-cycle-14
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) (define-non-fungible-token b-14 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 u100000000) (define-constant time-limit u695150) ;; 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-14 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-14 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-14 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-14 id))) (define-read-only (get-owner-raw? (id uint)) (nft-get-owner? b-14 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/bafkreihjmmwgqi6aapcbqxroguz5j2pal623t22g4uuw2s5v6kdbavks2q"))) ;; 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"))
0xcd432c449dce95625c29193e158c9e94af26c2c970760fb4fd2f6bc99140c7d9
24,567
success
claim-stacking-reward
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP13AD8N820ECQM69DMBP0TFHV43MMWBNFJ68KTTE) (define-fungible-token novel-token-19) (begin (ft-mint? novel-token-19 u12 sender)) (begin (ft-transfer? novel-token-19 u2 sender recipient)) (define-non-fungible-token hello-nft uint) (begin (nft-mint? hello-nft u1 sender)) (begin (nft-mint? hello-nft u2 sender)) (begin (nft-transfer? hello-nft u1 sender recipient)) (define-public (test-emit-event) (begin (print "Event! Hello world") (ok u1))) (begin (test-emit-event)) (define-public (test-event-types) (begin (unwrap-panic (ft-mint? novel-token-19 u3 recipient)) (unwrap-panic (nft-mint? hello-nft u2 recipient)) (unwrap-panic (stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)) (unwrap-panic (stx-burn? u20 tx-sender)) (ok u1))) (define-map store {key: (buff 32)} {value: (buff 32)}) (define-public (get-value (key (buff 32))) (begin (match (map-get? store {key: key}) entry (ok (get value entry)) (err 0)))) (define-public (set-value (key (buff 32)) (value (buff 32))) (begin (map-set store {key: key} {value: value}) (ok u1)))
0x4c88b49707db1791a803d9498449935eb201119036294eb632bba689b0c146c5
24,599
success
miamicoin-core-v1
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP2YSTAQBK2YGPTSHEF9NFPXCQCG5BB4RX6VNFGPE) (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)))
0x59b6193694e493baaeb606dc332fc9755bbd4503cdc607bd2394a88897677b59
24,605
success
miserable-jade-ape
;; hello-world contract (define-constant sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) (define-constant recipient 'SP2MT9KM3H9AM79EW0ER6SQTWJB2HDB3CY6GPKH8V) (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)))
0xf0cb3635d7186198ba822236a57888f46a9dc3b0b50d5c234f9b3be40011d08a
24,628
success