Skip to content

Commit

Permalink
🚧 check_and_calculate_fees
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Sep 20, 2023
1 parent a2c8e9e commit cc21769
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/core/lockup_linear.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ use starknet::{ContractAddress, ClassHash};
// Local imports.
use za_warudo::types::lockup_linear::{Range, Broker};

// ███████╗ █████╗ ██╗ ██╗ █████╗ ██████╗ ██╗ ██╗██████╗ ██████╗
// ╚══███╔╝██╔══██╗ ██║ ██║██╔══██╗██╔══██╗██║ ██║██╔══██╗██╔═══██╗
// ███╔╝ ███████║ ██║ █╗ ██║███████║██████╔╝██║ ██║██║ ██║██║ ██║
// ███╔╝ ██╔══██║ ██║███╗██║██╔══██║██╔══██╗██║ ██║██║ ██║██║ ██║
// ███████╗██║ ██║ ╚███╔███╔╝██║ ██║██║ ██║╚██████╔╝██████╔╝╚██████╔╝
// ╚══════╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝

// ██╗ ██████╗ ██████╗██╗ ██╗██╗ ██╗██████╗ ██╗ ██╗███╗ ██╗███████╗ █████╗ ██████╗
// ██║ ██╔═══██╗██╔════╝██║ ██╔╝██║ ██║██╔══██╗ ██║ ██║████╗ ██║██╔════╝██╔══██╗██╔══██╗
// ██║ ██║ ██║██║ █████╔╝ ██║ ██║██████╔╝ ██║ ██║██╔██╗ ██║█████╗ ███████║██████╔╝
// ██║ ██║ ██║██║ ██╔═██╗ ██║ ██║██╔═══╝ ██║ ██║██║╚██╗██║██╔══╝ ██╔══██║██╔══██╗
// ███████╗╚██████╔╝╚██████╗██║ ██╗╚██████╔╝██║ ███████╗██║██║ ╚████║███████╗██║ ██║██║ ██║
// ╚══════╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝

