GameFi
Ask or search…
K

Smart Contract

Check it on Mainnet

Explanations

Traits used

These traits enforce those standard are implemented in this smart contract
1
(use-trait nft-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
2
(use-trait ft-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)

Constants

Used for signaling different errors , an ok messages and the default price used if one can't be found for the lobby
1
(define-constant ERR-NOT-AUTHORIZED (err u401))
2
(define-constant ERR-NOT-FOUND (err u404))
3
(define-constant ERR-NOT-ACTIVE (err u403))
4
(define-constant ERR-ALREADY-JOINED (err u405))
5
(define-constant ERR-JOIN-FAILED (err u500))
6
(define-constant OK-SUCCESS u200)
7
(define-constant DEFAULT-PRICE u100)

Variable Data Stored - Maps and Vars

Store the information for each lobby.
Store the highscore of each user to that lobby.
Keep the count of created lobbies, also used as id for the next lobby.
Keep the admin that makes the smart contract calls to submit highscores, pay users, end lobbies etc.
1
(define-map lobbies {id: uint} {owner: principal, description: (string-ascii 99), balance: uint, price: uint, factor: uint, commission: uint, mapy: (string-ascii 30), length: (string-ascii 10), traffic: (string-ascii 10), curves: (string-ascii 10), hours: uint, active: bool})
2
(define-map scoreboard {lobby-id: uint, address: principal} {score: uint, rank: uint, sum-rank-factor: uint, rank-factor: uint, rewards: uint, rac: uint, nft: (string-ascii 99)})
3
(define-data-var lobby-count uint u0)
4
(define-data-var contract-owner principal tx-sender)

General Functions

Functions that anyone can call:
Create a lobby.
Join a lobby.
Get a lobby info - read only. Can get the information without paying STX directly calling the endpoint, as it does not write information to the blockchain.
Get the high score of a user address for a specified lobby-id.
1
(define-public (create-lobby
2
(description (string-ascii 99)) (price uint) (factor uint) (commission uint)
3
(mapy (string-ascii 30)) (length (string-ascii 10)) (traffic (string-ascii 10)) (curves (string-ascii 10)) (hours uint)
4
)
5
(let ((lobby-id (increment-lobby-count)))
6
(map-set lobbies {id: lobby-id} { owner: tx-sender, description: description, balance: u0, price: price, factor: factor, commission: commission,
7
mapy: mapy, length: length, traffic: traffic, curves: curves, hours: hours, active: true
8
})
9
(try! (join lobby-id))
10
(ok lobby-id)))
11
12
(define-public (join (id uint))
13
(let ((entry-price (default-to DEFAULT-PRICE (get price (map-get? lobbies {id: id}))))
14
(joined (map-insert scoreboard {lobby-id: id, address: tx-sender} {score: u0, rank: u0, sum-rank-factor: u0, rank-factor: u0, rewards: u0, rac: u0, nft: ""})))
15
(unwrap-panic (map-get? lobbies {id: id}))
16
(asserts! (default-to false (get active (map-get? lobbies {id: id}))) ERR-NOT-ACTIVE)
17
(asserts! joined ERR-ALREADY-JOINED)
18
(add-balance id tx-sender entry-price)
19
(print {action: "join", lobby-id: id, address: tx-sender })
20
(ok OK-SUCCESS)))
21
22
(define-read-only (get-lobby (id uint))
23
(ok (unwrap-panic (map-get? lobbies {id: id}))))
24
25
(define-read-only (get-score (lobby-id uint) (address principal))
26
(ok (unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address}))))

Admin Functions

Functions are public but only the admin can pass the asserts to make the calls. This is done for security reasons. Without it, anyone would be able to abuse the information. It also facilitates transparency on-chain by letting everybody see what the admin called.

Simple functions

Disable lobby - ends a specific lobby. After ending it high scores cannot be uploaded to it and payments cannot be done to the users from it.
Set owner - change the admin of the smart contract. The old address will not be able to call the admin functions after the call is confirmed, while the new address will be able afterwards.
1
(define-public (disable-lobby (id uint))
2
(begin
3
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
4
(match
5
(map-get? lobbies {id: id})
6
lobby
7
(map-set lobbies {id: id} (merge lobby {active: false}))
8
false)
9
(ok true)))
10
11
(define-public (set-owner (new-owner principal))
12
(begin
13
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
14
(var-set contract-owner new-owner)
15
(ok true)))

Complex functions

