Skip to content

Commit

Permalink
Merge pull request #36 from casweeney/test/withdraw
Browse files Browse the repository at this point in the history
Test/withdraw
  • Loading branch information
mubarak23 authored Nov 30, 2024
2 parents 6ba0de8 + 97382ae commit a4de3bd
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.snfoundry_cache
.snfoundry_cache/
target
.DS_Store
Scarb.lock
Expand Down
Empty file.
1 change: 1 addition & 0 deletions src/base/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pub mod Errors {
pub const INVALID_OWNER: felt252 = 'TGN: caller is not owner!';
pub const INVALID_CAMPAIGN: felt252 = 'TGN: campaign is not owner!';
pub const INSUFFICIENT_BALANCE: felt252 = 'TGN: insufficient balance!';
pub const TRANSFER_FAILED: felt252 = 'TGN: transfer failed!';
}
8 changes: 5 additions & 3 deletions src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ mod TokengiverCampaign {
use tokengiver::interfaces::IERC721::{IERC721Dispatcher, IERC721DispatcherTrait};
use tokengiver::interfaces::ICampaign::ICampaign;
use tokengiver::base::types::Campaign;
use tokengiver::base::errors::Errors::{NOT_CAMPAIGN_OWNER, INSUFFICIENT_BALANCE};
use tokengiver::base::errors::Errors::{
NOT_CAMPAIGN_OWNER, INSUFFICIENT_BALANCE, TRANSFER_FAILED
};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};

#[derive(Drop, Copy, Serde, starknet::Store)]
Expand Down Expand Up @@ -185,8 +187,8 @@ mod TokengiverCampaign {

let token_address = self.strk_address.read();
let token_dispatcher = IERC20Dispatcher { contract_address: token_address };
let transfer_result = token_dispatcher.transfer(caller, amount);
assert!(transfer_result, "Transfer failed");
let transfer_result = token_dispatcher.transfer_from(campaign_address, caller, amount);
assert(transfer_result, TRANSFER_FAILED);
self.withdrawal_balance.write(campaign_address, available_balance - amount);
}

Expand Down
46 changes: 46 additions & 0 deletions tests/test_campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,49 @@ fn test_donate_event_emission() {

spy.assert_emitted(@array![(token_giver.contract_address, expected_event)]);
}

#[test]
#[fork("Mainnet")]
fn test_withdraw() {
let (token_giver_address, strk_address) = __setup__();
let token_giver = ICampaignDispatcher { contract_address: token_giver_address };
let strk_dispatcher = IERC20Dispatcher { contract_address: strk_address };
let random_id = 1;

//create campaign
start_cheat_caller_address(token_giver_address, RECIPIENT());
let campaign_address = token_giver
.create_campaign(REGISTRY_HASH(), IMPLEMENTATION_HASH(), SALT());
stop_cheat_caller_address(token_giver_address);

/// Transfer STRK to Donor
start_cheat_caller_address(strk_address, OWNER());
let amount = 2000000; //
strk_dispatcher.transfer(DONOR(), amount);
assert(strk_dispatcher.balance_of(DONOR()) == amount, 'transfer failed');
stop_cheat_caller_address(strk_address);

// approve allowance
start_cheat_caller_address(strk_address, DONOR());
strk_dispatcher.approve(token_giver_address, amount);
stop_cheat_caller_address(strk_address);

// 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(campaign_address) == amount, 'donation failed');

// Campaign address (TBA) -> approves token giver contract
start_cheat_caller_address(strk_address, campaign_address);
strk_dispatcher.approve(token_giver_address, amount);
stop_cheat_caller_address(strk_address);

// Campaign creator (RECIPIENT()) -> withdraws donations
start_cheat_caller_address(token_giver_address, RECIPIENT());
token_giver.withdraw(campaign_address, amount);
stop_cheat_caller_address(token_giver_address);

assert(strk_dispatcher.balance_of(RECIPIENT()) == amount, 'withdrawal failed');
assert(token_giver.get_available_withdrawal(campaign_address) == 0, 'withdrawal failed');
}
46 changes: 1 addition & 45 deletions tests/test_token_giver_nft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use core::num::traits::zero::Zero;
use core::starknet::SyscallResultTrait;
use core::traits::{TryInto, Into};
use starknet::{ContractAddress, ClassHash, get_block_timestamp};
// use snforge_std::{declare, ContractClassTrait, CheatTarget, start_prank, stop_prank,};
use snforge_std::{
declare, start_cheat_caller_address, stop_cheat_caller_address, ContractClassTrait,
DeclareResultTrait, spy_events, EventSpyAssertionsTrait,
DeclareResultTrait, EventSpyAssertionsTrait,
};
use openzeppelin::{token::erc721::interface::{ERC721ABIDispatcher, ERC721ABIDispatcherTrait}};

Expand All @@ -28,49 +27,6 @@ fn __setup__() -> ContractAddress {
return (nft_contract_address);
}

fn deploy_campaign_contract() -> ContractAddress {
let nft_class_hash = declare("TokenGiverNFT").unwrap().contract_class();

let mut constructor_calldata: Array<felt252> = ArrayTrait::new();
nft_class_hash.serialize(ref constructor_calldata);

let contract = declare("TokengiverCampaign").unwrap().contract_class();
let (contract_address, _) = contract.deploy(@constructor_calldata).unwrap();

contract_address
}

// #[test]
// #[fork("SEPOLIA_LATEST")]
// fn test_create_campaign() {
// let campaign_contract_address = deploy_campaign_contract();
// let campaign_contract = ICampaignDispatcher { contract_address: campaign_contract_address };

// // Using Sepolia V2 (Audited contract)

// let registry_hash: ClassHash = starknet::class_hash_const::<
// 0x046163525551f5a50ed027548e86e1ad023c44e0eeb0733f0dab2fb1fdc31ed0
// >();
// let registry_hash_in_felt: felt252 = registry_hash.into();

// let implementation_hash = starknet::class_hash_const::<
// 0x45d67b8590561c9b54e14dd309c9f38c4e2c554dd59414021f9d079811621bd
// >();
// let implementation_hash_in_felt: felt252 = implementation_hash.into();

// let salt = get_block_timestamp();
// let salt_in_felt: felt252 = salt.into();

// let recipient: ContractAddress = starknet::contract_address_const::<
// 0x01526C92E52c337b7B04d1307c6080Ece3a17071F2F0295197cEa8d077c6FF80
// >();

// campaign_contract
// .create_campaign(
// registry_hash_in_felt, implementation_hash_in_felt, salt_in_felt, recipient
// );
// }

// #[test]
// fn test_metadata() {
// let nft_contract_address = __setup__();
Expand Down

0 comments on commit a4de3bd

Please sign in to comment.