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: withdraw function #32

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/base/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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!';
}
19 changes: 18 additions & 1 deletion src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};


Expand Down Expand Up @@ -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
// *************************************************************************
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/ICampaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub trait ICampaign<TState> {
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
Expand Down