diff --git a/Cargo.lock b/Cargo.lock index 5ee9e2103..eef6dc885 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,21 +22,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "andromeda-address-list" version = "2.0.0" @@ -596,7 +581,6 @@ dependencies = [ "andromeda-modules", "andromeda-std", "andromeda-testing", - "chrono 0.4.38", "cosmwasm-schema", "cosmwasm-std", "cw-multi-test", @@ -626,7 +610,7 @@ dependencies = [ "andromeda-finance", "andromeda-std", "andromeda-testing", - "chrono 0.3.0", + "chrono", "cosmwasm-schema", "cosmwasm-std", "cw-multi-test", @@ -757,12 +741,6 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56953345e39537a3e18bdaeba4cb0c58a78c1f61f361dc0fa7c5c7340ae87c5f" -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - [[package]] name = "byteorder" version = "1.5.0" @@ -775,12 +753,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" -[[package]] -name = "cc" -version = "1.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f" - [[package]] name = "cfg-if" version = "1.0.0" @@ -797,32 +769,12 @@ dependencies = [ "time", ] -[[package]] -name = "chrono" -version = "0.4.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "wasm-bindgen", - "windows-targets", -] - [[package]] name = "const-oid" version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" -[[package]] -name = "core-foundation-sys" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" - [[package]] name = "cosmwasm-crypto" version = "1.5.5" @@ -1482,29 +1434,6 @@ dependencies = [ "digest 0.10.7", ] -[[package]] -name = "iana-time-zone" -version = "0.1.60" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "itertools" version = "0.10.5" @@ -1538,15 +1467,6 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" -[[package]] -name = "js-sys" -version = "0.3.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "k256" version = "0.13.1" @@ -1585,12 +1505,6 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" -[[package]] -name = "log" -version = "0.4.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" - [[package]] name = "memchr" version = "2.7.4" @@ -2230,60 +2144,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wasm-bindgen" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn 2.0.68", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.68", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" - [[package]] name = "winapi" version = "0.3.9" @@ -2306,79 +2166,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - [[package]] name = "zeroize" version = "1.8.1" diff --git a/contracts/modules/andromeda-time-gate/Cargo.toml b/contracts/modules/andromeda-time-gate/Cargo.toml index a70341776..6ce2c1884 100644 --- a/contracts/modules/andromeda-time-gate/Cargo.toml +++ b/contracts/modules/andromeda-time-gate/Cargo.toml @@ -20,7 +20,6 @@ cosmwasm-std = { workspace = true } cosmwasm-schema = { workspace = true } cw-storage-plus = { workspace = true } cw-utils = { workspace = true } -chrono = "0.4.38" andromeda-std = { workspace = true, features = [] } andromeda-modules = { workspace = true } diff --git a/contracts/modules/andromeda-time-gate/src/contract.rs b/contracts/modules/andromeda-time-gate/src/contract.rs index 89115f7a1..99275a02c 100644 --- a/contracts/modules/andromeda-time-gate/src/contract.rs +++ b/contracts/modules/andromeda-time-gate/src/contract.rs @@ -3,20 +3,24 @@ use cosmwasm_std::entry_point; use cosmwasm_std::{ attr, ensure, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, Storage, }; +use cw_utils::Expiration; use crate::state::{CYCLE_START_TIME, GATE_ADDRESSES, TIME_INTERVAL}; -use andromeda_modules::time_gate::CycleStartTime; use andromeda_modules::time_gate::{ExecuteMsg, InstantiateMsg, QueryMsg}; use andromeda_std::{ ado_base::{InstantiateMsg as BaseInstantiateMsg, MigrateMsg}, ado_contract::ADOContract, amp::AndrAddr, - common::{actions::call_action, context::ExecuteContext, encode_binary}, + common::{ + actions::call_action, + context::ExecuteContext, + encode_binary, + expiration::{get_and_validate_start_time, Expiry}, + Milliseconds, + }, error::ContractError, }; -use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; - // version info for migration info const CONTRACT_NAME: &str = "crates.io:andromeda-time-gate"; const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -32,7 +36,7 @@ pub fn instantiate( ) -> Result { let resp = ADOContract::default().instantiate( deps.storage, - env, + env.clone(), deps.api, &deps.querier, info, @@ -44,13 +48,20 @@ pub fn instantiate( }, )?; - let cycle_start_time = msg.cycle_start_time; - cycle_start_time.validate()?; + let cycle_start_time_millisecons = match msg.cycle_start_time.clone() { + None => Milliseconds::from_nanos(env.block.time.nanos()), + Some(start_time) => start_time.get_time(&env.block), + }; + + let (cycle_start_time, _) = get_and_validate_start_time(&env, msg.cycle_start_time)?; let time_interval_seconds = msg.time_interval.unwrap_or(DEFAULT_TIME_INTERVAL); GATE_ADDRESSES.save(deps.storage, &msg.gate_addresses)?; - CYCLE_START_TIME.save(deps.storage, &cycle_start_time)?; + CYCLE_START_TIME.save( + deps.storage, + &(cycle_start_time, cycle_start_time_millisecons), + )?; TIME_INTERVAL.save(deps.storage, &time_interval_seconds)?; Ok(resp) @@ -104,7 +115,7 @@ fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result, action: String, ) -> Result { let ExecuteContext { deps, info, .. } = ctx; @@ -114,18 +125,26 @@ fn execute_update_cycle_start_time( ContractError::Unauthorized {} ); - let old_cycle_start_time = CYCLE_START_TIME.load(deps.storage)?; + let (new_cycle_start_time, _) = + get_and_validate_start_time(&ctx.env, cycle_start_time.clone())?; + let new_cycle_start_time_millisecons = match cycle_start_time.clone() { + None => Milliseconds::from_nanos(ctx.env.block.time.nanos()), + Some(start_time) => start_time.get_time(&ctx.env.block), + }; + + let (old_cycle_start_time, _) = CYCLE_START_TIME.load(deps.storage)?; ensure!( - old_cycle_start_time != cycle_start_time, + old_cycle_start_time != new_cycle_start_time, ContractError::InvalidParameter { error: Some("Same as an existed cycle start time".to_string()) } ); - cycle_start_time.validate()?; - - CYCLE_START_TIME.save(deps.storage, &cycle_start_time)?; + CYCLE_START_TIME.save( + deps.storage, + &(new_cycle_start_time, new_cycle_start_time_millisecons), + )?; Ok(Response::new().add_attributes(vec![attr("action", action), attr("sender", info.sender)])) } @@ -198,9 +217,11 @@ pub fn get_gate_addresses(storage: &dyn Storage) -> Result, Contra Ok(gate_addresses) } -pub fn get_cycle_start_time(storage: &dyn Storage) -> Result { - let cycle_start_time = CYCLE_START_TIME.load(storage)?; - Ok(cycle_start_time) +pub fn get_cycle_start_time( + storage: &dyn Storage, +) -> Result<(Expiration, Milliseconds), ContractError> { + let (cycle_start_time, cycle_start_time_milliseconds) = CYCLE_START_TIME.load(storage)?; + Ok((cycle_start_time, cycle_start_time_milliseconds)) } pub fn get_time_interval(storage: &dyn Storage) -> Result { @@ -210,39 +231,20 @@ pub fn get_time_interval(storage: &dyn Storage) -> Result pub fn get_current_ado_path(deps: Deps, env: Env) -> Result { let storage = deps.storage; - let cycle_start_time = CYCLE_START_TIME.load(storage)?; + let (cycle_start_time, cycle_start_time_milliseconds) = CYCLE_START_TIME.load(storage)?; let gate_addresses = GATE_ADDRESSES.load(storage)?; let time_interval = TIME_INTERVAL.load(storage)?; - let CycleStartTime { - year, - month, - day, - hour, - minute, - second, - } = cycle_start_time; - - let date = NaiveDate::from_ymd_opt(year, month, day).unwrap(); - let time = NaiveTime::from_hms_nano_opt(hour, minute, second, 0).unwrap(); - let datetime = NaiveDateTime::new(date, time); - - let duration = datetime.signed_duration_since(NaiveDateTime::new( - NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(), - NaiveTime::from_hms_nano_opt(0, 0, 0, 0).unwrap(), - )); - - let cycle_start_nanos = duration.num_nanoseconds().unwrap() as u64; - - let current_time_nanos = env.block.time.nanos(); - ensure!( - current_time_nanos >= cycle_start_nanos, + cycle_start_time.is_expired(&env.block), ContractError::CustomError { msg: "Cycle is not started yet".to_string() } ); + let current_time_nanos = env.block.time.nanos(); + let cycle_start_nanos = cycle_start_time_milliseconds.nanos(); + let time_interval_nanos = time_interval.checked_mul(1_000_000_000).unwrap(); let gate_length = gate_addresses.len() as u64; let time_delta = current_time_nanos.checked_sub(cycle_start_nanos).unwrap(); diff --git a/contracts/modules/andromeda-time-gate/src/state.rs b/contracts/modules/andromeda-time-gate/src/state.rs index 2f10155cc..98aef6cfd 100644 --- a/contracts/modules/andromeda-time-gate/src/state.rs +++ b/contracts/modules/andromeda-time-gate/src/state.rs @@ -1,7 +1,7 @@ -use andromeda_modules::time_gate::CycleStartTime; -use andromeda_std::amp::AndrAddr; +use andromeda_std::{amp::AndrAddr, common::Milliseconds}; use cw_storage_plus::Item; +use cw_utils::Expiration; pub const GATE_ADDRESSES: Item> = Item::new("gate_addresses"); -pub const CYCLE_START_TIME: Item = Item::new("cycle_start_time"); +pub const CYCLE_START_TIME: Item<(Expiration, Milliseconds)> = Item::new("cycle_start_time"); pub const TIME_INTERVAL: Item = Item::new("time_interval"); diff --git a/contracts/modules/andromeda-time-gate/src/testing/mock.rs b/contracts/modules/andromeda-time-gate/src/testing/mock.rs index f2877dea0..989bdf1e4 100644 --- a/contracts/modules/andromeda-time-gate/src/testing/mock.rs +++ b/contracts/modules/andromeda-time-gate/src/testing/mock.rs @@ -1,15 +1,16 @@ -use andromeda_modules::time_gate::CycleStartTime; use andromeda_modules::time_gate::{ExecuteMsg, InstantiateMsg, QueryMsg}; use andromeda_std::{ amp::AndrAddr, + common::{expiration::Expiry, Milliseconds}, error::ContractError, testing::mock_querier::{mock_dependencies_custom, WasmMockQuerier, MOCK_KERNEL_CONTRACT}, }; use cosmwasm_std::{ from_json, testing::{mock_env, mock_info, MockApi, MockStorage}, - Addr, Deps, DepsMut, Env, MessageInfo, OwnedDeps, Response, + Addr, BlockInfo, Deps, DepsMut, Env, MessageInfo, OwnedDeps, Response, Timestamp, }; +use cw_utils::Expiration; use crate::contract::{execute, instantiate, query}; @@ -17,7 +18,7 @@ pub type MockDeps = OwnedDeps; pub fn proper_initialization( gate_addresses: Vec, - cycle_start_time: CycleStartTime, + cycle_start_time: Option, time_interval: Option, ) -> (MockDeps, MessageInfo) { let mut deps = mock_dependencies_custom(&[]); @@ -29,7 +30,12 @@ pub fn proper_initialization( cycle_start_time, time_interval, }; - let env = mock_env(); + let mut env = mock_env(); + env.block = BlockInfo { + height: 100, + time: Timestamp::from_nanos(100000000000u64), + chain_id: "test-chain".to_string(), + }; let res = instantiate(deps.as_mut(), env, info.clone(), msg).unwrap(); assert_eq!(0, res.messages.len()); (deps, info) @@ -37,12 +43,18 @@ pub fn proper_initialization( pub fn update_cycle_start_time( deps: DepsMut<'_>, - cycle_start_time: CycleStartTime, + cycle_start_time: Option, sender: &str, ) -> Result { let msg = ExecuteMsg::UpdateCycleStartTime { cycle_start_time }; let info = mock_info(sender, &[]); - execute(deps, mock_env(), info, msg) + let mut env = mock_env(); + env.block = BlockInfo { + height: 100, + time: Timestamp::from_nanos(100000000000u64), + chain_id: "test-chain".to_string(), + }; + execute(deps, env, info, msg) } pub fn update_gate_addresses( @@ -67,8 +79,14 @@ pub fn update_time_interval( execute(deps, mock_env(), info, msg) } -pub fn query_cycle_start_time(deps: Deps) -> Result { - let res = query(deps, mock_env(), QueryMsg::GetCycleStartTime {}); +pub fn query_cycle_start_time(deps: Deps) -> Result<(Expiration, Milliseconds), ContractError> { + let mut env = mock_env(); + env.block = BlockInfo { + height: 100, + time: Timestamp::from_nanos(100000000000u64), + chain_id: "test-chain".to_string(), + }; + let res = query(deps, env, QueryMsg::GetCycleStartTime {}); match res { Ok(res) => Ok(from_json(res).unwrap()), Err(err) => Err(err), @@ -76,7 +94,13 @@ pub fn query_cycle_start_time(deps: Deps) -> Result Result, ContractError> { - let res = query(deps, mock_env(), QueryMsg::GetGateAddresses {}); + let mut env = mock_env(); + env.block = BlockInfo { + height: 100, + time: Timestamp::from_nanos(100000000000u64), + chain_id: "test-chain".to_string(), + }; + let res = query(deps, env, QueryMsg::GetGateAddresses {}); match res { Ok(res) => Ok(from_json(res).unwrap()), Err(err) => Err(err), @@ -84,7 +108,13 @@ pub fn query_gate_addresses(deps: Deps) -> Result, ContractError> } pub fn query_time_interval(deps: Deps) -> Result { - let res = query(deps, mock_env(), QueryMsg::GetTimeInterval {}); + let mut env = mock_env(); + env.block = BlockInfo { + height: 100, + time: Timestamp::from_nanos(100000000000u64), + chain_id: "test-chain".to_string(), + }; + let res = query(deps, env, QueryMsg::GetTimeInterval {}); match res { Ok(res) => Ok(from_json(res).unwrap()), Err(err) => Err(err), diff --git a/contracts/modules/andromeda-time-gate/src/testing/tests.rs b/contracts/modules/andromeda-time-gate/src/testing/tests.rs index 483b51690..82fb18d5c 100644 --- a/contracts/modules/andromeda-time-gate/src/testing/tests.rs +++ b/contracts/modules/andromeda-time-gate/src/testing/tests.rs @@ -2,10 +2,13 @@ use super::mock::{ proper_initialization, query_current_ado_path, query_cycle_start_time, query_gate_addresses, query_time_interval, update_cycle_start_time, update_gate_addresses, update_time_interval, }; -use andromeda_modules::time_gate::CycleStartTime; -use andromeda_std::amp::AndrAddr; use andromeda_std::error::ContractError; -use cosmwasm_std::testing::mock_env; +use andromeda_std::{ + amp::AndrAddr, + common::{expiration::Expiry, Milliseconds}, +}; +use cosmwasm_std::{testing::mock_env, BlockInfo, Timestamp}; +use cw_utils::Expiration; #[test] fn test_instantiation() { @@ -15,14 +18,7 @@ fn test_instantiation() { AndrAddr::from_string("mock_ado_2".to_string()), AndrAddr::from_string("mock_ado_3".to_string()), ], - CycleStartTime { - year: 2022, - month: 2, - day: 28, - hour: 0, - minute: 0, - second: 0, - }, + Some(Expiry::FromNow(Milliseconds(5000000000))), None, ); @@ -39,14 +35,10 @@ fn test_instantiation() { let res = query_cycle_start_time(deps.as_ref()).unwrap(); assert_eq!( res, - CycleStartTime { - year: 2022, - month: 2, - day: 28, - hour: 0, - minute: 0, - second: 0, - } + ( + Expiration::AtTime(Timestamp::from_nanos(5000100000000000)), + Milliseconds::from_seconds(5000100) + ) ); let res = query_time_interval(deps.as_ref()).unwrap(); @@ -61,27 +53,13 @@ fn test_update_cycle_start_time() { AndrAddr::from_string("mock_ado_2".to_string()), AndrAddr::from_string("mock_ado_3".to_string()), ], - CycleStartTime { - year: 2022, - month: 2, - day: 28, - hour: 0, - minute: 0, - second: 0, - }, + Some(Expiry::FromNow(Milliseconds(5000000000))), None, ); let err_res = update_cycle_start_time( deps.as_mut(), - CycleStartTime { - year: 2022, - month: 2, - day: 28, - hour: 0, - minute: 0, - second: 0, - }, + Some(Expiry::FromNow(Milliseconds(5000000000))), info.sender.as_ref(), ) .unwrap_err(); @@ -95,14 +73,7 @@ fn test_update_cycle_start_time() { update_cycle_start_time( deps.as_mut(), - CycleStartTime { - year: 2023, - month: 1, - day: 1, - hour: 0, - minute: 0, - second: 0, - }, + Some(Expiry::FromNow(Milliseconds(4000000000))), info.sender.as_ref(), ) .unwrap(); @@ -110,14 +81,10 @@ fn test_update_cycle_start_time() { let res = query_cycle_start_time(deps.as_ref()).unwrap(); assert_eq!( res, - CycleStartTime { - year: 2023, - month: 1, - day: 1, - hour: 0, - minute: 0, - second: 0, - } + ( + Expiration::AtTime(Timestamp::from_nanos(4000100000000000)), + Milliseconds::from_seconds(4000100) + ) ); update_time_interval(deps.as_mut(), 7200, info.sender.as_ref()).unwrap(); @@ -134,14 +101,7 @@ fn test_update_gate_addresses() { AndrAddr::from_string("mock_ado_2".to_string()), AndrAddr::from_string("mock_ado_3".to_string()), ], - CycleStartTime { - year: 2024, - month: 1, - day: 1, - hour: 0, - minute: 0, - second: 0, - }, + Some(Expiry::FromNow(Milliseconds(5000000000))), None, ); @@ -195,18 +155,17 @@ fn test_query_current_ado_path_not_started_yet() { AndrAddr::from_string("mock_ado_2".to_string()), AndrAddr::from_string("mock_ado_3".to_string()), ], - CycleStartTime { - year: 2024, - month: 1, - day: 1, - hour: 0, - minute: 0, - second: 0, - }, + Some(Expiry::FromNow(Milliseconds(5000000000))), None, ); - let env = mock_env(); + let mut env = mock_env(); + env.block = BlockInfo { + height: 100, + time: Timestamp::from_nanos(100000000000u64), + chain_id: "test-chain".to_string(), + }; + let res = query_current_ado_path(deps.as_ref(), env).unwrap_err(); assert_eq!( res, @@ -226,22 +185,22 @@ fn test_query_current_ado_path() { AndrAddr::from_string("mock_ado_4".to_string()), AndrAddr::from_string("mock_ado_5".to_string()), ], - CycleStartTime { - year: 2019, - month: 10, - day: 23, - hour: 1, - minute: 30, - second: 0, - }, + Some(Expiry::FromNow(Milliseconds(5000000000))), None, ); let mut env = mock_env(); + env.block = BlockInfo { + height: 100, + time: Timestamp::from_nanos(100000000000u64), + chain_id: "test-chain".to_string(), + }; + + env.block.time = env.block.time.plus_seconds(5000100); let res = query_current_ado_path(deps.as_ref(), env.clone()).unwrap(); assert_eq!(res, "mock_ado_1".to_string()); - env.block.time = env.block.time.plus_seconds(3600); + env.block.time = env.block.time.plus_seconds(6000); let res = query_current_ado_path(deps.as_ref(), env.clone()).unwrap(); assert_eq!(res, "mock_ado_2".to_string()); diff --git a/packages/andromeda-modules/src/time_gate.rs b/packages/andromeda-modules/src/time_gate.rs index 7c3fcc294..1a5cf7efc 100644 --- a/packages/andromeda-modules/src/time_gate.rs +++ b/packages/andromeda-modules/src/time_gate.rs @@ -1,19 +1,24 @@ -use andromeda_std::{amp::AndrAddr, andr_exec, andr_instantiate, andr_query, error::ContractError}; +use andromeda_std::{ + amp::AndrAddr, + andr_exec, andr_instantiate, andr_query, + common::{expiration::Expiry, Milliseconds}, +}; use cosmwasm_schema::{cw_serde, QueryResponses}; -use cosmwasm_std::{ensure, Addr}; +use cosmwasm_std::Addr; +use cw_utils::Expiration; #[andr_instantiate] #[cw_serde] pub struct InstantiateMsg { pub gate_addresses: Vec, - pub cycle_start_time: CycleStartTime, + pub cycle_start_time: Option, pub time_interval: Option, } #[andr_exec] #[cw_serde] pub enum ExecuteMsg { - UpdateCycleStartTime { cycle_start_time: CycleStartTime }, + UpdateCycleStartTime { cycle_start_time: Option }, UpdateGateAddresses { new_gate_addresses: Vec }, UpdateTimeInterval { time_interval: u64 }, } @@ -24,78 +29,10 @@ pub enum ExecuteMsg { pub enum QueryMsg { #[returns(Addr)] GetCurrentAdoPath {}, - #[returns(CycleStartTime)] + #[returns((Expiration, Milliseconds))] GetCycleStartTime {}, #[returns(Vec)] GetGateAddresses {}, #[returns(String)] GetTimeInterval {}, } - -#[cw_serde] -pub struct CycleStartTime { - pub year: i32, - pub month: u32, - pub day: u32, - pub hour: u32, - pub minute: u32, - pub second: u32, -} - -impl CycleStartTime { - pub fn validate(&self) -> Result<(), ContractError> { - ensure!( - self.year >= 1970, - ContractError::InvalidParameter { - error: Some("Year must be bigger than 1969".to_string()) - } - ); - ensure!( - self.month <= 12 && self.month >= 1, - ContractError::InvalidParameter { - error: Some("Month must be between 1 and 12".to_string()) - } - ); - let year = self.year; - let month = self.month; - let days_in_month_feb_29: [u32; 12] = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; - let days_in_month_feb_28: [u32; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; - - if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) { - ensure!( - self.day <= days_in_month_feb_29[(month - 1) as usize] && self.day >= 1, - ContractError::InvalidParameter { - error: Some("Wrong days of month".to_string()) - } - ); - } else { - ensure!( - self.day <= days_in_month_feb_28[(month - 1) as usize] && self.day >= 1, - ContractError::InvalidParameter { - error: Some("Wrong days of month".to_string()) - } - ); - } - - ensure!( - self.hour <= 23, - ContractError::InvalidParameter { - error: Some("Hour must be less than 24".to_string()) - } - ); - ensure!( - self.minute <= 59, - ContractError::InvalidParameter { - error: Some("Minute must be less than 60".to_string()) - } - ); - ensure!( - self.second <= 59, - ContractError::InvalidParameter { - error: Some("Second must be less than 60".to_string()) - } - ); - - Ok(()) - } -}