Publish - posts the high scores to the blockchain. It gets from the admin a list of maximum 50 high scores for one or more active lobbies and sets them in the scoreboard map.
1
(define-public (publish-result-many (run-result (list 50 { lobby-id: uint, address: principal, score: uint, rank: uint, sum-rank-factor: uint, rank-factor: uint, rewards: uint, rac: uint, nft: (string-ascii 99)})))
2
(fold check-err
3
(map publish-result run-result)
4
(ok true)))
5
6
(define-private (publish-result (run-result { lobby-id: uint, address: principal, score: uint, rank: uint, sum-rank-factor: uint, rank-factor: uint, rewards: uint, rac: uint, nft: (string-ascii 99)}))
7
(publish-only (get lobby-id run-result) (get address run-result) (get score run-result) (get rank run-result) (get sum-rank-factor run-result) (get rank-factor run-result) (get rewards run-result) (get rac run-result) (get nft run-result)))
8
9
(define-private (publish-only (lobby-id uint) (address principal) (score uint) (rank uint) (sum-rank-factor uint) (rank-factor uint) (rewards uint) (rac uint) (nft (string-ascii 99)))
10
(let
11
((publishOk (try! (publish lobby-id address score rank sum-rank-factor rank-factor rewards rac nft))))
12
(ok publishOk)))
13
14
(define-private (publish (lobby-id uint) (address principal) (score uint) (rank uint) (sum-rank-factor uint) (rank-factor uint) (rewards uint) (rac uint) (nft (string-ascii 99)))
15
(begin
16
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
17
(unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address}))
18
(asserts! (default-to false (get active (map-get? lobbies {id: lobby-id}))) ERR-NOT-ACTIVE)
19
(map-set scoreboard {lobby-id: lobby-id, address: address} {score: score, rank: rank, sum-rank-factor: sum-rank-factor, rank-factor: rank-factor, rewards: rewards, rac: rac, nft: nft})
20
(print {action: "publish", lobby-id: lobby-id, address: address, score: score, rank: rank, sum-rank-factor: sum-rank-factor, rank-factor: rank-factor, rewards: rewards, rac: rac, nft: nft})
21
(ok true)))
Finish - when a lobby is done, finish it by submitting the last high scores. Calculate off-chain the rewards for each player who took part in that lobby and call it on-chain to distribute them directly from the pool.
1
(define-public (finish-result-many (run-result (list 50 { lobby-id: uint, address: principal, score: uint, rank: uint, sum-rank-factor: uint, rank-factor: uint, rewards: uint, rac: uint, nft: (string-ascii 99)})))
2
(fold check-err
3
(map finish-result run-result)
4
(ok true)))
5
6
(define-private (finish-result (run-result { lobby-id: uint, address: principal, score: uint, rank: uint, sum-rank-factor: uint, rank-factor: uint, rewards: uint, rac: uint, nft: (string-ascii 99)}))
7
(finish-only (get lobby-id run-result) (get address run-result) (get score run-result) (get rank run-result) (get sum-rank-factor run-result) (get rank-factor run-result) (get rewards run-result) (get rac run-result) (get nft run-result)))
8
9
(define-private (finish-only (lobby-id uint) (address principal) (score uint) (rank uint) (sum-rank-factor uint) (rank-factor uint) (rewards uint) (rac uint) (nft (string-ascii 99)))
10
(let
11
((finishOk (try! (finish lobby-id address score rank sum-rank-factor rank-factor rewards rac nft))))
12
(ok finishOk)))
13
14
;; distribute rewards for all runs in a lobby
15
(define-private (finish (lobby-id uint) (address principal) (score uint) (rank uint) (sum-rank-factor uint) (rank-factor uint) (rewards uint) (rac uint) (nft (string-ascii 99)))
16
(begin
17
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
18
(unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address}))
19
(asserts! (default-to false (get active (map-get? lobbies {id: lobby-id}))) ERR-NOT-ACTIVE)
20
(map-set scoreboard {lobby-id: lobby-id, address: address} {score: score, rank: rank, sum-rank-factor: sum-rank-factor, rank-factor: rank-factor, rewards: rewards, rac: rac, nft: nft})
21
(try! (as-contract (stx-transfer? rac tx-sender address)))
22
(print {action: "finish", lobby-id: lobby-id, address: address, score: score, rank: rank, sum-rank-factor: sum-rank-factor, rank-factor: rank-factor, rewards: rewards, rac: rac, nft: nft})
23
(ok true)))

Helper Functions

Functions are private and directly used when they are needed, in other functions.
Increment lobby id.
Add balance - send x amount of STX from the tx-sender and add it to the lobby pool.
Check response is error.is
1
(define-private (increment-lobby-count)
2
(begin
3
(var-set lobby-count (+ (var-get lobby-count) u1))
4
(var-get lobby-count)))
5
6
(define-private (add-balance (id uint) (participant principal) (amount uint))
7
(begin
8
(unwrap-panic (stx-transfer? amount participant (as-contract tx-sender)))
9
(match
10
(map-get? lobbies {id: id})
11
lobby
12
(map-set lobbies {id: id} (merge lobby {balance: (+ (default-to u0 (get balance (map-get? lobbies {id: id}))) amount)}))
13
false)))
14
15
(define-private (check-err (result (response bool uint)) (prior (response bool uint)))
16
(match prior ok-value result
17
err-value (err err-value)))

Safety Functions

Implement the standard trait for transferring assets: STX, FT and NFTs.
1
(define-public (transfer-stx (address principal) (amount uint))
2
(begin
3
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
4
(unwrap-panic (as-contract (stx-transfer? amount (as-contract tx-sender) address)))
5
(ok true)))
6
7
(define-public (transfer-ft-token (address principal) (amount uint) (token <ft-trait>))
8
(begin
9
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
10
(try! (as-contract (contract-call? token transfer amount tx-sender address none)))
11
(ok true)))
12
13
(define-public (transfer-nft-token (address principal) (id uint) (token <nft-trait>))
14
(begin
15
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
16
(try! (as-contract (contract-call? token transfer id tx-sender address)))
17
(ok true)))