Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the first starknet components exercise #207

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions exercises/starknet/components/starknet6.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// starknet6.cairo
// This is very example of the Starknet components.
raghav-rama marked this conversation as resolved.
Show resolved Hide resolved
// It will show you how to get started with components
raghav-rama marked this conversation as resolved.
Show resolved Hide resolved
// But something is fishy here as this component is not working, can you find the error and make the tests pass?

// I AM NOT DONE

use starknet::ContractAddress;
#[starknet::interface]
raghav-rama marked this conversation as resolved.
Show resolved Hide resolved
trait IOwnable<TContractState> {
fn owner(self: @TContractState) -> ContractAddress;
fn set_owner(ref self: TContractState, new_owner: ContractAddress);
}

mod OwnableComponent {
use starknet::ContractAddress;
use super::IOwnable;

#[storage]
struct Storage {
owner: ContractAddress,
}

#[embeddable_as(Ownable)]
impl OwnableImpl<
TContractState, +HasComponent<TContractState>
> of IOwnable<ComponentState<TContractState>> {
fn owner(self: @ComponentState<TContractState>) -> ContractAddress {
self.owner.read()
}
fn set_owner(ref self: ComponentState<TContractState>, new_owner: ContractAddress) {
self.owner.write(new_owner);
}
}
}

#[starknet::contract]
mod OwnableCounter {
use starknet::ContractAddress;
use super::OwnableComponent;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::Ownable<ContractState>;

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
OwnableEvent: OwnableComponent::Event,
}
#[storage]
struct Storage {
counter: u128,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
}
}

#[cfg(test)]
mod tests {
use super::OwnableCounter;
use super::{IOwnableDispatcher, IOwnable, IOwnableDispatcherTrait};
use starknet::contract_address_const;
use starknet::syscalls::deploy_syscall;

#[test]
#[available_gas(200_000_000)]
fn test_contract_read() {
let dispatcher = deploy_contract();
dispatcher.set_owner(contract_address_const::<0>());
assert(contract_address_const::<0>() == dispatcher.owner(), 'Some fuck up happened');
}
#[test]
#[available_gas(200_000_000)]
#[should_panic]
fn test_contract_read_fail() {
let dispatcher = deploy_contract();
dispatcher.set_owner(contract_address_const::<1>());
assert(contract_address_const::<2>() == dispatcher.owner(), 'Some fuck up happened');
}
fn deploy_contract() -> IOwnableDispatcher {
let mut calldata = ArrayTrait::new();
let (address0, _) = deploy_syscall(
OwnableCounter::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap();
let contract0 = IOwnableDispatcher { contract_address: address0 };
contract0
}
}
8 changes: 8 additions & 0 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,11 @@ hint = """
You can call other contracts from inside a contract. To do this, you will need to create a Dispatcher object
of the type of the called contract. Dispatchers have associated methods available under the `DispatcherTrait`, corresponding to the external functions of the contract that you want to call.
"""

[[exercises]]
name = "starknet6"
path = "exercises/starknet/components/starknet6.cairo"
mode = "test"
hint = """
Is there maybe a decorator that annotates that a component is a components? 🤔🤔🤔
raghav-rama marked this conversation as resolved.
Show resolved Hide resolved
"""
Loading