Skip to content

Commit

Permalink
Merge pull request #40 from Token-Giver/fix-donate
Browse files Browse the repository at this point in the history
fix: donate func
  • Loading branch information
Oshioke-Salaki authored Dec 10, 2024
2 parents 1919eb2 + c8e6665 commit f0e13cb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
84 changes: 43 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,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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
// *************************************************************************
Expand Down Expand Up @@ -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(),
}
);
}
}
}
7 changes: 5 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 @@ -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');
Expand Down

0 comments on commit f0e13cb

Please sign in to comment.