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

Maintain: chapter00 #182

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 0 additions & 23 deletions listings/getting-started/calling_other_contracts/src/callee.cairo

This file was deleted.

74 changes: 68 additions & 6 deletions listings/getting-started/calling_other_contracts/src/caller.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
use starknet::ContractAddress;

// We need to have the interface of the callee contract defined
// so that we can import the Dispatcher.
// ANCHOR: callee_contract
// This will automatically generate ICalleeDispatcher and ICalleeDispatcherTrait
#[starknet::interface]
pub trait ICallee<TContractState> {
fn set_value(ref self: TContractState, value: u128) -> u128;
}

#[starknet::contract]
pub mod Callee {
#[storage]
struct Storage {
value: u128,
}

#[abi(embed_v0)]
impl ICalleeImpl of super::ICallee<ContractState> {
fn set_value(ref self: ContractState, value: u128) -> u128 {
self.value.write(value);
value
}
}
}
// ANCHOR_END: callee_contract

#[starknet::interface]
pub trait ICaller<TContractState> {
fn set_value_from_address(ref self: TContractState, addr: ContractAddress, value: u128);
fn set_value_from_address(
ref self: TContractState, addr: starknet::ContractAddress, value: u128
);
}

// ANCHOR: caller_contract
#[starknet::contract]
pub mod Caller {
// We import the Dispatcher of the called contract
// We need to import the dispatcher of the callee contract
// If you don't have a proper import, you can redefine the interface by yourself
use super::{ICalleeDispatcher, ICalleeDispatcherTrait};
use starknet::ContractAddress;

Expand All @@ -28,3 +47,46 @@ pub mod Caller {
}
}
}
// ANCHOR_END: caller_contract

