Skip to content

Commit 245a69e

Browse files
authored
add round number to storage (#661)
1 parent e7de6dd commit 245a69e

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

contracts/button/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub enum GameError {
1919
InkEnvError(String),
2020
/// Couldn't have retrieved own code hash
2121
CantRetrieveOwnCodeHash,
22+
/// Overflow error
23+
Arithmethic,
2224
}
2325

2426
impl From<PSP22Error> for GameError {

contracts/button/lib.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ mod button_game {
9595
pub marketplace: AccountId,
9696
/// scoring strategy
9797
pub scoring: Scoring,
98+
/// current round number
99+
pub round: u64,
98100
}
99101

100102
impl AccessControlled for ButtonGame {
@@ -117,7 +119,7 @@ mod button_game {
117119
let required_role = Role::Initializer(code_hash);
118120
let access_control = AccountId::from(ACCESS_CONTROL_PUBKEY);
119121

120-
match ButtonGame::check_role(&access_control, &caller, required_role) {
122+
match ButtonGame::check_role(access_control, caller, required_role) {
121123
Ok(_) => Self::init(
122124
ticket_token,
123125
reward_token,
@@ -137,6 +139,12 @@ mod button_game {
137139
self.last_press + self.button_lifetime
138140
}
139141

142+
/// Returns the curent round number
143+
#[ink(message)]
144+
pub fn round(&self) -> u64 {
145+
self.round
146+
}
147+
140148
/// Returns the buttons status
141149
#[ink(message)]
142150
pub fn is_dead(&self) -> bool {
@@ -232,7 +240,7 @@ mod button_game {
232240
pub fn reset(&mut self) -> ButtonResult<()> {
233241
self.ensure_dead()?;
234242
self.reward_pressiah()?;
235-
self.reset_state();
243+
self.reset_state()?;
236244
self.transfer_tickets_to_marketplace()?;
237245
self.reset_marketplace()
238246
}
@@ -246,7 +254,7 @@ mod button_game {
246254
let caller = self.env().caller();
247255
let this = self.env().account_id();
248256
let required_role = Role::Owner(this);
249-
ButtonGame::check_role(&self.access_control, &caller, required_role)?;
257+
ButtonGame::check_role(self.access_control, caller, required_role)?;
250258
self.access_control = new_access_control;
251259
Ok(())
252260
}
@@ -259,7 +267,7 @@ mod button_game {
259267
let caller = self.env().caller();
260268
let this = self.env().account_id();
261269
let required_role = Role::Owner(this);
262-
ButtonGame::check_role(&self.access_control, &caller, required_role)?;
270+
ButtonGame::check_role(self.access_control, caller, required_role)?;
263271
self.env().terminate_contract(caller)
264272
}
265273

@@ -286,6 +294,7 @@ mod button_game {
286294
last_presser: None,
287295
presses: 0,
288296
total_rewards: 0,
297+
round: 0,
289298
};
290299

291300
Self::emit_event(
@@ -301,15 +310,17 @@ mod button_game {
301310
contract
302311
}
303312

304-
fn reset_state(&mut self) {
313+
fn reset_state(&mut self) -> ButtonResult<()> {
305314
let now = self.env().block_number();
306315

307316
self.presses = 0;
308317
self.last_presser = None;
309318
self.last_press = now;
310319
self.total_rewards = 0;
320+
self.round.checked_add(1).ok_or(GameError::Arithmethic)?;
311321

312322
Self::emit_event(self.env(), Event::GameReset(GameReset { when: now }));
323+
Ok(())
313324
}
314325

315326
fn reward_pressiah(&self) -> ButtonResult<()> {
@@ -323,7 +334,7 @@ mod button_game {
323334

324335
fn ensure_dead(&self) -> ButtonResult<()> {
325336
if !self.is_dead() {
326-
return Err(GameError::BeforeDeadline);
337+
Err(GameError::BeforeDeadline)
327338
} else {
328339
Ok(())
329340
}
@@ -370,22 +381,18 @@ mod button_game {
370381
Ok(())
371382
}
372383

373-
fn check_role(
374-
access_control: &AccountId,
375-
account: &AccountId,
376-
role: Role,
377-
) -> ButtonResult<()>
384+
fn check_role(access_control: AccountId, account: AccountId, role: Role) -> ButtonResult<()>
378385
where
379386
Self: AccessControlled,
380387
{
381388
<Self as AccessControlled>::check_role(
382-
access_control.clone(),
383-
account.clone(),
389+
access_control,
390+
account,
384391
role,
385392
|why: InkEnvError| {
386393
GameError::InkEnvError(format!("Calling access control has failed: {:?}", why))
387394
},
388-
|role: Role| GameError::MissingRole(role),
395+
GameError::MissingRole,
389396
)
390397
}
391398

0 commit comments

Comments
 (0)