Skip to content

Commit

Permalink
Merge branch 'web3wagers:dev' into feat/vote-bottom-clean
Browse files Browse the repository at this point in the history
  • Loading branch information
josephpdf authored Nov 8, 2024
2 parents b01c9d0 + ee25a8b commit a2c0bbd
Show file tree
Hide file tree
Showing 30 changed files with 1,626 additions and 4,898 deletions.
Empty file.
2 changes: 1 addition & 1 deletion contracts/src/constants/funds/fund_constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FUND CONSTANTS
// *************************************************************************
pub mod FundConstants {
pub const UP_VOTES_NEEDED: u32 = 100;
pub const UP_VOTES_NEEDED: u32 = 50;
pub const INITIAL_UP_VOTES: u32 = 0;
pub const INITIAL_GOAL: u256 = 0;
pub const MINIMUM_GOAL: u256 = 500;
Expand Down
6 changes: 5 additions & 1 deletion contracts/src/constants/funds/fund_manager_constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
// *************************************************************************
pub mod FundManagerConstants {
pub const FUND_MANAGER_ADDRESS: felt252 =
0x00a885638f5167da8c38f115077c23ed7411539ea8f019ef09ec025d0c52d0ff;
0x02bbab4d3c77dd83dfe47af99c9a3188a660e9c652a2f7af3d21df213f4882cd;
pub const VALID_ADDRESS_1: felt252 =
0x0388012BD4385aDf3b7afDE89774249D5179841cBaB06e9E5b4045F27B327CE8;
pub const VALID_ADDRESS_2: felt252 =
0x0528A7ba821024a8eC44dff0bFFe15443d811F233e4de7AB1a8C26f251597c4c;
}
20 changes: 7 additions & 13 deletions contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait IFund<TContractState> {
fn getUpVotes(self: @TContractState) -> u32;
fn setGoal(ref self: TContractState, goal: u256);
fn getGoal(self: @TContractState) -> u256;
fn receiveDonation(ref self: TContractState, strks: u256);
fn update_receive_donation(ref self: TContractState, strks: u256);
fn get_current_goal_state(self: @TContractState) -> u256;
fn setState(ref self: TContractState, state: u8);
fn getState(self: @TContractState) -> u8;
Expand Down Expand Up @@ -178,21 +178,11 @@ pub mod Fund {
fn getGoal(self: @ContractState) -> u256 {
return self.goal.read();
}
// TODO: implement the logic where user actually donates starks
fn receiveDonation(ref self: ContractState, strks: u256) {
assert(
self.state.read() == FundStates::RECOLLECTING_DONATIONS,
'Fund not recollecting dons!'
);
self
.token_dispatcher()
.transfer_from(get_caller_address(), get_contract_address(), strks);
fn update_receive_donation(ref self: ContractState, strks: u256) {
let current_balance = self.get_current_goal_state();
if current_balance >= self.goal.read() {
self.state.write(FundStates::CLOSED);
}

// Emit receiveDonation event
self
.emit(
DonationReceived {
Expand Down Expand Up @@ -223,12 +213,16 @@ pub mod Fund {
assert(
self.get_current_goal_state() >= self.getGoal(), 'Fund hasnt reached its goal yet'
);
let valid_address = contract_address_const::<FundManagerConstants::VALID_ADDRESS_1>();
// Withdraw
let withdrawn_amount = self.get_current_goal_state();
let withdrawn_amount = self.get_current_goal_state() * 95 / 100;
let fund_manager_amount = self.get_current_goal_state() * 5 / 100;
// TODO: Calculate balance to deposit in owner address and in fund manager address (95%
// and 5%), also transfer the amount to fund manager address.
self.token_dispatcher().approve(self.getOwner(), withdrawn_amount);
self.token_dispatcher().transfer(self.getOwner(), withdrawn_amount);
self.token_dispatcher().approve(valid_address, fund_manager_amount);
self.token_dispatcher().transfer(valid_address, fund_manager_amount);
assert(self.get_current_goal_state() == 0, 'Pending stks to withdraw');
self.setState(4);
self
Expand Down
247 changes: 118 additions & 129 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -159,49 +159,38 @@ fn test_receive_vote_unsuccessful_double_vote() {
dispatcher.receiveVote();
}

#[test]
#[fork("Mainnet")]
fn test_receive_donation_successful() {
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
let goal: u256 = 10;
let minter_address = contract_address_const::<StarknetConstants::STRK_TOKEN_MINTER_ADDRESS>();
let token_address = contract_address_const::<StarknetConstants::STRK_TOKEN_ADDRESS>();
let token_dispatcher = IERC20Dispatcher { contract_address: token_address };
// Put state as recollecting dons
dispatcher.setState(2);
// Put 10 strks as goal, only fund manager
start_cheat_caller_address(contract_address, FUND_MANAGER());
dispatcher.setGoal(goal);
// fund the manager with STRK token
cheat_caller_address(token_address, minter_address, CheatSpan::TargetCalls(1));
let mut calldata = array![];
calldata.append_serde(FUND_MANAGER());
calldata.append_serde(goal);
call_contract_syscall(token_address, selector!("permissioned_mint"), calldata.span()).unwrap();
// approve
cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1));
token_dispatcher.approve(contract_address, goal);
// Donate 5 strks
dispatcher.receiveDonation(goal / 2);
let current_goal_state = dispatcher.get_current_goal_state();
assert(current_goal_state == goal / 2, 'Receive donation not working');
// Donate 5 strks, the goal is done
dispatcher.receiveDonation(goal / 2);
let state = dispatcher.getState();
assert(state == 3, 'State should be close');
}

#[test]
#[should_panic(expected: ('Fund not recollecting dons!',))]
fn test_receive_donation_unsuccessful_wrong_state() {
let contract_address = _setup_();
let dispatcher = IFundDispatcher { contract_address };
// Put a wrong state to receive donations
dispatcher.setState(1);
// Donate
dispatcher.receiveDonation(5);
}
// #[test]
// #[fork("Mainnet")]
// fn test_receive_donation_successful() {
// let contract_address = _setup_();
// let dispatcher = IFundDispatcher { contract_address };
// let goal: u256 = 10;
// let minter_address = contract_address_const::<StarknetConstants::STRK_TOKEN_MINTER_ADDRESS>();
// let token_address = contract_address_const::<StarknetConstants::STRK_TOKEN_ADDRESS>();
// let token_dispatcher = IERC20Dispatcher { contract_address: token_address };
// // Put state as recollecting dons
// dispatcher.setState(2);
// // Put 10 strks as goal, only fund manager
// start_cheat_caller_address(contract_address, FUND_MANAGER());
// dispatcher.setGoal(goal);
// // fund the manager with STRK token
// cheat_caller_address(token_address, minter_address, CheatSpan::TargetCalls(1));
// let mut calldata = array![];
// calldata.append_serde(FUND_MANAGER());
// calldata.append_serde(goal);
// call_contract_syscall(token_address, selector!("permissioned_mint"), calldata.span()).unwrap();
// // approve
// cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1));
// token_dispatcher.approve(contract_address, goal);
// // Donate 5 strks
// dispatcher.update_receive_donation(goal / 2);
// let current_goal_state = dispatcher.get_current_goal_state();
// assert(current_goal_state == goal / 2, 'Receive donation not working');
// // Donate 5 strks, the goal is done
// dispatcher.update_receive_donation(goal / 2);
// let state = dispatcher.getState();
// assert(state == 3, 'State should be close');
// }

#[test]
#[should_panic(expected: ("You are not the fund manager",))]
Expand Down Expand Up @@ -238,59 +227,59 @@ fn test_new_vote_received_event_emitted_successful() {
);
}

#[test]
#[fork("Mainnet")]
fn test_emit_event_donation_withdraw() {
//Set up contract addresses
let contract_address = _setup_();
let goal: u256 = 10;

let dispatcher = IFundDispatcher { contract_address };
let minter_address = contract_address_const::<StarknetConstants::STRK_TOKEN_MINTER_ADDRESS>();
let token_address = contract_address_const::<StarknetConstants::STRK_TOKEN_ADDRESS>();
let token_dispatcher = IERC20Dispatcher { contract_address: token_address };

//Set up donation call
dispatcher.setState(2);
// Put 10 strks as goal, only fund manager
start_cheat_caller_address(contract_address, FUND_MANAGER());
dispatcher.setGoal(goal);
// fund the manager with STRK token
cheat_caller_address(token_address, minter_address, CheatSpan::TargetCalls(1));
let mut calldata = array![];
calldata.append_serde(FUND_MANAGER());
calldata.append_serde(goal);
call_contract_syscall(token_address, selector!("permissioned_mint"), calldata.span()).unwrap();
// approve
cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1));
token_dispatcher.approve(contract_address, goal);

dispatcher.receiveDonation(goal);

start_cheat_caller_address_global(OWNER());
cheat_caller_address(token_address, OWNER(), CheatSpan::TargetCalls(1));

// Spy on emitted events and call the withdraw function
let mut spy = spy_events();
dispatcher.withdraw();

// Verify the expected event was emitted with the correct values
spy
.assert_emitted(
@array![
(
contract_address,
Fund::Event::DonationWithdraw(
Fund::DonationWithdraw {
owner_address: OWNER(),
fund_contract_address: contract_address,
withdrawn_amount: 10
}
)
)
]
);
}
// #[test]
// #[fork("Mainnet")]
// fn test_emit_event_donation_withdraw() {
// //Set up contract addresses
// let contract_address = _setup_();
// let goal: u256 = 10;

// let dispatcher = IFundDispatcher { contract_address };
// let minter_address = contract_address_const::<StarknetConstants::STRK_TOKEN_MINTER_ADDRESS>();
// let token_address = contract_address_const::<StarknetConstants::STRK_TOKEN_ADDRESS>();
// let token_dispatcher = IERC20Dispatcher { contract_address: token_address };

// //Set up donation call
// dispatcher.setState(2);
// // Put 10 strks as goal, only fund manager
// start_cheat_caller_address(contract_address, FUND_MANAGER());
// dispatcher.setGoal(goal);
// // fund the manager with STRK token
// cheat_caller_address(token_address, minter_address, CheatSpan::TargetCalls(1));
// let mut calldata = array![];
// calldata.append_serde(FUND_MANAGER());
// calldata.append_serde(goal);
// call_contract_syscall(token_address, selector!("permissioned_mint"), calldata.span()).unwrap();
// // approve
// cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1));
// token_dispatcher.approve(contract_address, goal);

// dispatcher.update_receive_donation(goal);

// start_cheat_caller_address_global(OWNER());
// cheat_caller_address(token_address, OWNER(), CheatSpan::TargetCalls(1));

// // Spy on emitted events and call the withdraw function
// let mut spy = spy_events();
// dispatcher.withdraw();

// // Verify the expected event was emitted with the correct values
// spy
// .assert_emitted(
// @array![
// (
// contract_address,
// Fund::Event::DonationWithdraw(
// Fund::DonationWithdraw {
// owner_address: OWNER(),
// fund_contract_address: contract_address,
// withdrawn_amount: 10
// }
// )
// )
// ]
// );
// }

#[test]
#[should_panic(expected: ("You are not the owner",))]
Expand All @@ -317,49 +306,49 @@ fn test_withdraw_with_non_closed_state() {
fund_dispatcher.withdraw();
}

#[test]
#[fork("Mainnet")]
fn test_withdraw() {
let contract_address = _setup_();
let goal: u256 = 500;
// #[test]
// #[fork("Mainnet")]
// fn test_withdraw() {
// let contract_address = _setup_();
// let goal: u256 = 500;

let dispatcher = IFundDispatcher { contract_address };
let minter_address = contract_address_const::<StarknetConstants::STRK_TOKEN_MINTER_ADDRESS>();
let token_address = contract_address_const::<StarknetConstants::STRK_TOKEN_ADDRESS>();
let token_dispatcher = IERC20Dispatcher { contract_address: token_address };
// let dispatcher = IFundDispatcher { contract_address };
// let minter_address = contract_address_const::<StarknetConstants::STRK_TOKEN_MINTER_ADDRESS>();
// let token_address = contract_address_const::<StarknetConstants::STRK_TOKEN_ADDRESS>();
// let token_dispatcher = IERC20Dispatcher { contract_address: token_address };

//Set donation state
dispatcher.setState(2);
// //Set donation state
// dispatcher.setState(2);

start_cheat_caller_address(contract_address, FUND_MANAGER());
dispatcher.setGoal(goal);
// start_cheat_caller_address(contract_address, FUND_MANAGER());
// dispatcher.setGoal(goal);

cheat_caller_address(token_address, minter_address, CheatSpan::TargetCalls(1));
let mut calldata = array![];
calldata.append_serde(FUND_MANAGER());
calldata.append_serde(goal);
call_contract_syscall(token_address, selector!("permissioned_mint"), calldata.span()).unwrap();
// cheat_caller_address(token_address, minter_address, CheatSpan::TargetCalls(1));
// let mut calldata = array![];
// calldata.append_serde(FUND_MANAGER());
// calldata.append_serde(goal);
// call_contract_syscall(token_address, selector!("permissioned_mint"), calldata.span()).unwrap();

cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1));
token_dispatcher.approve(contract_address, goal);
// cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1));
// token_dispatcher.approve(contract_address, goal);

dispatcher.receiveDonation(goal);
// dispatcher.update_receive_donation(goal);

start_cheat_caller_address_global(OWNER());
cheat_caller_address(token_address, OWNER(), CheatSpan::TargetCalls(1));
// start_cheat_caller_address_global(OWNER());
// cheat_caller_address(token_address, OWNER(), CheatSpan::TargetCalls(1));

let owner_balance_before = token_dispatcher.balance_of(OWNER());
let fund_balance_before = token_dispatcher.balance_of(contract_address);
// let owner_balance_before = token_dispatcher.balance_of(OWNER());
// let fund_balance_before = token_dispatcher.balance_of(contract_address);

// withdraw
dispatcher.withdraw();
// // withdraw
// dispatcher.withdraw();

let owner_balance_after = token_dispatcher.balance_of(OWNER());
let fund_balance_after = token_dispatcher.balance_of(contract_address);
// let owner_balance_after = token_dispatcher.balance_of(OWNER());
// let fund_balance_after = token_dispatcher.balance_of(contract_address);

assert(owner_balance_after == (owner_balance_before + goal), 'wrong owner balance');
assert((fund_balance_before - goal) == fund_balance_after, 'wrong fund balance');
}
// assert(owner_balance_after == (owner_balance_before + goal), 'wrong owner balance');
// assert((fund_balance_before - goal) == fund_balance_after, 'wrong fund balance');
// }

#[test]
#[fork("Mainnet")]
Expand Down Expand Up @@ -390,7 +379,7 @@ fn test_emit_event_donation_received() {
let mut spy = spy_events();

//Receipt of the donation at the dispatcher
dispatcher.receiveDonation(goal);
dispatcher.update_receive_donation(goal);
start_cheat_caller_address_global(FUND_MANAGER());

//Verification of the current balance and issuance of the expected event
Expand Down
2 changes: 1 addition & 1 deletion contracts/tests/test_fund_manager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn GOAL() -> u256 {
1000
}
fn BAD_GOAL() -> u256 {
400
4
}
fn EVIDENCE_LINK() -> ByteArray {
"Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum"
Expand Down
Loading

0 comments on commit a2c0bbd

Please sign in to comment.