weweswap / contracts

smart contracts
MIT License
1 stars 4 forks source link

ADD: LM Contract #6

Closed jpthor closed 1 month ago

jpthor commented 3 months ago

Overview

Liquidity Manager Contract is critical for WAMMv1.

white paper: https://docs.google.com/document/d/1W-QpSgGRFDVXzTo8ux2MkH2GBzLxUET7c9pFzXmH8_M/edit#heading=h.csxc2h5o0r77

LM Contract wraps the NonfungiblePositionManager https://github.com/Uniswap/v3-periphery/blob/main/contracts/NonfungiblePositionManager.sol

Underlying AMM is Univ3.

There is n LM contract that handles all the pools.

Implementation

Start Params

Owner sets the 3 liquidity band parameters in the master LM Factory.

POOL Params
narrowPriceRange // 40%*targetPriceRange
midPriceRange // 100%*targetPriceRange
widePriceRange // 170%*targetPriceRange

Yield Params
yieldCollectionFees = 5 //basispoints
feeCallInterval = 86400 seconds //

Rebalance Params
whitelistedRebalancers[] // Owner can add WL bot
allowAnyRebalancer = 0|1 // let anyone call rebalancing function
rebalancerFees = 5 //5basispoitns
secondsOutsideRangeBeforeRebalance // before rebalance rules

MIGRATE Liquidity

1) User adds ASSET + USDC 2) LM Contract splits into 33% chunks 3) LM Contract NFPM.increaseLiquidity() to the 3 bands; or if first, NFPM.addLiquidity() 4) LM Contract mints ownership. (if first, then deploy token.sol) 5) LM contract refunds any left-over assets

Add liquidity - SINGLE

1) User adds 1 asset to LM 2) Zapper zaps to ASSET + USDC in 3 bands

Rebalance Flow

Bot calls the function: 1) Can only re-balance once a band is outside of price range for some time. 2) Removes liquidity, Collects any "left-over assets" in the contract from last re-balance 3) ZAP it, taking fee 3) Fetches latest band parameters 4) ADDS - LIQUIDITY to the single band 5) Any excess assets are left in the contract to be collected in next re-balance

leftoverBalance0
leftoverBalance1

Collect Fees

1) Anyone can call collectFees(pool) (parasite off claimFees() once per feeCallInterval) 2) LM Contract collects Token0 and Token1 from the pool via NFPM.collect() 3) Then swap (using own liquidity) Token1 to Token0 (USDC) 4) LM Contract now accrues USDC for user claiming

feeBalance0

Claim Yield

1) Any user can claim share of accrued fees (USDC) or Subsidy token claimShare(USDC) 2) Calculate ownership as userUnits / totalUnits 3) Send Yield to user 4) reset timeSinceLastClaimed to stop over-claiming other's shares 5) collectFees(pool) (if once a day)

Withdraw

1) User can withdraw their share userUnits / totalUnits 2) - 3) REMOVE Liquidity equally from all 3 bands, plus claim fees NFPM.decreaseLiquidity() 4) Swap USDC for the ASSET 5) Send to user

Safety - Owner

1) Owner can pause if bug, pause all re-balancing and ADD Liquidity, can't pause withdraw 2) Owner can renounceOwnership() to stop being able to pause or change parameters 3) Owner can set default params again