Skip to content

Commit

Permalink
feat: get timezone (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ authored Oct 23, 2024
1 parent d8375b1 commit 6bd2ea2
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sweat_jar"
version = "3.3.6"
version = "3.3.8"
authors = ["Sweat Economy"]
edition = "2021"

Expand Down
12 changes: 6 additions & 6 deletions contract/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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": [
[
Expand Down Expand Up @@ -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": [
{
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions contract/src/score/account_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Score> {
let today = self.timezone.today();
Expand Down Expand Up @@ -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);
}
}
19 changes: 18 additions & 1 deletion contract/src/score/score_api.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -67,4 +72,16 @@ impl ScoreApi for Contract {

emit(EventKind::RecordScore(event));
}

fn get_timezone(&self, account_id: AccountId) -> Option<I64> {
self.accounts
.get(&account_id)
.map(|account| I64(*account.score.timezone))
}

fn get_score_interest(&self, account_id: AccountId) -> Option<U128> {
let account = self.accounts.get(&account_id).and_then(|a| a.score())?;

Some((account.active_score() as u128).into())
}
}
5 changes: 5 additions & 0 deletions contract/src/score/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use fake::Fake;
use near_sdk::{
json_types::{I64, U128},
store::LookupMap,
test_utils::test_env::{alice, bob},
NearToken,
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion model/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sweat-jar-model"
version = "3.3.6"
version = "3.3.8"
publish = false
edition = "2021"

Expand Down
8 changes: 7 additions & 1 deletion model/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use near_sdk::{
json_types::{Base64VecU8, U128},
json_types::{Base64VecU8, I64, U128},
AccountId,
};
#[cfg(feature = "integration-api")]
Expand Down Expand Up @@ -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<I64>;

/// Returns current active score interest if user has any step jars
fn get_score_interest(&self, account_id: AccountId) -> Option<U128>;
}

#[cfg(feature = "integration-methods")]
Expand Down
Binary file modified res/sweat_jar.wasm
Binary file not shown.

0 comments on commit 6bd2ea2

Please sign in to comment.