Vault Module

Overview

The satay::vault module holds the code for vault logic.

Vaults are represented by the Vault struct, which holds the base_coin_type, base_coin_decimals, management_fee, performance_fee, debt_ratio, and total_debt of the vault.

The debt_ratio of a vault determines the total percentage of vault funds that can be deployed to strategies. In most cases, debt_ratio will be 10000, meaning all of the funds are available for strategies. The management_fee and performance_fee values are used to assess fees for the vault.

struct Vault has key {
    base_coin_type: TypeInfo,
    base_coin_decimals: u8,
    management_fee: u64,
    performance_fee: u64,
    debt_ratio: u64,
    total_debt: u64,
}

The VaultCapability struct holds a SignerCapability for a vault's resource account, unique vault_id, and the resource account's vault_addr.

struct VaultCapability has store, drop {    
    storage_cap: SignerCapability,    
    vault_id: u64,    
    vault_addr: address
}

The VaultCoin struct represents a share of a vault's holdings and is burned to claim a share of the pooled capital. There is a unique VaultCoin for each BaseCoin.

struct VaultCoin<phantom BaseCoin> has key {}

The MintCapability, BurnCapability, and FreezeCapability are stored in of a VaultCoin are stored in the vault's resource account.

struct VaultCoinCaps<phantom BaseCoin> has key {    
    mint_cap: MintCapability<VaultCoin<BaseCoin>>,    
    freeze_cap: FreezeCapability<VaultCoin<BaseCoin>>,    
    burn_cap: BurnCapability<VaultCoin<BaseCoin>>
}

The VaultStrategy resource stores information about a Vault's approved strategies, including its strategy_coin_type, debt_ratio, total_debt, information about its performance to date, and information used in harvest_trigger.

struct VaultStrategy<phantom StrategyType> has key, store {
    strategy_coin_type: TypeInfo,
    debt_ratio: u64,
    total_debt: u64,
    total_gain: u64,
    total_loss: u64,
    last_report: u64,
    max_report_delay: u64,
    force_harvest_trigger_once: bool,
    credit_threshold: u64
}

Access Control

All mutating functions for the vault are restricted to friend modules satay::satay and satay::base_strategy.

Satay Functions

Create Vault

new creates a new vault for BaseCoin managed by vault_owner with a unique vault_id. The function also initializes the management and performance fee amounts. This function is called by the satay::satay module. management_fee must not exceed MAX_MANAGEMENT_FEE_BPS and performance_fee must not exceed MAX_PERFORMANCE_FEE_BPS.

public(friend) fun new<BaseCoin>(
    vault_owner: &signer, 
    seed: vector<u8>, 
    vault_id: u64,
    management_fee: u64,
    performance_fee: u64
): VaultCapability

User Functions

deposit_as_user deposits base_coin of Coin<BaseCoin>into the vault, minting a calculated amount of Coin<VaultCoin<BaseCoin>> to user. This function is called by the satay::satay module.

public(friend) fun deposit_as_user<BaseCoin>(    
    user: &signer,    
    vault_cap: &VaultCapability,    
    base_coin: Coin<BaseCoin>
)

withdraw_as_user burns amount of Coin<VaultCoin<BaseCoin>>, returning a calculated amount of Coin<BaseCoin> to user. This function is called by the satay::satay module.

public(friend) fun withdraw_as_user<BaseCoin>(    
    user: &signer,    
    vault_cap: &VaultCapability,    
    amount: u64
): Coin<BaseCoin>

Admin Functions

approve_strategy creates a VaultStrategy<StrategyType> resource and moves it to the vault resource account referenced by vault_cap. vault.debt_ratio + debt_ratio must not exceed MAX_DEBT_RATIO_BPS.

public(friend) fun approve_strategy<StrategyType: drop>(    
    vault_cap: &VaultCapability,    
    strategy_coin_type: TypeInfo,    
    debt_ratio: u64
)

update_fee updates the fee vault.management_fee and vault.performance_fee for the vault referenced by vault_cap. management_fee must not exceed MAX_MANAGEMENT_FEE_BPS and performance_fee must not exceed MAX_PERFORMANCE_FEE_BPS.

public(friend) fun update_fee(    
    vault_cap: &VaultCapability,    
    management_fee: u64,    
    performance_fee: u64
)

