Skip to content

Commit

Permalink
Merge pull request #133 from EmmanuelAR/feat/withdraw_fund
Browse files Browse the repository at this point in the history
[feat] withdraw fund method
  • Loading branch information
adrianvrj authored Oct 15, 2024
2 parents 9685c33 + ddc4b7b commit ffc002b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 22 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;
}
32 changes: 27 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,12 @@ mod Fund {
// *************************************************************************
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::contract_address_const;
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::{starknet_constants::StarknetConstants},};


// *************************************************************************
Expand Down Expand Up @@ -127,15 +131,33 @@ 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
let caller = get_caller_address();
assert!(self.owner.read() == caller, "You are not the owner");
assert(self.state.read() == FundStates::CLOSED, 'Fund not close goal yet.');
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
};
let balance = starknet_dispatcher.balance_of(get_contract_address());
//TODO: Calculate balance to deposit in owner address and in fund manager address (95% and 5%), also transfer the amount to fund manager address.
starknet_dispatcher.transfer(self.getOwner(), balance);
assert(self.getCurrentGoalState() != 0, 'Fund hasnt reached its goal yet');
self.setState(4);
}
}
}
6 changes: 3 additions & 3 deletions contracts/src/fundManager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod FundManager {
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);
self.current_id.write(1);
}

// ***************************************************************************************
Expand All @@ -60,8 +60,8 @@ mod FundManager {
self.fund_class_hash.read(), 12345, call_data.span(), false
)
.unwrap();
self.current_id.write(self.current_id.read() + 1);
self.funds.write(self.current_id.read(), address_0);
self.current_id.write(self.current_id.read() + 1);
}
fn getCurrentId(self: @ContractState) -> u128 {
return self.current_id.read();
Expand All @@ -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);
}
12 changes: 5 additions & 7 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');
}
assert(current_id == 2 , 'Invalid current ID');
}

0 comments on commit ffc002b

Please sign in to comment.