Deposit, withdraw, and migrate
YieldBasis exposes four integration paths for entering, exiting, staking, and migrating liquidity: direct LT calls, LTDepositZap, LTMigratorGated / LTMigrator, LiquidityGauge (and StakeZap), and HybridVault. Choosing the right path depends on whether the market's Curve Twocrypto pool runs YBOraclizedPolicy and whether you need HybridVault allocation headroom.
1. Depositing into a market
When a user deposits asset_token, LT pulls debt in crvUSD from AMM, adds [debt, assets] to the Curve Twocrypto pool, deposits the resulting Curve LP tokens into AMM as collateral, and mints yb-LP shares.
Why you should use LTDepositZap
On a pool running YBOraclizedPolicy, the pool's update_pool_state callback enforces an LT admission gate. The gate rejects any deposit whose borrowed debt deviates from the AMM book's ratio by more than policy.ratio_tolerance():
Because added_lp depends on debt itself and any swap before your transaction shifts the pool balances, a debt computed off-chain can fail on-chain. LTDepositZap solves the fixed point on-chain in the same transaction using two Newton iterations from d0 = assets * cryptopool.price_oracle() // 10**asset_decimals (with finite-difference step DEBT_STEP = 1000):
Using LTDepositZap is safe on non-gated pools too: matching the book's ratio avoids trading value against existing yb-LP holders.
LTDepositZap interface
interface ILTDepositZap {
function calc_debt(address lt, uint256 assets) external view returns (uint256);
function preview_deposit(address lt, uint256 assets, bool stake) external view returns (uint256);
function deposit(
address lt,
uint256 assets,
uint256 min_shares,
address receiver,
bool stake
) external returns (uint256);
}
- Approve
LTDepositZap(notLT) to spendassetsofLT.ASSET_TOKEN(). - Call
preview_deposit(lt, assets, stake)off-chain to computemin_shareswith your slippage tolerance. - Set
stake = trueto mintyb-LPand stake it intoLT.staker()(LiquidityGauge) atomically.min_sharesis then checked against the resulting gauge shares. LTDepositZapholds no state and retains no token balances between transactions.
Direct LT.deposit and LT.preview_deposit
If you call LT directly (for example, from a custom router that solves debt on-chain):
interface ILT {
function preview_deposit(uint256 assets, uint256 debt, bool raise_overflow) external view returns (uint256);
function deposit(uint256 assets, uint256 debt, uint256 min_shares, address receiver) external returns (uint256);
}
Key constraints enforced by LT.deposit:
receivermust not equalLT.staker()(reverts with"Deposit to staker").- Post-deposit
AMMvalue must satisfyamm.max_debt() // 2 >= v.value(reverts with"Debt too high"). Passraise_overflow = truetopreview_depositto detect this before submitting. - Total supply after deposit must be at least
MIN_SHARE_REMAINDER = 10**6(reverts with"Remainder too small"). - Minted shares must satisfy
shares >= min_shares(reverts with"Slippage").
2. Withdrawing from a market
Calling LT.withdraw burns yb-LP, withdraws a proportional share of LP collateral and debt from AMM, removes liquidity from the Curve Twocrypto pool with fixed crvUSD output (remove_liquidity_fixed_out) to repay the debt, and sends the remaining asset_token to receiver.
Standard withdrawal (LT.withdraw)
interface ILTWithdraw {
function preview_withdraw(uint256 tokens) external view returns (uint256);
function withdraw(uint256 shares, uint256 min_assets, address receiver) external returns (uint256);
}
- Quote
min_assetsoff-chain viaLT.preview_withdraw(shares)and apply slippage tolerance. - Neither
msg.sendernorreceivercan beLT.staker()(reverts with"Withdraw to/from staker"). - You must leave either
0total supply or at leastMIN_SHARE_REMAINDER(10**6) shares remaining (reverts with"Remainder too small"). - If
AMM.is_killed()istrue,LT.withdrawreverts with"We're dead. Use emergency_withdraw".
Emergency withdrawal (LT.emergency_withdraw)
Use LT.emergency_withdraw when AMM.is_killed() is true or when AMM is imbalanced enough that value_oracle() reverts:
interface ILTEmergency {
function preview_emergency_withdraw(uint256 shares) external view returns (uint256 assets, int256 net_stables);
function emergency_withdraw(uint256 shares, address receiver, address owner) external returns (uint256 assets, int256 net_stables);
}
emergency_withdraw performs a balanced CRYPTOPOOL.remove_liquidity(withdrawn_lp, [0, 0]) instead of remove_liquidity_fixed_out:
- If
net_stables > 0,LTsends bothassetsand excesscrvUSDtoreceiver. - If
net_stables < 0,LTpullsabs(net_stables)ofcrvUSDfrommsg.senderto finish repayingAMMdebt. ApproveLTforcrvUSDbefore calling whenpreview_emergency_withdrawreturns a negativenet_stables. - When
AMMis live and healthy, onlyowner == msg.sendermay callemergency_withdraw(reverts with"Not killed"). When killed or whenvalue_oracle()reverts,Factory.emergency_admin()may also unwind on behalf of anowneras long asreceiver == owner.
3. Staking and unstaking in LiquidityGauge
LiquidityGauge (Market.staker) is an ERC-4626 vault whose underlying asset() is LT (yb-LP). Staking converts yb-LP into gauge shares (g(yb...)) that accrue YB emissions from GaugeController plus up to 7 optional external reward tokens (MAX_REWARDS = 8).
LiquidityGauge interface
interface ILiquidityGauge {
function asset() external view returns (address);
function previewDeposit(uint256 assets) external view returns (uint256);
function previewRedeem(uint256 shares) external view returns (uint256);
function deposit(uint256 assets, address receiver) external returns (uint256);
function mint(uint256 shares, address receiver) external returns (uint256);
function withdraw(uint256 assets, address receiver, address owner) external returns (uint256);
function redeem(uint256 shares, address receiver, address owner) external returns (uint256);
function preview_claim(address reward, address user) external view returns (uint256);
function claim(address reward, address user) external returns (uint256);
}
Integration rules for LiquidityGauge:
- Every
deposit,mint,withdraw, andredeemcallsLT.checkpoint_staker_rebase()first, checkpoints user reward integrals, enforcesMIN_SHARES(10**12when total supply is non-zero), and callsGaugeController.emit(). - Because
LTreducesLT.balanceOf(staker)when trading profits accrue, gauge shares represent a shrinking number ofyb-LPtokens (whose per-token asset value rises proportionally, keeping staked asset value flat unless the market is below the watermark). Always usepreviewRedeem(gauge_shares)rather than assuming a 1:1 ratio. StakeZapprovides single-transactionwithdraw_and_unstake(gauge, shares, min_assets, receiver)for exiting staked positions toasset_token. For entering staked positions on a pool runningYBOraclizedPolicy, preferLTDepositZap.deposit(lt, assets, min_shares, receiver, true)overStakeZap.deposit_and_stakebecauseStakeZaprequires an off-chaindebtparameter.
4. Migrating positions (LTMigratorGated and LTMigrator)
When a newer market supersedes an older market for the same asset_token, migrator contracts move plain or staked positions in one transaction while temporarily expanding lt_to's crvUSD allocation through MigrationFactoryOwner / HybridFactoryOwner.
Choosing the right migrator
| Contract | Target pool policy | Debt calculation | debt_coefficient parameter |
|---|---|---|---|
LTMigratorGated | Pools running YBOraclizedPolicy (and any other pool) | Solves exact book-ratio fixed point on-chain after lt_from.withdraw | Accepted for ABI compatibility, ignored |
LTMigrator | Legacy non-gated pools | Preserves withdrawn debt (same cryptopool) or assets * cpool_to.price_oracle() // 10**decimals (new cryptopool) | Scales debt (10**18 = 1.0) |
Both contracts share the exact same external ABI:
interface ILTMigrator {
function preview_migrate_plain(
address lt_from,
address lt_to,
uint256 shares_in,
uint256 debt_coefficient
) external view returns (uint256);
function preview_migrate_staked(
address lt_from,
address lt_to,
uint256 shares_in,
uint256 debt_coefficient
) external view returns (uint256);
function migrate_plain(
address lt_from,
address lt_to,
uint256 shares_in,
uint256 min_out,
uint256 debt_coefficient
) external;
function migrate_staked(
address lt_from,
address lt_to,
uint256 shares_in,
uint256 min_out,
uint256 debt_coefficient
) external;
}
Migration rules and approvals:
- Forward-only check:
lt_frommust be registered inFactory,lt_tomust not be disabled, andlt_tomust be the newest market inFactoryforlt_from.ASSET_TOKEN(). Otherwise calls revert with"same LT","lt_from not in factory","lt_to deprecated", or"lt_to superseded". - Plain migration: Approve the migrator on
lt_fromforshares_in, then callmigrate_plain. - Staked migration: Approve the migrator on
lt_from.staker()(LiquidityGauge) forshares_in, then callmigrate_staked. The migrator redeems fromgauge_from, migratesyb-LP, stakes intogauge_to, and checksshares_out >= min_out("not enough out").
5. Integrating HybridVault (ERC-4626 backed capacity)
When a market reaches its standard crvUSD allocation cap, users can deploy a personal HybridVault clone through HybridVaultFactory. A HybridVault pairs deposited asset_token positions across up to MAX_VAULTS = 16 markets with ERC-4626 crvUSD vault shares (such as scrvUSD) that satisfy HybridVaultFactory.stablecoin_fraction().
Setup and deposit flow
- Create a vault (one per address): Call
HybridVaultFactory.create_vault(crvusd_vault)whereHybridVaultFactory.allowed_crvusd_vaults(crvusd_vault)istrue. Look up an existing vault withHybridVaultFactory.user_to_vault(user). - Compute
debtand requiredcrvUSD:- Query
debt = LTDepositZap.calc_debt(market.lt, assets). - Verify
HybridVault.safe_to_deposit(pool_id, assets, debt)returnstrue. - Query
needed_crvusd = HybridVault.crvusd_for_deposit(pool_id, assets, debt).
- Query
- Approve and deposit:
- Approve
HybridVaultonmarket.asset_tokenforassets. - If
needed_crvusd > 0, approveHybridVaultoncrvUSDfor at leastneeded_crvusdand passdeposit_stablecoins = true(or pre-fund the vault viadeposit_crvusd). - Call
HybridVault.deposit(pool_id, assets, debt, min_shares, stake, deposit_stablecoins).
- Approve
interface IHybridVaultFactory {
function create_vault(address crvusd_vault) external returns (address);
function user_to_vault(address user) external view returns (address);
function stablecoin_fraction() external view returns (uint256);
function pool_limits(uint256 pool_id) external view returns (uint256);
}
interface IHybridVault {
function safe_to_deposit(uint256 pool_id, uint256 assets, uint256 debt) external view returns (bool);
function crvusd_for_deposit(uint256 pool_id, uint256 assets, uint256 debt) external view returns (uint256);
function deposit(
uint256 pool_id,
uint256 assets,
uint256 debt,
uint256 min_shares,
bool stake,
bool deposit_stablecoins
) external returns (uint256);
function withdraw(
uint256 pool_id,
uint256 shares,
uint256 min_assets,
bool unstake,
address receiver,
bool withdraw_stablecoins
) external returns (uint256);
function emergency_withdraw(uint256 pool_id, uint256 shares, bool crvusd_from_wallet) external;
function stake(uint256 pool_id, uint256 pool_shares) external returns (uint256);
function unstake(uint256 pool_id, uint256 gauge_shares) external returns (uint256);
function claim_reward(address token) external returns (uint256);
}
HybridVault.deposit accepts an explicit debt parameter rather than solving the fixed point internally. On a pool running YBOraclizedPolicy, if another swap moves the cryptopool between your calc_debt read and transaction inclusion, the policy's LT gate can revert the deposit. Submit via a private mempool or retry with a fresh calc_debt quote.
Withdrawal and emergency exit from HybridVault
- Pass
shares = type(uint256).maxtoHybridVault.withdrawto exit your entire position inpool_id. - Set
unstake = trueto redeem gauge shares before withdrawing fromLT, andwithdraw_stablecoins = trueto release excesscrvUSDfrom the backing ERC-4626 vault back toreceiver. - For killed or untradable markets, unstake first via
HybridVault.unstake(pool_id, gauge_shares)and then callHybridVault.emergency_withdraw(pool_id, shares, crvusd_from_wallet). Settingcrvusd_from_wallet = falseredeems backing vault shares automatically to cover any negativestables_to_return.