Skip to content

Commit

Permalink
temp calculate_fees
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinsoza committed Nov 7, 2024
1 parent 32e9b44 commit 7f5cc70
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions apps/contracts/vault/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
get_defindex_protocol_fee_receiver, get_factory, get_last_fee_assesment, get_vault_fee,
set_last_fee_assesment,
},
token::internal_mint,
token::{internal_mint, VaultToken},
utils::calculate_dftokens_from_asset_amounts,
ContractError,
};
Expand All @@ -28,27 +28,33 @@ fn fetch_defindex_fee(e: &Env) -> u32 {

/// Calculates the required fees in dfTokens based on the current APR fee rate.
fn calculate_fees(e: &Env, time_elapsed: u64, fee_rate: u32) -> Result<i128, ContractError> {
let total_managed_funds = fetch_total_managed_funds(e); // Get total managed funds per asset
let total_supply = VaultToken::total_supply(e.clone());

let seconds_per_year = SECONDS_PER_YEAR; // 365 days in seconds
// (fee_rate as i128 * total_supply * time_elapsed) / ((SECONDS_PER_YEAR * MAX_BPS) - (fee_rate as i128 * time_elapsed));
let fees = (fee_rate as i128).checked_mul(total_supply).unwrap().checked_mul(time_elapsed as i128).unwrap().checked_div((SECONDS_PER_YEAR.checked_mul(MAX_BPS).unwrap().checked_sub((fee_rate as i128).checked_mul(time_elapsed as i128).unwrap()).unwrap())).unwrap();

Ok(fees)
// let total_managed_funds = fetch_total_managed_funds(e); // Get total managed funds per asset

let mut total_fees_per_asset: Map<Address, i128> = Map::new(&e);
// let seconds_per_year = SECONDS_PER_YEAR; // 365 days in seconds

// Iterate over each asset in the vault
for (asset_address, amount) in total_managed_funds.iter() {
// Fetch current managed funds for each asset
let current_asset_value = amount;
// let mut total_fees_per_asset: Map<Address, i128> = Map::new(&e);

// Calculate the fee for this asset based on the fee rate and time elapsed
let asset_fee = (current_asset_value * fee_rate as i128 * time_elapsed as i128)
/ (seconds_per_year * MAX_BPS);
// // Iterate over each asset in the vault
// for (asset_address, amount) in total_managed_funds.iter() {
// // Fetch current managed funds for each asset
// let current_asset_value = amount;

total_fees_per_asset.set(asset_address.clone(), asset_fee);
}
// // Calculate the fee for this asset based on the fee rate and time elapsed
// let asset_fee = (current_asset_value * fee_rate as i128 * time_elapsed as i128)
// / (seconds_per_year * MAX_BPS);

// total_fees_per_asset.set(asset_address.clone(), asset_fee);
// }

let total_fees_in_dftokens = calculate_dftokens_from_asset_amounts(e, total_fees_per_asset, total_managed_funds)?;
// let total_fees_in_dftokens = calculate_dftokens_from_asset_amounts(e, total_fees_per_asset, total_managed_funds)?;

Ok(total_fees_in_dftokens)
// Ok(total_fees_in_dftokens)
}

/// Collects and mints fees in dfTokens, distributing them to the appropriate fee receivers.
Expand Down

0 comments on commit 7f5cc70

Please sign in to comment.