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: test_fund_manager #118

Merged
merged 9 commits into from
Oct 3, 2024
28 changes: 18 additions & 10 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 @@ -56,14 +58,20 @@ 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();
}
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();
}
}
}
}
83 changes: 83 additions & 0 deletions contracts/tests/test_fund_manager.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// ***************************************************************************************
// 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 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) {
// Fund
let fund = declare("Fund").unwrap();
let mut fund_calldata: Array<felt252> = 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);

// Fund Manager
let fund_manager = declare("FundManager").unwrap();
let mut fund_manager_calldata: Array<felt252> = 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_();
let fund_manager_contract = IFundManagerDispatcher { contract_address };
EmmanuelAR marked this conversation as resolved.
Show resolved Hide resolved
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 fund class hash');
}

#[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(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');
}
Loading