Skip to content

Commit

Permalink
LpSugar: refactor ALM integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed Aug 2, 2024
1 parent 10192c9 commit d26d0c3
Showing 1 changed file with 70 additions and 25 deletions.
95 changes: 70 additions & 25 deletions contracts/LpSugar.vy
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct Position:
tick_upper: int24 # Position upper tick on CL, 0 on v2
sqrt_ratio_lower: uint160 # sqrtRatio at lower tick on CL, 0 on v2
sqrt_ratio_upper: uint160 # sqrtRatio at upper tick on CL, 0 on v2
alm_vault: address

struct Token:
token_address: address
Expand Down Expand Up @@ -138,21 +139,6 @@ struct Reward:
fee: address
bribe: address

# See:
# https://github.com/mellow-finance/mellow-alm-toolkit/blob/main/src/interfaces/utils/IVeloDeployFactory.sol#L46-L52
struct AlmAddresses:
farm: address
lp_wrapper: address

# See:
# https://github.com/mellow-finance/mellow-alm-toolkit/blob/main/src/interfaces/utils/IVeloDeployFactory.sol#L24-L33
struct AlmImmutableParams:
core: IALMCore
strategyModule: address
veloModule: address
depositWithdrawModule: address
helper: address

# See:
# https://github.com/mellow-finance/mellow-alm-toolkit/blob/main/src/interfaces/ICore.sol#L12-L60
struct AlmManagedPositionInfo:
Expand Down Expand Up @@ -261,22 +247,24 @@ interface ISlipstreamHelper:
def fees(_nfpm: address, _position_id: uint256) -> Amounts: view
def poolFees(_pool: address, _liquidity: uint128, _current_tick: int24, _lower_tick: int24, _upper_tick: int24) -> Amounts: view

interface IALMFactory:
def poolToAddresses(pool: address) -> AlmAddresses: view
def getImmutableParams() -> AlmImmutableParams: view
interface IAlmFactory:
def poolToAddresses(pool: address) -> address[2]: view
def getImmutableParams() -> address[5]: view

interface IALMCore:
interface IAlmCore:
def managedPositionAt(_id: uint256) -> AlmManagedPositionInfo: view

interface IALMLpWrapper:
interface IAlmLpWrapper:
def positionId() -> uint256: view
def balanceOf(_account: address) -> uint256: view
def totalSupply() -> uint256: view

# Vars
registry: public(IFactoryRegistry)
voter: public(IVoter)
convertor: public(address)
cl_helper: public(ISlipstreamHelper)
alm_factory: public(IALMFactory)
alm_factory: public(IAlmFactory)

# Methods

Expand All @@ -290,7 +278,7 @@ def __init__(_voter: address, _registry: address, _convertor: address, \
self.registry = IFactoryRegistry(_registry)
self.convertor = _convertor
self.cl_helper = ISlipstreamHelper(_slipstream_helper)
self.alm_factory = IALMFactory(_alm_factory)
self.alm_factory = IAlmFactory(_alm_factory)

@internal
@view
Expand Down Expand Up @@ -669,7 +657,7 @@ def _positions(
pools_done: uint256 = 0

factories_count: uint256 = len(_factories)
alm_core: IALMCore = self.alm_factory.getImmutableParams().core
alm_core: IAlmCore = IAlmCore(self.alm_factory.getImmutableParams()[0])

for index in range(0, MAX_FACTORIES):
if index >= factories_count:
Expand Down Expand Up @@ -732,9 +720,66 @@ def _positions(
if pos.lp != empty(address):
positions.append(pos)

# fetch staked CL positions
pools_count: uint256 = factory.allPoolsLength()

# fetch ALM positions
for pindex in range(0, MAX_POOLS):
if pindex >= pools_count or pools_done >= _limit:
break

# Basically skip calls for offset records...
if to_skip > 0:
to_skip -= 1
continue
else:
pools_done += 1

pool_addr: address = factory.allPools(pindex)
gauge: ICLGauge = ICLGauge(self.voter.gauges(pool_addr))
alm_addresses: address[2] = self.alm_factory.poolToAddresses(pool_addr)
alm_farm: IGauge = IGauge(alm_addresses[0])
alm_vault: IAlmLpWrapper = IAlmLpWrapper(alm_addresses[1])

if alm_vault.address == empty(address):
continue

alm_user_liq: uint256 = alm_vault.balanceOf(_account)

if alm_user_liq == 0:
continue

alm_pos: AlmManagedPositionInfo = alm_core.managedPositionAt(
alm_vault.positionId()
)

pos: Position = self._cl_position(
alm_pos.ammPositionIds[0],
alm_vault.address,
pool_addr,
gauge.address,
factory.address,
nfpm.address
)

alm_liq: uint256 = alm_vault.totalSupply()
# adjust user share of the vault...
pos.amount0 = (alm_user_liq * pos.amount0) / alm_liq
pos.amount1 = (alm_user_liq * pos.amount1) / alm_liq
pos.staked0 = (alm_user_liq * pos.staked0) / alm_liq
pos.staked1 = (alm_user_liq * pos.staked1) / alm_liq

pos.unstaked_earned0 = (alm_user_liq * pos.unstaked_earned0) / alm_liq
pos.unstaked_earned1 = (alm_user_liq * pos.unstaked_earned1) / alm_liq
pos.emissions_earned = alm_farm.earned(_account)

pos.liquidity = (alm_user_liq * pos.liquidity) / alm_liq
pos.staked = (alm_user_liq * pos.staked) / alm_liq

pos.alm_vault = alm_vault.address

positions.append(pos)

# fetch staked CL positions
for pindex in range(0, MAX_POOLS):
if pindex >= pools_count or pools_done >= _limit:
break
Expand Down Expand Up @@ -978,7 +1023,7 @@ def _cl_lp(_data: address[4], _token0: address, _token1: address) -> Lp:
token1_fees: token1_fees,

nfpm: _data[3],
alm_vault: self.alm_factory.poolToAddresses(pool.address).lp_wrapper
alm_vault: self.alm_factory.poolToAddresses(pool.address)[1]
})

@external
Expand Down

0 comments on commit d26d0c3

Please sign in to comment.