Skip to content

Commit

Permalink
lock and is_lock function of lockable component
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Aug 16, 2024
1 parent d311f7e commit 496d0db
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/components/lockable/lockable.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,102 @@
// lockable component
// *************************************************************************
// LOCKABLE COMPONENT
// *************************************************************************
#[starknet::component]
mod LockableComponent {
// *************************************************************************
// IMPORTS
// *************************************************************************
use starknet::{ContractAddress, get_caller_address, get_block_timestamp};
use token_bound_accounts::account::AccountComponent;
use token_bound_accounts::interfaces::IAccount::{
IAccount, IAccountDispatcherTrait, ILockableDispatcher
};

component!(path: AccountComponent, storage: account, event: AccountEvent);

// Account
#[abi(embed_v0)]
impl AccountImpl = AccountComponent::AccountImpl<ContractState>;
impl AccountInternalImpl = AccountComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
lock_util: u64,
#[substorage(v0)]
account: AccountComponent::Storage,
}
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
LockUpdated: LockUpdated
}

/// @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
#[derive(Drop, starknet::Event)]
struct AccountLocked {
#[key]
account: ContractAddress,
locked_at: u64,
lock_util: u64,
}


// *************************************************************************
// ERRORS
// *************************************************************************
mod Errors {
const UNAUTHORIZED: felt252 = 'Account: unauthorized';
const NOT_OWNER: felt252 = 'Not Account Owner';
const EXCEEDS_MAX_LOCK_TIME: felt252 = 'Lock time exceeded';
const LOCKED_ACCOUNT: felt252 = 'Account Locked';
}


// storage that store the token_id and the lock_util perioed

// *************************************************************************
// EXTERNAL FUNCTIONS
// *************************************************************************
#[embeddable_as(LockableImpl)]
impl Lockable<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of ILockable<ComponentState<TContractState>> {
fn lock(ref self: @ComponentState<TContractState>, lock_until: u64) {
let current_timestamp = get_block_timestamp();

// get the token
let (token_contract, token_id, chain_id) = self.account.token();

// get the token owner
let owner = self.account.owner();

assert(owner.is_non_zero(), Errors::UNAUTHORIZED);
assert(get_caller_address != owner, Errors::NOT_OWNER);

assert(lock_until <= current_timestamp + 356, EXCEEDS_MAX_LOCK_TIME);

// _beforeLock may be call before upating the lock period
let ock_status = self._is_locked();
assert(!lock_status, Errors::LOCKED_ACCOUNT);
// set the lock_util which set the period the account is lock
self.lock_util.write(lock_until);

// emit event
self
.emit(
AccountLocked {
account: get_contract_address(), locked_at: current_timestamp, lock_util
}
);
}

fn is_lock(self: @TContractState) -> bool {
self.lock_until.read() > get_block_timestamp()
}
}
}

1 change: 1 addition & 0 deletions src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ mod IERC721;
mod IRegistry;
mod IUpgradeable;
mod IExecutable;
mod ILockable;
7 changes: 7 additions & 0 deletions src/interfaces/ILockable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use starknet::ContractAddress;

#[starknet::interface]
trait ILockable<TContractState> {
fn lock(ref self: TContractState, lock_until: u64);
fn is_lock(self: @TContractState) -> bool;
}

0 comments on commit 496d0db

Please sign in to comment.