Skip to content

Commit

Permalink
Merge pull request #156 from derianrddev/fix/fund-manager-setgoal-assert
Browse files Browse the repository at this point in the history
fix: Ensure only fund manager can modify goal in setGoal
  • Loading branch information
adrianvrj authored Oct 25, 2024
2 parents a0c1e34 + 080d996 commit d79050a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions contracts/src/constants/funds.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod state_constants;
pub mod fund_constants;
pub mod fund_manager_constants;
pub mod starknet_constants;
7 changes: 7 additions & 0 deletions contracts/src/constants/funds/fund_manager_constants.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// *************************************************************************
// FUND MANAGER CONSTANTS
// *************************************************************************
pub mod FundManagerConstants {
pub const FUND_MANAGER_ADDRESS: felt252 =
0x00a885638f5167da8c38f115077c23ed7411539ea8f019ef09ec025d0c52d0ff;
}
7 changes: 5 additions & 2 deletions contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod Fund {
use starknet::get_contract_address;
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use gostarkme::constants::{funds::{state_constants::FundStates},};
use gostarkme::constants::{funds::{fund_constants::FundConstants},};
use gostarkme::constants::{funds::{fund_constants::FundConstants, fund_manager_constants::FundManagerConstants},};
use gostarkme::constants::{funds::{starknet_constants::StarknetConstants},};

// *************************************************************************
Expand Down Expand Up @@ -144,7 +144,10 @@ mod Fund {
}
fn setGoal(ref self: ContractState, goal: u256) {
let caller = get_caller_address();
assert!(self.owner.read() == caller, "You are not the owner");
let fund_manager_address = contract_address_const::<
FundManagerConstants::FUND_MANAGER_ADDRESS
>();
assert!(fund_manager_address == caller, "You are not the fund manager");
self.goal.write(goal);
}
fn getGoal(self: @ContractState) -> u256 {
Expand Down
19 changes: 16 additions & 3 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use openzeppelin::utils::serde::SerializedAppend;

use gostarkme::fund::IFundDispatcher;
use gostarkme::fund::IFundDispatcherTrait;
use gostarkme::constants::{funds::{fund_manager_constants::FundManagerConstants},};

fn ID() -> u128 {
1
Expand All @@ -19,6 +20,9 @@ fn OWNER() -> ContractAddress {
fn OTHER_USER() -> ContractAddress {
contract_address_const::<'USER'>()
}
fn FUND_MANAGER() -> ContractAddress {
contract_address_const::<FundManagerConstants::FUND_MANAGER_ADDRESS>()
}
fn NAME() -> felt252 {
'NAME_FUND_TEST'
}
Expand Down Expand Up @@ -93,7 +97,7 @@ fn test_set_goal() {
let dispatcher = IFundDispatcher { contract_address };
let goal = dispatcher.getGoal();
assert(goal == GOAL(), 'Invalid goal');
start_cheat_caller_address_global(OWNER());
start_cheat_caller_address_global(FUND_MANAGER());
dispatcher.setGoal(123);
let new_goal = dispatcher.getGoal();
assert(new_goal == 123, 'Set goal method not working')
Expand Down Expand Up @@ -132,8 +136,8 @@ fn test_receive_donation_successful() {
let dispatcher = IFundDispatcher { contract_address };
// Put state as recollecting dons
dispatcher.setState(2);
// Put 10 strks as goal, only owner
start_cheat_caller_address_global(OWNER());
// Put 10 strks as goal, only fund manager
start_cheat_caller_address_global(FUND_MANAGER());
dispatcher.setGoal(10);
// Donate 5 strks
dispatcher.receiveDonation(5);
Expand All @@ -155,3 +159,12 @@ fn test_receive_donation_unsuccessful_wrong_state() {
// Donate
dispatcher.receiveDonation(5);
}

#[test]
#[should_panic(expected: ("You are not the fund manager",))]
fn test_set_goal_unauthorized() {
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
// Change the goal without being the fund manager
dispatcher.setGoal(22);
}

0 comments on commit d79050a

Please sign in to comment.