diff --git a/src/base/errors.cairo b/src/base/errors.cairo index 0d1bcf7..ecee6fa 100644 --- a/src/base/errors.cairo +++ b/src/base/errors.cairo @@ -7,4 +7,5 @@ pub mod Errors { pub const INITIALIZED: felt252 = 'TGN: already initialized!'; 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!'; } diff --git a/src/campaign.cairo b/src/campaign.cairo index 0992110..398a785 100644 --- a/src/campaign.cairo +++ b/src/campaign.cairo @@ -20,7 +20,7 @@ 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; + use tokengiver::base::errors::Errors::{NOT_CAMPAIGN_OWNER, INSUFFICIENT_BALANCE}; use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait}; @@ -167,6 +167,23 @@ mod TokengiverCampaign { self.withdrawal_balance.write(campaign_address, amount); } + // withdraw function + fn withdraw(ref self: ContractState, campaign_address: ContractAddress, amount: u256) { + let campaign: Campaign = self.campaign.read(campaign_address); + let caller: ContractAddress = get_caller_address(); + + assert(caller == campaign.campaign_owner, NOT_CAMPAIGN_OWNER); + + 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_dispatcher = IERC20Dispatcher { contract_address: token_address }; + let transfer_result = token_dispatcher.transfer(caller, amount); + assert!(transfer_result, "Transfer failed"); + self.withdrawal_balance.write(campaign_address, available_balance - amount); + } + // ************************************************************************* // GETTERS // ************************************************************************* diff --git a/src/interfaces/ICampaign.cairo b/src/interfaces/ICampaign.cairo index d2c7c18..30b4a17 100644 --- a/src/interfaces/ICampaign.cairo +++ b/src/interfaces/ICampaign.cairo @@ -20,6 +20,7 @@ pub trait ICampaign { fn set_available_withdrawal(ref self: TState, campaign_address: ContractAddress, amount: u256); 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); // Getters