Skip to content

Commit

Permalink
refac: campaign contract
Browse files Browse the repository at this point in the history
  • Loading branch information
manlikeHB committed Nov 30, 2024
1 parent f071d17 commit 30e2381
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 65 deletions.
1 change: 0 additions & 1 deletion .snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
tokengiver_integrationtest::test_campaign::test_donate
1 change: 1 addition & 0 deletions src/base/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ pub struct Campaign {
pub campaign_address: ContractAddress,
pub campaign_owner: ContractAddress,
pub metadata_URI: ByteArray,
pub token_id: u256,
}
61 changes: 18 additions & 43 deletions src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod TokengiverCampaign {
donation_count: Map<ContractAddress, u16>,
donation_details: Map<ContractAddress, DonationDetails>,
strk_address: ContractAddress,
token_giver_nft_class_hash: ClassHash,
token_giver_nft_address: ContractAddress,
}

// *************************************************************************
Expand All @@ -64,7 +64,7 @@ mod TokengiverCampaign {
#[key]
campaign_address: ContractAddress,
token_id: u256,
token_giverNft_contract_address: ContractAddress,
token_giver_nft_address: ContractAddress,
}

#[derive(Drop, starknet::Event)]
Expand All @@ -91,10 +91,10 @@ mod TokengiverCampaign {
#[constructor]
fn constructor(
ref self: ContractState,
token_giver_nft_class_hash: ClassHash,
token_giver_nft_address: ContractAddress,
strk_address: ContractAddress
) {
self.token_giver_nft_class_hash.write(token_giver_nft_class_hash);
self.token_giver_nft_address.write(token_giver_nft_address);
self.strk_address.write(strk_address);
}

Expand All @@ -108,35 +108,35 @@ mod TokengiverCampaign {
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252,
recipient: ContractAddress
) -> ContractAddress {
let caller = get_caller_address();
let nft_address = self.token_giver_nft_address.read();

/// mint token giver NFT
let token_giver_nft = ITokenGiverNftDispatcher { contract_address: nft_address };

let token_id = token_giver_nft.mint_token_giver_nft(caller);

/// create TBA account
let count: u16 = self.count.read() + 1;
let token_Nft_address = self.token_giver_nft_class_hash.read();
let token_giverNft_contract_address = self
.deploy_token_giver_nft(token_Nft_address, count.into());
let token_id = ITokenGiverNftDispatcher {
contract_address: token_giverNft_contract_address
}
.get_user_token_id(recipient);
let campaign_address = IRegistryLibraryDispatcher {
class_hash: registry_hash.try_into().unwrap()
}
.create_account(
implementation_hash, token_giverNft_contract_address, token_id, salt
);
.create_account(implementation_hash, nft_address, token_id, salt);

let new_campaign = Campaign {
campaign_address, campaign_owner: recipient, metadata_URI: "",
campaign_address, campaign_owner: caller, metadata_URI: "", token_id,
};
self.campaign.write(campaign_address, new_campaign);
self.campaigns.write(count, campaign_address);
self.count.write(count);
self
.emit(
CreateCampaign {
owner: recipient,
owner: caller,
campaign_address,
token_id,
token_giverNft_contract_address
token_giver_nft_address: nft_address
}
);
campaign_address
Expand Down Expand Up @@ -284,29 +284,4 @@ mod TokengiverCampaign {
);
}
}

#[generate_trait]
impl InternalImpl of InternalTrait {
fn deploy_token_giver_nft(
ref self: ContractState, token_giver_nft_class_hash: ClassHash, campaign_id: u16
) -> ContractAddress {
let mut constructor_calldata = array![];
campaign_id.serialize(ref constructor_calldata);

let (token_giver_nft_address, _) = deploy_syscall(
token_giver_nft_class_hash, 'salt'.into(), constructor_calldata.span(), false
)
.unwrap();

self
.emit(
DeployedTokenGiverNFT {
campaign_id: campaign_id.into(),
token_giver_nft_contract_address: token_giver_nft_address,
block_timestamp: get_block_timestamp()
}
);
token_giver_nft_address
}
}
}
6 changes: 1 addition & 5 deletions src/interfaces/ICampaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ use tokengiver::base::types::Campaign;
#[starknet::interface]
pub trait ICampaign<TState> {
fn create_campaign(
ref self: TState,
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252,
recipient: ContractAddress
ref self: TState, registry_hash: felt252, implementation_hash: felt252, salt: felt252,
) -> ContractAddress;
fn set_campaign_metadata_uri(
ref self: TState, campaign_address: ContractAddress, metadata_uri: ByteArray
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/ITokenGiverNft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use starknet::ContractAddress;
// *************************************************************************
#[starknet::interface]
pub trait ITokenGiverNft<TState> {
fn mint_token_giver_nft(ref self: TState, address: ContractAddress);
fn mint_token_giver_nft(ref self: TState, address: ContractAddress) -> u256;
fn get_last_minted_id(self: @TState) -> u256;
fn get_user_token_id(self: @TState, user: ContractAddress) -> u256;
fn get_token_mint_timestamp(self: @TState, token_id: u256) -> u64;
Expand Down
3 changes: 2 additions & 1 deletion src/token_giver_nft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ pub mod TokenGiverNFT {
// EXTERNAL
// *************************************************************************

fn mint_token_giver_nft(ref self: ContractState, address: ContractAddress) {
fn mint_token_giver_nft(ref self: ContractState, address: ContractAddress) -> u256 {
let mut token_id = self.last_minted_id.read() + 1;
self.erc721.mint(address, token_id);
let timestamp: u64 = get_block_timestamp();

self.user_token_id.write(address, token_id);
self.last_minted_id.write(token_id);
self.mint_timestamp.write(token_id, timestamp);
token_id
}


Expand Down
23 changes: 9 additions & 14 deletions tests/test_campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,24 @@ const ADMIN: felt252 = 'ADMIN';
fn __setup__() -> (ContractAddress, ContractAddress) {
let class_hash = declare("TokengiverCampaign").unwrap().contract_class();
let strk_address = deploy_erc20();
let nft_class_hash = __declare_token_giver_NFT__();
let nft_address = __deploy_token_giver_NFT__();

let mut calldata = array![];
nft_class_hash.serialize(ref calldata);
nft_address.serialize(ref calldata);
strk_address.serialize(ref calldata);

let (contract_address, _) = class_hash.deploy(@calldata).unwrap();

(contract_address, strk_address)
}

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

fn __declare_token_giver_NFT__() -> ClassHash {
*declare("TokenGiverNFT").unwrap().contract_class().class_hash
let mut events_constructor_calldata: Array<felt252> = array![ADMIN];
let (nft_contract_address, _) = nft_class_hash.deploy(@events_constructor_calldata).unwrap();

return (nft_contract_address);
}

fn deploy_erc20() -> ContractAddress {
Expand All @@ -74,43 +78,34 @@ fn test_donate() {
let random_id = 1;
let mut spy = spy_events();



//create campaign
start_cheat_caller_address(token_giver_address, RECIPIENT());


let campaign_address = token_giver
.create_campaign(REGISTRY_HASH(), IMPLEMENTATION_HASH(), SALT(), RECIPIENT());
.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, 'strk bal too low');
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(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');


let expected_event = Event::DonationCreated(
DonationCreated {
campaign_id: random_id,
Expand Down

0 comments on commit 30e2381

Please sign in to comment.