Skip to content

Commit

Permalink
storage access component (#5959)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomer-StarkWare authored Jul 8, 2024
1 parent 9e72a1c commit 80fcb7d
Show file tree
Hide file tree
Showing 11 changed files with 3,240 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod erc20;
pub mod mintable;
pub mod ownable;
pub mod upgradable;
pub mod ownable_mini;
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.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ mod ownable_erc20;
mod upgradable_counter;
mod mintable;
mod storage_accesses;
mod with_ownable_mini;
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod storage_accesses {
use starknet::get_caller_address;
use starknet::ContractAddress;
use super::{UserInfo, TransactionInfo};
use starknet::storage::{StoragePathEntry};
use starknet::storage::StoragePathEntry;

#[storage]
struct Storage {
Expand Down
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);
}
}
}
1 change: 1 addition & 0 deletions crates/cairo-lang-starknet/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::test_utils::{get_example_file_path, get_test_contract};
#[test_case("token_bridge::token_bridge")]
#[test_case("with_erc20::erc20_contract")]
#[test_case("with_ownable::ownable_balance")]
#[test_case("with_ownable_mini::ownable_mini_contract")]
#[test_case("ownable_erc20::ownable_erc20_contract")]
#[test_case("upgradable_counter::counter_contract")]
#[test_case("mintable::mintable_erc20_ownable")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod storage_accesses {
use starknet::get_caller_address;
use starknet::ContractAddress;
use super::{UserInfo, TransactionInfo};
use starknet::storage::{StoragePathEntry};
use starknet::storage::StoragePathEntry;

#[constructor]
fn constructor(
Expand Down Expand Up @@ -1763,7 +1763,7 @@ impl MutableIERC20SafeDispatcherSubPointersCopy of core::traits::Copy::<MutableI
use starknet::get_caller_address;
use starknet::ContractAddress;
use super::{UserInfo, TransactionInfo};
use starknet::storage::{StoragePathEntry};
use starknet::storage::StoragePathEntry;

#[constructor]
fn constructor(
Expand Down
Loading

0 comments on commit 80fcb7d

Please sign in to comment.