Skip to content

Commit

Permalink
Distributing fees, calculating managed funds without locked fees, tot…
Browse files Browse the repository at this point in the history
…al amounts can bee seen with report()
  • Loading branch information
joaquinsoza committed Dec 18, 2024
1 parent a336415 commit ddfa29a
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 191 deletions.
3 changes: 1 addition & 2 deletions apps/contracts/vault/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub(crate) const MAX_BPS: i128 = 10_000;
pub(crate) const SECONDS_PER_YEAR: i128 = 31_536_000;
pub(crate) const MAX_BPS: i128 = 10_000;
2 changes: 1 addition & 1 deletion apps/contracts/vault/src/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
token::{internal_mint, VaultToken},
utils::{calculate_deposit_amounts_and_shares_to_mint, check_nonnegative_amount},
ContractError, MINIMUM_LIQUIDITY,
models::{CurrentAssetInvestmentAllocation},
models::CurrentAssetInvestmentAllocation,
};

/// Common logic for processing deposits.
Expand Down
25 changes: 8 additions & 17 deletions apps/contracts/vault/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,22 @@ pub(crate) fn emit_emergency_manager_changed_event(e: &Env, new_emergency_manage
.publish(("DeFindexVault", symbol_short!("nemanager")), event);
}

// FEES MINTED EVENT
// FEES DISTRIBUTED EVENT
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FeesMintedEvent {
pub defindex_protocol_receiver: Address,
pub defindex_shares: i128,
pub vault_receiver: Address,
pub vault_shares: i128,
pub struct FeesDistributedEvent {
pub distributed_fees: Vec<(Address, i128)>
}

/// Publishes an `EmergencyManagerChangedEvent` to the event stream.
pub(crate) fn emit_fees_minted_event(
pub(crate) fn emit_fees_distributed_event(
e: &Env,
defindex_protocol_receiver: Address,
defindex_shares: i128,
vault_receiver: Address,
vault_shares: i128,
distributed_fees: Vec<(Address, i128)>,
) {
let event = FeesMintedEvent {
defindex_protocol_receiver,
defindex_shares,
vault_receiver,
vault_shares,
let event = FeesDistributedEvent {
distributed_fees,
};

e.events()
.publish(("DeFindexVault", symbol_short!("mfees")), event);
.publish(("DeFindexVault", symbol_short!("dfees")), event);
}
108 changes: 0 additions & 108 deletions apps/contracts/vault/src/fee.rs

This file was deleted.

17 changes: 10 additions & 7 deletions apps/contracts/vault/src/funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use soroban_sdk::{Address, Env, Map, Vec};

use common::models::AssetStrategySet;
use crate::models::{StrategyAllocation, CurrentAssetInvestmentAllocation};
use crate::storage::get_assets;
use crate::storage::{get_assets, get_report};
use crate::strategies::get_strategy_client;

/// Retrieves the idle funds for a given asset.
Expand All @@ -21,21 +21,24 @@ pub fn fetch_idle_funds_for_asset(e: &Env, asset: &Address) -> i128 {
TokenClient::new(e, &asset).balance(&e.current_contract_address())
}

/// Retrieves the total funds invested in a specified strategy.
/// Retrieves the total funds invested in a specified strategy, excluding any locked fees.
///
/// Since only the strategy contract itself can accurately determine the amount invested,
/// this function performs a cross-contract call to the strategy to fetch the current balance
/// of the investment.
/// This function performs a cross-contract call to the strategy to fetch the current balance
/// of the investment. It then subtracts any locked fees from the total to provide an accurate
/// representation of the funds that are actively invested and available to the user.
///
/// # Arguments
/// * `e` - The current environment instance.
/// * `strategy_address` - The address of the strategy whose investment balance is to be retrieved.
///
/// # Returns
/// The total invested funds in the strategy as an `i128`.
/// The total invested funds in the strategy as an `i128`, excluding locked fees.
pub fn fetch_strategy_invested_funds(e: &Env, strategy_address: &Address) -> i128 {
let strategy_client = get_strategy_client(e, strategy_address.clone());
strategy_client.balance(&e.current_contract_address())
let strategy_invested_funds = strategy_client.balance(&e.current_contract_address());

let report = get_report(e, strategy_address);
strategy_invested_funds.checked_sub(report.locked_fee).unwrap_or(0)
}


Expand Down
28 changes: 25 additions & 3 deletions apps/contracts/vault/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ pub trait VaultTrait {
/// - `manager`: Primary vault manager with permissions for vault control.
/// - `emergency_manager`: Address with emergency access for emergency control over the vault.
/// - `vault_fee_receiver`: Address designated to receive the vault fee receiver's portion of management fees.
/// - `vault_fee`: Vault-specific fee percentage in basis points (typically set at 0-2% APR).
/// - `defindex_protocol_receiver`: Address receiving DeFindex’s protocol-wide fee in basis points (0.5% APR).
/// - `vault_fee`: Vault-specific fee percentage in basis points.
/// - `defindex_protocol_receiver`: Address receiving DeFindex’s protocol-wide fee in basis points.
/// - `defindex_protocol_rate`: DeFindex’s protocol fee percentage in basis points.
/// - `factory`: Factory contract address for deployment linkage.
/// - `vault_name`: Name of the vault token to be displayed in metadata.
/// - `vault_symbol`: Symbol representing the vault’s token.
Expand All @@ -38,6 +39,7 @@ pub trait VaultTrait {
vault_fee_receiver: Address,
vault_fee: u32,
defindex_protocol_receiver: Address,
defindex_protocol_rate: u32,
factory: Address,
vault_name: String,
vault_symbol: String,
Expand Down Expand Up @@ -347,7 +349,27 @@ pub trait VaultManagementTrait {
/// * `Result<Vec<(Address, i128)>, ContractError>` - A vector of tuples with strategy addresses and locked fee amounts in their underlying_asset.
fn lock_fees(e: Env, new_fee_bps: Option<u32>) -> Result<Vec<Report>, ContractError>;

/// Releases locked fees for a specific strategy.
///
/// # Arguments
/// * `e` - The environment reference.
/// * `strategy` - The address of the strategy for which to release fees.
/// * `amount` - The amount of fees to release.
///
/// # Returns
/// * `Result<Report, ContractError>` - A report of the released fees or a `ContractError` if the operation fails.
fn release_fees(e: Env, strategy: Address, amount: i128) -> Result<Report, ContractError>;

fn distribute_fees(e: Env) -> Result<(), ContractError>;
/// Distributes the locked fees for all assets and their strategies.
///
/// This function iterates through each asset and its strategies, calculating the fees to be distributed
/// to the vault fee receiver and the DeFindex protocol fee receiver based on their respective fee rates.
/// It ensures proper authorization and validation checks before proceeding with the distribution.
///
/// # Arguments
/// * `e` - The environment reference.
///
/// # Returns
/// * `Result<Vec<(Address, i128)>, ContractError>` - A vector of tuples with asset addresses and the total distributed fee amounts.
fn distribute_fees(e: Env) -> Result<Vec<(Address, i128)>, ContractError>;
}
Loading

0 comments on commit ddfa29a

Please sign in to comment.