Skip to content

Commit 134de04

Browse files
committed
some optimizations
1 parent 31c6411 commit 134de04

File tree

2 files changed

+27
-55
lines changed

2 files changed

+27
-55
lines changed

contracts/trustless-rewards.clar

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
(define-constant DEFAULT-PRICE u100)
1616

1717
;; data maps and vars
18-
(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})
18+
(define-map lobbies 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})
1919
(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)})
2020
(define-data-var lobby-count uint u0)
2121
(define-data-var contract-owner principal tx-sender)
@@ -29,14 +29,10 @@
2929
)
3030

3131
(define-private (add-balance (id uint) (participant principal) (amount uint))
32-
(begin
33-
(unwrap-panic (stx-transfer? amount participant (as-contract tx-sender)))
34-
(match
35-
(map-get? lobbies {id: id})
36-
lobby
37-
(map-set lobbies {id: id} (merge lobby {balance: (+ (default-to u0 (get balance (map-get? lobbies {id: id}))) amount)}))
38-
false
39-
)
32+
(let ((lobby (unwrap! (map-get? lobbies id) (ok false))))
33+
(try! (stx-transfer? amount participant (as-contract tx-sender)))
34+
(map-set lobbies id (merge lobby {balance: (+ (get balance lobby) amount)}))
35+
(ok true)
4036
)
4137
)
4238

@@ -52,7 +48,7 @@
5248
(lobby-id (increment-lobby-count))
5349
)
5450
;; (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
55-
(map-set lobbies {id: lobby-id}
51+
(map-set lobbies lobby-id
5652
{
5753
owner: tx-sender, description: description, balance: u0, price: price, factor: factor, commission: commission,
5854
mapy: mapy, length: length, traffic: traffic, curves: curves, hours: hours, active: true
@@ -64,40 +60,35 @@
6460
)
6561

6662
(define-read-only (get-lobby (id uint))
67-
(ok (unwrap-panic (map-get? lobbies {id: id})))
63+
(map-get? lobbies id)
6864
)
6965

7066

7167
;; anyone can call to join lobbies
7268
(define-public (join (id uint))
7369
(let (
74-
(entry-price (default-to DEFAULT-PRICE (get price (map-get? lobbies {id: id}))))
70+
(lobby (unwrap! (map-get? lobbies id) ERR-NOT-FOUND))
71+
(entry-price (get price lobby))
7572
(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: ""}))
7673
)
77-
(unwrap-panic (map-get? lobbies {id: id}))
78-
(asserts! (default-to false (get active (map-get? lobbies {id: id}))) ERR-NOT-ACTIVE)
74+
(asserts! (get active lobby) ERR-NOT-ACTIVE)
7975
(asserts! joined ERR-ALREADY-JOINED)
80-
(add-balance id tx-sender entry-price)
76+
(try! (add-balance id tx-sender entry-price))
8177
(print {action: "join", lobby-id: id, address: tx-sender })
8278
(ok OK-SUCCESS)
8379
)
8480
)
8581

8682
(define-public (disable-lobby (id uint))
87-
(begin
83+
(let ((lobby (unwrap! (map-get? lobbies id) ERR-NOT-FOUND)))
8884
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
89-
(match
90-
(map-get? lobbies {id: id})
91-
lobby
92-
(map-set lobbies {id: id} (merge lobby {active: false}))
93-
false
94-
)
85+
(map-set lobbies id (merge lobby {active: false}))
9586
(ok true)
9687
)
9788
)
9889

9990
(define-read-only (get-score (lobby-id uint) (address principal))
100-
(ok (unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address})))
91+
(map-get? scoreboard {lobby-id: lobby-id, address: address})
10192
)
10293

10394
;; PUBLISH-MANY
@@ -108,21 +99,13 @@
10899
)
109100
)
110101
(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)}))
111-
(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))
112-
)
113-
(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)))
114-
(let
115-
(
116-
(publishOk (try! (publish lobby-id address score rank sum-rank-factor rank-factor rewards rac nft)))
117-
)
118-
(ok publishOk)
119-
)
102+
(publish (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))
120103
)
121104
(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)))
122105
(begin
123106
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
124-
(unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address}))
125-
(asserts! (default-to false (get active (map-get? lobbies {id: lobby-id}))) ERR-NOT-ACTIVE)
107+
(asserts! (is-some (map-get? scoreboard {lobby-id: lobby-id, address: address})) ERR-NOT-FOUND)
108+
(asserts! (default-to false (get active (map-get? lobbies lobby-id))) ERR-NOT-ACTIVE)
126109
(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})
127110
(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})
128111
(ok true)
@@ -137,23 +120,12 @@
137120
)
138121
)
139122
(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)}))
140-
(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))
141-
)
142-
(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)))
143-
(let
144-
(
145-
(finishOk (try! (finish lobby-id address score rank sum-rank-factor rank-factor rewards rac nft)))
146-
)
147-
(ok finishOk)
148-
)
123+
(finish (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))
149124
)
150125
;; distribute rewards for all runs in a lobby
151126
(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)))
152127
(begin
153-
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
154-
(unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address}))
155-
(asserts! (default-to false (get active (map-get? lobbies {id: lobby-id}))) ERR-NOT-ACTIVE)
156-
(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})
128+
(try! (publish lobby-id address score rank sum-rank-factor rank-factor rewards rac nft))
157129
(try! (as-contract (stx-transfer? rac tx-sender address)))
158130
(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})
159131
(ok true)

