Skip to content

Commit

Permalink
fix: added get methods to fundManager and assert constructor values
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoTech14 committed Oct 2, 2024
1 parent 9af9fc3 commit 232eaa1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
26 changes: 17 additions & 9 deletions contracts/src/fundManager.cairo
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ pub trait IFundManager<TContractState> {
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;
use starknet::syscalls::deploy_syscall;
use starknet::class_hash::ClassHash;
use starknet::get_caller_address;

// *************************************************************************
// *************************
// STORAGE
// *************************************************************************
// *************************
#[storage]
struct Storage {
owner: ContractAddress,
Expand All @@ -31,19 +33,19 @@ mod FundManager {
fund_class_hash: ClassHash,
}

// *************************************************************************
// *************************
// CONSTRUCTOR
// *************************************************************************
// *************************
#[constructor]
fn constructor(ref self: ContractState, fund_class_hash: felt252) {
self.owner.write(get_caller_address());
self.fund_class_hash.write(fund_class_hash.try_into().unwrap());
self.current_id.write(0);
}

// *************************************************************************
// *************************
// EXTERNALS
// *************************************************************************
// *************************
#[abi(embed_v0)]
impl FundManagerImpl of super::IFundManager<ContractState> {
fn newFund(ref self: ContractState, name: felt252, goal: u64) {
Expand All @@ -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();
}
}
}
}
23 changes: 13 additions & 10 deletions contracts/tests/test_fund_manager.cairo
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************
// ***************************************************************************************
// FUND MANAGER TEST
// ******************************************************************
// ***************************************************************************************
use starknet::{ContractAddress, contract_address_const};
use starknet::class_hash::{ClassHash};
use starknet::syscalls::deploy_syscall;
Expand Down Expand Up @@ -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<felt252> = array![];
Expand All @@ -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');
}

}

0 comments on commit 232eaa1

Please sign in to comment.