diff --git a/contract/src/common/mod.rs b/contract/src/common/mod.rs index c0109c9..c4c16d3 100644 --- a/contract/src/common/mod.rs +++ b/contract/src/common/mod.rs @@ -6,5 +6,5 @@ pub(crate) mod tests; pub(crate) fn unix_timestamp(ms: u64) -> UnixTimestamp { u32::try_from(ms / 1000) - .unwrap_or_else(|err| panic_str(&format!("Failed to get convert milliseconds to Unix timestamp: {err}"))) + .unwrap_or_else(|err| panic_str(&format!("Failed to get convert milliseconds to Unix timestamp"))) } diff --git a/contract/src/common/tests.rs b/contract/src/common/tests.rs index 74142c1..a2b8f3f 100644 --- a/contract/src/common/tests.rs +++ b/contract/src/common/tests.rs @@ -5,7 +5,7 @@ use std::time::Duration; use model::api::InitApi; use near_sdk::{test_utils::VMContextBuilder, testing_env, AccountId}; -use crate::Contract; +use crate::{common::unix_timestamp, Contract}; pub(crate) struct Context { builder: VMContextBuilder, @@ -138,3 +138,20 @@ pub(crate) mod data { assert!(get_test_future_success(name)); } } + +#[test] +fn convert_milliseconds_to_unix_timestamp_successfully() { + let millis: u64 = 1_699_038_575_819; + let timestamp = unix_timestamp(millis); + + assert_eq!(1_699_038_575, timestamp); +} + +#[test] +#[should_panic(expected = "Failed to get convert milliseconds to Unix timestamp")] +fn convert_milliseconds_to_unix_timestamp_with_unsuccessfully() { + let millis: u64 = u64::MAX; + let timestamp = unix_timestamp(millis); + + assert_eq!(1_699_038_575, timestamp); +} diff --git a/contract/src/config/api.rs b/contract/src/config/api.rs new file mode 100644 index 0000000..20fdb1d --- /dev/null +++ b/contract/src/config/api.rs @@ -0,0 +1,24 @@ +use model::Duration; +use near_sdk::near_bindgen; + +use crate::{Contract, ContractExt}; + +pub trait ConfigApi { + fn set_claim_period(&mut self, period: Duration); + fn set_burn_period(&mut self, period: Duration); +} + +#[near_bindgen] +impl ConfigApi for Contract { + fn set_claim_period(&mut self, period: Duration) { + self.assert_oracle(); + + self.claim_period = period; + } + + fn set_burn_period(&mut self, period: Duration) { + self.assert_oracle(); + + self.burn_period = period; + } +} diff --git a/contract/src/config/mod.rs b/contract/src/config/mod.rs new file mode 100644 index 0000000..2220e53 --- /dev/null +++ b/contract/src/config/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod api; +mod tests; diff --git a/contract/src/config/tests.rs b/contract/src/config/tests.rs new file mode 100644 index 0000000..23f1116 --- /dev/null +++ b/contract/src/config/tests.rs @@ -0,0 +1,57 @@ +#![cfg(test)] + +use crate::{common::tests::Context, config::api::ConfigApi}; + +#[test] +fn set_claim_period_by_oracle() { + let (mut context, mut contract, accounts) = Context::init_with_oracle(); + + let claim_period = 1_000_000; + context.switch_account(&accounts.oracle); + contract.set_claim_period(claim_period); + + assert_eq!(claim_period, contract.claim_period); + + let claim_period = 3_000_000; + context.switch_account(&accounts.oracle); + contract.set_claim_period(claim_period); + + assert_eq!(claim_period, contract.claim_period); +} + +#[test] +#[should_panic(expected = "Unauthorized access")] +fn set_claim_period_by_not_oracle() { + let (mut context, mut contract, accounts) = Context::init_with_oracle(); + + let claim_period = 1_000_000; + context.switch_account(&accounts.alice); + contract.set_claim_period(claim_period); +} + +#[test] +fn set_burn_period_by_oracle() { + let (mut context, mut contract, accounts) = Context::init_with_oracle(); + + let burn_period = 1_000_000; + context.switch_account(&accounts.oracle); + contract.set_burn_period(burn_period); + + assert_eq!(burn_period, contract.burn_period); + + let burn_period = 3_000_000; + context.switch_account(&accounts.oracle); + contract.set_burn_period(burn_period); + + assert_eq!(burn_period, contract.burn_period); +} + +#[test] +#[should_panic(expected = "Unauthorized access")] +fn set_burn_period_by_not_oracle() { + let (mut context, mut contract, accounts) = Context::init_with_oracle(); + + let burn_period = 1_000_000; + context.switch_account(&accounts.alice); + contract.set_burn_period(burn_period); +} diff --git a/contract/src/lib.rs b/contract/src/lib.rs index 147ebe4..5a05fd0 100644 --- a/contract/src/lib.rs +++ b/contract/src/lib.rs @@ -11,6 +11,7 @@ mod burn; mod claim; mod clean; mod common; +mod config; mod record; const INITIAL_CLAIM_PERIOD_MS: u32 = 24 * 60 * 60; @@ -58,16 +59,4 @@ impl InitApi for Contract { is_service_call_running: false, } } - - fn set_claim_period(&mut self, period: Duration) { - self.assert_oracle(); - - self.claim_period = period; - } - - fn set_burn_period(&mut self, period: Duration) { - self.assert_oracle(); - - self.burn_period = period; - } } diff --git a/contract/src/record/mod.rs b/contract/src/record/mod.rs index 6563534..2220e53 100644 --- a/contract/src/record/mod.rs +++ b/contract/src/record/mod.rs @@ -1 +1,2 @@ pub(crate) mod api; +mod tests; diff --git a/contract/src/record/tests.rs b/contract/src/record/tests.rs new file mode 100644 index 0000000..bfc4444 --- /dev/null +++ b/contract/src/record/tests.rs @@ -0,0 +1,35 @@ +#![cfg(test)] + +use model::api::{ClaimApi, RecordApi}; +use near_sdk::{json_types::U128, test_utils::accounts}; + +use crate::common::tests::Context; + +#[test] +fn record_by_oracle() { + let (mut context, mut contract, accounts) = Context::init_with_oracle(); + + let alice_balance_1 = 1_000_000; + + context.switch_account(&accounts.oracle); + contract.record_batch_for_hold(vec![(accounts.alice.clone(), U128(alice_balance_1))]); + + let alice_actual_balance = contract.get_claimable_balance_for_account(accounts.alice.clone()); + assert_eq!(alice_balance_1, alice_actual_balance.0); + + context.set_block_timestamp_in_seconds(1_000); + + let alice_balance_2 = 500_000; + let bob_balance = 200_000; + + contract.record_batch_for_hold(vec![ + (accounts.alice.clone(), U128(alice_balance_2)), + (accounts.bob.clone(), U128(bob_balance)), + ]); + + let alice_actual_balance = contract.get_claimable_balance_for_account(accounts.alice.clone()); + assert_eq!(alice_balance_1 + alice_balance_2, alice_actual_balance.0); + + let bob_actual_balance = contract.get_claimable_balance_for_account(accounts.bob.clone()); + assert_eq!(bob_balance, bob_actual_balance.0); +} diff --git a/model/src/api.rs b/model/src/api.rs index f55c39f..2fcb749 100644 --- a/model/src/api.rs +++ b/model/src/api.rs @@ -6,8 +6,6 @@ use crate::{ClaimAvailabilityView, Duration}; #[make_integration_version] pub trait InitApi { fn init(token_account_id: AccountId) -> Self; - fn set_claim_period(&mut self, period: Duration); - fn set_burn_period(&mut self, period: Duration); } #[make_integration_version]