Skip to content

Commit 6bd2ea2

Browse files
authored
feat: get timezone (#115)
1 parent d8375b1 commit 6bd2ea2

File tree

9 files changed

+70
-12
lines changed

9 files changed

+70
-12
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contract/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sweat_jar"
3-
version = "3.3.6"
3+
version = "3.3.8"
44
authors = ["Sweat Economy"]
55
edition = "2021"
66

contract/src/event.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ mod test {
187187
fn test_contract_version() {
188188
let admin = admin();
189189
let context = Context::new(admin);
190-
assert_eq!(context.contract().contract_version(), "sweat_jar-3.3.6");
190+
assert_eq!(context.contract().contract_version(), "sweat_jar-3.3.8");
191191
}
192192

193193
#[test]
@@ -200,7 +200,7 @@ mod test {
200200
.to_json_event_string(),
201201
r#"EVENT_JSON:{
202202
"standard": "sweat_jar",
203-
"version": "3.3.6",
203+
"version": "3.3.8",
204204
"event": "top_up",
205205
"data": {
206206
"id": 10,
@@ -228,7 +228,7 @@ mod test {
228228
.to_json_event_string(),
229229
r#"EVENT_JSON:{
230230
"standard": "sweat_jar",
231-
"version": "3.3.6",
231+
"version": "3.3.8",
232232
"event": "create_jar",
233233
"data": {
234234
"id": 555,
@@ -248,7 +248,7 @@ mod test {
248248
SweatJarEvent::from(EventKind::Claim(vec![(1, 1.into()), (2, 2.into())])).to_json_event_string(),
249249
r#"EVENT_JSON:{
250250
"standard": "sweat_jar",
251-
"version": "3.3.6",
251+
"version": "3.3.8",
252252
"event": "claim",
253253
"data": [
254254
[
@@ -277,7 +277,7 @@ mod test {
277277
.to_json_event_string(),
278278
r#"EVENT_JSON:{
279279
"standard": "sweat_jar",
280-
"version": "3.3.6",
280+
"version": "3.3.8",
281281
"event": "record_score",
282282
"data": [
283283
{
@@ -306,7 +306,7 @@ mod test {
306306
SweatJarEvent::from(EventKind::OldScoreWarning((111, Local(5)))).to_json_event_string(),
307307
r#"EVENT_JSON:{
308308
"standard": "sweat_jar",
309-
"version": "3.3.6",
309+
"version": "3.3.8",
310310
"event": "old_score_warning",
311311
"data": [
312312
111,

contract/src/score/account_score.rs

+30
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ impl AccountScore {
4545
}
4646
}
4747

48+
pub fn active_score(&self) -> Score {
49+
let today = self.timezone.today();
50+
let update_day = self.update_day();
51+
52+
if today == update_day {
53+
self.scores[0]
54+
} else {
55+
0
56+
}
57+
}
58+
4859
/// On claim we need to clear active scores so they aren't claimed twice or more.
4960
pub fn claim_score(&mut self) -> Vec<Score> {
5061
let today = self.timezone.today();
@@ -215,4 +226,23 @@ mod test {
215226
ctx.set_block_timestamp_in_ms(MS_IN_DAY * 11);
216227
assert_eq!(score.claim_score(), vec![1006, 0]);
217228
}
229+
230+
#[test]
231+
fn active_score() {
232+
let score = AccountScore {
233+
updated: UTC(MS_IN_DAY * 10),
234+
timezone: Timezone::hour_shift(0),
235+
scores: [1000, 2000],
236+
};
237+
238+
let mut ctx = TestBuilder::new().build();
239+
240+
ctx.set_block_timestamp_in_ms(MS_IN_DAY * 10);
241+
242+
assert_eq!(score.active_score(), 1000);
243+
244+
ctx.set_block_timestamp_in_ms(MS_IN_DAY * 11);
245+
246+
assert_eq!(score.active_score(), 0);
247+
}
218248
}

contract/src/score/score_api.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use near_sdk::{env, env::block_timestamp_ms, near_bindgen, AccountId};
1+
use near_sdk::{
2+
env,
3+
env::block_timestamp_ms,
4+
json_types::{I64, U128},
5+
near_bindgen, AccountId,
6+
};
27
use sweat_jar_model::{api::ScoreApi, Score, U32, UTC};
38

49
use crate::{
@@ -67,4 +72,16 @@ impl ScoreApi for Contract {
6772

6873
emit(EventKind::RecordScore(event));
6974
}
75+
76+
fn get_timezone(&self, account_id: AccountId) -> Option<I64> {
77+
self.accounts
78+
.get(&account_id)
79+
.map(|account| I64(*account.score.timezone))
80+
}
81+
82+
fn get_score_interest(&self, account_id: AccountId) -> Option<U128> {
83+
let account = self.accounts.get(&account_id).and_then(|a| a.score())?;
84+
85+
Some((account.active_score() as u128).into())
86+
}
7087
}

contract/src/score/tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use fake::Fake;
44
use near_sdk::{
5+
json_types::{I64, U128},
56
store::LookupMap,
67
test_utils::test_env::{alice, bob},
78
NearToken,
@@ -81,6 +82,8 @@ fn same_interest_in_score_jar_as_in_const_jar() {
8182
.jar(SCORE_JAR, JarField::Timezone(Timezone::hour_shift(3)))
8283
.build();
8384

85+
assert_eq!(ctx.contract().get_timezone(alice()), Some(I64(10800000)));
86+
8487
// Difference of 1 is okay because the missing yoctosweat is stored in claim remainder
8588
// and will eventually be added to total claimed balance
8689
fn compare_interest(ctx: &Context) {
@@ -96,6 +99,8 @@ fn same_interest_in_score_jar_as_in_const_jar() {
9699
ctx.switch_account(admin());
97100
ctx.record_score(UTC(day * MS_IN_DAY), 20_000, alice());
98101

102+
assert_eq!(ctx.contract().get_score_interest(alice()), Some(U128(20000)));
103+
99104
compare_interest(&ctx);
100105

101106
if day == HALF_PERIOD {

model/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sweat-jar-model"
3-
version = "3.3.6"
3+
version = "3.3.8"
44
publish = false
55
edition = "2021"
66

model/src/api.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use near_sdk::{
2-
json_types::{Base64VecU8, U128},
2+
json_types::{Base64VecU8, I64, U128},
33
AccountId,
44
};
55
#[cfg(feature = "integration-api")]
@@ -289,6 +289,12 @@ pub trait ScoreApi {
289289
/// - This function will panic if an account does not have score jars.
290290
/// - This function will panic if a product associated with a jar does not exist.
291291
fn record_score(&mut self, batch: Vec<(AccountId, Vec<(Score, UTC)>)>);
292+
293+
/// Return users timezone if user has any step jars
294+
fn get_timezone(&self, account_id: AccountId) -> Option<I64>;
295+
296+
/// Returns current active score interest if user has any step jars
297+
fn get_score_interest(&self, account_id: AccountId) -> Option<U128>;
292298
}
293299

294300
#[cfg(feature = "integration-methods")]

res/sweat_jar.wasm

3.83 KB
Binary file not shown.

0 commit comments

Comments
 (0)