Skip to content

Commit

Permalink
Merge pull request #28 from manlikeHB/test-donate
Browse files Browse the repository at this point in the history
Test & Reafac
  • Loading branch information
mubarak23 authored Nov 30, 2024
2 parents 0f8a85c + 96695ff commit c773ab0
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.snfoundry_cache
target
.DS_Store
Scarb.lock
Scarb.lock
snfoundry_cache/https___starknet_mainnet_public_blastapi_io_rpc_v0_7_631894_v3.json
Empty file.
115 changes: 115 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "openzeppelin"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"
dependencies = [
"openzeppelin_access",
"openzeppelin_account",
"openzeppelin_governance",
"openzeppelin_introspection",
"openzeppelin_presets",
"openzeppelin_security",
"openzeppelin_token",
"openzeppelin_upgrades",
"openzeppelin_utils",
]

[[package]]
name = "openzeppelin_access"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"
dependencies = [
"openzeppelin_introspection",
"openzeppelin_utils",
]

[[package]]
name = "openzeppelin_account"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"
dependencies = [
"openzeppelin_introspection",
"openzeppelin_token",
"openzeppelin_utils",
]

[[package]]
name = "openzeppelin_governance"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"
dependencies = [
"openzeppelin_access",
"openzeppelin_introspection",
]

[[package]]
name = "openzeppelin_introspection"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"

[[package]]
name = "openzeppelin_presets"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"
dependencies = [
"openzeppelin_access",
"openzeppelin_account",
"openzeppelin_introspection",
"openzeppelin_token",
"openzeppelin_upgrades",
]

[[package]]
name = "openzeppelin_security"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"

[[package]]
name = "openzeppelin_token"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"
dependencies = [
"openzeppelin_account",
"openzeppelin_governance",
"openzeppelin_introspection",
]

[[package]]
name = "openzeppelin_upgrades"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"

[[package]]
name = "openzeppelin_utils"
version = "0.15.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634"

[[package]]
name = "snforge_scarb_plugin"
version = "0.31.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.31.0#72ea785ca354e9e506de3e5d687da9fb2c1b3c67"

[[package]]
name = "snforge_std"
version = "0.31.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.31.0#72ea785ca354e9e506de3e5d687da9fb2c1b3c67"
dependencies = [
"snforge_scarb_plugin",
]

[[package]]
name = "token_bound_accounts"
version = "0.3.0"
source = "git+https://github.com/Starknet-Africa-Edu/TBA?tag=v0.3.0#1f8b5e3c45422fb188ef2cf874b46d02f642973b"

[[package]]
name = "tokengiver"
version = "0.1.0"
dependencies = [
"openzeppelin",
"snforge_std",
"token_bound_accounts",
]
7 changes: 6 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ test = "snforge test"
name = "SEPOLIA_LATEST"
url = "https://starknet-sepolia.public.blastapi.io/rpc/v0_7"
block_id.tag = "latest"
# block_id.number = "325081"
# block_id.number = "325081"