#[cfg(test)]
mod tests {
use super::{
Callee, ICalleeDispatcher, ICalleeDispatcherTrait, Callee::valueContractMemberStateTrait,
Caller, ICallerDispatcher, ICallerDispatcherTrait
};
use starknet::{
ContractAddress, contract_address_const, testing::set_contract_address,
syscalls::deploy_syscall, SyscallResultTrait
};

fn deploy() -> (ICalleeDispatcher, ICallerDispatcher) {
let (address_callee, _) = deploy_syscall(
Callee::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
let (address_caller, _) = deploy_syscall(
Caller::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
(
ICalleeDispatcher { contract_address: address_callee },
ICallerDispatcher { contract_address: address_caller }
)
}

#[test]
fn test_caller() {
let init_value: u128 = 42;

let (callee, caller) = deploy();
caller.set_value_from_address(callee.contract_address, init_value);

let state = Callee::contract_state_for_testing();
set_contract_address(callee.contract_address);

let value_read: u128 = state.value.read();

assert_eq!(value_read, init_value);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
mod callee;
mod caller;

#[cfg(test)]
mod tests;
43 changes: 0 additions & 43 deletions listings/getting-started/calling_other_contracts/src/tests.cairo

This file was deleted.

6 changes: 3 additions & 3 deletions listings/getting-started/events/src/counter.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// ANCHOR: contract
#[starknet::interface]
pub trait IEventCounter<TContractState> {
fn increment(ref self: TContractState, amount: u128);
}

// ANCHOR: contract
#[starknet::contract]
pub mod EventCounter {
use starknet::{get_caller_address, ContractAddress};
Expand Down Expand Up @@ -94,17 +94,17 @@ mod tests {
assert_eq!(state.counter.read(), amount);

// Notice the order: the first event emitted is the first to be popped.
/// ANCHOR: test_event
/// ANCHOR: test_events
assert_eq!(
starknet::testing::pop_log(contract_address),
Option::Some(Event::CounterIncreased(CounterIncreased { amount }))
);
// ANCHOR_END: test_events
assert_eq!(
starknet::testing::pop_log(contract_address),
Option::Some(
Event::UserIncreaseCounter(UserIncreaseCounter { user: caller, new_value: amount })
)
);
// ANCHOR_END: test_events
}
}
4 changes: 0 additions & 4 deletions listings/getting-started/factory/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
mod simple_factory;
mod target;

#[cfg(test)]
mod tests;
124 changes: 122 additions & 2 deletions listings/getting-started/factory/src/simple_factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub trait ICounterFactory<TContractState> {

#[starknet::contract]
pub mod CounterFactory {
use starknet::{ContractAddress, ClassHash, SyscallResultTrait};
use starknet::syscalls::deploy_syscall;
use starknet::{ContractAddress, ClassHash, SyscallResultTrait, syscalls::deploy_syscall};

#[storage]
struct Storage {
Expand Down Expand Up @@ -67,4 +66,125 @@ pub mod CounterFactory {
}
// ANCHOR_END: contract

#[cfg(test)]
mod tests {
use super::{CounterFactory, ICounterFactoryDispatcher, ICounterFactoryDispatcherTrait};
use starknet::{
SyscallResultTrait, ContractAddress, ClassHash, contract_address_const,
syscalls::deploy_syscall
};

// Define a target contract to deploy
mod target {
#[starknet::interface]
pub trait ISimpleCounter<TContractState> {
fn get_current_count(self: @TContractState) -> u128;
fn increment(ref self: TContractState);
fn decrement(ref self: TContractState);
}

#[starknet::contract]
pub mod SimpleCounter {
#[storage]
struct Storage {
// Counter variable
counter: u128,
}

#[constructor]
fn constructor(ref self: ContractState, init_value: u128) {
// Store initial value
self.counter.write(init_value);
}

#[abi(embed_v0)]
impl SimpleCounter of super::ISimpleCounter<ContractState> {
fn get_current_count(self: @ContractState) -> u128 {
self.counter.read()
}

fn increment(ref self: ContractState) {
// Store counter value + 1
let mut counter: u128 = self.counter.read() + 1;
self.counter.write(counter);
}
fn decrement(ref self: ContractState) {
// Store counter value - 1
let mut counter: u128 = self.counter.read() - 1;
self.counter.write(counter);
}
}
}
}
use target::{ISimpleCounterDispatcher, ISimpleCounterDispatcherTrait};

/// Deploy a counter factory contract
fn deploy_factory(
counter_class_hash: ClassHash, init_value: u128
) -> ICounterFactoryDispatcher {
let mut constructor_calldata: Array::<felt252> = array![
init_value.into(), counter_class_hash.into()
];

let (contract_address, _) = deploy_syscall(
CounterFactory::TEST_CLASS_HASH.try_into().unwrap(),
0,
constructor_calldata.span(),
false
)
.unwrap_syscall();

ICounterFactoryDispatcher { contract_address }
}

#[test]
fn test_deploy_counter_constructor() {
let init_value = 10;

let counter_class_hash: ClassHash = target::SimpleCounter::TEST_CLASS_HASH
.try_into()
.unwrap();
let factory = deploy_factory(counter_class_hash, init_value);

let counter_address = factory.create_counter();
let counter = target::ISimpleCounterDispatcher { contract_address: counter_address };

assert_eq!(counter.get_current_count(), init_value);
}

#[test]
fn test_deploy_counter_argument() {
let init_value = 10;
let argument_value = 20;

let counter_class_hash: ClassHash = target::SimpleCounter::TEST_CLASS_HASH
.try_into()
.unwrap();
let factory = deploy_factory(counter_class_hash, init_value);

let counter_address = factory.create_counter_at(argument_value);
let counter = target::ISimpleCounterDispatcher { contract_address: counter_address };

assert_eq!(counter.get_current_count(), argument_value);
}

#[test]
fn test_deploy_multiple() {
let init_value = 10;
let argument_value = 20;

let counter_class_hash: ClassHash = target::SimpleCounter::TEST_CLASS_HASH
.try_into()
.unwrap();
let factory = deploy_factory(counter_class_hash, init_value);

let mut counter_address = factory.create_counter();
let counter_1 = target::ISimpleCounterDispatcher { contract_address: counter_address };

counter_address = factory.create_counter_at(argument_value);
let counter_2 = target::ISimpleCounterDispatcher { contract_address: counter_address };

assert_eq!(counter_1.get_current_count(), init_value);
assert_eq!(counter_2.get_current_count(), argument_value);
}
}
39 changes: 0 additions & 39 deletions listings/getting-started/factory/src/target.cairo

This file was deleted.

Loading