Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions lib/chainsync/chainsync/db/hyperdrive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
convert_hyperdrive_transactions_for_block,
convert_pool_config,
convert_pool_info,
get_wallet_info,
)
from .interface import (
add_checkpoint_infos,
add_pool_config,
add_pool_infos,
add_transactions,
add_wallet_deltas,
add_wallet_infos,
get_all_traders,
get_all_wallet_info,
get_checkpoint_info,
get_current_wallet,
get_current_wallet_info,
get_latest_block_number_from_analysis_table,
get_latest_block_number_from_pool_info_table,
get_latest_block_number_from_table,
Expand All @@ -29,7 +25,6 @@
get_total_wallet_pnl_over_time,
get_transactions,
get_wallet_deltas,
get_wallet_info_history,
get_wallet_pnl,
get_wallet_positions_over_time,
)
Expand All @@ -42,6 +37,5 @@
PoolInfo,
Ticker,
WalletDelta,
WalletInfoFromChain,
WalletPNL,
)
56 changes: 15 additions & 41 deletions lib/chainsync/chainsync/db/hyperdrive/chain_to_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,8 @@
convert_hyperdrive_transactions_for_block,
convert_pool_config,
convert_pool_info,
get_wallet_info,
)
from .interface import (
add_checkpoint_infos,
add_pool_config,
add_pool_infos,
add_transactions,
add_wallet_deltas,
add_wallet_infos,
)
from .interface import add_checkpoint_infos, add_pool_config, add_pool_infos, add_transactions, add_wallet_deltas

