diff --git a/Cargo.lock b/Cargo.lock index b368df67..0d311251 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5525,7 +5525,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sweat-jar-model" -version = "3.3.6" +version = "3.3.8" dependencies = [ "fake", "near-sdk 5.5.0", @@ -5549,7 +5549,7 @@ dependencies = [ [[package]] name = "sweat_jar" -version = "3.3.6" +version = "3.3.8" dependencies = [ "anyhow", "base64 0.22.1", diff --git a/contract/Cargo.toml b/contract/Cargo.toml index 3cd79fca..b7aaa2c7 100644 --- a/contract/Cargo.toml +++ b/contract/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sweat_jar" -version = "3.3.6" +version = "3.3.8" authors = ["Sweat Economy"] edition = "2021" diff --git a/contract/src/event.rs b/contract/src/event.rs index 20172678..7a0d2094 100644 --- a/contract/src/event.rs +++ b/contract/src/event.rs @@ -187,7 +187,7 @@ mod test { fn test_contract_version() { let admin = admin(); let context = Context::new(admin); - assert_eq!(context.contract().contract_version(), "sweat_jar-3.3.6"); + assert_eq!(context.contract().contract_version(), "sweat_jar-3.3.8"); } #[test] @@ -200,7 +200,7 @@ mod test { .to_json_event_string(), r#"EVENT_JSON:{ "standard": "sweat_jar", - "version": "3.3.6", + "version": "3.3.8", "event": "top_up", "data": { "id": 10, @@ -228,7 +228,7 @@ mod test { .to_json_event_string(), r#"EVENT_JSON:{ "standard": "sweat_jar", - "version": "3.3.6", + "version": "3.3.8", "event": "create_jar", "data": { "id": 555, @@ -248,7 +248,7 @@ mod test { SweatJarEvent::from(EventKind::Claim(vec![(1, 1.into()), (2, 2.into())])).to_json_event_string(), r#"EVENT_JSON:{ "standard": "sweat_jar", - "version": "3.3.6", + "version": "3.3.8", "event": "claim", "data": [ [ @@ -277,7 +277,7 @@ mod test { .to_json_event_string(), r#"EVENT_JSON:{ "standard": "sweat_jar", - "version": "3.3.6", + "version": "3.3.8", "event": "record_score", "data": [ { @@ -306,7 +306,7 @@ mod test { SweatJarEvent::from(EventKind::OldScoreWarning((111, Local(5)))).to_json_event_string(), r#"EVENT_JSON:{ "standard": "sweat_jar", - "version": "3.3.6", + "version": "3.3.8", "event": "old_score_warning", "data": [ 111, diff --git a/contract/src/score/account_score.rs b/contract/src/score/account_score.rs index 8147b7ff..f4c570d5 100644 --- a/contract/src/score/account_score.rs +++ b/contract/src/score/account_score.rs @@ -45,6 +45,17 @@ impl AccountScore { } } + pub fn active_score(&self) -> Score { + let today = self.timezone.today(); + let update_day = self.update_day(); + + if today == update_day { + self.scores[0] + } else { + 0 + } + } + /// On claim we need to clear active scores so they aren't claimed twice or more. pub fn claim_score(&mut self) -> Vec { let today = self.timezone.today(); @@ -215,4 +226,23 @@ mod test { ctx.set_block_timestamp_in_ms(MS_IN_DAY * 11); assert_eq!(score.claim_score(), vec![1006, 0]); } + + #[test] + fn active_score() { + let score = AccountScore { + updated: UTC(MS_IN_DAY * 10), + timezone: Timezone::hour_shift(0), + scores: [1000, 2000], + }; + + let mut ctx = TestBuilder::new().build(); + + ctx.set_block_timestamp_in_ms(MS_IN_DAY * 10); + + assert_eq!(score.active_score(), 1000); + + ctx.set_block_timestamp_in_ms(MS_IN_DAY * 11); + + assert_eq!(score.active_score(), 0); + } } diff --git a/contract/src/score/score_api.rs b/contract/src/score/score_api.rs index 8e2f5ef3..c561b3d6 100644 --- a/contract/src/score/score_api.rs +++ b/contract/src/score/score_api.rs @@ -1,4 +1,9 @@ -use near_sdk::{env, env::block_timestamp_ms, near_bindgen, AccountId}; +use near_sdk::{ + env, + env::block_timestamp_ms, + json_types::{I64, U128}, + near_bindgen, AccountId, +}; use sweat_jar_model::{api::ScoreApi, Score, U32, UTC}; use crate::{ @@ -67,4 +72,16 @@ impl ScoreApi for Contract { emit(EventKind::RecordScore(event)); } + + fn get_timezone(&self, account_id: AccountId) -> Option { + self.accounts + .get(&account_id) + .map(|account| I64(*account.score.timezone)) + } + + fn get_score_interest(&self, account_id: AccountId) -> Option { + let account = self.accounts.get(&account_id).and_then(|a| a.score())?; + + Some((account.active_score() as u128).into()) + } } diff --git a/contract/src/score/tests.rs b/contract/src/score/tests.rs index b995e2d0..8de8f236 100644 --- a/contract/src/score/tests.rs +++ b/contract/src/score/tests.rs @@ -2,6 +2,7 @@ use fake::Fake; use near_sdk::{ + json_types::{I64, U128}, store::LookupMap, test_utils::test_env::{alice, bob}, NearToken, @@ -81,6 +82,8 @@ fn same_interest_in_score_jar_as_in_const_jar() { .jar(SCORE_JAR, JarField::Timezone(Timezone::hour_shift(3))) .build(); + assert_eq!(ctx.contract().get_timezone(alice()), Some(I64(10800000))); + // Difference of 1 is okay because the missing yoctosweat is stored in claim remainder // and will eventually be added to total claimed balance fn compare_interest(ctx: &Context) { @@ -96,6 +99,8 @@ fn same_interest_in_score_jar_as_in_const_jar() { ctx.switch_account(admin()); ctx.record_score(UTC(day * MS_IN_DAY), 20_000, alice()); + assert_eq!(ctx.contract().get_score_interest(alice()), Some(U128(20000))); + compare_interest(&ctx); if day == HALF_PERIOD { diff --git a/model/Cargo.toml b/model/Cargo.toml index 19434786..53feb5da 100644 --- a/model/Cargo.toml +++ b/model/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sweat-jar-model" -version = "3.3.6" +version = "3.3.8" publish = false edition = "2021" diff --git a/model/src/api.rs b/model/src/api.rs index fe771b13..a13daa3c 100644 --- a/model/src/api.rs +++ b/model/src/api.rs @@ -1,5 +1,5 @@ use near_sdk::{ - json_types::{Base64VecU8, U128}, + json_types::{Base64VecU8, I64, U128}, AccountId, }; #[cfg(feature = "integration-api")] @@ -289,6 +289,12 @@ pub trait ScoreApi { /// - This function will panic if an account does not have score jars. /// - This function will panic if a product associated with a jar does not exist. fn record_score(&mut self, batch: Vec<(AccountId, Vec<(Score, UTC)>)>); + + /// Return users timezone if user has any step jars + fn get_timezone(&self, account_id: AccountId) -> Option; + + /// Returns current active score interest if user has any step jars + fn get_score_interest(&self, account_id: AccountId) -> Option; } #[cfg(feature = "integration-methods")] diff --git a/res/sweat_jar.wasm b/res/sweat_jar.wasm index ee56f052..df592287 100755 Binary files a/res/sweat_jar.wasm and b/res/sweat_jar.wasm differ