Skip to content

Commit

Permalink
test: ch00/factory
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed May 3, 2024
1 parent 1637154 commit 3364c20
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub trait ICallee<TContractState> {

#[starknet::contract]
pub mod Callee {

#[storage]
struct Storage {
value: u128,
Expand Down
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.

80 changes: 0 additions & 80 deletions listings/getting-started/factory/src/tests.cairo

This file was deleted.

2 changes: 1 addition & 1 deletion src/ch00/interacting/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Using the factory pattern, we can deploy multiple instances of the same contract
Here's a minimal example of a factory contract that deploy the `SimpleCounter` contract:

```rust
{{#include ../../../listings/getting-started/factory/src/simple_factory.cairo:contract}}
{{#rustdoc_include ../../../listings/getting-started/factory/src/simple_factory.cairo:contract}}
```

This factory can be used to deploy multiple instances of the `SimpleCounter` contract by calling the `create_counter` and `create_counter_at` functions.
Expand Down

0 comments on commit 3364c20

Please sign in to comment.