Skip to content

Commit

Permalink
fix: rebalance data in non indexed log
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Aug 24, 2022
1 parent dad14a7 commit 9137f87
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 11 deletions.
6 changes: 5 additions & 1 deletion balanced_backend/models/volumes_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class VolumeIntervalBase(BaseModel):
decimals: float = 1e18
contract_name: str
method: str
indexed_position: int

# Can be either one of indexed position or non_indexed_position
indexed_position: int = None
non_indexed_position: int = None

update_interval: int = 86400

init_chart_time: int = None
Expand Down
39 changes: 35 additions & 4 deletions balanced_backend/utils/values.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
from typing import List
from typing import List, TYPE_CHECKING
import ast


if TYPE_CHECKING:
from balanced_backend.models.volumes_base import VolumeIntervalBase

def convert_hex_int(hex_string: str) -> int:
return int(hex_string, 16)


def extract_indexed_log(indexed_log: str, position: int):
log_list = ast.literal_eval(indexed_log)

try:
return log_list[position + 1]
except IndexError as e:
raise Exception(f"Index error at position={position} on indexed_log={indexed_log}", e)
raise Exception(str(e) + f"Index error at position={position} on indexed_log={indexed_log}")


def extract_non_indexed_log(data: str, position: int):
log_list = ast.literal_eval(data)
try:
return log_list[position]
except IndexError as e:
raise Exception(str(e) + f"Index error at position={position} on indexed_log={data}")