[[tool.snforge.fork]]
name = "Mainnet"
url = "https://starknet-mainnet.public.blastapi.io/rpc/v0_7"
block_id.number = "631894"
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,
}
75 changes: 24 additions & 51 deletions src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod TokengiverCampaign {
use core::traits::TryInto;
use starknet::{
ContractAddress, get_caller_address, get_block_timestamp, ClassHash,
syscalls::deploy_syscall,
syscalls::deploy_syscall, SyscallResultTrait,
storage::{Map, StoragePathEntry, StoragePointerReadAccess, StoragePointerWriteAccess}
};
use tokengiver::interfaces::ITokenGiverNft::{
Expand All @@ -23,7 +23,6 @@ mod TokengiverCampaign {
use tokengiver::base::errors::Errors::{NOT_CAMPAIGN_OWNER, INSUFFICIENT_BALANCE};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};


#[derive(Drop, Copy, Serde, starknet::Store)]
pub struct DonationDetails {
token_id: u256,
Expand All @@ -43,8 +42,8 @@ mod TokengiverCampaign {
donations: Map<ContractAddress, u256>,
donation_count: Map<ContractAddress, u16>,
donation_details: Map<ContractAddress, DonationDetails>,
erc20_token: ContractAddress,
token_giver_nft_class_hash: ClassHash,
strk_address: ContractAddress,
token_giver_nft_address: ContractAddress,
}

// *************************************************************************
Expand All @@ -65,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 @@ -90,8 +89,13 @@ mod TokengiverCampaign {
// CONSTRUCTOR
// *************************************************************************
#[constructor]
fn constructor(ref self: ContractState, token_giver_nft_class_hash: ClassHash) {
self.token_giver_nft_class_hash.write(token_giver_nft_class_hash);
fn constructor(
ref self: ContractState,
token_giver_nft_address: ContractAddress,
strk_address: ContractAddress
) {
self.token_giver_nft_address.write(token_giver_nft_address);
self.strk_address.write(strk_address);
}

// *************************************************************************
Expand All @@ -104,28 +108,24 @@ mod TokengiverCampaign {
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252,
recipient: ContractAddress
) -> ContractAddress {
let caller = get_caller_address();
let count: u16 = self.count.read() + 1;
let nft_address = self.token_giver_nft_address.read();
let token_giver_nft = ITokenGiverNftDispatcher { contract_address: nft_address };

let token_giverNft_contract_address = self
.deploy_token_giver_nft(self.token_giver_nft_class_hash.read(), caller);

let token_id = ITokenGiverNftDispatcher {
contract_address: token_giverNft_contract_address
}
.get_user_token_id(recipient);
/// mint token giver NFT
let token_id = token_giver_nft.mint_token_giver_nft(caller);

/// create TBA account
let count: u16 = self.count.read() + 1;
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);

/// create campaign
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);
Expand All @@ -134,10 +134,10 @@ mod TokengiverCampaign {
self
.emit(
CreateCampaign {
owner: recipient,
owner: caller,
campaign_address,
token_id,
token_giverNft_contract_address
token_giver_nft_address: nft_address
}
);

Expand Down Expand Up @@ -183,7 +183,7 @@ mod TokengiverCampaign {
let available_balance: u256 = self.withdrawal_balance.read(campaign_address);
assert(amount <= available_balance, INSUFFICIENT_BALANCE);

let token_address = self.erc20_token.read();
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");
Expand Down Expand Up @@ -257,7 +257,7 @@ mod TokengiverCampaign {
) {
let donor = get_caller_address();

let token_address = self.erc20_token.read();
let token_address = self.strk_address.read();

IERC20Dispatcher { contract_address: token_address }
.transfer_from(donor, campaign_address, amount);
Expand Down Expand Up @@ -286,31 +286,4 @@ mod TokengiverCampaign {
);
}
}

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

let (token_giver_nft_address, _) = deploy_syscall(
token_giver_nft_class_hash,
get_block_timestamp().try_into().unwrap(),
constructor_calldata.span(),
false
)
.unwrap();

// self
// .emit(
// DeployedTokenGiverNFT {
// campaign_id: campaign_id,
// 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
1 change: 1 addition & 0 deletions src/presets.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod campaign;
mod erc20;
36 changes: 36 additions & 0 deletions src/presets/erc20.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[starknet::contract]
mod MyToken {
use openzeppelin::token::erc20::ERC20Component;
use openzeppelin::token::erc20::ERC20HooksEmptyImpl;
use starknet::ContractAddress;

component!(path: ERC20Component, storage: erc20, event: ERC20Event);

#[abi(embed_v0)]
impl ERC20Impl = ERC20Component::ERC20Impl<ContractState>;

impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
erc20: ERC20Component::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
ERC20Event: ERC20Component::Event
}

#[constructor]
fn constructor(ref self: ContractState, recipient: ContractAddress,) {
let name: ByteArray = "My Token";
let symbol: ByteArray = "MYT";
let initial_supply = 100_000_000_u256;

self.erc20.initializer(name, symbol);
self.erc20.mint(recipient, initial_supply);
}
}
4 changes: 3 additions & 1 deletion src/token_giver_nft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod TokenGiverNFT {
component!(path: SRC5Component, storage: src5, event: SRC5Event);

// ERC721 Mixin
#[abi(embed_v0)]
impl ERC721MixinImpl = ERC721Component::ERC721MixinImpl<ContractState>;
impl ERC721InternalImpl = ERC721Component::InternalImpl<ContractState>;

Expand Down Expand Up @@ -74,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
Loading

0 comments on commit c773ab0

Please sign in to comment.