Skip to content

Commit

Permalink
new parameters in fund constructor
Browse files Browse the repository at this point in the history
fix name test string
fix tests
  • Loading branch information
EmmanuelAR committed Nov 3, 2024
1 parent 59ec696 commit 9dbab8e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
12 changes: 9 additions & 3 deletions contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ pub mod Fund {
// *************************************************************************
#[constructor]
fn constructor(
ref self: ContractState, id: u128, owner: ContractAddress, name: ByteArray, goal: u256
ref self: ContractState,
id: u128,
owner: ContractAddress,
name: ByteArray,
goal: u256,
evidence_link: ByteArray,
contact_handle: ByteArray
) {
self.id.write(id);
self.owner.write(owner);
Expand All @@ -106,8 +112,8 @@ pub mod Fund {
self.up_votes.write(FundConstants::INITIAL_UP_VOTES);
self.goal.write(goal);
self.state.write(FundStates::RECOLLECTING_VOTES);
self.evidence_link.write(" ");
self.contact_handle.write(" ");
self.evidence_link.write(evidence_link);
self.contact_handle.write(contact_handle);
}

// *************************************************************************
Expand Down
20 changes: 17 additions & 3 deletions contracts/src/fundManager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ use starknet::class_hash::ClassHash;

#[starknet::interface]
pub trait IFundManager<TContractState> {
fn newFund(ref self: TContractState, name: ByteArray, goal: u256);
fn newFund(
ref self: TContractState,
name: ByteArray,
goal: u256,
evidence_link: ByteArray,
contact_handle: ByteArray
);
fn getCurrentId(self: @TContractState) -> u128;
fn getFund(self: @TContractState, id: u128) -> ContractAddress;
fn getOwner(self: @TContractState) -> ContractAddress;
Expand All @@ -22,7 +28,7 @@ pub mod FundManager {
use starknet::class_hash::ClassHash;
use starknet::get_caller_address;
use openzeppelin::utils::serde::SerializedAppend;
use gostarkme::constants::{ funds::{fund_constants::FundConstants}, };
use gostarkme::constants::{funds::{fund_constants::FundConstants},};


// ***************************************************************************************
Expand Down Expand Up @@ -71,13 +77,21 @@ pub mod FundManager {

#[abi(embed_v0)]
impl FundManagerImpl of super::IFundManager<ContractState> {
fn newFund(ref self: ContractState, name: ByteArray, goal: u256) {
fn newFund(
ref self: ContractState,
name: ByteArray,
goal: u256,
evidence_link: ByteArray,
contact_handle: ByteArray
) {
assert(goal >= FundConstants::MINIMUM_GOAL, 'Goal must be at least 500');
let mut call_data: Array<felt252> = array![];
Serde::serialize(@self.current_id.read(), ref call_data);
Serde::serialize(@get_caller_address(), ref call_data);
Serde::serialize(@name, ref call_data);
Serde::serialize(@goal, ref call_data);
Serde::serialize(@evidence_link, ref call_data);
Serde::serialize(@contact_handle, ref call_data);
let (new_fund_address, _) = deploy_syscall(
self.fund_class_hash.read(), 12345, call_data.span(), false
)
Expand Down
30 changes: 19 additions & 11 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@ fn FUND_MANAGER() -> ContractAddress {
contract_address_const::<FundManagerConstants::FUND_MANAGER_ADDRESS>()
}
fn NAME() -> ByteArray {
"NAME_FUND_TEST"
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn REASON() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn GOAL() -> u256 {
1000
}
fn EVIDENCE_LINK() -> ByteArray {
fn EVIDENCE_LINK_1() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn EVIDENCE_LINK_2() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn CONTACT_HANDLE_1() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn CONTACT_HANDLE() -> ByteArray {
fn CONTACT_HANDLE_2() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn _setup_() -> ContractAddress {
Expand All @@ -55,6 +61,8 @@ fn _setup_() -> ContractAddress {
calldata.append_serde(OWNER());
calldata.append_serde(NAME());
calldata.append_serde(GOAL());
calldata.append_serde(EVIDENCE_LINK_1());
calldata.append_serde(CONTACT_HANDLE_1());
let (contract_address, _) = contract.deploy(@calldata).unwrap();
contract_address
}
Expand Down Expand Up @@ -406,11 +414,11 @@ fn test_set_evidence_link() {
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
let evidence_link = dispatcher.get_evidence_link();
assert(evidence_link == " ", 'Invalid evidence_link');
assert(evidence_link == EVIDENCE_LINK_1(), 'Invalid evidence_link');
start_cheat_caller_address_global(OWNER());
dispatcher.set_evidence_link(EVIDENCE_LINK());
dispatcher.set_evidence_link(EVIDENCE_LINK_2());
let new_evidence_link = dispatcher.get_evidence_link();
assert(new_evidence_link == EVIDENCE_LINK(), 'Set evidence method not working')
assert(new_evidence_link == EVIDENCE_LINK_2(), 'Set evidence method not working')
}

#[test]
Expand All @@ -420,19 +428,19 @@ fn test_set_evidence_link_wrong_owner() {

// call set_evidence_link fn with wrong owner
start_cheat_caller_address_global(OTHER_USER());
IFundDispatcher { contract_address }.set_evidence_link(EVIDENCE_LINK());
IFundDispatcher { contract_address }.set_evidence_link(EVIDENCE_LINK_2());
}

#[test]
fn test_set_contact_handle() {
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
let contact_handle = dispatcher.get_contact_handle();
assert(contact_handle == " ", 'Invalid contact handle');
assert(contact_handle == CONTACT_HANDLE_1(), 'Invalid contact handle');
start_cheat_caller_address_global(OWNER());
dispatcher.set_contact_handle(CONTACT_HANDLE());
dispatcher.set_contact_handle(CONTACT_HANDLE_2());
let new_contact_handle = dispatcher.get_contact_handle();
assert(new_contact_handle == CONTACT_HANDLE(), 'Set contact method not working')
assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working')
}

#[test]
Expand All @@ -442,5 +450,5 @@ fn test_set_contact_handle_wrong_owner() {

// call set_contact_handle fn with wrong owner
start_cheat_caller_address_global(OTHER_USER());
IFundDispatcher { contract_address }.set_contact_handle(CONTACT_HANDLE());
IFundDispatcher { contract_address }.set_contact_handle(CONTACT_HANDLE_2());
}
19 changes: 14 additions & 5 deletions contracts/tests/test_fund_manager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn OTHER_USER() -> ContractAddress {
contract_address_const::<'USER'>()
}
fn NAME() -> ByteArray {
"NAME_FUND_TEST"
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn REASON() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
Expand All @@ -36,7 +36,13 @@ fn GOAL() -> u256 {
}
fn BAD_GOAL() -> u256 {
400
}
}
fn EVIDENCE_LINK() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}
fn CONTACT_HANDLE() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
}

fn _setup_() -> (ContractAddress, ClassHash) {
// Fund
Expand All @@ -46,6 +52,9 @@ fn _setup_() -> (ContractAddress, ClassHash) {
fund_calldata.append_serde(OWNER());
fund_calldata.append_serde(NAME());
fund_calldata.append_serde(GOAL());
fund_calldata.append_serde(EVIDENCE_LINK());
fund_calldata.append_serde(CONTACT_HANDLE());

let (fund_contract_address, _) = fund.deploy(@fund_calldata).unwrap();
let fund_class_hash = get_class_hash(fund_contract_address);

Expand Down Expand Up @@ -78,7 +87,7 @@ 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());
fund_manager_contract.newFund(NAME(), GOAL(), EVIDENCE_LINK(), CONTACT_HANDLE());
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');
Expand All @@ -91,7 +100,7 @@ fn test_new_fund_bad_goal() {
start_cheat_caller_address_global(OWNER());
let (contract_address, _) = _setup_();
let fund_manager_contract = IFundManagerDispatcher { contract_address };
fund_manager_contract.newFund(NAME(), BAD_GOAL());
fund_manager_contract.newFund(NAME(), BAD_GOAL(), EVIDENCE_LINK(), CONTACT_HANDLE());
}

#[test]
Expand All @@ -104,7 +113,7 @@ fn test_fund_deployed_event() {
let mut spy = spy_events();

let current_id = fund_manager_contract.getCurrentId();
fund_manager_contract.newFund(NAME(), GOAL());
fund_manager_contract.newFund(NAME(), GOAL(), EVIDENCE_LINK(), CONTACT_HANDLE());

let expected_fund_class_hash = fund_manager_contract.getFund(1);

Expand Down

0 comments on commit 9dbab8e

Please sign in to comment.