From 7f5cc7098c732ae7540dafd9dee986d4a64b2aaf Mon Sep 17 00:00:00 2001 From: coderipper Date: Thu, 7 Nov 2024 16:48:52 -0300 Subject: [PATCH] temp calculate_fees --- apps/contracts/vault/src/fee.rs | 36 +++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/apps/contracts/vault/src/fee.rs b/apps/contracts/vault/src/fee.rs index 3a472e71..dc0c7112 100644 --- a/apps/contracts/vault/src/fee.rs +++ b/apps/contracts/vault/src/fee.rs @@ -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, }; @@ -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 { - 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 = 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 = 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.