Skip to content

Commit 1f2638d

Browse files
authored
feat: add tick data to Lp output (#45)
* feat: add tick data to Lp output * feat: create separate tick prices functions * refactor: add individual tick price function * clean up prices name and return statement, deployment
1 parent 7233c2d commit 1f2638d

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

contracts/LpSugar.vy

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ MAX_LPS: constant(uint256) = 500
1414
MAX_EPOCHS: constant(uint256) = 200
1515
MAX_REWARDS: constant(uint256) = 16
1616
MAX_POSITIONS: constant(uint256) = 10
17+
MAX_PRICES: constant(uint256) = 20
1718
WEEK: constant(uint256) = 7 * 24 * 60 * 60
1819

19-
# Slot0 from V3Pool.sol
20+
# Slot0 from CLPool.sol
2021
struct Slot:
2122
sqrt_price: uint160
2223
tick: int24
@@ -25,7 +26,7 @@ struct Slot:
2526
cardinality_next: uint16
2627
unlocked: bool
2728

28-
# GaugeFees from V3Pool.sol
29+
# GaugeFees from CLPool.sol
2930
struct GaugeFees:
3031
token0: uint128
3132
token1: uint128
@@ -43,6 +44,19 @@ struct PositionData:
4344
unstaked_earned0: uint128
4445
unstaked_earned1: uint128
4546

47+
# Tick.Info from CLPool.sol
48+
struct TickInfo:
49+
liquidity_gross: uint128
50+
liquidity_net: int128
51+
staked_liquidity_net: int128
52+
fee_growth_outside0: uint256
53+
fee_growth_outside1: uint256
54+
reward_growth_outside: uint256
55+
tick_cumulative_outside: int56
56+
seconds_per_liquidity_outside: uint160
57+
seconds_outside: uint32
58+
initialized: bool
59+
4660
struct Position:
4761
id: uint256 # NFT ID on v3, 0 on v2
4862
manager: address # NFT Position Manager on v3, router on v2
@@ -55,6 +69,10 @@ struct Position:
5569
tick_upper: int24 # Position upper tick on v3, 0 on v2
5670
alm: bool # True if Position is deposited into ALM on v3, False on v2
5771

72+
struct Price:
73+
tick_price: int24
74+
liquidity_gross: uint128
75+
5876
struct Token:
5977
token_address: address
6078
symbol: String[100]
@@ -172,6 +190,7 @@ interface IPool:
172190
def gaugeFees() -> GaugeFees: view # v3 gauge fees amounts
173191
def fee() -> uint24: view # v3 fee level
174192
def unstakedFee() -> uint24: view # v3 unstaked fee level
193+
def ticks(_tick: int24) -> TickInfo: view # v3 tick data
175194

176195
interface IVoter:
177196
def gauges(_pool_addr: address) -> address: view
@@ -579,6 +598,7 @@ def _byDataCL(_data: address[3], _token0: address, _token1: address, _account: a
579598
emissions_token: address = empty(address)
580599
token0: IERC20 = IERC20(_token0)
581600
token1: IERC20 = IERC20(_token1)
601+
tick_spacing: int24 = pool.tickSpacing()
582602

583603
fee_voting_reward = gauge.feesVotingReward()
584604
emissions_token = gauge.rewardToken()
@@ -628,7 +648,7 @@ def _byDataCL(_data: address[3], _token0: address, _token1: address, _account: a
628648
total_supply: 0,
629649

630650
nft: nft.address,
631-
type: pool.tickSpacing(),
651+
type: tick_spacing,
632652
tick: slot.tick,
633653
price: price,
634654

@@ -972,3 +992,44 @@ def _is_cl_factory(_factory: address) -> (bool):
972992
)[1]
973993

974994
return len(response) > 0
995+
996+
@external
997+
@view
998+
def prices(_pool: address, _factory: address) -> DynArray[Price, MAX_PRICES]:
999+
"""
1000+
@notice Returns price data at surrounding ticks for a pool
1001+
@param _pool The pool to check price data of
1002+
@param _factory The factory of the pool
1003+
@return Array of Price structs
1004+
"""
1005+
is_cl_factory: bool = self._is_cl_factory(_factory)
1006+
1007+
if is_cl_factory:
1008+
return self._price(_pool)
1009+
else:
1010+
return empty(DynArray[Price, MAX_PRICES])
1011+
1012+
@internal
1013+
@view
1014+
def _price(_pool: address) -> DynArray[Price, MAX_PRICES]:
1015+
"""
1016+
@notice Returns price data at surrounding ticks for a v3 pool
1017+
@param _pool The pool to check price data of
1018+
@return Array of Price structs
1019+
"""
1020+
prices: DynArray[Price, MAX_PRICES] = empty(DynArray[Price, MAX_PRICES])
1021+
pool: IPool = IPool(_pool)
1022+
tick_spacing: int24 = pool.tickSpacing()
1023+
slot: Slot = pool.slot0()
1024+
1025+
# fetch liquidity from the ticks surrounding the current tick
1026+
for index in range((-1 * MAX_PRICES / 2), (MAX_PRICES / 2)):
1027+
tick: int24 = slot.tick + (index * tick_spacing)
1028+
tick_info: TickInfo = pool.ticks(tick)
1029+
1030+
prices.append(Price({
1031+
tick_price: tick,
1032+
liquidity_gross: tick_info.liquidity_gross
1033+
}))
1034+
1035+
return prices

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Below is the list of datasets we support.
3535

3636
### Liquidity Pools Data
3737

38-
`LpSugar.vy` is deployed at `0x90b18D906aa461a45229877469eD4c6A41c31Da6`
38+
`LpSugar.vy` is deployed at `0x3c971c178Bb0697F3D59CC043855e96f389dF61c`
3939

4040
It allows fetching on-chain pools data.
4141
The returned data/struct of type `Lp` values represent:

0 commit comments

Comments
 (0)