Beefy
  • Overview
  • Get Started
    • How to set up a wallet
    • Funding your wallet
    • Connecting your wallet to Beefy
  • Beefy Ecosystem
    • Introduction to Beefy
    • Beefy Protocol
      • Revenue Bridge
      • Fee Batch
      • Incentive Programmes
    • $BIFI Token
      • Token Bridge
    • Beefy Bulletins
      • What is Beefy?
      • The Big Beefy Opportunity
      • What Makes Beefy Different?
      • How Does Beefy Work?
      • Beefy Fees Breakdown
      • Why Beefy Beats Your Bank
      • Introducing Beefy's Unique Revenue Share Model
      • Beefy's Coveted Advantages Revealed
  • Beefy Products
    • Vaults
    • Strategies
    • CLM
    • Boost
    • Beefy-escrowed Tokens
      • beS
      • beQI
      • Deprecated Products
        • beFTM
        • binSPIRIT
        • beJOE
        • beVELO
        • beOPX
    • Advanced Vaults
      • GMX and GLP
  • Beefy DAO
    • Team & Goals
    • Contributor Compensation
    • Governance
    • Proposal Repository
    • Treasury
    • Cowmoonity
    • Partnerships
  • Safety
    • SAFU Standards
    • Contracts & Timelocks
    • Bug Bounty Program
    • Beefy Safety Score
    • Token Allowance
    • Beefy Backup
    • Insurance
  • FAQ
    • General
    • Infographics
    • mooVaults APY
    • How-To Guides
      • How to deposit in a Vault
      • How to Add a Custom Token to Metamask
      • How to Add and Remove Liquidity
      • How to use Beefy ZAP
      • How to add and switch networks on Beefy
      • How to check the harvesting and compounding rate of a vault
  • Developer Documentation
    • Vault Contract
    • Strategy Contract
      • StratFeeManager Contract
      • GasFeeThrottler Contract
    • Other Beefy Contracts
      • FeeConfigurator Contract
      • BeefyWrapper Contract
      • GaugeStaker Contract
    • Third Party Contracts
      • DelegateRegistry Contract
      • Oracle Contracts
    • Beefy API
  • Additional Resources
    • Contract Addresses
    • Code Repositories
    • Blog
    • Discord
    • Github
    • Media Kit
    • Telegram
    • Twitter
Powered by GitBook
On this page
  • View Functions
  • want()
  • balance()
  • available()
  • totalSupply()
  • getPricePerFullShare()
  • strategy()
  • Write Functions
  • deposit()
  • withdraw()
  • earn()
  • proposeStrat()
  • upgradeStrat()
  • BeefyVaultV7.sol

Was this helpful?

  1. Developer Documentation

Vault Contract

Last Update: February 2023

Last updated 1 year ago

Was this helpful?

The is the central user-facing implementation of the Beefy protocol, which accepts and manages user deposits and mints mooTokens as a proof of receipt to facilitate withdrawals. It follows the ERC-20 for fungible, transferrable tokens.

Besides handling deposits and withdrawals, the primary function of the vault is to direct deposited funds to the relevant autocompounding Strategy Contract. The vault and strategy contracts are kept separate to isolate any risks in the strategy from user deposits.

View Functions

want()

Returns the address of the underlying farm token (e.g. the LP token) used in both the Beefy Vault and Strategy contracts. Note that this is not the same as the underlying assets used for the farm.

function want() public view returns (IERC20Upgradeable) {
    return IERC20Upgradeable(strategy.want());
}

balance()

Returns the amount of "want" (i.e. underlying farm token) stored in the vault and strategy and yield source as an integer.

function balance() public view returns (uint) {
    return want().balanceOf(address(this)) + IStrategyV7(strategy).balanceOf();
}

available()

Returns the amount of "want" (i.e. underlying farm token) stored in the vault alone as an integer.

function available() public view returns (uint256) {
    return want().balanceOf(address(this));
}

totalSupply()

function totalSupply() public view virtual override returns (uint256) {
    return _totalSupply;
}

getPricePerFullShare()

Returns the current price per share of the vault (i.e. per mooToken) as an integer denominated in the "want" (i.e. underlying farm token). This is calculated as Price per Full Share = balance() / totalSupply().

