Skip to content

Commit

Permalink
switch lock_until value from milliseconds to seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Aug 20, 2024
1 parent 3dd5703 commit f53d27b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/components/lockable/lockable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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

Expand All @@ -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);

Expand All @@ -99,7 +99,7 @@ pub mod LockableComponent {
);
}

fn is_lock(self: @ComponentState<TContractState>) -> (bool, u64) {
fn is_locked (self: @ComponentState<TContractState>) -> (bool, u64) {
let unlock_timestamp = self.lock_until.read();
let current_time = get_block_timestamp();
if (current_time < unlock_timestamp) {
Expand Down
12 changes: 6 additions & 6 deletions src/components/presets/account_preset.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub mod AccountPreset {
impl Executable of IExecutable<ContractState> {
fn execute(ref self: ContractState, mut calls: Array<Call>) -> Array<Span<felt252>> {
// 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)
}
}
Expand All @@ -78,8 +78,8 @@ pub mod AccountPreset {
impl Upgradeable of IUpgradeable<ContractState> {
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);
}
}
Expand All @@ -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()
}
}
}
2 changes: 1 addition & 1 deletion src/interfaces/ILockable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ use starknet::ContractAddress;
#[starknet::interface]
pub trait ILockable<TContractState> {
fn lock(ref self: TContractState, lock_until: u64);
fn is_lock(self: @TContractState) -> (bool, u64);
fn is_locked(self: @TContractState) -> (bool, u64);
}
6 changes: 3 additions & 3 deletions tests/test_lockable_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 };

Expand Down

0 comments on commit f53d27b

Please sign in to comment.