From fbc4654e05f4f7193ba7158d5b3f0f7626d037e7 Mon Sep 17 00:00:00 2001 From: Oshioke-Salaki Date: Mon, 9 Dec 2024 17:24:54 +0100 Subject: [PATCH 1/2] fix: donate func --- src/campaign.cairo | 92 ++++++++++++++++++++++----------------- tests/test_campaign.cairo | 8 +++- 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/src/campaign.cairo b/src/campaign.cairo index e44bdb4..6c98fef 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,50 @@ mod TokengiverCampaign { self.campaign.write(campaign_address, campaign); } + fn donate( + ref self: ContractState, campaign_address: ContractAddress, amount: u256, token_id: u256 + ) { + println!("Riight here {}", 1); + let donor = get_caller_address(); + + let token_address = self.strk_address.read(); + + println!("Riight here {}", 2); + + println!( + "Balance -------- {}", + IERC20Dispatcher { contract_address: token_address }.balance_of(donor) + ); + + 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 +224,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 +251,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 +318,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..4cf9f91 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); @@ -97,11 +101,11 @@ fn test_donate() { strk_dispatcher.approve(token_giver_address, amount); stop_cheat_caller_address(strk_address); + println!("Donor balance: {}", strk_dispatcher.balance_of(DONOR())); // 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'); From c8e666569a26d155c8f2c4b514c0a2c120a5ca05 Mon Sep 17 00:00:00 2001 From: Oshioke-Salaki Date: Mon, 9 Dec 2024 17:30:27 +0100 Subject: [PATCH 2/2] chore: remove printlns --- src/campaign.cairo | 8 -------- tests/test_campaign.cairo | 1 - 2 files changed, 9 deletions(-) diff --git a/src/campaign.cairo b/src/campaign.cairo index 6c98fef..0c12c08 100644 --- a/src/campaign.cairo +++ b/src/campaign.cairo @@ -173,18 +173,10 @@ mod TokengiverCampaign { fn donate( ref self: ContractState, campaign_address: ContractAddress, amount: u256, token_id: u256 ) { - println!("Riight here {}", 1); let donor = get_caller_address(); let token_address = self.strk_address.read(); - println!("Riight here {}", 2); - - println!( - "Balance -------- {}", - IERC20Dispatcher { contract_address: token_address }.balance_of(donor) - ); - IERC20Dispatcher { contract_address: token_address } .approve(get_contract_address(), amount); diff --git a/tests/test_campaign.cairo b/tests/test_campaign.cairo index 4cf9f91..2f4a1b5 100644 --- a/tests/test_campaign.cairo +++ b/tests/test_campaign.cairo @@ -101,7 +101,6 @@ fn test_donate() { strk_dispatcher.approve(token_giver_address, amount); stop_cheat_caller_address(strk_address); - println!("Donor balance: {}", strk_dispatcher.balance_of(DONOR())); // donate start_cheat_caller_address(token_giver_address, DONOR()); token_giver.donate(campaign_address, amount, random_id);