tests/trustless-rewards_test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Clarinet.test({
3939
);
4040
assertEquals(
4141
tx.result,
42-
'(ok {active: true, balance: u5, commission: u5, curves: "straight", description: "lobby description", factor: u5, hours: u24, length: "long", mapy: "miamiBeach", owner: ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5, price: u5, traffic: "intense"})'
42+
'(some {active: true, balance: u5, commission: u5, curves: "straight", description: "lobby description", factor: u5, hours: u24, length: "long", mapy: "miamiBeach", owner: ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5, price: u5, traffic: "intense"})'
4343
);
4444
},
4545
});
@@ -82,7 +82,7 @@ Clarinet.test({
8282
);
8383
assertEquals(
8484
tx.result,
85-
'(ok {active: true, balance: u5, commission: u5, curves: "straight", description: "lobby description", factor: u5, hours: u24, length: "long", mapy: "miamiBeach", owner: ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5, price: u5, traffic: "intense"})'
85+
'(some {active: true, balance: u5, commission: u5, curves: "straight", description: "lobby description", factor: u5, hours: u24, length: "long", mapy: "miamiBeach", owner: ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5, price: u5, traffic: "intense"})'
8686
);
8787

8888
let block2 = chain.mineBlock([
@@ -146,7 +146,7 @@ Clarinet.test({
146146
// console.log(`tx `, tx);
147147
assertEquals(
148148
tx.result,
149-
'(ok {nft: "", rac: u0, rank: u0, rank-factor: u0, rewards: u0, score: u0, sum-rank-factor: u0})'
149+
'(some {nft: "", rac: u0, rank: u0, rank-factor: u0, rewards: u0, score: u0, sum-rank-factor: u0})'
150150
);
151151
},
152152
});
@@ -219,7 +219,7 @@ Clarinet.test({
219219
// console.log(`tx `, tx);
220220
assertEquals(
221221
tx.result,
222-
'(ok {nft: "", rac: u0, rank: u0, rank-factor: u0, rewards: u0, score: u0, sum-rank-factor: u0})'
222+
'(some {nft: "", rac: u0, rank: u0, rank-factor: u0, rewards: u0, score: u0, sum-rank-factor: u0})'
223223
);
224224
},
225225
});
@@ -437,9 +437,9 @@ Clarinet.test({
437437
let block = chain.mineBlock([Tx.contractCall('trustless-rewards', 'finish-result-many', [args], deployer.address)]);
438438
// console.log('block ', block.receipts[0].events);
439439
block.receipts[0].result.expectOk().expectBool(true);
440-
assertEquals(block.receipts[0].events[0].stx_transfer_event.amount, '4'); // 4 = rac value provided by owner
441-
assertEquals(block.receipts[0].events[2].stx_transfer_event.amount, '4'); // 4 = rac value provided by owner
440+
assertEquals(block.receipts[0].events[1].stx_transfer_event.amount, '4'); // 4 = rac value provided by owner
442441
assertEquals(block.receipts[0].events[4].stx_transfer_event.amount, '4'); // 4 = rac value provided by owner
442+
assertEquals(block.receipts[0].events[7].stx_transfer_event.amount, '4'); // 4 = rac value provided by owner
443443
},
444444
});
445445

@@ -489,8 +489,8 @@ Clarinet.test({
489489
Tx.contractCall('trustless-rewards', 'publish-result-many', [args], deployer.address),
490490
]);
491491
// console.log('block ', block);
492-
assertEquals(block.receipts.length, 0);
493-
// block.receipts[0].result.expectOk().expectUint(1);
492+
assertEquals(block.receipts.length, 1);
493+
block.receipts[0].result.expectErr().expectUint(404);
494494
},
495495
});
496496

0 commit comments

Comments
 (0)