From f53d27b065aa8db628721643eb0a5a735378cf27 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Tue, 20 Aug 2024 18:14:03 +0100 Subject: [PATCH] switch lock_until value from milliseconds to seconds --- src/components/lockable/lockable.cairo | 14 +++++++------- src/components/presets/account_preset.cairo | 12 ++++++------ src/interfaces/ILockable.cairo | 2 +- tests/test_lockable_component.cairo | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/components/lockable/lockable.cairo b/src/components/lockable/lockable.cairo index 92233a4..f710f9a 100644 --- a/src/components/lockable/lockable.cairo +++ b/src/components/lockable/lockable.cairo @@ -31,7 +31,7 @@ pub mod LockableComponent { /// @notice Emitted when the account is locked /// @param account tokenbound account who's lock function was triggered /// @param locked_at timestamp at which the lock function was triggered - /// @param duration time duration for which the account remains locked + /// @param lock_until time duration for which the account remains locked in second #[derive(Drop, starknet::Event)] pub struct AccountLocked { #[key] @@ -51,8 +51,8 @@ pub mod LockableComponent { pub const LOCKED_ACCOUNT: felt252 = 'Account: Locked'; } - pub const YEARS_DAYS_MILLISECONS: u64 = 31536000000; - pub const ONE_DAY_IN_MILLISECONDS: u64 = 86400000; + pub const YEAR_DAYS_SECONDS: u64 = 31536000; + // storage that store the token_id and the lock_util perioed @@ -72,13 +72,13 @@ pub mod LockableComponent { let is_valid = account_comp._is_valid_signer(get_caller_address()); assert(is_valid, Errors::UNAUTHORIZED); - let lock_until_in_milliseconds: u64 = lock_until * ONE_DAY_IN_MILLISECONDS; + assert( - lock_until_in_milliseconds <= current_timestamp + YEARS_DAYS_MILLISECONS, + lock_until <= current_timestamp + YEAR_DAYS_SECONDS, Errors::EXCEEDS_MAX_LOCK_TIME ); - let (lock_status, _) = self.is_lock(); + let (lock_status, _) = self.is_locked(); assert(lock_status != true, Errors::LOCKED_ACCOUNT); @@ -99,7 +99,7 @@ pub mod LockableComponent { ); } - fn is_lock(self: @ComponentState) -> (bool, u64) { + fn is_locked (self: @ComponentState) -> (bool, u64) { let unlock_timestamp = self.lock_until.read(); let current_time = get_block_timestamp(); if (current_time < unlock_timestamp) { diff --git a/src/components/presets/account_preset.cairo b/src/components/presets/account_preset.cairo index fbdcf2b..7a87a86 100644 --- a/src/components/presets/account_preset.cairo +++ b/src/components/presets/account_preset.cairo @@ -65,8 +65,8 @@ pub mod AccountPreset { impl Executable of IExecutable { fn execute(ref self: ContractState, mut calls: Array) -> Array> { // cannot make this call when the account is lock - let (is_lock, _) = self.lockable.is_lock(); - assert(is_lock != true, 'Account: locked'); + let (is_locked, _) = self.lockable.is_locked(); + assert(is_locked != true, 'Account: locked'); self.account._execute(calls) } } @@ -78,8 +78,8 @@ pub mod AccountPreset { impl Upgradeable of IUpgradeable { fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { // cannot make this call when the account is lock - let (is_lock, _) = self.lockable.is_lock(); - assert(is_lock != true, 'Account: locked'); + let (is_locked, _) = self.lockable.is_locked(); + assert(is_locked != true, 'Account: locked'); self.upgradeable._upgrade(new_class_hash); } } @@ -92,8 +92,8 @@ pub mod AccountPreset { fn lock(ref self: ContractState, lock_until: u64) { self.lockable.lock(lock_until); } - fn is_lock(self: @ContractState) -> (bool, u64) { - self.lockable.is_lock() + fn is_locked(self: @ContractState) -> (bool, u64) { + self.lockable.is_locked() } } } diff --git a/src/interfaces/ILockable.cairo b/src/interfaces/ILockable.cairo index cb5bebe..d075dd2 100644 --- a/src/interfaces/ILockable.cairo +++ b/src/interfaces/ILockable.cairo @@ -3,5 +3,5 @@ use starknet::ContractAddress; #[starknet::interface] pub trait ILockable { fn lock(ref self: TContractState, lock_until: u64); - fn is_lock(self: @TContractState) -> (bool, u64); + fn is_locked(self: @TContractState) -> (bool, u64); } diff --git a/tests/test_lockable_component.cairo b/tests/test_lockable_component.cairo index 73db1a3..61c19d5 100644 --- a/tests/test_lockable_component.cairo +++ b/tests/test_lockable_component.cairo @@ -93,7 +93,7 @@ fn test_lockable() { let lockable_dispatcher = ILockableDispatcher { contract_address }; let lock_duration = 40_u64; lockable_dispatcher.lock(lock_duration); - let (check_lock, _) = lockable_dispatcher.is_lock(); + let (check_lock, _) = lockable_dispatcher.is_locked(); assert(check_lock == true, 'Account Not Lock'); stop_cheat_caller_address(contract_address); @@ -114,7 +114,7 @@ fn test_unlock_once_lock_duration_end() { start_cheat_block_timestamp(contract_address, lock_duration); start_cheat_block_timestamp(contract_address, lock_duration); - let (check_lock, _) = lockable_dispatcher.is_lock(); + let (check_lock, _) = lockable_dispatcher.is_locked(); assert(check_lock != true, 'Account Not Lock'); stop_cheat_block_timestamp(contract_address); stop_cheat_caller_address(contract_address); @@ -212,7 +212,7 @@ fn test_should_fail_for_greater_than_a_year_lock_time() { let acct_dispatcher = IAccountDispatcher { contract_address: contract_address }; let owner = acct_dispatcher.owner(); - let lock_duration = 3000_u64; + let lock_duration = 315365000_u64; let lockable_dispatcher = ILockableDispatcher { contract_address };