Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is a readme file
# High Level
3 changes: 3 additions & 0 deletions high-level_contracts/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tests/** linguist-vendored
vitest.config.js linguist-vendored
* text=lf
13 changes: 13 additions & 0 deletions high-level_contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

**/settings/Mainnet.toml
**/settings/Testnet.toml
.cache/**
history.txt

logs
*.log
npm-debug.log*
coverage
*.info
costs-reports.json
node_modules
4 changes: 4 additions & 0 deletions high-level_contracts/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

{
"files.eol": "\n"
}
19 changes: 19 additions & 0 deletions high-level_contracts/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

{
"version": "2.0.0",
"tasks": [
{
"label": "check contracts",
"group": "test",
"type": "shell",
"command": "clarinet check"
},
{
"type": "npm",
"script": "test",
"group": "test",
"problemMatcher": [],
"label": "npm test"
}
]
}
34 changes: 34 additions & 0 deletions high-level_contracts/Clarinet.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[project]
name = 'high-level_contracts'
description = ''
authors = []
telemetry = true
cache_dir = '.\.cache'
requirements = []
[contracts.prediction-market-bets]
path = 'contracts/prediction-market-bets.clar'
clarity_version = 2
epoch = 2.5

[contracts.prediction-market-core]
path = 'contracts/prediction-market-core.clar'
clarity_version = 2
epoch = 2.5

[contracts.prediction-market-payout]
path = 'contracts/prediction-market-payout.clar'
clarity_version = 2
epoch = 2.5

[contracts.prediction-market_markets]
path = 'contracts/prediction-market_markets.clar'
clarity_version = 2
epoch = 2.5
[repl.analysis]
passes = ['check_checker']

[repl.analysis.check_checker]
strict = false
trusted_sender = false
trusted_caller = false
callee_filter = false
168 changes: 168 additions & 0 deletions high-level_contracts/contracts/prediction-market-bets.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
;; Prediction Market Betting Contract

;; Constants
(define-constant CONTRACT_OWNER tx-sender)
(define-constant ERR_UNAUTHORIZED (err u100))
(define-constant ERR_INVALID_MARKET (err u101))
(define-constant ERR_INVALID_BET_AMOUNT (err u102))
(define-constant ERR_MARKET_CLOSED (err u103))
(define-constant ERR_INSUFFICIENT_BALANCE (err u104))
(define-constant ERR_MARKET_NOT_RESOLVED (err u105))

;; Data Variables
(define-data-var total-bets uint u0)

;; Maps
(define-map markets
{ market-id: uint }
{
creator: principal,
description: (string-ascii 256),
options: (list 10 (string-ascii 64)),
total-pool: uint,
is-open: bool,
winning-option: (optional uint)
}
)

(define-map bets
{ market-id: uint, better: principal }
{
amount: uint,
option: uint
}
)

(define-map user-balances principal uint)

;; Private Functions
(define-private (transfer-stx (amount uint) (sender principal) (recipient principal))
(stx-transfer? amount sender recipient)
)

;; Public Functions
(define-public (create-market (description (string-ascii 256)) (options (list 10 (string-ascii 64))))
(let ((market-id (var-get total-bets)))
(begin
(asserts! (> (len options) u0) ERR_INVALID_MARKET)
(map-set markets
{ market-id: market-id }
{
creator: tx-sender,
description: description,
options: options,
total-pool: u0,
is-open: true,
winning-option: none
}
)
(var-set total-bets (+ market-id u1))
(ok market-id)
)
)
)

(define-public (place-bet (market-id uint) (amount uint) (option uint))
(let (
(market (unwrap! (map-get? markets { market-id: market-id }) ERR_INVALID_MARKET))
(user-balance (default-to u0 (map-get? user-balances tx-sender)))
)
(begin
(asserts! (get is-open market) ERR_MARKET_CLOSED)
(asserts! (>= user-balance amount) ERR_INSUFFICIENT_BALANCE)
(asserts! (> amount u0) ERR_INVALID_BET_AMOUNT)
(asserts! (< option (len (get options market))) ERR_INVALID_BET_AMOUNT)

(map-set bets
{ market-id: market-id, better: tx-sender }
{ amount: amount, option: option }
)
(map-set markets
{ market-id: market-id }
(merge market { total-pool: (+ (get total-pool market) amount) })
)
(map-set user-balances
tx-sender
(- user-balance amount)
)
(ok true)
)
)
)

(define-public (resolve-market (market-id uint) (winning-option uint))
(let ((market (unwrap! (map-get? markets { market-id: market-id }) ERR_INVALID_MARKET)))
(begin
(asserts! (is-eq (get creator market) tx-sender) ERR_UNAUTHORIZED)
(asserts! (get is-open market) ERR_MARKET_CLOSED)
(asserts! (< winning-option (len (get options market))) ERR_INVALID_BET_AMOUNT)

(map-set markets
{ market-id: market-id }
(merge market {
is-open: false,
winning-option: (some winning-option)
})
)
(ok true)
)
)
)

(define-public (claim-winnings (market-id uint))
(let (
(market (unwrap! (map-get? markets { market-id: market-id }) ERR_INVALID_MARKET))
(bet (unwrap! (map-get? bets { market-id: market-id, better: tx-sender }) ERR_INVALID_BET_AMOUNT))
(winning-option (unwrap! (get winning-option market) ERR_MARKET_NOT_RESOLVED))
)
(begin
(asserts! (not (get is-open market)) ERR_MARKET_NOT_RESOLVED)
(asserts! (is-eq (get option bet) winning-option) ERR_INVALID_BET_AMOUNT)

(let (
(payout (/ (* (get amount bet) (get total-pool market)) (get amount bet)))
)
(map-delete bets { market-id: market-id, better: tx-sender })
(map-set user-balances
tx-sender
(+ (default-to u0 (map-get? user-balances tx-sender)) payout)
)
(ok payout)
)
)
)
)

(define-public (deposit-funds (amount uint))
(let ((current-balance (default-to u0 (map-get? user-balances tx-sender))))
(begin
(try! (stx-transfer? amount tx-sender (as-contract tx-sender)))
(map-set user-balances tx-sender (+ current-balance amount))
(ok true)
)
)
)

(define-public (withdraw-funds (amount uint))
(let ((current-balance (default-to u0 (map-get? user-balances tx-sender))))
(begin
(asserts! (>= current-balance amount) ERR_INSUFFICIENT_BALANCE)
(try! (as-contract (stx-transfer? amount (as-contract tx-sender) tx-sender)))
(map-set user-balances tx-sender (- current-balance amount))
(ok true)
)
)
)

;; Read-only Functions
(define-read-only (get-market (market-id uint))
(map-get? markets { market-id: market-id })
)

(define-read-only (get-bet (market-id uint) (better principal))
(map-get? bets { market-id: market-id, better: better })
)

(define-read-only (get-balance (user principal))
(default-to u0 (map-get? user-balances user))
)
Loading