-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e72a1c
commit 80fcb7d
Showing
11 changed files
with
3,240 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod erc20; | |
pub mod mintable; | ||
pub mod ownable; | ||
pub mod upgradable; | ||
pub mod ownable_mini; |
53 changes: 53 additions & 0 deletions
53
crates/cairo-lang-starknet/cairo_level_tests/components/ownable_mini.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use starknet::ContractAddress; | ||
use starknet::storage::{StorageAsPath, StorageNode, MutableStorageNode}; | ||
|
||
#[starknet::interface] | ||
pub trait TransferTrait<TContractState> { | ||
fn owner(self: @TContractState) -> ContractAddress; | ||
fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress); | ||
} | ||
|
||
/// A trait that, given a path to a storage, provides the storage node and mutable storage node. | ||
pub trait HasStorage< | ||
TContractState, | ||
/// The storage type. | ||
Storage, | ||
/// The storage node. | ||
impl Node: starknet::storage::StorageNode<Storage>, | ||
/// The mutable storage node. | ||
impl NodeMut: starknet::storage::MutableStorageNode<Storage> | ||
> { | ||
fn storage(self: @TContractState) -> Node::NodeType; | ||
fn storage_mut(ref self: TContractState) -> NodeMut::NodeType; | ||
} | ||
|
||
#[starknet::storage_node] | ||
pub struct OwnableStorage { | ||
owner: ContractAddress, | ||
} | ||
|
||
#[starknet::embeddable] | ||
pub impl TransferImpl< | ||
TContractState, +HasStorage<TContractState, OwnableStorage>, +Drop<TContractState> | ||
> of TransferTrait<TContractState> { | ||
fn owner(self: @TContractState) -> ContractAddress { | ||
self.storage().owner.read() | ||
} | ||
|
||
fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress) { | ||
self.validate_ownership(); | ||
self.storage_mut().owner.write(new_owner); | ||
} | ||
} | ||
|
||
#[generate_trait] | ||
pub impl OwnableHelperImpl< | ||
TContractState, +HasStorage<TContractState, OwnableStorage>, +Drop<TContractState> | ||
> of OwnableHelperTrait<TContractState> { | ||
fn init_ownable(ref self: TContractState, owner: ContractAddress) { | ||
self.storage_mut().owner.write(owner); | ||
} | ||
fn validate_ownership(self: @TContractState) { | ||
assert(self.storage().owner.read() == starknet::get_caller_address(), 'Wrong owner.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ mod ownable_erc20; | |
mod upgradable_counter; | ||
mod mintable; | ||
mod storage_accesses; | ||
mod with_ownable_mini; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
crates/cairo-lang-starknet/cairo_level_tests/contracts/with_ownable_mini.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use starknet::ContractAddress; | ||
#[starknet::contract] | ||
mod ownable_mini_contract { | ||
use cairo_level_tests::components::ownable_mini; | ||
use starknet::ContractAddress; | ||
use starknet::storage::{StorageAsPath, StorageNode, MutableStorageNode}; | ||
|
||
#[storage] | ||
struct Storage { | ||
ownable: ownable_mini::OwnableStorage, | ||
balance: u128, | ||
} | ||
|
||
#[abi(embed_v0)] | ||
impl OwnershipTransfer = ownable_mini::TransferImpl<ContractState>; | ||
|
||
impl OwnershipHelper = ownable_mini::OwnableHelperImpl<ContractState>; | ||
|
||
impl OwnableHasStorage of ownable_mini::HasStorage< | ||
ContractState, ownable_mini::OwnableStorage | ||
> { | ||
fn storage(self: @ContractState) -> StorageNode::<ownable_mini::OwnableStorage>::NodeType { | ||
self.ownable.as_path().storage_node() | ||
} | ||
fn storage_mut( | ||
ref self: ContractState | ||
) -> MutableStorageNode::<ownable_mini::OwnableStorage>::NodeType { | ||
self.ownable.as_path().mutable_storage_node() | ||
} | ||
} | ||
|
||
#[abi(per_item)] | ||
#[generate_trait] | ||
impl OwnableBalanceImpl of OwnableBalance { | ||
#[constructor] | ||
fn constructor(ref self: ContractState, owner: ContractAddress, initial: u128) { | ||
self.init_ownable(owner); | ||
self.balance.write(initial); | ||
} | ||
#[external(v0)] | ||
fn get_balance(self: @ContractState) -> u128 { | ||
self.balance.read() | ||
} | ||
#[external(v0)] | ||
fn set_balance(ref self: ContractState, new_balance: u128) { | ||
self.validate_ownership(); | ||
self.balance.write(new_balance); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.