Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgradability #44

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion 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, get_contract_address,
syscalls::deploy_syscall, SyscallResultTrait,
syscalls::deploy_syscall, SyscallResultTrait, syscalls, class_hash::class_hash_const,
storage::{Map, StoragePathEntry, StoragePointerReadAccess, StoragePointerWriteAccess}
};
use tokengiver::interfaces::ITokenGiverNft::{
Expand All @@ -24,6 +24,19 @@ mod TokengiverCampaign {
NOT_CAMPAIGN_OWNER, INSUFFICIENT_BALANCE, TRANSFER_FAILED
};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::UpgradeableComponent;
use openzeppelin::upgrades::interface::IUpgradeable;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);

#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

/// Upgradeable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[derive(Drop, Copy, Serde, starknet::Store)]
pub struct DonationDetails {
Expand All @@ -46,6 +59,10 @@ mod TokengiverCampaign {
donation_details: Map<ContractAddress, DonationDetails>,
strk_address: ContractAddress,
token_giver_nft_address: ContractAddress,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
}

// *************************************************************************
Expand All @@ -58,6 +75,10 @@ mod TokengiverCampaign {
DonationMade: DonationMade,
DeployedTokenGiverNFT: DeployedTokenGiverNFT,
WithdrawalMade: WithdrawalMade,
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
}

#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -107,15 +128,22 @@ mod TokengiverCampaign {
token_giver_nft_address: ContractAddress,
strk_address: ContractAddress
) {
let owner = get_caller_address();
self.token_giver_nft_address.write(token_giver_nft_address);
self.strk_address.write(strk_address);
self.ownable.initializer(owner);
}

// *************************************************************************
// EXTERNAL FUNCTIONS
// *************************************************************************
#[abi(embed_v0)]
impl CampaignImpl of ICampaign<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
// This function can only be called by the owner
self.ownable.assert_only_owner();
self.upgradeable.upgrade(new_class_hash);
}
fn create_campaign(
ref self: ContractState,
registry_hash: felt252,
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/ICampaign.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use starknet::ContractAddress;
use starknet::{ContractAddress, ClassHash};
use tokengiver::base::types::Campaign;
// *************************************************************************
// INTERFACE of TOKEN GIVER NFT
Expand All @@ -17,7 +17,7 @@ pub trait ICampaign<TState> {
fn set_donations(ref self: TState, campaign_address: ContractAddress, amount: u256);
fn donate(ref self: TState, campaign_address: ContractAddress, amount: u256, token_id: u256);
fn withdraw(ref self: TState, campaign_address: ContractAddress, amount: u256);

fn upgrade(ref self: TState, new_class_hash: ClassHash);

// Getters
fn get_campaign_metadata(self: @TState, campaign_address: ContractAddress) -> ByteArray;
Expand Down
18 changes: 18 additions & 0 deletions tests/test_campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ fn __deploy_token_giver_NFT__() -> ContractAddress {
return (nft_contract_address);
}


fn test_upgradability() {
Akshola00 marked this conversation as resolved.
Show resolved Hide resolved
let class_hash = declare("TokengiverCampaign").unwrap().contract_class();
let strk_address = deploy_erc20();
let nft_address = __deploy_token_giver_NFT__();

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

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

let campaign_dispatcher = ICampaignDispatcher { contract_address };
let new_class_hash = declare("TokengiverCampaign").unwrap().contract_class().class_hash;
campaign_dispatcher.upgrade(*new_class_hash);
}


Akshola00 marked this conversation as resolved.
Show resolved Hide resolved
fn deploy_erc20() -> ContractAddress {
let class = declare("MyToken").unwrap().contract_class();

Expand Down
Loading