-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_fee_by_hour.py
28 lines (27 loc) · 1.46 KB
/
aggregate_fee_by_hour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import calendar
import time
import datetime
from common import *
#Please enter UTC time !
#Cannot be less than an hour
#First hour is incomplete and not used
def aggregate_fee_by_hour_when_in_range(pool_id, token_0_decimal, token_1_decimal, priceA, priceB, year, month, day, hour, minutes, seconds, differenceInDays, L):
SECONDS_IN_DAY = 86400
s = f"{seconds}/{minutes}/{hour}/{day}/{month}/{year}"
timestamp_now = calendar.timegm(datetime.datetime.strptime(s, "%S/%M/%H/%d/%m/%Y").timetuple())
timestamp_old = timestamp_now - SECONDS_IN_DAY * differenceInDays
fee_prices = getFeesAndPrices(timestamp_old, timestamp_now, pool_id)
total_fee_0 = 0
total_fee_1 = 0
fee_0_previous = int(fee_prices[0]["feeGrowthGlobal0X128"])
fee_1_previous = int(fee_prices[0]["feeGrowthGlobal1X128"])
# Fees earned during each hour are sumed up.
for fee_price in fee_prices:
price = price_to_int(int(fee_price["sqrtPrice"]), token_0_decimal, token_1_decimal)
# This is an apporximation. Price is considered to to be in range or not during an hour.
if price > priceA and price < priceB
total_fee_0 = total_fee_0 + int(fee_price["feeGrowthGlobal0X128"]) - fee_0_previous
total_fee_1 = total_fee_1 + int(fee_price["feeGrowthGlobal1X128"]) - fee_1_previous
fee_0_previous = int(fee_price["feeGrowthGlobal0X128"])
fee_1_previous = int(fee_price["feeGrowthGlobal1X128"])
return L*total_fee_0, L*total_fee_1