dress
New LT implementation address (optional)
virtual_pool
address
New Virtual Pool implementation address (optional)
price_oracle
address
New Price Oracle implementation address (optional)
staker
address
New Staker implementation address (optional)
📄 View Source Code
@external
def
set_implementations
(
amm
:
address
,
lt
:
address
,
virtual_pool
:
address
,
price_oracle
:
address
,
staker
:
address
)
:
assert
msg
.
sender
==
self
.
admin
,
"Access"
if
amm
!=
empty
(
address
)
:
self
.
amm_impl
=
amm
if
lt
!=
empty
(
address
)
:
self
.
lt_impl
=
lt
if
virtual_pool
!=
empty
(
address
)
:
self
.
virtual_pool_impl
=
virtual_pool
if
price_oracle
!=
empty
(
address
)
:
self
.
price_oracle_impl
=
price_oracle
if
staker
!=
empty
(
address
)
:
self
.
staker_impl
=
staker
log SetImplementations
(
amm
=
amm
,
lt
=
lt
,
virtual_pool
=
virtual_pool
,
price_oracle
=
price_oracle
,
staker
=
staker
)
Related
Core: LT
Core: AMM (LEVAMM)
Core: VirtualPool
Core: CryptopoolLPOracle
Contract Addresses
Overview
Function Documentation
Public Variables
Data Structs
Events
add_market()
fill_staker_vpool()
set_mint_factory()
set_allocator()
set_agg()
set_flash()
set_admin()
set_fee_receiver()
set_gauge_controller()
set_min_admin_fee()
set_implementations()
Related
## /dev/core/cryptopool-lp-oracle
On this page
The source code of the
CryptopoolLPOracle.vy
contract can be found on
GitHub
. The contract is written with
Vyper
version 0.4.3.
Overview
The
CryptopoolLPOracle
is a price oracle contract that provides LP token pricing for Curve cryptopools. It combines the LP token price from a Curve cryptopool with a price aggregator to return the LP token value in aggregated USD terms. This oracle is used by the Yield Basis protocol to determine the value of LP tokens for various calculations including debt limits, collateral values, and trading operations.
Oracle Principle and Reliability
The oracle operates on a
crvUSD price normalization principle
that enhances reliability and reduces manipulation risks:
Cryptopool components
— the oracle reads
POOL.virtual_price()
and
POOL.price_scale()
directly. It does
not
call
POOL.lp_price()
; the formula is computed locally to avoid depending on any per-pool helper.
Internal LP price
:
lp_price_internal = 2 · virtual_price · sqrt(price_scale · 1e18) / 1e18
. This is the Curve LP price formula for a 2-coin pool, expressed in pool units (numeraire =
coins(0)
, i.e. crvUSD).
crvUSD aggregation
: A price aggregator (
AGG
) provides the "real" price of crvUSD across multiple major liquidity pools.
Final price
:
price = lp_price_internal · AGG.price() / 1e18
— LP value in aggregated USD.
This approach is reliable because:
Curve pools are battle-tested
and the LP-price formula derives from the AMM invariant rather than an external helper.
crvUSD price aggregation
uses multiple major liquidity pools to avoid price swings from any single source.
Local computation
keeps the oracle independent of any future
POOL.lp_price()
behaviour change in the Twocrypto fork.
Staleness and update cadence
The oracle stores no price and has no update transaction to miss, so it cannot go stale in the push-feed (heartbeat) sense. Every call recomputes from live chain state:
virtual_price
/
price_scale
are read from the pool in the block of the call.
price_scale
is slow-moving by design — it shifts only when Cryptoswap's internal rebalance condition triggers.
AGG.price()
is the crvUSD aggregator's EMA over multiple liquidity pools. That EMA is the intentional lag in the system: it makes the feed sandwich-resistant, and it means the oracle tracks sharp crvUSD price moves with a delay rather than instantly.
price_w()
(used by state-mutating protocol calls) ticks the aggregator's EMA via
AGG.price_w()
, so routine protocol operations refresh the aggregator as a side effect. There is no keeper dependency.
There is
no explicit staleness check and no fallback feed in this contract
. If the aggregator's underlying pools stopped updating,
price()
would keep returning values computed from the last-observed aggregator state. The only hard validation on the aggregator is the Factory's
(
0.90
,
1.10
)
(0.90, 1.10)
(
0.90
,
1.10
)
band check at market init and on
set_agg
; routine operations do not re-validate the band (see
Factory
).
Function Documentation
Public Variables
Immutable Variables
POOL
(
Cryptopool
) - Curve cryptopool contract address (immutable)
AGG
(
PriceOracle
) - Price aggregator contract address (immutable)
lp_price()
(internal)
Description:
Internal helper that computes the LP token's value in pool-numeraire units (
coins(0)
= crvUSD) directly from
virtual_price
and
price_scale
. Never reverts; never reads
POOL.lp_price()
.
@internal
@view
def
lp_price
(
)
-
>
uint256
:
virtual_price
:
uint256
=
staticcall POOL
.
virtual_price
(
)
p_scale
:
uint256
=
staticcall POOL
.
price_scale
(
)
return
2
*
virtual_price
*
isqrt
(
p_scale
*
10
**
18
)
//
10
**
18
price()
Description:
Returns the current LP token price in aggregated USD terms. View function — multiplies the internal LP price by the crvUSD aggregator's USD value.
Returns:
uint256
- LP token price in aggregated USD (scaled by 10^18).
📄 View Source Code
@external
@view
def
price
(
)
-
>
uint256
:
return
self
.
lp_price
(
)
*
staticcall AGG
.
price
(
)
//
10
**
18
price_w()
Description:
Returns the current LP token price in aggregated USD terms. Mutates the aggregator's EMA tick — used when fresh pricing is needed for state-mutating calls.
Returns:
uint256
- LP token price in aggregated USD (scaled by 10^18).
📄 View Source Code
@external
def
price_w
(
)
-
>
uint256
:
return
self
.
lp_price
(
)
*
extcall AGG
.
price_w
(
)
//
10
**
18
Related
Core: AMM
— price oracle consumer.
User: Oracle Design
— user-level explainer.
Overview
Oracle Principle and Reliability
Staleness and update cadence
Function Documentation
Public Variables
lp_price()
(internal)
price()
price_w()
Related
## /dev/core/hybrid-vault
On this page
The source code of the
HybridVault.vy
contract can be found on
GitHub
. The contract is written with
Vyper
version 0.4.3.
Overview
The
HybridVault
combines YB market positions with a crvUSD savings vault (e.g., scrvUSD). Each user gets a personal vault instance (minimal proxy deployed by the
HybridVaultFactory
) that holds both their staked/unstaked LT positions across up to 16 markets and their crvUSD backing in a single contract.
The vault enforces a
stablecoin backing requirement
: the crvUSD held in the savings vault must cover a configurable fraction (
stablecoin_fraction
) of the total crvUSD-equivalent value of all active pool positions. This links pool access to peg-supporting crvUSD deposits.
Key mechanics:
Deposit flow:
User deposits assets + debt into a YB market through the vault. The vault verifies sufficient crvUSD backing, temporarily raises the market's stablecoin allocation cap to allow the LT deposit, then tightens it back down.
Stablecoin allocation tracking:
The vault tracks how much stablecoin allocation it has contributed to each market (
stablecoin_allocation[pool_id]
), and reduces it proportionally on withdrawal.
crvUSD vault integration:
crvUSD is deposited into an ERC-4626 vault (e.g., scrvUSD). The vault reads
previewRedeem()
to determine available crvUSD backing. Users can deposit/withdraw crvUSD or transfer vault shares directly.
Safe limits:
Before depositing, the AMM enforces that its debt/collateral ratio stays within safe bounds (via
AMM._deposit()
→
get_x0(safe_limits=True)
). The vault exposes a gas-free
safe_to_deposit()
view to check this before sending a transaction, but the actual enforcement happens at the AMM layer — deposits outside safe bounds revert in
AMM._deposit()
.
Deployed by the HybridVaultFactory:
Vault instances are deployed as minimal proxies of the implementation contract, then initialized with
initialize(user, crvusd_vault)
.
System Architecture
┌──────────────────────────────────────────────────────────────────┐
│ HybridVaultFactory │
│ (Deploys vaults, manages global limits & allowed crvUSD vaults) │
└──────────────────────────────────────────────────────────────────┘
│ create_vault()
▼
┌──────────────────────────────────────────────────────────────────┐
│ HybridVault (per user) │
│ │
│ ┌─────────────┐ ┌──────────────────────────────────────────┐ │
│ │ crvUSD │ │ YB Market Positions (up to 16) │ │
│ │ Vault │ │ ┌──────────┐ ┌──────────┐ │ │
│ │ (scrvUSD) │ │ │ Pool 0 │ │ Pool 1 │ ... │ │
│ │ │ │ │ LT+Staker│ │ LT+Staker│ │ │
│ └─────────────┘ │ └──────────┘ └──────────┘ │ │
│ └──────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────────┘
│ │
▼ ▼
scrvUSD / ERC4626 YB Factory Markets
(crvUSD savings) (LT, AMM, Staker)
Function Documentation
Public Variables
Constants
MAX_VAULTS
(
uint256
) - Maximum simultaneous pool positions per vault (16)
AMM_MIN_SAFE_DEBT
(
uint256
) - Minimum safe debt/collateral ratio for deposits
AMM_MAX_SAFE_DEBT
(
uint256
) - Maximum safe debt/collateral ratio for deposits
Immutable Variables
FACTORY
(
Factory
) - YB Factory contract
CRVUSD
(
IERC20
) - crvUSD token
VAULT_FACTORY
(
VaultFactory
) - HybridVaultFactory contract
State Variables
owner
(
address
) - Vault owner (set once at initialization)
crvusd_vault
(
IERC4626
) - Current crvUSD savings vault (e.g., scrvUSD)
used_vaults
(
DynArray[uint256, 16]
) - Pool IDs with active positions
stablecoin_allocation
(
HashMap[uint256, uint256]
) - Tracked allocation per pool
personal_limit
(
HashMap[uint256, uint256]
) - Per-pool personal limit boost
Data Structs
Market
(read from Factory):
asset_token
,
cryptopool
,
amm
,
lt
,
price_oracle
,
virtual_pool
,
staker
.
OraclizedValue
:
p_o
(oracle price),
value
(USD value).
LiquidityValues
:
admin
,
total
,
ideal_staked
,
staked
.
Events
SetPersonalLimit
—
pool_id
,
limit
SetCrvusdVault
—
vault
initialize()
Description:
Initialize a cloned vault instance for a user. Sets the vault owner and approves crvUSD spending on the savings vault. One-shot — reverts if
owner
is already set.
Returns:
bool
- True if initialization succeeded.
Input
Type
Description
user
address
Address that will own this vault
crvusd_vault
IERC4626
crvUSD vault to use (e.g., scrvUSD)
📄 View Source Code
@external
def
initialize
(
user
:
address
,
crvusd_vault
:
IERC4626
)
-
>
bool
:
assert
self
.
owner
==
empty
(
address
)
,
"Already initialized"
self
.
owner
=
user
self
.
crvusd_vault
=
crvusd_vault
extcall CRVUSD
.
approve
(
crvusd_vault
.
address
,
max_value
(
uint256
)
)
return
True
deposit()
Description:
Deposit assets into a YB market through this vault. The vault verifies sufficient crvUSD backing (pulls additional crvUSD from sender if
deposit_stablecoins=True
), checks the position stays within pool limits, temporarily raises the market's stablecoin allocation to allow the LT deposit, calls
LT.deposit()
and optionally stakes the resulting shares, then tightens the allocation back down.
A debt sanity check enforces
debt ≤ 110% of additional_crvusd
— the debt taken cannot exceed 110% of the position's crvUSD-equivalent value. Choosing a debt value above this cap will revert with
"Debt made too high"
. Only callable by the
owner
.
Returns:
uint256
- LT shares received (or staked gauge shares if
stake=True
).
Input
Type
Description
pool_id
uint256
Market pool identifier
assets
uint256
Amount of asset token to deposit
debt
uint256
Amount of crvUSD debt to take on
min_shares
uint256
Minimum LT shares to receive (slippage protection)
stake
bool
If True, auto-stake LT shares in gauge (default: False)
deposit_stablecoins
bool
If True, pull additional crvUSD from sender if backing is insufficient (default: False)
📄 View Source Code
@external
def
deposit
(
pool_id
:
uint256
,
assets
:
uint256
,
debt
:
uint256
,
min_shares
:
uint256
,
stake
:
bool
=
False
,
deposit_stablecoins
:
bool
=
False
)
-
>
uint256
:
assert
self
.
owner
==
msg
.
sender
,
"Access"
market
:
Market
=
staticcall FACTORY
.
markets
(
pool_id
)
assert
market
.
lt
.
address
!=
empty
(
address
)
,
"Bad pool_id"
if
not
self
.
pool_approved
[
pool_id
]
:
assert
extcall market
.
asset_token
.
approve
(
market
.
lt
.
address
,
max_value
(
uint256
)
,
default_return_value
=
True
)
extcall market
.
lt
.
approve
(
market
.
staker
.
address
,
max_value
(
uint256
)
)
self
.
pool_approved
[
pool_id
]
=
True
self
.
token_in_use
[
market
.
lt
.
address
]
=
True
self
.
token_in_use
[
market
.
staker
.
address
]
=
True
# Trigger checkpoint_staker_rebase() by staking 0 tokens
extcall market
.
staker
.
deposit
(
0
,
self
)
pool_value
:
uint256
=
0
additional_crvusd
:
uint256
=
0
pool_value
,
additional_crvusd
=
self
.
_required_crvusd_for
(
market
,
assets
,
debt
)
crvusd_available
:
uint256
=
self
.
_crvusd_available
(
)
crvusd_required
:
uint256
=
self
.
_downscale
(
self
.
_required_crvusd
(
)
+
additional_crvusd
)
if
crvusd_available
<
crvusd_required
:
if
deposit_stablecoins
:
self
.
_deposit_crvusd
(
crvusd_required
-
crvusd_available
)
else
:
raise
"Not enough crvUSD"
assert
pool_value
+
additional_crvusd
<=
self
.
_pool_limits
(
pool_id
)
,
"Beyond pool limit"
# Temporarily make the cap bigger than necessary
assert
debt
<=
11
*
additional_crvusd
//
10
,
"Debt made too high"
previous_allocation
:
uint256
=
staticcall market
.
lt
.
stablecoin_allocation
(
)
self
.
_allocate_stablecoins
(
market
.
lt
,
max
(
(
pool_value
+
additional_crvusd
)
*
22
//
10
,
previous_allocation
)
)
assert
extcall market
.
asset_token
.
transferFrom
(
msg
.
sender
,
self
,
assets
,
default_return_value
=
True
)
lt_shares
:
uint256
=
extcall market
.
lt
.
deposit
(
assets
,
debt
,
min_shares
)
assert
lt_shares
>
0
,
"No liquidity given"
self
.
_add_to_used
(
pool_id
)
# Only after the deposit confirms lt_shares > 0
# Reduce cap to what it should be
self
.
_allocate_stablecoins
(
market
.
lt
,
previous_allocation
+
2
*
additional_crvusd
)
self
.
stablecoin_allocation
[
pool_id
]
+=
2
*
additional_crvusd
extcall VAULT_FACTORY
.
update_vault_required
(
self
.
crvusd_vault
.
address
,
crvusd_required
,
True
)
if
not
stake
:
return
lt_shares
else
:
return
extcall market
.
staker
.
deposit
(
lt_shares
,
self
)
withdraw()
Description:
Withdraw assets from a YB market. Pass
max_value(uint256)
for
shares
to withdraw all. If
withdraw_stablecoins=True
, excess crvUSD backing is returned to the receiver. On withdrawal, the vault reduces stablecoin allocation proportionally and updates the factory's tracked required crvUSD. Only callable by
owner
.
Note:
Setting
withdraw_stablecoins=True
will revert if any pool's oracle is failing (returns
max_value
). If you encounter this, use
withdraw_stablecoins=False
and fall back to
emergency_withdraw()
if needed.
Returns:
uint256
- Amount of assets withdrawn.
Input
Type
Description
pool_id
uint256
Market pool identifier
shares
uint256
LT shares to withdraw (max_value(uint256) for all)
min_assets
uint256
Minimum assets to receive (slippage protection)
unstake
bool
If True, unstake from gauge first (default: False)
receiver
address
Address to receive withdrawn assets (default: msg.sender)
withdraw_stablecoins
bool
If True, return excess crvUSD to receiver (default: False)
📄 View Source Code
@external
def
withdraw
(
pool_id
:
uint256
,
shares
:
uint256
,
min_assets
:
uint256
,
unstake
:
bool
=
False
,
receiver
:
address
=
msg
.
sender
,
withdraw_stablecoins
:
bool
=
False
)
-
>
uint256
:
assert
self
.
owner
==
msg
.
sender
,
"Access"
market
:
Market
=
staticcall FACTORY
.
markets
(
pool_id
)
assert
market
.
lt
.
address
!=
empty
(
address
)
,
"Bad pool_id"
required_before
:
uint256
=
self
.
_required_crvusd
(
)
pool_crvusd_before
:
uint256
=
self
.
_pool_crvusd
(
market
)
lt_shares
:
uint256
=
shares
if
unstake
:
staker_shares
:
uint256
=
shares
if
staker_shares
==
max_value
(
uint256
)
:
staker_shares
=
staticcall market
.
staker
.
balanceOf
(
self
)
lt_shares
=
extcall market
.
staker
.
redeem
(
staker_shares
,
self
,
self
)
elif
lt_shares
==
max_value
(
uint256
)
:
lt_shares
=
staticcall market
.
lt
.
balanceOf
(
self
)
lt_supply
:
uint256
=
staticcall market
.
lt
.
totalSupply
(
)
assets
:
uint256
=
extcall market
.
lt
.
withdraw
(
lt_shares
,
min_assets
,
receiver
)
removed
:
bool
=
False
if
staticcall market
.
lt
.
balanceOf
(
self
)
==
0
and
staticcall market
.
staker
.
balanceOf
(
self
)
==
0
:
self
.
_remove_from_used
(
pool_id
)
removed
=
True
required_after
:
uint256
=
self
.
_required_crvusd
(
)
if
not
removed
:
previous_allocation
:
uint256
=
staticcall market
.
lt
.
stablecoin_allocation
(
)
reduction
:
uint256
=
0
if
required_before
==
max_value
(
uint256
)
or
required_after
==
max_value
(
uint256
)
:
pool_crvusd_after
:
uint256
=
self
.
_pool_crvusd
(
market
)
assert
pool_crvusd_before
!=
max_value
(
uint256
)
and
pool_crvusd_after
!=
max_value
(
uint256
)
,
"Oracle is broken"
assert
not
withdraw_stablecoins
,
"Cannot withdraw stables"
if
pool_crvusd_before
>
pool_crvusd_after
:
reduction
=
min
(
2
*
(
pool_crvusd_before
-
pool_crvusd_after
)
,
self
.
stablecoin_allocation
[
pool_id
]
)
else
:
if
required_before
>
required_after
:
if
withdraw_stablecoins
:
if
required_after
>
0
:
self
.
_withdraw_crvusd
(
self
.
_downscale
(
required_before
-
required_after
)
,
receiver
,
True
)
else
:
self
.
_redeem_crvusd
(
staticcall self
.
crvusd_vault
.
balanceOf
(
self
)
,
receiver
)
reduction
=
min
(
2
*
(
required_before
-
required_after
)
,
self
.
stablecoin_allocation
[
pool_id
]
)
if
reduction
>
0
:
if
reduction
>
previous_allocation
:
reduction
=
previous_allocation
self
.
_allocate_stablecoins
(
market
.
lt
,
previous_allocation
-
reduction
)
self
.
stablecoin_allocation
[
pool_id
]
-=
reduction
else
:
if
withdraw_stablecoins
:
assert
required_after
!=
max_value
(
uint256
)
,
"Cannot withdraw stables"
if
required_after
>
0
:
available
:
uint256
=
self
.
_crvusd_available
(
)
needed
:
uint256
=
self
.
_downscale
(
required_after
)
if
available
>
needed
:
self
.
_withdraw_crvusd
(
available
-
needed
,
receiver
,
True
)
else
:
self
.
_redeem_crvusd
(
staticcall self
.
crvusd_vault
.
balanceOf
(
self
)
,
receiver
)
if
required_after
!=
max_value
(
uint256
)
:
extcall VAULT_FACTORY
.
update_vault_required
(
self
.
crvusd_vault
.
address
,
self
.
_downscale
(
required_after
)
,
False
)
return
assets
emergency_withdraw()
Description:
Emergency withdraw from a YB market. Uses fallback accounting when oracle-dependent paths fail:
If both per-pool oracle values are available, allocation is reduced based on pool value change.
If oracles are unavailable, allocation is estimated from the crvUSD consumed during withdrawal and the stablecoin fraction.
If oracles are fully working, normal required-crvUSD reduction applies.
Use when
withdraw()
is blocked due to oracle failure. If
crvusd_from_wallet=False
, the vault redeems all its scrvUSD to cover potential debt repayment, then re-deposits any remaining crvUSD back to the savings vault. If
crvusd_from_wallet=True
, the caller provides crvUSD directly and receives any excess back. Only callable by
owner
.
Important:
You must
unstake LT tokens before calling
emergency_withdraw()
. Staked shares are not accessible to this function — call
unstake()
first.
Input
Type
Description
pool_id
uint프로젝트로 돌아가기