def get_total_indexed(
Expand All @@ -22,8 +32,29 @@ def get_total_indexed(
) -> float:
fees = 0
for i in events:
value = extract_indexed_log(i['indexed'], indexed_position)
value = extract_indexed_log(indexed_log=i['indexed'], position=indexed_position)
fee = convert_hex_int(value) / decimals
fees += fee

return fees


def get_total_non_indexed(
events: List[dict],
non_indexed_position: int,
decimals: float = 1e18
) -> float:
fees = 0
for i in events:
value = extract_non_indexed_log(data=i['indexed'], position=non_indexed_position)
fee = convert_hex_int(value) / decimals
fees += fee

return fees


def get_total_volume(events: List[dict], context: 'VolumeIntervalBase') -> float:
if context.indexed_position is None:
return get_total_non_indexed(
events, context.non_indexed_position, context.decimals)
return get_total_indexed(events, context.indexed_position, context.decimals)
7 changes: 3 additions & 4 deletions balanced_backend/workers/volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from balanced_backend.metrics import prom_metrics
from balanced_backend.utils.time_to_block import get_block_from_timestamp
from balanced_backend.utils.api import get_logs_in_blocks
from balanced_backend.utils.values import get_total_indexed
from balanced_backend.utils.values import get_total_volume

from balanced_backend.models.volumes_base import VolumeIntervalBase
from balanced_backend.tables.volumes import ContractMethodVolume
Expand Down Expand Up @@ -37,10 +37,9 @@ def set_table_value_from_time_period(

if events is not None:
try:
value = get_total_indexed(
value = get_total_volume(
events=events,
indexed_position=context.indexed_position,
decimals=context.decimals,
context=context,
)
except IndexError as e:
logger.info(f"Error processing "
Expand Down
3 changes: 2 additions & 1 deletion balanced_backend/workers/volumes_addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
'contract_name': 'loans',
'address': addresses.LOANS_CONTRACT_ADDRESS,
'method': 'Rebalance',
'indexed_position': 3,
# 'indexed_position': 3,
'non_indexed_position': 2,
# 'init_chart_time': 1619308800,
'init_chart_block': 49969594,
'decimals': 1e18,
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/utils/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ def test_get_logs_in_blocks():
)

assert len(events) == 138


def test_get_logs_in_blocks_rebalance():
events = get_logs_in_blocks(
address="cx66d4d90f5f113eba575bf793570135f9b10cece1",
method="Rebalance",
block_start=39340509,
block_end=39390509 + 1000,
)

assert len(events) > 1
9 changes: 8 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from balanced_backend.utils.values import extract_indexed_log
from balanced_backend.utils.values import extract_indexed_log, extract_non_indexed_log


def test_extract_indexed_log():
Expand All @@ -8,3 +8,10 @@ def test_extract_indexed_log():
1,
)
assert x == "0x395ed40634fd7f2"


def test_extract_non_indexed_log():
data: str = \
str(["{1239: {'d': -945083222835344506, 'c': -678180511240212612}, 838: {'d': -397845214119725128, 'c': -285489006879973336}, 4951: {'d': -8765848392046875091, 'c': -6290268835941734569}, 2970: {'d': -4816438747964220353, 'c': -3456219318603468639}, 4953: {'d': -66425697738647313, 'c': -47666292833697024}, 1246: {'d': -55693307228090922, 'c': -39964856698324189}, 1248: {'d': -1296054805810992622, 'c': -930033556371118219}, 1251: {'d': -500812671914522658, 'c': -359377233314553591}, 1258: {'d': -75943926404420253, 'c': -54496460830214050}, 1241: {'d': -58903825251227064, 'c': -42268686352335224}, 947: {'d': -40524946551792612562, 'c': -29080220986279452100}, 4625: {'d': -234742655611567121, 'c': -168448545425373330}, 5272: {'d': -83260956311823715, 'c': -59747074705757737}, 5378: {'d': -82997327612525686, 'c': -59557897877995259}, 5356: {'d': -8054950709158261973, 'c': -5780137090533660402}, 5383: {'d': -270030616104503386, 'c': -193770767330791054}, 5382: {'d': -901396147789192976, 'c': -646831184351832943}, 5385: {'d': -40107776210308462, 'c': -28780864497214715}, 740: {'d': -55998077417266965, 'c': -40183556171251131}, 1536: {'d': -794340585838346357, 'c': -570009382863158602}, 4780: {'d': -23299507895257729, 'c': -16719450514258263}, 1266: {'d': -109517179799843975, 'c': -78588229260297065}, 1254: {'d': -649558600097928, 'c': -466115547129586}, 618: {'d': -7024419969444160505, 'c': -5040640454659270330}, 1276: {'d': -6170534928121672526, 'c': -4427902676787039906}, 1277: {'d': -54450063719098853, 'c': -39072719902164796}, 1280: {'d': -136199628274874016, 'c': -97735237810134760}, 1278: {'d': -1377013901950722632, 'c': -988128843519341493}, 1281: {'d': -77532477262391323, 'c': -55636386084370840}, 1256: {'d': -22510324369427954, 'c': -16153141776490270}, 1283: {'d': -192210909197245084, 'c': -137928268660044135}, 1290: {'d': -6276600173138664, 'c': -4504013838589956}, 1293: {'d': -1729456082999581451, 'c': -1241037172385077153}, 1289: {'d': -1690096615843917310, 'c': -1212793285589912686}, 1292: {'d': -284519743055905090, 'c': -204167992978122126}, 1308: {'d': -2900161859710395450, 'c': -2081121752216767352}, 1316: {'d': -42522989235982946, 'c': -30513992718020420}, 1315: {'d': -3975827570567562931, 'c': -2853006708045441140}, 1318: {'d': -1091571954048340265, 'c': -783299087281497291}, 1298: {'d': -197649823841183688, 'c': -141831169298521341}, 1321: {'d': -170571922328638, 'c': -122400388339361}, 4587: {'d': -9873770370359734, 'c': -7085300506710679}, 3015: {'d': -3507077252916001237, 'c': -2516637039864025035}, 868: {'d': -22856130576191885, 'c': -16401288208917269}, 1331: {'d': -1273660623462055061, 'c': -913963756730991710}, 827: {'d': -3375816671701593005, 'c': -2422446003643309164}, 1337: {'d': -67645671792865135813, 'c': -48541731745105341159}, 1117: {'d': -828388383612624523, 'c': -594441678711019877}, 4515: {'d': -908808387220928, 'c': -652150119452773}, 4523: {'d': -778148839000334767, 'c': -558390377379672277}}","0x24a78f3f84e93f0115dc"])
x = extract_non_indexed_log(data=data, position=1)
assert x

0 comments on commit 9137f87

Please sign in to comment.