256
Market pool identifier
shares
uint256
LT shares to withdraw (max_value(uint256) for all)
crvusd_from_wallet
bool
If True, pull crvUSD from caller instead of redeeming from savings vault (default: False)
📄 View Source Code (condensed)
@external
def
emergency_withdraw
(
pool_id
:
uint256
,
shares
:
uint256
,
crvusd_from_wallet
:
bool
=
False
)
:
assert
self
.
owner
==
msg
.
sender
,
"Access"
# ... (resolves lt_shares, handles crvusd source)
assets
:
uint256
=
(
extcall market
.
lt
.
emergency_withdraw
(
lt_shares
,
self
,
self
)
)
[
0
]
# ... (restores allocation, updates factory required)
See source for full oracle-fallback branching.
stake()
Description:
Stake LT shares in the market's gauge to earn YB emissions. Only callable by
owner
.
Returns:
uint256
- Amount of staked (gauge) shares received.
Input
Type
Description
pool_id
uint256
Market pool identifier
pool_shares
uint256
Amount of LT shares to stake
📄 View Source Code
@external
def
stake
(
pool_id
:
uint256
,
pool_shares
:
uint256
)
-
>
uint256
:
assert
self
.
owner
==
msg
.
sender
,
"Access"
market
:
Market
=
staticcall FACTORY
.
markets
(
pool_id
)
assert
market
.
lt
.
address
!=
empty
(
address
)
,
"Bad pool_id"
return
extcall market
.
staker
.
deposit
(
pool_shares
,
self
)
unstake()
Description:
Unstake shares from the market's gauge, receiving LT shares back. Only callable by
owner
.
Returns:
uint256
- Amount of LT shares received.
Input
Type
Description
pool_id
uint256
Market pool identifier
gauge_shares
uint256
Amount of staked shares to unstake
📄 View Source Code
@external
def
unstake
(
pool_id
:
uint256
,
gauge_shares
:
uint256
)
-
>
uint256
:
assert
self
.
owner
==
msg
.
sender
,
"Access"
market
:
Market
=
staticcall FACTORY
.
markets
(
pool_id
)
assert
market
.
lt
.
address
!=
empty
(
address
)
,
"Bad pool_id"
return
extcall market
.
staker
.
redeem
(
gauge_shares
,
self
,
self
)
claim_reward()
Description:
Claim rewards from all staked positions and send to the vault owner. Iterates over all
used_vaults
and calls
claim()
on each staker contract.
Anyone can call this function
— no owner check. Rewards are always sent to the vault
owner
, making this safe for keeper/bot automation.
Returns:
uint256
- Total amount claimed and transferred to owner.
Input
Type
Description
token
IERC20
The reward token to claim
📄 View Source Code
@external
def
claim_reward
(
token
:
IERC20
)
-
>
uint256
:
total
:
uint256
=
0
for
pool_id
:
uint256
in
self
.
used_vaults
:
market
:
Market
=
staticcall FACTORY
.
markets
(
pool_id
)
success
:
bool
=
False
res
:
Bytes
[
32
]
=
empty
(
Bytes
[
32
]
)
success
,
res
=
raw_call
(
market
.
staker
.
address
,
abi_encode
(
token
.
address
,
self
,
method_id
=
method_id
(
"claim(address,address)"
)
)
,
max_outsize
=
32
,
revert_on_failure
=
False
)
if
success
:
total
+=
convert
(
res
,
uint256
)
if
total
>
0
:
assert
extcall token
.
transfer
(
self
.
owner
,
total
,
default_return_value
=
True
)
return
total
preview_claim_reward()
Description:
Preview total claimable rewards across all staked positions. View function.
Returns:
uint256
- Total claimable amount of the reward token.
Input
Type
Description
token
IERC20
The reward token to query
📄 View Source Code
@external
@view
def
preview_claim_reward
(
token
:
IERC20
)
-
>
uint256
:
total
:
uint256
=
0
for
pool_id
:
uint256
in
self
.
used_vaults
:
market
:
Market
=
staticcall FACTORY
.
markets
(
pool_id
)
success
:
bool
=
False
res
:
Bytes
[
32
]
=
empty
(
Bytes
[
32
]
)
success
,
res
=
raw_call
(
market
.
staker
.
address
,
abi_encode
(
token
.
address
,
self
,
method_id
=
method_id
(
"preview_claim(address,address)"
)
)
,
max_outsize
=
32
,
is_static_call
=
True
,
revert_on_failure
=
False
)
if
success
:
total
+=
convert
(
res
,
uint256
)
return
total
deposit_crvusd()
Description:
Deposit crvUSD into the crvUSD savings vault. Pulls crvUSD from the caller via
transferFrom
. Anyone can call to add backing.
Returns:
uint256
- Amount of crvUSD vault shares received.
Input
Type
Description
assets
uint256
Amount of crvUSD to deposit
📄 View Source Code
@external
def
deposit_crvusd
(
assets
:
uint256
)
-
>
uint256
:
return
self
.
_deposit_crvusd
(
assets
)
redeem_crvusd()
Description:
Redeem crvUSD vault shares for crvUSD. Reverts if withdrawal would leave insufficient backing for active positions. Only callable by
owner
.
Returns:
uint256
- Amount of crvUSD withdrawn.
Input
Type
Description
shares
uint256
Amount of crvUSD vault shares to redeem
📄 View Source Code
@external
def
redeem_crvusd
(
shares
:
uint256
)
-
>
uint256
:
assert
self
.
owner
==
msg
.
sender
,
"Access"
return
self
.
_redeem_crvusd
(
shares
,
msg
.
sender
)
deposit_scrvusd()
/
withdraw_scrvusd()
Description:
Deposit or withdraw crvUSD vault shares (e.g., scrvUSD) directly via
transferFrom
/
transfer
. Withdraw reverts if it would leave insufficient backing. Deposit is open to anyone; withdraw is
owner
-only.
📄 View Source Code
@external
def
deposit_scrvusd
(
shares
:
uint256
)
:
extcall self
.
crvusd_vault
.
transferFrom
(
msg
.
sender
,
self
,
shares
)
@external
def
withdraw_scrvusd
(
shares
:
uint256
)
:
assert
self
.
owner
==
msg
.
sender
,
"Access"
extcall self
.
crvusd_vault
.
transfer
(
msg
.
sender
,
shares
)
assert
self
.
_crvusd_available
(
)
>=
self
.
_downscale
(
self
.
_required_crvusd
(
)
)
,
"Not enough crvUSD left"
set_crvusd_vault()
Description:
Change the crvUSD vault used by this HybridVault. Only possible when there are no active positions. Old vault shares are returned to the owner (redeemed as crvUSD by default, or transferred as shares if
redeem=False
). The new vault must be on the governance-approved allowlist. Only callable by
owner
.
Emits:
SetCrvusdVault
.
Input
Type
Description
new_vault
IERC4626
New crvUSD vault to use (must be in allowed list)
redeem
bool
If True, redeem old vault shares for crvUSD; if False, transfer shares directly (default: True)
📄 View Source Code
@external
def
set_crvusd_vault
(
new_vault
:
IERC4626
,
redeem
:
bool
=
True
)
:
assert
msg
.
sender
==
self
.
owner
,
"Only owner"
assert
staticcall VAULT_FACTORY
.
allowed_crvusd_vaults
(
new_vault
.
address
)
,
"Vault not allowed"
assert
len
(
self
.
used_vaults
)
==
0
,
"Has active positions"
old_vault
:
IERC4626
=
self
.
crvusd_vault
if
old_vault
!=
empty
(
IERC4626
)
:
extcall VAULT_FACTORY
.
update_vault_required
(
old_vault
.
address
,
0
,
False
)
shares
:
uint256
=
staticcall old_vault
.
balanceOf
(
self
)
if
shares
>
0
:
if
redeem
:
extcall old_vault
.
redeem
(
shares
,
msg
.
sender
,
self
)
else
:
extcall old_vault
.
transfer
(
msg
.
sender
,
shares
)
extcall CRVUSD
.
approve
(
old_vault
.
address
,
0
)
self
.
crvusd_vault
=
new_vault
extcall CRVUSD
.
approve
(
new_vault
.
address
,
max_value
(
uint256
)
)
log SetCrvusdVault
(
vault
=
new_vault
)
set_personal_limit()
Description:
Set a personal pool limit for this vault, added to the global pool limit. Only callable by
ADMIN
via
VAULT_FACTORY.ADMIN()
.
Emits:
SetPersonalLimit
.
Input
Type
Description
pool_id
uint256
Market pool identifier
limit
uint256
Personal limit to set
📄 View Source Code
@external
def
set_personal_limit
(
pool_id
:
uint256
,
limit
:
uint256
)
:
assert
msg
.
sender
==
staticcall VAULT_FACTORY
.
ADMIN
(
)
,
"Only admin"
self
.
personal_limit
[
pool_id
]
=
limit
log SetPersonalLimit
(
pool_id
=
pool_id
,
limit
=
limit
)
recover_tokens()
Description:
Recover accidentally sent tokens. Cannot recover LT or staker tokens that are actively in use, nor the crvUSD vault shares. Only callable by
owner
.
Input
Type
Description
token
IERC20
The token to recover
📄 View Source Code
@external
def
recover_tokens
(
token
:
IERC20
)
:
assert
self
.
owner
==
msg
.
sender
,
"Access"
assert
not
self
.
token_in_use
[
token
.
address
]
and
token
.
address
!=
self
.
crvusd_vault
.
address
,
"Token not allowed"
assert
extcall token
.
transfer
(
msg
.
sender
,
staticcall token
.
balanceOf
(
self
)
,
default_return_value
=
True
)
safe_to_deposit()
Description:
Check whether it is safe to deposit into a given market. Returns False if the AMM's debt/collateral ratio would be outside safe bounds after the deposit. View function.
Returns:
bool
- True if within safe limits.
Input
Type
Description
pool_id
uint256
Market pool identifier
assets
uint256
Amount of assets to deposit (0 to check current state)
debt
uint256
Amount of debt to take on (0 to check current state)
📄 View Source Code
@external
@view
def
safe_to_deposit
(
pool_id
:
uint256
,
assets
:
uint256
,
debt
:
uint256
)
-
>
bool
:
return
self
.
_check_safe_limits
(
staticcall FACTORY
.
markets
(
pool_id
)
,
assets
,
debt
)
View helpers
pool_limits(pool_id)
— effective limit (personal + global factory).
required_crvusd()
— total downscaled crvUSD required across all positions.
raw_required_crvusd_for(pool_id, assets, debt, downscale=True) → uint256
— crvUSD required for a potential deposit. Returns a single
uint256
(the post-downscale amount when
downscale=True
, raw amount when
False
). The internal helper
_required_crvusd_for
returns the
(pool_value, additional_crvusd)
pair; this external view exposes only the second leg.
crvusd_for_deposit(pool_id, assets, debt)
— additional crvUSD user must provide.
withdrawable_crvusd_for(pool_id, shares, is_staked)
— crvUSD freed up by withdrawing shares.
assets_for_crvusd(pool_id, crvusd_amount)
— corresponding assets amount for a crvUSD input.
Related
Core: HybridVaultFactory
— deploys and governs this vault.
Core: Factory
— YB core markets.
Core: LT
,
Core: AMM
— target markets.
User: Hybrid Vaults
— user-level explainer.
User: Use Hybrid Vaults
— task walkthrough.
Overview
System Architecture
Function Documentation
Public Variables
Data Structs
Events
initialize()
deposit()
withdraw()
emergency_withdraw()
stake()
unstake()
claim_reward()
preview_claim_reward()
deposit_crvusd()
redeem_crvusd()
deposit_scrvusd()
/
withdraw_scrvusd()
set_crvusd_vault()
set_personal_limit()
recover_tokens()
safe_to_deposit()
View helpers
Related
## /dev/core/hybrid-vault-factory
On this page
The source code of the
HybridVaultFactory.vy
contract can be found on
GitHub
. The contract is written with
Vyper
version 0.4.3.
Overview
The
HybridVaultFactory
deploys and manages HybridVault instances. Each address can create exactly one vault. The factory also holds global configuration that vaults read at runtime: the stablecoin fraction, per-pool deposit limits, allowed crvUSD vaults, and per-vault crvUSD tracking for enforcing global caps.
Key responsibilities:
Vault deployment:
Deploys minimal proxies of the vault implementation and initializes them with the user's chosen crvUSD vault.
Stablecoin fraction:
Configures what fraction of position value must be backed by crvUSD (default 55%).
Pool limits:
Sets global maximum deposit sizes per pool. Note: the effective limit for a given vault is
personal_limit[pool_id] + factory.pool_limits(pool_id)
— each vault can also have a personal limit boost set by admin via
HybridVault.set_personal_limit()
.
crvUSD vault allowlist:
Controls which ERC-4626 crvUSD vaults (e.g., scrvUSD) can be used, with optional global deposit limits per vault.
Required crvUSD tracking:
Tracks each vault's required crvUSD and enforces global limits per crvUSD vault (
crvusd_vault_total_required
).
Stablecoin allocation forwarding:
Vaults call
lt_allocate_stablecoins()
through the factory, which forwards to the YB Factory's admin.
Function Documentation
Public Variables
Immutable Variables
FACTORY
(
Factory
) - YB Factory contract
ADMIN
(
address
) - Admin address, derived via a two-hop chain at deploy time:
factory.admin().ADMIN()
(Factory → HybridFactoryOwner → ADMIN)
State Variables
vault_impl
(
address
) - Current vault implementation for new deployments
user_to_vault
(
HashMap[address, HybridVault]
) - Maps user address to vault
vault_to_user
(
HashMap[HybridVault, address]
) - Maps vault to user address
stablecoin_fraction
(
uint256
) - Required crvUSD backing as fraction of 1e18 (default: 55e16)
pool_limits
(
HashMap[uint256, uint256]
) - Global deposit limit per pool ID
allowed_crvusd_vaults
(
HashMap[address, bool]
) - Allowlist for crvUSD vaults
crvusd_vault_limits
(
HashMap[address, uint256]
) - Max total required crvUSD per crvUSD vault (0 = unlimited)
crvusd_vault_total_required
(
HashMap[address, uint256]
) - Current total required crvUSD across all vaults using a given crvUSD vault
crvusd_vault_required
(
HashMap[HybridVault, uint256]
) - Required crvUSD tracked per individual vault
Events
VaultCreated
—
user
(indexed),
vault
(indexed)
SetVaultImpl
—
impl
SetStablecoinFraction
—
stablecoin_fraction
SetPoolLimit
—
pool_id
,
limit
SetAllowedCrvusdVault
—
vault
,
allowed
SetCrvusdVaultLimit
—
crvusd_vault
,
limit
create_vault()
Description:
Create a new HybridVault for the caller. Deploys a minimal proxy pointing to
vault_impl
and initializes it with the caller's chosen crvUSD vault. Each address can only create one vault.
Returns:
HybridVault
.
Emits:
VaultCreated
.
Input
Type
Description
crvusd_vault
address
The crvUSD vault (e.g., scrvUSD) to use for this vault
📄 View Source Code
@external
def
create_vault
(
crvusd_vault
:
address
)
-
>
HybridVault
:
assert
self
.
user_to_vault
[
msg
.
sender
]
==
empty
(
HybridVault
)
,
"Already created"
assert
self
.
allowed_crvusd_vaults
[
crvusd_vault
]
,
"Vault not allowed"
assert
self
.
vault_impl
!=
empty
(
address
)
,
"Vault impl not set"
vault
:
HybridVault
=
HybridVault
(
create_minimal_proxy_to
(
self
.
vault_impl
)
)
extcall vault
.
initialize
(
msg
.
sender
,
crvusd_vault
)
self
.
user_to_vault
[
msg
.
sender
]
=
vault
self
.
vault_to_user
[
vault
]
=
msg
.
sender
log VaultCreated
(
user
=
msg
.
sender
,
vault
=
vault
)
return
vault
set_vault_impl()
Description:
Update the vault implementation used for new vault deployments. Does not affect existing vaults. Only callable by
ADMIN
.
Emits:
SetVaultImpl
.
Input
Type
Description
impl
address
New vault implementation address
📄 View Source Code
@external
def
set_vault_impl
(
impl
:
address
)
:
assert
msg
.
sender
==
ADMIN
,
"Access"
self
.
vault_impl
=
impl
log SetVaultImpl
(
impl
=
impl
)
set_stablecoin_fraction()
Description:
Set the target fraction of deposits to be held as stablecoins (scrvUSD). Value is in 18-decimal precision (e.g., 55e16 = 55%). Only callable by
ADMIN
.
Emits:
SetStablecoinFraction
.
Input
Type
Description
frac
uint256
The stablecoin fraction as a fraction of 1e18
📄 View Source Code
@external
def
set_stablecoin_fraction
(
frac
:
uint256
)
:
assert
msg
.
sender
==
ADMIN
,
"Access"
self
.
stablecoin_fraction
=
frac
log SetStablecoinFraction
(
stablecoin_fraction
=
frac
)
set_pool_limit()
Description:
Set the global deposit limit for a specific pool. Only callable by
ADMIN
.
Emits:
SetPoolLimit
.
Input
Type
Description
pool_id
uint256
The identifier of the pool
pool_limit
uint256
The maximum deposit amount allowed for the pool
📄 View Source Code
@external
def
set_pool_limit
(
pool_id
:
uint256
,
pool_limit
:
uint256
)
:
assert
msg
.
sender
==
ADMIN
,
"Access"
self
.
pool_limits
[
pool_id
]
=
pool_limit
log SetPoolLimit
(
pool_id
=
pool_id
,
limit
=
pool_limit
)
set_allowed_crvusd_vault()
Description:
Enable or disable a crvUSD vault for use, with an optional deposit limit. A limit of 0 means unlimited. Only callable by
ADMIN
.
Caution:
The
limit
parameter defaults to 0 (unlimited). Calling this function without explicitly passing a limit will silently reset any previously set limit to 0. If toggling
allowed
on/off, be sure to re-pass the desired limit value.
Emits:
SetAllowedCrvusdVault
,
SetCrvusdVaultLimit
.
Input
Type
Description
vault
address
The crvUSD vault address
allowed
bool
Whether the vault is allowed
limit
uint256
Maximum total required crvUSD across all HybridVaults using this vault (default: 0)
📄 View Source Code
@external
def
set_allowed_crvusd_vault
(
vault
:
address
,
allowed
:
bool
,
limit
:
uint256
=
0
)
:
assert
msg
.
sender
==
ADMIN
,
"Access"
self
.
allowed_crvusd_vaults
[
vault
]
=
allowed
self
.
crvusd_vault_limits
[
vault
]
=
limit
log SetAllowedCrvusdVault
(
vault
=
vault
,
allowed
=
allowed
)
log SetCrvusdVaultLimit
(
crvusd_vault
=
vault
,
limit
=
limit
)
set_crvusd_vault_limit()
Description:
Set the total deposit limit for a crvUSD vault across all HybridVaults. 0 means no limit. Only callable by
ADMIN
.
Emits:
SetCrvusdVaultLimit
.
Input
Type
Description
crvusd_vault
address
The crvUSD vault address
limit
uint256
The maximum total required crvUSD allowed
📄 View Source Code
@external
def
set_crvusd_vault_limit
(
crvusd_vault
:
address
,
limit
:
uint256
)
:
assert
msg
.
sender
==
ADMIN
,
"Access"
self
.
crvusd_vault_limits
[
crvusd_vault
]
=
limit
log SetCrvusdVaultLimit
(
crvusd_vault
=
crvusd_vault
,
limit
=
limit
)
update_vault_required()
Description:
Update the tracked required crvUSD for a HybridVault. On increase, enforces the global vault limit if
check_limit=True
. On decrease, reduces the global total. Only callable by registered HybridVaults.
Note:
When
check_limit=True
, the function also asserts
allowed_crvusd_vaults[crvusd_vault]
— if a crvUSD vault has been removed from the allowlist, no vault can increase its requirement (reverts with
"Vault wind-down"
). Vaults can only decrease their requirement in this state.
Input
Type
Description
crvusd_vault
address
The crvUSD vault address the HybridVault uses
new_required
uint256
The new total required crvUSD for this HybridVault
check_limit
bool
If True, revert when increase would exceed the vault limit (default: True)
📄 View Source Code
@external
def
update_vault_required
(
crvusd_vault
:
address
,
new_required
:
uint256
,
check_limit
:
bool
=
True
)
:
hybrid_vault
:
HybridVault
=
HybridVault
(
msg
.
sender
)
assert
self
.
vault_to_user
[
hybrid_vault
]
!=
empty
(
address
)
,
"Only vaults can call"
old_required
:
uint256
=
self
.
crvusd_vault_required
[
hybrid_vault
]
if
new_required
>
old_required
:
increase
:
uint256
=
new_required
-
old_required
total
:
uint256
=
self
.
crvusd_vault_total_required
[
crvusd_vault
]
+
increase
if
check_limit
:
assert
self
.
allowed_crvusd_vaults
[
crvusd_vault
]
,
"Vault wind-down"
vault_limit
:
uint256
=
self
.
crvusd_vault_limits
[
crvusd_vault
]
if
vault_limit
>
0
:
assert
total
<=
vault_limit
,
"Beyond vault limit"
self
.
crvusd_vault_total_required
[
crvusd_vault
]
=
total
elif
new_required
<
old_required
:
decrease
:
uint256
=
old_required
-
new_required
self
.
crvusd_vault_total_required
[
crvusd_vault
]
-=
decrease
self
.
crvusd_vault_required
[
hybrid_vault
]
=
new_required
lt_allocate_stablecoins()
Description:
Allocate stablecoins to a liquidity token via the factory admin. Calls
FACTORY.admin()
which returns the
HybridFactoryOwner
contract, then calls
lt_allocate_stablecoins()
on it. The HybridFactoryOwner checks if the caller is the ADMIN or a registered
limit_setter
. Only callable by registered HybridVaults.
Input
Type
Description
lt
address
The liquidity token address to allocate stablecoins for
limit
uint256
The allocation limit
📄 View Source Code
@external
def
lt_allocate_stablecoins
(
lt
:
address
,
limit
:
uint256
)
:
assert
self
.
vault_to_user
[
HybridVault
(
msg
.
sender
)
]
!=
empty
(
address
)
,
"Only vaults can call"
extcall
(
staticcall FACTORY
.
admi프로젝트로 돌아가기