Skip to content

Commit

Permalink
fix: donate func
Browse files Browse the repository at this point in the history
  • Loading branch information
Oshioke-Salaki committed Dec 9, 2024
1 parent 1919eb2 commit fbc4654
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 43 deletions.
92 changes: 51 additions & 41 deletions src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
// *************************************************************************
Expand Down Expand Up @@ -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(),
}
);
}
}
}
8 changes: 6 additions & 2 deletions tests/test_campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down

0 comments on commit fbc4654

Please sign in to comment.