diff --git a/src/campaign.cairo b/src/campaign.cairo index e44bdb4..0c12c08 100644 --- a/src/campaign.cairo +++ b/src/campaign.cairo @@ -7,7 +7,7 @@ mod TokengiverCampaign { // ************************************************************************* use core::traits::TryInto; use starknet::{ - ContractAddress, get_caller_address, get_block_timestamp, ClassHash, + ContractAddress, get_caller_address, get_block_timestamp, ClassHash, get_contract_address, syscalls::deploy_syscall, SyscallResultTrait, storage::{Map, StoragePathEntry, StoragePointerReadAccess, StoragePointerWriteAccess} }; @@ -170,6 +170,42 @@ mod TokengiverCampaign { self.campaign.write(campaign_address, campaign); } + fn donate( + ref self: ContractState, campaign_address: ContractAddress, amount: u256, token_id: u256 + ) { + let donor = get_caller_address(); + + let token_address = self.strk_address.read(); + + IERC20Dispatcher { contract_address: token_address } + .approve(get_contract_address(), amount); + + IERC20Dispatcher { contract_address: token_address } + .transfer_from(donor, campaign_address, amount); + + let prev_count = self.donation_count.read(campaign_address); + self.donation_count.write(campaign_address, prev_count + 1); + + let prev_donations = self.donations.read(campaign_address); + self.donations.write(campaign_address, prev_donations + amount); + + let donation_details = DonationDetails { token_id, donor_address: donor, amount, }; + self.donation_details.write(donor, donation_details); + + let prev_withdrawal = self.withdrawal_balance.read(campaign_address); + self.withdrawal_balance.write(campaign_address, prev_withdrawal + amount); + + self + .emit( + DonationMade { + campaign_id: token_id, + donor_address: donor, + amount: amount, + token_id, + block_timestamp: get_block_timestamp(), + } + ); + } fn set_donation_count(ref self: ContractState, campaign_address: ContractAddress) { let prev_count: u16 = self.donation_count.read(campaign_address); @@ -180,12 +216,6 @@ mod TokengiverCampaign { self.donations.write(campaign_address, amount); } - fn set_available_withdrawal( - ref self: ContractState, campaign_address: ContractAddress, amount: u256 - ) { - self.withdrawal_balance.write(campaign_address, amount); - } - // withdraw function fn withdraw(ref self: ContractState, campaign_address: ContractAddress, amount: u256) { let campaign: Campaign = self.campaign.read(campaign_address); @@ -213,6 +243,12 @@ mod TokengiverCampaign { ); } + fn set_available_withdrawal( + ref self: ContractState, campaign_address: ContractAddress, amount: u256 + ) { + self.withdrawal_balance.write(campaign_address, amount); + } + // ************************************************************************* // GETTERS // ************************************************************************* @@ -274,39 +310,5 @@ mod TokengiverCampaign { fn get_donation_count(self: @ContractState, campaign_address: ContractAddress) -> u16 { self.donation_count.read(campaign_address) } - - fn donate( - ref self: ContractState, campaign_address: ContractAddress, amount: u256, token_id: u256 - ) { - let donor = get_caller_address(); - - let token_address = self.strk_address.read(); - - IERC20Dispatcher { contract_address: token_address } - .transfer_from(donor, campaign_address, amount); - - let prev_count = self.donation_count.read(campaign_address); - self.donation_count.write(campaign_address, prev_count + 1); - - let prev_donations = self.donations.read(campaign_address); - self.donations.write(campaign_address, prev_donations + amount); - - let donation_details = DonationDetails { token_id, donor_address: donor, amount, }; - self.donation_details.write(donor, donation_details); - - let prev_withdrawal = self.withdrawal_balance.read(campaign_address); - self.withdrawal_balance.write(campaign_address, prev_withdrawal + amount); - - self - .emit( - DonationMade { - campaign_id: token_id, - donor_address: donor, - amount: amount, - token_id, - block_timestamp: get_block_timestamp(), - } - ); - } } } diff --git a/tests/test_campaign.cairo b/tests/test_campaign.cairo index 78ea9ab..2f4a1b5 100644 --- a/tests/test_campaign.cairo +++ b/tests/test_campaign.cairo @@ -21,6 +21,10 @@ fn DONOR() -> ContractAddress { 'donor'.try_into().unwrap() } +fn STRK_ADDR() -> ContractAddress { + 0x4718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d.try_into().unwrap() +} + fn SALT() -> felt252 { 'salty'.try_into().unwrap() } @@ -87,7 +91,7 @@ fn test_donate() { /// Transfer STRK to Donor start_cheat_caller_address(strk_address, OWNER()); - let amount = 2000000; // + let amount = 35; // strk_dispatcher.transfer(DONOR(), amount); assert(strk_dispatcher.balance_of(DONOR()) >= amount, 'strk bal too low'); stop_cheat_caller_address(strk_address); @@ -101,7 +105,6 @@ fn test_donate() { start_cheat_caller_address(token_giver_address, DONOR()); token_giver.donate(campaign_address, amount, random_id); stop_cheat_caller_address(token_giver_address); - assert(strk_dispatcher.balance_of(DONOR()) == 0, 'wrong balance'); assert(token_giver.get_donations(campaign_address) == amount, 'wrong donation amount'); assert(token_giver.get_donation_count(campaign_address) == 1, 'wrong donation amount');