Skip to main content

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.

Deposit call sequence across LT, Curve Twocrypto pool, and AMMDeposit call sequence across LT, Curve Twocrypto pool, and AMM

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():

debt≈added_lp⋅amm.get_debt()amm.collateral_amount()\text{debt} \approx \text{added\_lp} \cdot \frac{\text{amm.get\_debt()}}{\text{amm.collateral\_amount()}}

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):

f(x)=cryptopool.calc_token_amount([x,assets],True)⋅amm.get_debt()amm.collateral_amount()f(x) = \text{cryptopool.calc\_token\_amount}([x, \text{assets}], \text{True}) \cdot \frac{\text{amm.get\_debt()}}{\text{amm.collateral\_amount()}}

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 (not LT) to spend assets of LT.ASSET_TOKEN().
  • Call preview_deposit(lt, assets, stake) off-chain to compute min_shares with your slippage tolerance.
  • Set stake = true to mint yb-LP and stake it into LT.staker() (LiquidityGauge) atomically. min_shares is then checked against the resulting gauge shares.
  • LTDepositZap holds 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:

  • receiver must not equal LT.staker() (reverts with "Deposit to staker").
  • Post-deposit AMM value must satisfy amm.max_debt() // 2 >= v.value (reverts with "Debt too high"). Pass raise_overflow = true to preview_deposit to 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.

Withdrawal call sequence across LT, AMM, and Curve Twocrypto poolWithdrawal call sequence across LT, AMM, and Curve Twocrypto pool

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_assets off-chain via LT.preview_withdraw(shares) and apply slippage tolerance.
  • Neither msg.sender nor receiver can be LT.staker() (reverts with "Withdraw to/from staker").
  • You must leave either 0 total supply or at least MIN_SHARE_REMAINDER (10**6) shares remaining (reverts with "Remainder too small").
  • If AMM.is_killed() is true, LT.withdraw reverts 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, LT sends both assets and excess crvUSD to receiver.
  • If net_stables < 0, LT pulls abs(net_stables) of crvUSD from msg.sender to finish repaying AMM debt. Approve LT for crvUSD before calling when preview_emergency_withdraw returns a negative net_stables.
  • When AMM is live and healthy, only owner == msg.sender may call emergency_withdraw (reverts with "Not killed"). When killed or when value_oracle() reverts, Factory.emergency_admin() may also unwind on behalf of an owner as long as receiver == 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).

Staking call sequence between user, LiquidityGauge, LT, and GaugeControllerStaking call sequence between user, LiquidityGauge, LT, and GaugeController Unstaking call sequence between user, LiquidityGauge, LT, and GaugeControllerUnstaking call sequence between user, LiquidityGauge, LT, and GaugeController

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, and redeem calls LT.checkpoint_staker_rebase() first, checkpoints user reward integrals, enforces MIN_SHARES (10**12 when total supply is non-zero), and calls GaugeController.emit().
  • Because LT reduces LT.balanceOf(staker) when trading profits accrue, gauge shares represent a shrinking number of yb-LP tokens (whose per-token asset value rises proportionally, keeping staked asset value flat unless the market is below the watermark). Always use previewRedeem(gauge_shares) rather than assuming a 1:1 ratio.
  • StakeZap provides single-transaction withdraw_and_unstake(gauge, shares, min_assets, receiver) for exiting staked positions to asset_token. For entering staked positions on a pool running YBOraclizedPolicy, prefer LTDepositZap.deposit(lt, assets, min_shares, receiver, true) over StakeZap.deposit_and_stake because StakeZap requires an off-chain debt parameter.

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​

ContractTarget pool policyDebt calculationdebt_coefficient parameter
LTMigratorGatedPools running YBOraclizedPolicy (and any other pool)Solves exact book-ratio fixed point on-chain after lt_from.withdrawAccepted for ABI compatibility, ignored
LTMigratorLegacy non-gated poolsPreserves 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_from must be registered in Factory, lt_to must not be disabled, and lt_to must be the newest market in Factory for lt_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_from for shares_in, then call migrate_plain.
  • Staked migration: Approve the migrator on lt_from.staker() (LiquidityGauge) for shares_in, then call migrate_staked. The migrator redeems from gauge_from, migrates yb-LP, stakes into gauge_to, and checks shares_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​

  1. Create a vault (one per address): Call HybridVaultFactory.create_vault(crvusd_vault) where HybridVaultFactory.allowed_crvusd_vaults(crvusd_vault) is true. Look up an existing vault with HybridVaultFactory.user_to_vault(user).
  2. Compute debt and required crvUSD:
    • Query debt = LTDepositZap.calc_debt(market.lt, assets).
    • Verify HybridVault.safe_to_deposit(pool_id, assets, debt) returns true.
    • Query needed_crvusd = HybridVault.crvusd_for_deposit(pool_id, assets, debt).
  3. Approve and deposit:
    • Approve HybridVault on market.asset_token for assets.
    • If needed_crvusd > 0, approve HybridVault on crvUSD for at least needed_crvusd and pass deposit_stablecoins = true (or pre-fund the vault via deposit_crvusd).
    • Call HybridVault.deposit(pool_id, assets, debt, min_shares, stake, deposit_stablecoins).
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);
}
caution

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).max to HybridVault.withdraw to exit your entire position in pool_id.
  • Set unstake = true to redeem gauge shares before withdrawing from LT, and withdraw_stablecoins = true to release excess crvUSD from the backing ERC-4626 vault back to receiver.
  • For killed or untradable markets, unstake first via HybridVault.unstake(pool_id, gauge_shares) and then call HybridVault.emergency_withdraw(pool_id, shares, crvusd_from_wallet). Setting crvusd_from_wallet = false redeems backing vault shares automatically to cover any negative stables_to_return.