KuruAMMVault Contract
ERC20 vault for AMM liquidity provision. Users deposit tokens to receive LP shares and earn trading fees.State Variables
token1&token2- The two tokens in the liquidity pair (one may be native)market- Associated OrderBook that uses this vault for AMM liquiditySPREAD_CONSTANT- Spread between bid/ask prices (e.g., 100 = 1% spread)
Core Functions
initialize
One-time initialization of the KuruAMMVault contract.setMarketParams
Fetch and cache market parameters from OrderBook.marketParams struct with current market configuration from the associated OrderBook.
Liquidity Operations
deposit
Deposit tokens and receive LP shares.mint
Mint specific amount of LP shares.previewMint, then calls deposit.
withdraw
Burn LP shares to withdraw tokens.Preview Functions
previewDeposit
Calculate shares for a given deposit.previewMint
Calculate required deposits to mint shares.previewWithdraw
Calculate token amounts from burning shares.totalAssets
Get vault’s total token balances.Ownership Functions
transferOwnership
Transfer vault ownership.Events
KuruVaultDeposit
amount1 and amount2 are the deposited tokens, shares is the LP tokens minted.
KuruVaultWithdraw
amount1 and amount2 are the withdrawn tokens, shares is the LP tokens burned.
Key Mechanics
Virtual Rebalancing
Unlike traditional AMMs that rebalance continuously, the Kuru vault accrues profits in base and quote tokens without rebalancing on-chain. This creates reserve skew where actual reserves may differ from the curve’s implied reserves. Problem: Standard Uniswap V2 share calculation would penalize depositors when reserves are skewed:- Actual reserves:
x' = x + profitInBase,y' = y + profitInQuote - Current vault price:
p - Total value:
value = x'*p + y'
x_rebalanced = (x'*p + y') / 2py_rebalanced = (x'*p + y') / 2
Share Calculation
- First deposit: Shares =
sqrt(baseDeposit * quoteDeposit) - MIN_LIQUIDITY - Subsequent deposits: Proportional to virtually rebalanced reserves
- Uses fixed-point math to handle decimal precision
Vault Rebalancing
Vault maintains bid/ask spread around current price:- Ask size:
(SPREAD_CONSTANT * baseAmount) / (20000 + SPREAD_CONSTANT) - Bid size:
(SPREAD_CONSTANT * baseAmount) / 20000
Price Updates
- On deposit: Recalculates ask price and updates OrderBook
- On withdraw: Updates sizes only (prices set on first deposit)
- Vault prices:
vaultBestAskandvaultBestBidstored in OrderBook
Partial Fills
When withdrawing with pending partial fills:- Adjusts owed amounts based on vault’s profit/loss from fills
- Returns
_nullifyPartialFillsflag to clear OrderBook state