Skip to content

Commit ee4cec0

Browse files
committed
More math
1 parent b862eb5 commit ee4cec0

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

integrations/uniswap_v4.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,29 @@ def get_position_balance(token_id, block, tick, sqrt_price):
123123
# f"tick: {tick}, tick_lower: {tick_lower}, tick_upper: {tick_upper}, token_id: {token_id}, liquidity: {liquidity}"
124124
# )
125125

126-
sqrt_price_l = math.sqrt(1.0001**tick_lower) * (2**96)
127-
sqrt_price_u = math.sqrt(1.0001**tick_upper) * (2**96)
128-
129-
t0 = liquidity * (sqrt_price_u - sqrt_price) / (sqrt_price * sqrt_price_u) * (2**96)
130-
t1 = liquidity * (sqrt_price - sqrt_price_l) / (2**96)
126+
sqrt_ratio_l = math.sqrt(1.0001**tick_lower) * (2**96)
127+
sqrt_ratio_u = math.sqrt(1.0001**tick_upper) * (2**96)
128+
129+
# t0 = liquidity * (sqrt_price_u - sqrt_price) / (sqrt_price * sqrt_price_u) * (2**96)
130+
# t1 = liquidity * (sqrt_price - sqrt_price_l) / (2**96)
131+
132+
if tick_lower < tick < tick_upper:
133+
t0 = (
134+
liquidity
135+
* (sqrt_ratio_u - sqrt_price)
136+
/ (sqrt_price * sqrt_ratio_u)
137+
* (2**96)
138+
)
139+
t1 = liquidity * (sqrt_price - sqrt_ratio_l) / (2**96)
140+
elif tick >= tick_upper:
141+
t1 = liquidity * (sqrt_ratio_u - sqrt_ratio_l) / (2**96)
142+
else:
143+
t0 = (
144+
liquidity
145+
* (sqrt_ratio_u - sqrt_ratio_l)
146+
/ (sqrt_ratio_u * sqrt_ratio_l)
147+
* (2**96)
148+
)
131149

132150
return [abs(t0 / 10**18), abs(t1 / 10**6)]
133151

0 commit comments

Comments
 (0)