function getPricePerFullShare() public view returns (uint256) {
    return totalSupply() == 0 ? 1e18 : balance() * 1e18 / totalSupply();
}

strategy()

Returns the address current underlying strategy contract that the vault is using to generate yield.

function strategy() external view returns (address);

Write Functions

deposit()

Executes a transfer of a specified _amount of "want" (i.e. underlying farm token) from the depositor to the vault, and then mints a proportional quantity of mooTokens to the depositor in return.

function deposit(uint _amount) public nonReentrant {
    strategy.beforeDeposit();
    uint256 _pool = balance();
    want().safeTransferFrom(msg.sender, address(this), _amount);
    earn();
    uint256 _after = balance();
    _amount = _after - _pool; // Additional check for deflationary tokens
    uint256 shares = 0;
    if (totalSupply() == 0) {
        shares = _amount;
    } else {
        shares = (_amount * totalSupply()) / _pool;
    }
    _mint(msg.sender, shares);
}

Additionally, there is a helper function depositAll() that deposits the entire balance of "want" in the user's wallet at the time of the transaction.

withdraw()

Executes a burn of a specified _amount of mooTokens from the depositor, and then transfers a proportional quantity of "want" (i.e. underlying farm token) to the depositor in return.

function withdraw(uint256 _shares) public {
    uint256 r = (balance() * _shares) / totalSupply();
    _burn(msg.sender, _shares);
    uint b = want().balanceOf(address(this));
    if (b < r) {
        uint _withdraw = r - b;
        strategy.withdraw(_withdraw);
        uint _after = want().balanceOf(address(this));
        uint _diff = _after - b;
        if (_diff < _withdraw) {
            r = b + _diff;
        }
    }
    want().safeTransfer(msg.sender, r);
}

Similarly to deposit(), there is a helper function withdrawAll() that withdraw the entire balance of mooTokens in the user's wallet at the time of the transaction.

earn()

Executes a transfer of available() "want" (i.e. underlying farm token) from the Vault Contract to the strategy contract and triggers the strategy's deposit() function to deploy the funds and begin earning.

function earn() public {
    uint _bal = available();
    want().safeTransfer(address(strategy), _bal);
    strategy.deposit();
}

proposeStrat()

Writes the address of an alternate strategy to the Vault Contract's memory, in anticipation of upgrade the current strategy to the alternate using upgradeStrat().

function proposeStrat(address _implementation) public onlyOwner {
    require(address(this) == IStrategyV7(_implementation).vault(), "Proposal not valid for this Vault");
    require(want() == IStrategyV7(_implementation).want(), "Different want");
    stratCandidate = StratCandidate({
        implementation: _implementation,
        proposedTime: block.timestamp
    });
    emit NewStratCandidate(_implementation);
}

upgradeStrat()

Replaces the address of the current strategy with an alternate strategy specified by proposeStrat().

function upgradeStrat() public onlyOwner {
    require(stratCandidate.implementation != address(0), "There is no candidate");
    require(stratCandidate.proposedTime + approvalDelay < block.timestamp, "Delay has not passed");
    emit UpgradeStrat(stratCandidate.implementation);
    strategy.retireStrat();
    strategy = IStrategyV7(stratCandidate.implementation);
    stratCandidate.implementation = address(0);
    stratCandidate.proposedTime = 5000000000;
    earn();
}

BeefyVaultV7.sol

  • Introduced vault upgradeability through proxy patterns, to facilitate updates and changes to live Beefy vaults without needing to deprecate and re-deploy;

  • Updated the strategy interface to allow for upgradeable strategies; and

  • Amended all contracts to remove reliance on the SafeMath library, which has been generally retired following incorporation of its features into Solidity v0.8.

Separately, a ERC-4646-compliant wrapper contract was released for the V7 vault in November 2022, which allows developers to incorporate Beefy Vaults into their projects with standardised vault functionality and interfaces. See BeefyWrapper Contract for more information.

Returns the total amount of mooTokens minted as an integer, which are always displayed as 18 decimal token. This is a standard method inherited from the ERC-20 standard. See for more details.

The current release of our standard Beefy Vault Contract is , which was in August 2022. The V7 release improved on the previous version in a few keys ways:

Beefy Vault Contract
standard
BeefyVaultV7.sol
released
What are mooTokens?