Struct defindex_vault::DeFindexVault
source · pub struct DeFindexVault;Implementations§
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_initialize() -> [u8; 1352]
pub const fn spec_xdr_initialize() -> [u8; 1352]
Initializes the DeFindex Vault contract with the required parameters.
This function sets the roles for manager, emergency manager, vault fee receiver, and manager. It also stores the list of assets to be managed by the vault, including strategies for each asset.
§Arguments
assets: List of asset allocations for the vault, including strategies associated with each asset.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).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.
§Returns
Result<(), ContractError>: ReturnsOk(())if initialization succeeds, or aContractErrorif any setup fails (e.g., strategy mismatch with asset).
§Errors
ContractError::AlreadyInitialized: If the vault has already been initialized.ContractError::StrategyDoesNotSupportAsset: If a strategy within an asset does not support the asset’s contract.
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_deposit() -> [u8; 1196]
pub const fn spec_xdr_deposit() -> [u8; 1196]
Handles user deposits into the DeFindex Vault and optionally allocates investments automatically.
This function processes a deposit by transferring each specified asset amount from the user’s address to
the vault, allocating assets according to the vault’s defined strategy ratios, and minting vault shares that
represent the user’s proportional share in the vault. Additionally, if the invest parameter is set to true,
the function will immediately generate and execute investment allocations based on the vault’s strategy configuration.
§Parameters
e- The current environment reference (Env), for access to the contract state and utilities.amounts_desired- A vector specifying the user’s intended deposit amounts for each asset.amounts_min- A vector of minimum deposit amounts required for the transaction to proceed.from- The address of the user making the deposit.invest- A boolean flag indicating whether to immediately invest the deposited funds into the vault’s strategies:true: Generate and execute investments after the deposit.false: Leave the deposited funds as idle assets in the vault.
§Returns
Result<(Vec<i128>, i128), ContractError>- Returns the actual depositedamountsandshares_to_mintif successful, otherwise aContractError.
§Function Flow
- Fee Collection: Collects accrued fees before processing the deposit.
- Validation: Checks that the lengths of
amounts_desiredandamounts_minmatch the vault’s assets. - Share Calculation: Calculates
shares_to_mintbased on the vault’s total managed funds and the deposit amount. - Asset Transfer: Transfers each specified amount from the user’s address to the vault as idle funds.
- Vault Shares Minting: Mints vault shares for the user to represent their ownership in the vault.
- Investment Execution: If
investistrue, generates and executes the investment allocations for the deposited funds.- Allocates funds across strategies proportionally to their current state.
- Executes the investment to transition idle funds into the vault’s strategies.
§Notes
- For the first deposit, if the vault has only one asset, shares are calculated directly based on the deposit amount.
- For multiple assets, the function delegates to
calculate_deposit_amounts_and_shares_to_mintfor precise share computation. - An event is emitted to log the deposit, including the actual deposited amounts and minted shares.
- If
investisfalse, deposited funds remain idle, allowing for manual investment at a later time.
§Errors
- Returns a
ContractErrorif any validation or execution step fails.
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_withdraw() -> [u8; 1132]
pub const fn spec_xdr_withdraw() -> [u8; 1132]
Handles the withdrawal process for a specified number of vault shares.
This function performs the following steps:
- Validates the environment and the inputs:
- Ensures the contract is initialized.
- Checks that the withdrawal amount (
withdraw_shares) is non-negative. - Verifies the authorization of the
fromaddress.
- Collects applicable fees.
- Calculates the proportionate withdrawal amounts for each asset based on the number of shares.
- Burns the specified shares from the user’s account.
- Processes the withdrawal for each asset:
- First attempts to cover the withdrawal amount using idle funds.
- If idle funds are insufficient, unwinds investments from the associated strategies to cover the remaining amount, accounting for rounding errors in the last strategy.
- Transfers the withdrawn funds to the user’s address (
from). - Emits an event to record the withdrawal details.
§Parameters:
e: The contract environment (Env).withdraw_shares: The number of vault shares to withdraw.from: The address initiating the withdrawal.
§Returns:
- A
Resultcontaining a vector of withdrawn amounts for each asset (Vec<i128>), or aContractErrorif the withdrawal fails.
§Errors:
ContractError::AmountOverTotalSupply: If the specified shares exceed the total supply.ContractError::ArithmeticError: If any arithmetic operation fails during calculations.ContractError::WrongAmountsLength: If there is a mismatch in asset allocation data.
§TODOs:
- Implement minimum amounts for withdrawals to ensure compliance with potential restrictions.
- Replace the returned vector with the original
asset_withdrawal_amountsmap for better structure. - avoid the usage of a Map, choose between using map or vector
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_emergency_withdraw() -> [u8; 712]
pub const fn spec_xdr_emergency_withdraw() -> [u8; 712]
Executes an emergency withdrawal from a specific strategy.
This function allows the emergency manager or manager to withdraw all assets from a particular strategy and store them as idle funds within the vault. It also pauses the strategy to prevent further use until unpaused.
§Arguments:
e- The environment.strategy_address- The address of the strategy to withdraw from.caller- The address initiating the emergency withdrawal (must be the manager or emergency manager).
§Returns:
Result<(), ContractError>- Ok if successful, otherwise returns a ContractError.
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_pause_strategy() -> [u8; 604]
pub const fn spec_xdr_pause_strategy() -> [u8; 604]
Pauses a strategy to prevent it from being used in the vault.
This function pauses a strategy by setting its paused field to true. Only the manager or emergency
manager can pause a strategy.
§Arguments:
e- The environment.strategy_address- The address of the strategy to pause.caller- The address initiating the pause (must be the manager or emergency manager).
§Returns:
Result<(), ContractError>- Ok if successful, otherwise returns a ContractError.
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_unpause_strategy() -> [u8; 572]
pub const fn spec_xdr_unpause_strategy() -> [u8; 572]
Unpauses a previously paused strategy.
This function unpauses a strategy by setting its paused field to false, allowing it to be used
again in the vault.
§Arguments:
e- The environment.strategy_address- The address of the strategy to unpause.caller- The address initiating the unpause (must be the manager or emergency manager).
§Returns:
Result<(), ContractError>- Ok if successful, otherwise returns a ContractError.
source§impl DeFindexVault
impl DeFindexVault
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_fetch_total_managed_funds() -> [u8; 456]
pub const fn spec_xdr_fetch_total_managed_funds() -> [u8; 456]
Returns the total managed funds of the vault, including both invested and idle funds.
This function provides a map where the key is the asset address and the value is the total amount of that asset being managed by the vault.
§Arguments:
e- The environment.
§Returns:
Map<Address, i128>- A map of asset addresses to their total managed amounts.
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_fetch_current_invested_funds() -> [u8; 440]
pub const fn spec_xdr_fetch_current_invested_funds() -> [u8; 440]
Returns the current invested funds, representing the total assets allocated to strategies.
This function provides a map where the key is the asset address and the value is the total amount of that asset currently invested in various strategies.
§Arguments:
e- The environment.
§Returns:
Map<Address, i128>- A map of asset addresses to their total invested amounts.
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_fetch_current_idle_funds() -> [u8; 440]
pub const fn spec_xdr_fetch_current_idle_funds() -> [u8; 440]
Returns the current idle funds, representing the total assets held directly by the vault (not invested).
This function provides a map where the key is the asset address and the value is the total amount of that asset held as idle funds within the vault.
§Arguments:
e- The environment.
§Returns:
Map<Address, i128>- A map of asset addresses to their total idle amounts.
source§impl DeFindexVault
impl DeFindexVault
This function extends the contract’s time-to-live and calculates how much of each asset corresponds
per the provided number of vault shares (vault_shares). It provides proportional allocations for each asset
in the vault relative to the specified shares.
§Arguments
e- The current environment reference.vault_shares- The number of vault shares for which the corresponding asset amounts are calculated.
§Returns
Map<Address, i128>- A map containing each asset address and its corresponding proportional amount.
source§impl DeFindexVault
impl DeFindexVault
pub const fn spec_xdr_get_fees() -> [u8; 44]
source§impl DeFindexVault
impl DeFindexVault
pub const fn spec_xdr_collect_fees() -> [u8; 68]
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_set_fee_receiver() -> [u8; 448]
pub const fn spec_xdr_set_fee_receiver() -> [u8; 448]
Sets the fee receiver for the vault.
This function allows the manager or emergency manager to set a new fee receiver address for the vault.
§Arguments:
e- The environment.caller- The address initiating the change (must be the manager or emergency manager).vault_fee_receiver- The new fee receiver address.
§Returns:
()- No return value.
source§impl DeFindexVault
impl DeFindexVault
source§impl DeFindexVault
impl DeFindexVault
source§impl DeFindexVault
impl DeFindexVault
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_set_emergency_manager() -> [u8; 364]
pub const fn spec_xdr_set_emergency_manager() -> [u8; 364]
source§impl DeFindexVault
impl DeFindexVault
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_invest() -> [u8; 1160]
pub const fn spec_xdr_invest() -> [u8; 1160]
Executes the investment of the vault’s idle funds based on the specified asset allocations. This function allows partial investments by providing an optional allocation for each asset, and it ensures proper authorization and validation checks before proceeding with investments.
§Arguments
e- The current environment reference.asset_investments- A vector of optionalAssetInvestmentAllocationstructures, where each element represents an allocation for a specific asset. The vector must match the number of vault assets in length.
§Returns
Result<(), ContractError>- ReturnsOk(())if the investments are successful or aContractErrorif any issue occurs during validation or execution.
§Function Flow
- Extend Instance TTL: Extends the contract instance’s time-to-live to keep the instance active.
- Check Initialization: Verifies that the vault is properly initialized before proceeding.
- Access Control: Ensures the caller has the
Managerrole required to initiate investments. - Asset Count Validation: Verifies that the length of the
asset_investmentsvector matches the number of assets managed by the vault. If they don’t match, aWrongInvestmentLengtherror is returned. - Investment Execution: Calls the
check_and_execute_investmentsfunction to perform the investment after validating the inputs and ensuring correct execution flows for each asset allocation.
§Errors
- Returns
ContractError::WrongInvestmentLengthif the length ofasset_investmentsdoes not match the vault assets. - Returns
ContractErrorif access control validation fails or if investment execution encounters an issue.
§Security
- Only addresses with the
Managerrole can call this function, ensuring restricted access to managing investments.
source§impl DeFindexVault
impl DeFindexVault
sourcepub const fn spec_xdr_rebalance() -> [u8; 428]
pub const fn spec_xdr_rebalance() -> [u8; 428]
Auto Trait Implementations§
impl Freeze for DeFindexVault
impl RefUnwindSafe for DeFindexVault
impl Send for DeFindexVault
impl Sync for DeFindexVault
impl Unpin for DeFindexVault
impl UnwindSafe for DeFindexVault
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,
type Error = E
fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W), ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>
§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,
type Error = E
fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X), ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.