// *************************************************************************
// Interface of the `ZaWarudoLockupLinear` contract.
// *************************************************************************
Expand Down Expand Up @@ -58,6 +72,7 @@ mod ZaWarudoLockupLinear {
use za_warudo::types::lockup_linear::{Range, Broker, LockupLinearStream};
use za_warudo::types::lockup::LockupAmounts;
use za_warudo::tokens::erc721::{ERC721, IERC721};
use za_warudo::libraries::helpers;

// *************************************************************************
// STORAGE
Expand Down Expand Up @@ -143,8 +158,18 @@ mod ZaWarudoLockupLinear {
range: Range,
broker: Broker,
) -> u64 {
// Checks: check the fees and calculate the fee amounts.
// Safe Interactions: query the protocol fee. This is safe because it's a known Za Warudo contract that does
// not call other unknown contracts.
// TODO: implement.
let protocol_fee = 0;

// TODO: Handle MAX_FEE as a constant, with handlign of fixed point numbers.
let MAX_FEE = 100;

// Checks: check the fees and calculate the fee amounts.
let create_amounts = helpers::check_and_calculate_fees(
total_amount, protocol_fee, broker.fee, MAX_FEE
);

// Read the next stream id from storage.
let stream_id = self.next_stream_id.read();
Expand Down
6 changes: 6 additions & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ mod types {
mod lockup_linear;
}

/// Module containing libraries.
mod libraries {
mod helpers;
mod errors;
}

/// Module containing tokens implementations.
/// TODO: remove and use OpenZeppelin dependency when it's ready.
mod tokens {
Expand Down
12 changes: 12 additions & 0 deletions src/libraries/errors.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod Lockup {
const PROTOCOL_FEE_TOO_HIGH: felt252 = 'protocol_fee_too_high';
const BROKER_FEE_TOO_HIGH: felt252 = 'broker_fee_too_high';

fn protocol_fee_too_high(protocol_fee: u128, max_fee: u128) {
panic(array![PROTOCOL_FEE_TOO_HIGH, protocol_fee.into(), max_fee.into()])
}

fn broker_fee_too_high(broker_fee: u128, max_fee: u128) {
panic(array![BROKER_FEE_TOO_HIGH, broker_fee.into(), max_fee.into()])
}
}
49 changes: 49 additions & 0 deletions src/libraries/helpers.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// *************************************************************************
// IMPORTS
// *************************************************************************

// Core lib imports.
use zeroable::Zeroable;

// Local imports.
use za_warudo::types::lockup::CreateAmounts;
use za_warudo::libraries::errors;

//Checks that neither fee is greater than `max_fee`, and then calculates the protocol fee amount, the
/// broker fee amount, and the deposit amount from the total amount.
fn check_and_calculate_fees(
total_amount: u128, protocol_fee: u128, broker_fee: u128, max_fee: u128
) -> CreateAmounts {
// TODO: Handle fixed point arithmetic everywhere.

// When the total amount is zero, the fees are also zero.
if (total_amount.is_zero()) {
return Zeroable::zero();
}

// Checks: the protocol fee is not greater than `max_fee`.
if (protocol_fee > max_fee) {
errors::Lockup::protocol_fee_too_high(protocol_fee, max_fee);
}

// Checks: the broker fee is not greater than `max_fee`.
if (broker_fee > max_fee) {
errors::Lockup::broker_fee_too_high(broker_fee, max_fee);
}

// Calculate the protocol fee amount.
let protocol_fee = total_amount * protocol_fee;

// Calculate the broker fee amount.
let broker_fee = total_amount * broker_fee;

// Assert that the total amount is strictly greater than the sum of the protocol fee amount and the
// broker fee amount.
assert(total_amount > protocol_fee + broker_fee, 'total_amount_too_low');

// Calculate the deposit amount (the amount to stream, net of fees).
let deposit = total_amount - protocol_fee - broker_fee;

// Return the amounts.
CreateAmounts { protocol_fee, broker_fee, deposit }
}
40 changes: 40 additions & 0 deletions src/types/lockup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use array::ArrayTrait;
use result::ResultTrait;
use option::OptionTrait;
use traits::{TryInto, Into};
use zeroable::Zeroable;
use debug::PrintTrait;

/// Represent Lockup amounts.
// Struct encapsulating the deposit, withdrawn, and refunded amounts, all denoted in units
Expand All @@ -20,3 +22,41 @@ struct LockupAmounts {
/// The amount refunded to the sender. Unless the stream was canceled, this is always zero.
refunded: u128,
}

/// Represent Create amounts.
/// Struct encapsulating the deposit amount, the protocol fee amount, and the broker fee amount,
/// all denoted in units of the asset's decimals.
#[derive(Drop, Copy, starknet::Store, Serde)]
struct CreateAmounts {
/// The amount to deposit in the stream.
deposit: u128,
/// The protocol fee amount.
protocol_fee: u128,
/// The broker fee amount.
broker_fee: u128,
}

/// Implementation of `Zeroable` trait for `CreateAmounts`.
impl CreateAmountsZeroable of Zeroable<CreateAmounts> {
fn zero() -> CreateAmounts {
CreateAmounts { deposit: 0, protocol_fee: 0, broker_fee: 0, }
}
#[inline(always)]
fn is_zero(self: CreateAmounts) -> bool {
self.deposit.is_zero() && self.protocol_fee.is_zero() && self.broker_fee.is_zero()
}
#[inline(always)]
fn is_non_zero(self: CreateAmounts) -> bool {
!self.is_zero()
}
}

/// Implementation of `PrintTrait` trait for `CreateAmounts`.
impl CreateAmountsPrintTrait of PrintTrait<CreateAmounts> {
fn print(self: CreateAmounts) {
let message = array![
'CreateAmounts: ', self.deposit.into(), self.protocol_fee.into(), self.broker_fee.into()
];
message.print();
}
}
2 changes: 1 addition & 1 deletion tests/test_lockup_linear.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Test file for `src/deposit/deposit_vault.cairo`.
//! Test file for `src/core/lockup_linear.cairo`.

// *************************************************************************
// IMPORTS
Expand Down

0 comments on commit cc21769

Please sign in to comment.