_RETRY_COUNT = 10
_RETRY_SLEEP_SECONDS = 1
Expand Down Expand Up @@ -63,33 +55,11 @@ def init_data_chain_to_db(

def data_chain_to_db(
web3: Web3,
base_contract: Contract,
hyperdrive_contract: Contract,
block_number: BlockNumber,
session: Session,
) -> None:
"""Function to query and insert data to dashboard."""
# Query and add block_pool_info
pool_info_dict = None
for _ in range(_RETRY_COUNT):
try:
pool_info_dict = process_hyperdrive_pool_info(
pool_info=get_hyperdrive_pool_info(hyperdrive_contract, block_number),
web3=web3,
hyperdrive_contract=hyperdrive_contract,
position_duration=int(get_hyperdrive_pool_config(hyperdrive_contract)["positionDuration"]),
block_number=block_number,
)
break
except ValueError:
logging.warning("Error in get_hyperdrive_pool_info, retrying")
time.sleep(_RETRY_SLEEP_SECONDS)
continue
if pool_info_dict is None:
raise ValueError("Error in getting pool info")
block_pool_info = convert_pool_info(pool_info_dict)
add_pool_infos([block_pool_info], session)

# Query and add block_checkpoint_info
checkpoint_info_dict = None
for _ in range(_RETRY_COUNT):
Expand Down Expand Up @@ -131,20 +101,24 @@ def data_chain_to_db(
add_transactions(block_transactions, session)
add_wallet_deltas(wallet_deltas, session)

# Query and add wallet info
# TODO put the wallet info query as an optional block,
# and check these wallet values with what we get from the deltas
wallet_info_for_transactions = None
# Query and add block_pool_info
# Adding this last as pool info is what we use to determine if this block is in the db for analysis
pool_info_dict = None
for _ in range(_RETRY_COUNT):
try:
wallet_info_for_transactions = get_wallet_info(
hyperdrive_contract, base_contract, block_number, block_transactions, block_pool_info
pool_info_dict = process_hyperdrive_pool_info(
pool_info=get_hyperdrive_pool_info(hyperdrive_contract, block_number),
web3=web3,
hyperdrive_contract=hyperdrive_contract,
position_duration=int(get_hyperdrive_pool_config(hyperdrive_contract)["positionDuration"]),
block_number=block_number,
)
break
except ValueError:
logging.warning("Error in fetch_contract_transactions_for_block, retrying")
logging.warning("Error in get_hyperdrive_pool_info, retrying")
time.sleep(_RETRY_SLEEP_SECONDS)
continue
if wallet_info_for_transactions is None:
raise ValueError("Error in getting wallet_info")
add_wallet_infos(wallet_info_for_transactions, session)
if pool_info_dict is None:
raise ValueError("Error in getting pool info")
block_pool_info = convert_pool_info(pool_info_dict)
add_pool_infos([block_pool_info], session)
125 changes: 3 additions & 122 deletions lib/chainsync/chainsync/db/hyperdrive/convert_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
from decimal import Decimal
from typing import Any

from eth_typing import BlockNumber
from ethpy.base import get_token_balance, get_transaction_logs
from ethpy.hyperdrive import AssetIdPrefix, decode_asset_id, encode_asset_id
from ethpy.base import get_transaction_logs
from ethpy.hyperdrive import decode_asset_id
from fixedpointmath import FixedPoint
from hexbytes import HexBytes
from web3 import Web3
from web3.contract.contract import Contract
from web3.types import TxData

from .schema import CheckpointInfo, HyperdriveTransaction, PoolConfig, PoolInfo, WalletDelta, WalletInfoFromChain
from .schema import CheckpointInfo, HyperdriveTransaction, PoolConfig, PoolInfo, WalletDelta


def convert_hyperdrive_transactions_for_block(
Expand Down Expand Up @@ -109,124 +108,6 @@ def _convert_scaled_value_to_decimal(input_val: int | None) -> Decimal | None:
return None


# TODO move this function to hyperdrive_interface and return a list of dictionaries
def get_wallet_info(
hyperdrive_contract: Contract,
base_contract: Contract,
block_number: BlockNumber,
transactions: list[HyperdriveTransaction],
pool_info: PoolInfo,
) -> list[WalletInfoFromChain]:
"""Retrieve wallet information at a given block given a transaction.

HyperdriveTransactions are needed here to get
(1) the wallet address of a transaction, and
(2) the token id of the transaction

Arguments
---------
hyperdrive_contract : Contract
The deployed hyperdrive contract instance.
base_contract : Contract
The deployed base contract instance
block_number : BlockNumber
The block number to query
transactions : list[HyperdriveTransaction]
The list of transactions to get events from
pool_info : PoolInfo
The associated pool info, used to extract share price

Returns
-------
list[WalletInfo]
The list of WalletInfo objects ready to be inserted into postgres
"""
# pylint: disable=too-many-locals
out_wallet_info = []
for transaction in transactions:
wallet_addr = transaction.event_operator
if wallet_addr is None:
continue

# Query and add base tokens to walletinfo
num_base_token = get_token_balance(base_contract, wallet_addr, block_number, None)
if num_base_token is not None:
out_wallet_info.append(
WalletInfoFromChain(
blockNumber=block_number,
walletAddress=wallet_addr,
baseTokenType="BASE",
tokenType="BASE",
tokenValue=_convert_scaled_value_to_decimal(num_base_token),
)
)

# Query and add LP tokens to wallet info
lp_token_prefix = AssetIdPrefix.LP.value
# LP tokens always have 0 maturity
lp_token_id = encode_asset_id(lp_token_prefix, timestamp=0)
num_lp_token = get_token_balance(hyperdrive_contract, wallet_addr, block_number, lp_token_id)
if num_lp_token is not None:
out_wallet_info.append(
WalletInfoFromChain(
blockNumber=block_number,
walletAddress=wallet_addr,
baseTokenType="LP",
tokenType="LP",
tokenValue=_convert_scaled_value_to_decimal(num_lp_token),
maturityTime=None,
sharePrice=None,
)
)

# Query and add withdraw tokens to wallet info
withdrawal_token_prefix = AssetIdPrefix.WITHDRAWAL_SHARE.value
# Withdrawal tokens always have 0 maturity
withdrawal_token_id = encode_asset_id(withdrawal_token_prefix, timestamp=0)
num_withdrawal_token = get_token_balance(hyperdrive_contract, wallet_addr, block_number, withdrawal_token_id)
if num_withdrawal_token is not None:
out_wallet_info.append(
WalletInfoFromChain(
blockNumber=block_number,
walletAddress=wallet_addr,
baseTokenType="WITHDRAWAL_SHARE",
tokenType="WITHDRAWAL_SHARE",
tokenValue=_convert_scaled_value_to_decimal(num_withdrawal_token),
maturityTime=None,
sharePrice=None,
)
)

# Query and add shorts and/or longs if they exist in transaction
token_id = transaction.event_id
token_prefix = transaction.event_prefix
token_maturity_time = transaction.event_maturity_time
if (token_id is not None) and (token_prefix is not None):
base_token_type = AssetIdPrefix(token_prefix).name
if base_token_type in ("LONG", "SHORT"):
token_type = base_token_type + "-" + str(token_maturity_time)
# Check here if token is short
# If so, add share price from pool info to data
share_price = None
if (base_token_type) == "SHORT":
share_price = pool_info.sharePrice

num_custom_token = get_token_balance(hyperdrive_contract, wallet_addr, block_number, int(token_id))
if num_custom_token is not None:
out_wallet_info.append(
WalletInfoFromChain(
blockNumber=block_number,
walletAddress=wallet_addr,
baseTokenType=base_token_type,
tokenType=token_type,
tokenValue=_convert_scaled_value_to_decimal(num_custom_token),
maturityTime=token_maturity_time,
sharePrice=share_price,
)
)
return out_wallet_info


def convert_pool_config(pool_config_dict: dict[str, Any]) -> PoolConfig:
"""Converts a pool_config_dict from a call in hyperdrive_interface to the postgres data type

Expand Down
Loading