update_strategy_debt_ratio updates the debt_ratio of StrategyType. vault.debt_ratio - old_debt_ratio + debt_ratio must not exceed MAX_DEBT_RATIO_BPS.

public(friend) fun update_strategy_debt_ratio<StrategyType: drop>(    
    vault_cap: &VaultCapability,    
    debt_ratio: u64
): u64

update_strategy_max_report_delay updates the strategy.max_report_delay value for VaultStrategy<StrategyType>.

public(friend) fun update_strategy_max_report_delay<StrategyType: drop>(    
    vault_cap: &VaultCapability,    
    max_report_delay: u64
)

update_strategy_credit_threshold updates the strategy.credit_threshold value for VaultStrategy<StrategyType>.

public(friend) fun update_strategy_credit_threshold<StrategyType: drop>(    
    vault_cap: &VaultCapability,    
    credit_threshold: u64
)

set_strategy_force_harvest_trigger_once updates the strategy.force_harvest_trigger value for VaultStrategy<StrategyType> to true.

public(friend) fun set_strategy_force_harvest_trigger_once<StrategyType: drop>(    
    vault_cap: &VaultCapability
)

Base Strategy Functions

Add Coin

add_coin<CoinType> creates a CoinStore<CoinType> resource for the vault, allowing it to accept Coin<CoinType> deposits.

public(friend) fun add_coin<CoinType>(    
    vault_cap: &VaultCapability
)

Deposit/Withdraw

deposit sends coin to the vault without minting VaultCoin<CoinType>

public(friend) fun deposit<CoinType>(
    vault_cap: &VaultCapability, 
    coin: Coin<CoinType>
)

withdraw extracts amount of Coin<CoinType> from the vault without burning Coin<VaultCoin<CoinType>>.

public(friend) fun withdraw<CoinType>(
    vault_cap: &VaultCapability, 
    amount: u64
): Coin<CoinType>

Fees

assess_fees charges the management and performance fees on a reported gain from StrategyType.

public(friend) fun assess_fees<StrategyType: drop, BaseCoin>(    
    gain: u64,    
    delegated_assets: u64,    
    vault_cap: &VaultCapability,   
    _witness: StrategyType
 )

Reporting

update_total_debt incrementstotal_debt of VaultStrategy<StrategyType> and Vault by credit - debt_payment.

public(friend) fun update_total_debt<StrategyType: drop>(
    vault_cap: &mut VaultCapability, 
    credit: u64, 
    debt_payment: u64
)

report_timestamp updates the last_report value of VaultStrategy<StrategyType> to the current timestamp.

public(friend) fun report_timestamp<StrategyType: drop>(
    vault_cap: &mut VaultCapability
)

report_gain increments total_gain value of VaultStrategy<StrategyType>.

public(friend) fun report_gain<StrategyType: drop>(
    vault_cap: &mut VaultCapability, 
    profit: u64
)

report_loss decrements total_debt, debt_ratio and increments total_loss on VaultStrategy<StrategyType>. Decrements total_debt and debt_ratio on Vault.

public(friend) fun report_loss<StrategyType: drop>(
    vault_cap: &mut VaultCapability, 
    loss: u64
)

Getter Functions

Gets the total assets of the vault, including liquid and deployed CoinType.

public fun total_assets<CoinType>(
    vault_cap: &VaultCapability
): u64

Gets the amount of remaining BaseCoin credit StrategyType can withdraw.

public fun credit_available<StrategyType: drop, CoinType>(
    vault_cap: &VaultCapability
): u64

Determines if StrategyType is past its debt limit. If StrategyType is above its limit, return the amount over the limit.

public fun debt_out_standing<StrategyType: drop, CoinType>(
    vault_cap: &VaultCapability
): u64

Calculates the amount of BaseCoin to return given an amount of VaultCoin<BaseCoin> to burn.

public fun calculate_base_coin_amount_from_share<BaseCoin>(
    vault_cap: &VaultCapability, 
    share: u64
): u64

Gets the vault address of a vault, given its VaultCapability resource.

public fun get_vault_addr(
    vault_cap: &VaultCapability
): address

Gets the strategy.strategy_coin_type from VaultStrategy<StrategyType> of Vault accessed by vault_cap.

public fun get_strategy_coin_type<StrategyType: drop>(
    vault_cap: &VaultCapability
): TypeInfo

Last updated