From 30e23811ba727a630a37417d3a1a170fc24264f9 Mon Sep 17 00:00:00 2001 From: manlikeHB Date: Sat, 30 Nov 2024 14:17:57 +0100 Subject: [PATCH] refac: campaign contract --- .snfoundry_cache/.prev_tests_failed | 1 - src/base/types.cairo | 1 + src/campaign.cairo | 61 +++++++++-------------------- src/interfaces/ICampaign.cairo | 6 +-- src/interfaces/ITokenGiverNft.cairo | 2 +- src/token_giver_nft.cairo | 3 +- tests/test_campaign.cairo | 23 +++++------ 7 files changed, 32 insertions(+), 65 deletions(-) diff --git a/.snfoundry_cache/.prev_tests_failed b/.snfoundry_cache/.prev_tests_failed index 619f58a..e69de29 100644 --- a/.snfoundry_cache/.prev_tests_failed +++ b/.snfoundry_cache/.prev_tests_failed @@ -1 +0,0 @@ -tokengiver_integrationtest::test_campaign::test_donate diff --git a/src/base/types.cairo b/src/base/types.cairo index ae91d5e..8301618 100644 --- a/src/base/types.cairo +++ b/src/base/types.cairo @@ -15,4 +15,5 @@ pub struct Campaign { pub campaign_address: ContractAddress, pub campaign_owner: ContractAddress, pub metadata_URI: ByteArray, + pub token_id: u256, } diff --git a/src/campaign.cairo b/src/campaign.cairo index 83ce1ef..8a70e88 100644 --- a/src/campaign.cairo +++ b/src/campaign.cairo @@ -43,7 +43,7 @@ mod TokengiverCampaign { donation_count: Map, donation_details: Map, strk_address: ContractAddress, - token_giver_nft_class_hash: ClassHash, + token_giver_nft_address: ContractAddress, } // ************************************************************************* @@ -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)] @@ -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); } @@ -108,24 +108,24 @@ 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); @@ -133,10 +133,10 @@ mod TokengiverCampaign { self .emit( CreateCampaign { - owner: recipient, + owner: caller, campaign_address, token_id, - token_giverNft_contract_address + token_giver_nft_address: nft_address } ); campaign_address @@ -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 - } - } } diff --git a/src/interfaces/ICampaign.cairo b/src/interfaces/ICampaign.cairo index 30b4a17..5b5740c 100644 --- a/src/interfaces/ICampaign.cairo +++ b/src/interfaces/ICampaign.cairo @@ -7,11 +7,7 @@ use tokengiver::base::types::Campaign; #[starknet::interface] pub trait ICampaign { 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 diff --git a/src/interfaces/ITokenGiverNft.cairo b/src/interfaces/ITokenGiverNft.cairo index 89c01ad..1c7dfd2 100644 --- a/src/interfaces/ITokenGiverNft.cairo +++ b/src/interfaces/ITokenGiverNft.cairo @@ -4,7 +4,7 @@ use starknet::ContractAddress; // ************************************************************************* #[starknet::interface] pub trait ITokenGiverNft { - 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; diff --git a/src/token_giver_nft.cairo b/src/token_giver_nft.cairo index c5bad54..d974670 100644 --- a/src/token_giver_nft.cairo +++ b/src/token_giver_nft.cairo @@ -75,7 +75,7 @@ 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(); @@ -83,6 +83,7 @@ pub mod TokenGiverNFT { self.user_token_id.write(address, token_id); self.last_minted_id.write(token_id); self.mint_timestamp.write(token_id, timestamp); + token_id } diff --git a/tests/test_campaign.cairo b/tests/test_campaign.cairo index 58fae8e..30532c7 100644 --- a/tests/test_campaign.cairo +++ b/tests/test_campaign.cairo @@ -38,10 +38,10 @@ 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(); @@ -49,9 +49,13 @@ fn __setup__() -> (ContractAddress, ContractAddress) { (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 = array![ADMIN]; + let (nft_contract_address, _) = nft_class_hash.deploy(@events_constructor_calldata).unwrap(); + + return (nft_contract_address); } fn deploy_erc20() -> ContractAddress { @@ -74,18 +78,13 @@ 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; // @@ -93,24 +92,20 @@ fn test_donate() { 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,