Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
1. Withdraw fund method
2. Refactor setIsActive to setState
3. Run scarb fmt
4. Add new withdraw fund state
  • Loading branch information
EmmanuelAR committed Oct 12, 2024
1 parent 9685c33 commit 911cdbf
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion contracts/src/constants/funds.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod state_constants;
pub mod fund_constants;
pub mod starknet_constants;
pub mod starknet_constants;
5 changes: 3 additions & 2 deletions contracts/src/constants/funds/starknet_constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ use starknet::ContractAddress;
// STARKNET CONSTANTS
// *************************************************************************
pub mod StarknetConstants {
pub const STRK_TOKEN_ADDRESS: felt252 = 0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d;
}
pub const STRK_TOKEN_ADDRESS: felt252 =
0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d;
}
1 change: 1 addition & 0 deletions contracts/src/constants/funds/state_constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod FundStates {
pub const RECOLLECTING_VOTES: u8 = 1;
pub const RECOLLECTING_DONATIONS: u8 = 2;
pub const CLOSED: u8 = 3;
pub const WITHDRAW: u8 = 4;
}
28 changes: 23 additions & 5 deletions contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ pub trait IFund<TContractState> {
fn getGoal(self: @TContractState) -> u256;
fn receiveDonation(ref self: TContractState, strks: u256);
fn getCurrentGoalState(self: @TContractState) -> u256;
fn setIsActive(ref self: TContractState, state: u8);
fn getIsActive(self: @TContractState) -> u8;
fn setState(ref self: TContractState, state: u8);
fn getState(self: @TContractState) -> u8;
fn getVoter(self: @TContractState) -> u32;
fn withdraw(ref self: TContractState);
}

#[starknet::contract]
Expand All @@ -26,9 +27,11 @@ mod Fund {
// *************************************************************************
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::contract_address_const;
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::{starknet_constants::StarknetConstants},};


// *************************************************************************
Expand Down Expand Up @@ -127,15 +130,30 @@ mod Fund {
fn getCurrentGoalState(self: @ContractState) -> u256 {
return self.current_goal_state.read();
}
// TODO: Validate to change method to change setState and getState
fn setIsActive(ref self: ContractState, state: u8) {
fn setState(ref self: ContractState, state: u8) {
self.state.write(state);
}
fn getIsActive(self: @ContractState) -> u8 {
fn getState(self: @ContractState) -> u8 {
return self.state.read();
}
fn getVoter(self: @ContractState) -> u32 {
return self.voters.read(get_caller_address());
}
fn withdraw(ref self: ContractState) {
// Verifications
assert(self.state.read() == FundStates::CLOSED, 'Fund not close goal yet.');
assert(get_caller_address() != self.owner.read(), 'You are not the owner');
assert(self.getCurrentGoalState() > 0, 'Fund hasnt reached its goal yet');
// Withdraw
let starknet_contract_address = contract_address_const::<
StarknetConstants::STRK_TOKEN_ADDRESS
>();
let starknet_dispatcher = IERC20Dispatcher {
contract_address: starknet_contract_address
};
starknet_dispatcher.transfer(self.getOwner(), self.getCurrentGoalState());
assert(self.getCurrentGoalState() != 0, 'Fund hasnt reached its goal yet');
self.setState(4);
}
}
}
2 changes: 1 addition & 1 deletion contracts/src/fundManager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ mod FundManager {
return self.fund_class_hash.read();
}
}
}
}
8 changes: 4 additions & 4 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn test_constructor() {
let up_votes = dispatcher.getUpVotes();
let goal = dispatcher.getGoal();
let current_goal_state = dispatcher.getCurrentGoalState();
let state = dispatcher.getIsActive();
let state = dispatcher.getState();
assert(id == ID(), 'Invalid id');
assert(owner == OWNER(), 'Invalid owner');
assert(name == NAME(), 'Invalid name');
Expand Down Expand Up @@ -131,7 +131,7 @@ fn test_receive_donation_successful() {
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
// Put state as recollecting dons
dispatcher.setIsActive(2);
dispatcher.setState(2);
// Put 10 strks as goal, only owner
start_cheat_caller_address_global(OWNER());
dispatcher.setGoal(10);
Expand All @@ -141,7 +141,7 @@ fn test_receive_donation_successful() {
assert(current_goal_state == 5, 'Receive donation not working');
// Donate 5 strks, the goal is done
dispatcher.receiveDonation(5);
let state = dispatcher.getIsActive();
let state = dispatcher.getState();
assert(state == 3, 'State should be close');
}

Expand All @@ -151,7 +151,7 @@ fn test_receive_donation_unsuccessful_wrong_state() {
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
// Put a wrong state to receive donations
dispatcher.setIsActive(1);
dispatcher.setState(1);
// Donate
dispatcher.receiveDonation(5);
}
10 changes: 4 additions & 6 deletions contracts/tests/test_fund_manager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn _setup_() -> (ContractAddress, ClassHash) {
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,);
}

Expand All @@ -69,15 +69,13 @@ fn test_constructor() {
}

#[test]
fn test_new_fund(){
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 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');
}
}

0 comments on commit 911cdbf

Please sign in to comment.