From 5140ab34363493bc8471fbb98ce06813759c5381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stas=20SU=C8=98COV?= Date: Tue, 27 Feb 2024 14:59:05 +0200 Subject: [PATCH] Refactor CL position fetching. --- contracts/LpSugar.vy | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/contracts/LpSugar.vy b/contracts/LpSugar.vy index 1f6f735..5fa63f6 100644 --- a/contracts/LpSugar.vy +++ b/contracts/LpSugar.vy @@ -217,6 +217,7 @@ interface ICLGauge: interface INFPositionManager: def positions(_position_id: uint256) -> PositionData: view def tokenOfOwnerByIndex(_account: address, _index: uint256) -> uint256: view + def balanceOf(_account: address) -> uint256: view interface IReward: def getPriorSupplyIndex(_ts: uint256) -> uint256: view @@ -626,7 +627,14 @@ def _byDataCL(_data: address[4], _token0: address, _token1: address, \ emissions_token: address = empty(address) token0: IERC20 = IERC20(_token0) token1: IERC20 = IERC20(_token1) - tick_spacing: int24 = pool.tickSpacing() + positions_count: uint256 = 0 + + slot: Slot = pool.slot0() + positions: DynArray[Position, MAX_POSITIONS] = \ + empty(DynArray[Position, MAX_POSITIONS]) + + if _account != empty(address): + positions_count = self.nfpm.balanceOf(_account) if gauge.address != empty(address): fee_voting_reward = gauge.feesVotingReward() @@ -635,18 +643,11 @@ def _byDataCL(_data: address[4], _token0: address, _token1: address, \ if gauge_alive: emissions = gauge.rewardRate() - slot: Slot = pool.slot0() - price: uint160 = slot.sqrtPriceX96 - - positions: DynArray[Position, MAX_POSITIONS] = \ - empty(DynArray[Position, MAX_POSITIONS]) - for index in range(0, MAX_POSITIONS): - position_id: uint256 = self.nfpm.tokenOfOwnerByIndex(_account, index) - - if position_id == 0: + if index >= positions_count: break + position_id: uint256 = self.nfpm.tokenOfOwnerByIndex(_account, index) position_data: PositionData = self.nfpm.positions(position_id) emissions_earned: uint256 = 0 @@ -676,9 +677,9 @@ def _byDataCL(_data: address[4], _token0: address, _token1: address, \ decimals: 0, total_supply: 0, - type: tick_spacing, + type: pool.tickSpacing(), tick: slot.tick, - price: price, + price: slot.sqrtPriceX96, token0: token0.address, reserve0: token0.balanceOf(pool.address),