From 1499fb3d8fbe398a25d5b6ab366840105e692a2a Mon Sep 17 00:00:00 2001 From: diegoTech14 Date: Tue, 1 Oct 2024 18:49:41 -0600 Subject: [PATCH 1/7] feat: test_fund_manager --- contracts/tests/test_fund_manager.cairo | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 contracts/tests/test_fund_manager.cairo diff --git a/contracts/tests/test_fund_manager.cairo b/contracts/tests/test_fund_manager.cairo new file mode 100644 index 0000000..109d61c --- /dev/null +++ b/contracts/tests/test_fund_manager.cairo @@ -0,0 +1,47 @@ +// ****************************************************************** +// FUND MANAGER TEST +// ****************************************************************** +use starknet::{ContractAddress, contract_address_const}; +use starknet::class_hash::{ClassHash}; +use starknet::syscalls::deploy_syscall; + +use snforge_std::{ + ContractClass, declare, ContractClassTrait, start_cheat_caller_address_global, get_class_hash +}; + +use openzeppelin::utils::serde::SerializedAppend; + +use gostarkme::fundManager::IFundManagerDispatcher; +use gostarkme::fundManager::IFundManagerDispatcherTrait; + +fn OWNER() -> ContractAddress { + contract_address_const::<'OWNER'>() +} + +fn __setup__() -> (ContractAddress, ClassHash){ + // Fund + let fund = declare("Fund").unwrap(); + let mut fund_calldata: Array = array![]; + fund_calldata.append_serde(OWNER()); + let (fund_contract_address, _) = fund.deploy(@fund_calldata).unwrap(); + let fund_class_hash = get_class_hash(fund_contract_address); + + // Fund Manager + let fund_manager = declare("FundManager").unwrap(); + let mut fund_manager_calldata: Array = array![]; + fund_manager_calldata.append_serde(fund_class_hash); + let (contract_address, _) = fund_manager.deploy(@fund_manager_calldata).unwrap(); + + return (contract_address, fund_class_hash,); +} + +// ************************************************************************* +// TEST +// ************************************************************************* + +#[test] +fn test_constructor() { + start_cheat_caller_address_global(OWNER()); + let (contract_address, fund_class_hash) = __setup__(); + +} \ No newline at end of file From b4b1e51e1226075a4074b4be6cda8e201e40d83f Mon Sep 17 00:00:00 2001 From: diegoTech14 Date: Tue, 1 Oct 2024 21:25:45 -0600 Subject: [PATCH 2/7] feat: test fund manager --- contracts/tests/test_fund_manager.cairo | 37 +++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/contracts/tests/test_fund_manager.cairo b/contracts/tests/test_fund_manager.cairo index 109d61c..0c94ded 100644 --- a/contracts/tests/test_fund_manager.cairo +++ b/contracts/tests/test_fund_manager.cairo @@ -14,15 +14,33 @@ use openzeppelin::utils::serde::SerializedAppend; use gostarkme::fundManager::IFundManagerDispatcher; use gostarkme::fundManager::IFundManagerDispatcherTrait; +fn ID() -> u128 { + 1 +} fn OWNER() -> ContractAddress { contract_address_const::<'OWNER'>() } +fn OTHER_USER() -> ContractAddress { + contract_address_const::<'USER'>() +} +fn NAME() -> felt252 { + 'NAME_FUND_TEST' +} +fn REASON() -> ByteArray { + "Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum" +} +fn GOAL() -> u64 { + 1000 +} -fn __setup__() -> (ContractAddress, ClassHash){ +fn __setup__() -> (ContractAddress, ClassHash) { // Fund let fund = declare("Fund").unwrap(); let mut fund_calldata: Array = array![]; + fund_calldata.append_serde(ID()); fund_calldata.append_serde(OWNER()); + fund_calldata.append_serde(NAME()); + fund_calldata.append_serde(GOAL()); let (fund_contract_address, _) = fund.deploy(@fund_calldata).unwrap(); let fund_class_hash = get_class_hash(fund_contract_address); @@ -43,5 +61,20 @@ fn __setup__() -> (ContractAddress, ClassHash){ fn test_constructor() { start_cheat_caller_address_global(OWNER()); let (contract_address, fund_class_hash) = __setup__(); + let fund_manager_contract = IFundManagerDispatcher { contract_address }; +} + +#[test] +fn test_new_fund(){ + start_cheat_caller_address_global(OWNER()); + let (contract_address, fund_class_hash) = __setup__(); + let fund_manager_contract = IFundManagerDispatcher { contract_address }; + fund_manager_contract.newFund(NAME(), GOAL()); + let expected_fund_class_hash = get_class_hash( + fund_manager_contract.getFund(0) + ); + let current_id = fund_manager_contract.getCurrentId(); + assert(expected_fund_class_hash == fund_class_hash, 'Invalid fund address'); + assert(current_id == 1, 'Invalid current ID'); +} -} \ No newline at end of file From 03a52a740fd9537b8ed88017e91e8ef21e999fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Ag=C3=BCero?= <54730752+EmmanuelAR@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:53:51 -0600 Subject: [PATCH 3/7] Fix: move increment id because the first fund was with id 0. --- contracts/src/fundManager.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/src/fundManager.cairo b/contracts/src/fundManager.cairo index 36426d8..7d81d97 100644 --- a/contracts/src/fundManager.cairo +++ b/contracts/src/fundManager.cairo @@ -56,8 +56,8 @@ mod FundManager { self.fund_class_hash.read(), 12345, calldata.span(), false ) .unwrap(); - self.funds.write(self.current_id.read(), address_0); self.current_id.write(self.current_id.read() + 1); + self.funds.write(self.current_id.read(), address_0); } fn getCurrentId(self: @ContractState) -> u128 { return self.current_id.read(); From 6c4d7ec37a48c4bf2245b46ee9bb9388fcbf05b3 Mon Sep 17 00:00:00 2001 From: diegoTech14 Date: Tue, 1 Oct 2024 18:49:41 -0600 Subject: [PATCH 4/7] feat: test_fund_manager --- contracts/tests/test_fund_manager.cairo | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 contracts/tests/test_fund_manager.cairo diff --git a/contracts/tests/test_fund_manager.cairo b/contracts/tests/test_fund_manager.cairo new file mode 100644 index 0000000..109d61c --- /dev/null +++ b/contracts/tests/test_fund_manager.cairo @@ -0,0 +1,47 @@ +// ****************************************************************** +// FUND MANAGER TEST +// ****************************************************************** +use starknet::{ContractAddress, contract_address_const}; +use starknet::class_hash::{ClassHash}; +use starknet::syscalls::deploy_syscall; + +use snforge_std::{ + ContractClass, declare, ContractClassTrait, start_cheat_caller_address_global, get_class_hash +}; + +use openzeppelin::utils::serde::SerializedAppend; + +use gostarkme::fundManager::IFundManagerDispatcher; +use gostarkme::fundManager::IFundManagerDispatcherTrait; + +fn OWNER() -> ContractAddress { + contract_address_const::<'OWNER'>() +} + +fn __setup__() -> (ContractAddress, ClassHash){ + // Fund + let fund = declare("Fund").unwrap(); + let mut fund_calldata: Array = array![]; + fund_calldata.append_serde(OWNER()); + let (fund_contract_address, _) = fund.deploy(@fund_calldata).unwrap(); + let fund_class_hash = get_class_hash(fund_contract_address); + + // Fund Manager + let fund_manager = declare("FundManager").unwrap(); + let mut fund_manager_calldata: Array = array![]; + fund_manager_calldata.append_serde(fund_class_hash); + let (contract_address, _) = fund_manager.deploy(@fund_manager_calldata).unwrap(); + + return (contract_address, fund_class_hash,); +} + +// ************************************************************************* +// TEST +// ************************************************************************* + +#[test] +fn test_constructor() { + start_cheat_caller_address_global(OWNER()); + let (contract_address, fund_class_hash) = __setup__(); + +} \ No newline at end of file From a5043bb687c033794c834e1d5ac296cdf77d14b1 Mon Sep 17 00:00:00 2001 From: diegoTech14 Date: Tue, 1 Oct 2024 21:25:45 -0600 Subject: [PATCH 5/7] feat: test fund manager --- contracts/tests/test_fund_manager.cairo | 37 +++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/contracts/tests/test_fund_manager.cairo b/contracts/tests/test_fund_manager.cairo index 109d61c..0c94ded 100644 --- a/contracts/tests/test_fund_manager.cairo +++ b/contracts/tests/test_fund_manager.cairo @@ -14,15 +14,33 @@ use openzeppelin::utils::serde::SerializedAppend; use gostarkme::fundManager::IFundManagerDispatcher; use gostarkme::fundManager::IFundManagerDispatcherTrait; +fn ID() -> u128 { + 1 +} fn OWNER() -> ContractAddress { contract_address_const::<'OWNER'>() } +fn OTHER_USER() -> ContractAddress { + contract_address_const::<'USER'>() +} +fn NAME() -> felt252 { + 'NAME_FUND_TEST' +} +fn REASON() -> ByteArray { + "Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum" +} +fn GOAL() -> u64 { + 1000 +} -fn __setup__() -> (ContractAddress, ClassHash){ +fn __setup__() -> (ContractAddress, ClassHash) { // Fund let fund = declare("Fund").unwrap(); let mut fund_calldata: Array = array![]; + fund_calldata.append_serde(ID()); fund_calldata.append_serde(OWNER()); + fund_calldata.append_serde(NAME()); + fund_calldata.append_serde(GOAL()); let (fund_contract_address, _) = fund.deploy(@fund_calldata).unwrap(); let fund_class_hash = get_class_hash(fund_contract_address); @@ -43,5 +61,20 @@ fn __setup__() -> (ContractAddress, ClassHash){ fn test_constructor() { start_cheat_caller_address_global(OWNER()); let (contract_address, fund_class_hash) = __setup__(); + let fund_manager_contract = IFundManagerDispatcher { contract_address }; +} + +#[test] +fn test_new_fund(){ + start_cheat_caller_address_global(OWNER()); + let (contract_address, fund_class_hash) = __setup__(); + let fund_manager_contract = IFundManagerDispatcher { contract_address }; + fund_manager_contract.newFund(NAME(), GOAL()); + let expected_fund_class_hash = get_class_hash( + fund_manager_contract.getFund(0) + ); + let current_id = fund_manager_contract.getCurrentId(); + assert(expected_fund_class_hash == fund_class_hash, 'Invalid fund address'); + assert(current_id == 1, 'Invalid current ID'); +} -} \ No newline at end of file From 232eaa1cec1b5159f93f543d823ac852c600e89e Mon Sep 17 00:00:00 2001 From: diegoTech14 Date: Wed, 2 Oct 2024 15:03:18 -0600 Subject: [PATCH 6/7] fix: added get methods to fundManager and assert constructor values --- contracts/src/fundManager.cairo | 26 ++++++++++++++++--------- contracts/tests/test_fund_manager.cairo | 23 ++++++++++++---------- 2 files changed, 30 insertions(+), 19 deletions(-) mode change 100644 => 100755 contracts/src/fundManager.cairo mode change 100644 => 100755 contracts/tests/test_fund_manager.cairo diff --git a/contracts/src/fundManager.cairo b/contracts/src/fundManager.cairo old mode 100644 new mode 100755 index 7d81d97..28e1a20 --- a/contracts/src/fundManager.cairo +++ b/contracts/src/fundManager.cairo @@ -6,13 +6,15 @@ pub trait IFundManager { fn newFund(ref self: TContractState, name: felt252, goal: u64); fn getCurrentId(self: @TContractState) -> u128; fn getFund(self: @TContractState, id: u128) -> ContractAddress; + fn getOwner(self: @TContractState) -> ContractAddress; + fn getFundClassHash(self: @TContractState) -> ClassHash; } #[starknet::contract] mod FundManager { - // ************************************************************************* + // ************************* // IMPORT - // ************************************************************************* + // ************************* use core::array::ArrayTrait; use core::traits::TryInto; use starknet::ContractAddress; @@ -20,9 +22,9 @@ mod FundManager { use starknet::class_hash::ClassHash; use starknet::get_caller_address; - // ************************************************************************* + // ************************* // STORAGE - // ************************************************************************* + // ************************* #[storage] struct Storage { owner: ContractAddress, @@ -31,9 +33,9 @@ mod FundManager { fund_class_hash: ClassHash, } - // ************************************************************************* + // ************************* // CONSTRUCTOR - // ************************************************************************* + // ************************* #[constructor] fn constructor(ref self: ContractState, fund_class_hash: felt252) { self.owner.write(get_caller_address()); @@ -41,9 +43,9 @@ mod FundManager { self.current_id.write(0); } - // ************************************************************************* + // ************************* // EXTERNALS - // ************************************************************************* + // ************************* #[abi(embed_v0)] impl FundManagerImpl of super::IFundManager { fn newFund(ref self: ContractState, name: felt252, goal: u64) { @@ -65,5 +67,11 @@ mod FundManager { fn getFund(self: @ContractState, id: u128) -> ContractAddress { return self.funds.read(id); } + fn getOwner(self: @ContractState) -> ContractAddress { + return self.owner.read(); + } + fn getFundClassHash(self: @ContractState) -> ClassHash { + return self.fund_class_hash.read(); + } } -} +} \ No newline at end of file diff --git a/contracts/tests/test_fund_manager.cairo b/contracts/tests/test_fund_manager.cairo old mode 100644 new mode 100755 index 0c94ded..b603b66 --- a/contracts/tests/test_fund_manager.cairo +++ b/contracts/tests/test_fund_manager.cairo @@ -1,6 +1,6 @@ -// ****************************************************************** +// *************************************************************************************** // FUND MANAGER TEST -// ****************************************************************** +// *************************************************************************************** use starknet::{ContractAddress, contract_address_const}; use starknet::class_hash::{ClassHash}; use starknet::syscalls::deploy_syscall; @@ -33,7 +33,7 @@ fn GOAL() -> u64 { 1000 } -fn __setup__() -> (ContractAddress, ClassHash) { +fn _setup_() -> (ContractAddress, ClassHash) { // Fund let fund = declare("Fund").unwrap(); let mut fund_calldata: Array = array![]; @@ -53,28 +53,31 @@ fn __setup__() -> (ContractAddress, ClassHash) { return (contract_address, fund_class_hash,); } -// ************************************************************************* +// ****************************************************************************** // TEST -// ************************************************************************* +// ****************************************************************************** #[test] fn test_constructor() { start_cheat_caller_address_global(OWNER()); - let (contract_address, fund_class_hash) = __setup__(); + let (contract_address, fund_class_hash) = _setup_(); let fund_manager_contract = IFundManagerDispatcher { contract_address }; + let expected_fund_address = fund_manager_contract.getFundClassHash(); + let owner = fund_manager_contract.getOwner(); + assert(owner == OWNER(), 'Invalid owner'); + assert(fund_class_hash == expected_fund_address, 'Invalid donator class hash'); } #[test] fn test_new_fund(){ start_cheat_caller_address_global(OWNER()); - let (contract_address, fund_class_hash) = __setup__(); + let (contract_address, fund_class_hash) = _setup_(); let fund_manager_contract = IFundManagerDispatcher { contract_address }; fund_manager_contract.newFund(NAME(), GOAL()); let expected_fund_class_hash = get_class_hash( - fund_manager_contract.getFund(0) + fund_manager_contract.getFund(1) ); let current_id = fund_manager_contract.getCurrentId(); assert(expected_fund_class_hash == fund_class_hash, 'Invalid fund address'); assert(current_id == 1, 'Invalid current ID'); -} - +} \ No newline at end of file From 471ec3c180bd4ea43c43d918f914b1a31ab82d1f Mon Sep 17 00:00:00 2001 From: diegoTech14 Date: Wed, 2 Oct 2024 15:10:11 -0600 Subject: [PATCH 7/7] fix: some comments details and expected fund message --- contracts/src/fundManager.cairo | 16 ++++++++-------- contracts/tests/test_fund_manager.cairo | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/src/fundManager.cairo b/contracts/src/fundManager.cairo index 28e1a20..7b815ac 100755 --- a/contracts/src/fundManager.cairo +++ b/contracts/src/fundManager.cairo @@ -12,9 +12,9 @@ pub trait IFundManager { #[starknet::contract] mod FundManager { - // ************************* + // *************************************************************************************** // IMPORT - // ************************* + // *************************************************************************************** use core::array::ArrayTrait; use core::traits::TryInto; use starknet::ContractAddress; @@ -22,9 +22,9 @@ mod FundManager { use starknet::class_hash::ClassHash; use starknet::get_caller_address; - // ************************* + // *************************************************************************************** // STORAGE - // ************************* + // *************************************************************************************** #[storage] struct Storage { owner: ContractAddress, @@ -33,9 +33,9 @@ mod FundManager { fund_class_hash: ClassHash, } - // ************************* + // *************************************************************************************** // CONSTRUCTOR - // ************************* + // *************************************************************************************** #[constructor] fn constructor(ref self: ContractState, fund_class_hash: felt252) { self.owner.write(get_caller_address()); @@ -43,9 +43,9 @@ mod FundManager { self.current_id.write(0); } - // ************************* + // *************************************************************************************** // EXTERNALS - // ************************* + // *************************************************************************************** #[abi(embed_v0)] impl FundManagerImpl of super::IFundManager { fn newFund(ref self: ContractState, name: felt252, goal: u64) { diff --git a/contracts/tests/test_fund_manager.cairo b/contracts/tests/test_fund_manager.cairo index b603b66..25ae3c2 100755 --- a/contracts/tests/test_fund_manager.cairo +++ b/contracts/tests/test_fund_manager.cairo @@ -65,7 +65,7 @@ fn test_constructor() { let expected_fund_address = fund_manager_contract.getFundClassHash(); let owner = fund_manager_contract.getOwner(); assert(owner == OWNER(), 'Invalid owner'); - assert(fund_class_hash == expected_fund_address, 'Invalid donator class hash'); + assert(fund_class_hash == expected_fund_address, 'Invalid fund class hash'); } #[test]