Skip to content

Commit

Permalink
no need for chain header
Browse files Browse the repository at this point in the history
  • Loading branch information
franzns committed Aug 22, 2024
1 parent 1ac76bf commit 0221326
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions automation/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ class PoolBalance:


def get_abi(contract_name: str) -> Union[Dict, List[Dict]]:
project_root_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
project_root_dir = os.path.abspath(
os.path.dirname(os.path.dirname(__file__)))
with open(f"{project_root_dir}/abi/{contract_name}.json") as f:
return json.load(f)

Expand All @@ -165,7 +166,8 @@ def get_balancer_pool_snapshots(block: int, graph_url: str) -> Optional[List[Dic
offset = 0
while True:
result = client.execute(
gql(POOLS_SNAPSHOTS_QUERY.format(first=limit, skip=offset, block=block))
gql(POOLS_SNAPSHOTS_QUERY.format(
first=limit, skip=offset, block=block))
)
all_pools.extend(result["poolSnapshots"])
offset += limit
Expand All @@ -183,7 +185,6 @@ def fetch_all_pools_info(chain: str) -> List[Dict]:
transport = RequestsHTTPTransport(
url=BAL_GQL_URL,
retries=2,
headers={"chainId": CHAIN_TO_CHAIN_ID_MAP[chain]} if chain != "mainnet" else {},
)
client = Client(transport=transport, fetch_schema_from_transport=True)
query = gql(BAL_GET_VOTING_LIST_QUERY)
Expand Down Expand Up @@ -240,13 +241,15 @@ def fetch_token_price_balgql(
transport = RequestsHTTPTransport(
url=BAL_GQL_URL,
retries=2,
headers={"chainId": CHAIN_TO_CHAIN_ID_MAP[chain]} if chain != "mainnet" else {},
headers={
"chainId": CHAIN_TO_CHAIN_ID_MAP[chain]} if chain != "mainnet" else {},
)
client = Client(transport=transport, fetch_schema_from_transport=True)
query = gql(BAL_GQL_QUERY.format(token_addr=token_addr.lower()))
result = client.execute(query)
# Sort result by timestamp desc
result["tokenGetPriceChartData"].sort(key=lambda x: x["timestamp"], reverse=True)
result["tokenGetPriceChartData"].sort(
key=lambda x: x["timestamp"], reverse=True)
# Filter results so they are in between start_date and end_date timestamps
result_slice = [
item
Expand All @@ -257,7 +260,8 @@ def fetch_token_price_balgql(
return None
# Sum all prices and divide by number of days
twap_price = Decimal(
sum([Decimal(item["price"]) for item in result_slice]) / len(result_slice)
sum([Decimal(item["price"])
for item in result_slice]) / len(result_slice)
)
return twap_price

Expand All @@ -280,7 +284,8 @@ def get_twap_bpt_price(
),
abi=get_abi("BalancerVault"),
)
balancer_pool_address, _ = balancer_vault.functions.getPool(balancer_pool_id).call()
balancer_pool_address, _ = balancer_vault.functions.getPool(
balancer_pool_id).call()
weighed_pool_contract = web3.eth.contract(
address=web3.to_checksum_address(balancer_pool_address),
abi=get_abi("WeighedPool"),
Expand Down Expand Up @@ -311,7 +316,8 @@ def get_twap_bpt_price(
if not all([balance.twap_price for balance in balances]):
return None
# Now we have all prices, let's calculate total price
total_price = sum([balance.balance * balance.twap_price for balance in balances])
total_price = sum(
[balance.balance * balance.twap_price for balance in balances])
return total_price / Decimal(total_supply)


Expand Down

0 comments on commit 0221326

Please sign in to comment.