Skip to content

Commit

Permalink
Parameterizing script
Browse files Browse the repository at this point in the history
  • Loading branch information
slundqui committed Nov 15, 2024
1 parent cbaf401 commit abaa8ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
51 changes: 34 additions & 17 deletions scripts/sync_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ def main(argv: Sequence[str] | None = None) -> None:
# pylint: disable=too-many-statements

# Placeholder in case we have cli args
_ = parse_arguments(argv)
parsed_args = parse_arguments(argv)

# TODO Abstract this method out for infra scripts
# Get the rpc uri from env variable
rpc_uri = os.getenv("RPC_URI", None)
if rpc_uri is None:
Expand All @@ -49,31 +48,28 @@ def main(argv: Sequence[str] | None = None) -> None:
interfaces = [pool.interface for pool in deployed_pools]

db_dump_path = Path(".db/")
# Ignore if file not found
# Make sure directory exists
db_dump_path.mkdir(exist_ok=True)

# Ignore if file not found, we start from scratch
try:
chain.load_db(db_dump_path)
print(f"Loaded db from {db_dump_path}")
logging.info("Loaded db from %s", db_dump_path)
except FileNotFoundError:
pass

# TODO load dump path if exists

# TODO make dir if not exist

# TODO sample period different per pool (want to target once per hour)
# backfill_sample_period = 300
backfill_sample_period = 3000

chain_id = chain.chain_id
earliest_block = EARLIEST_BLOCK_LOOKUP[chain_id]

# TODO add backfill to hyperdrive object creation

acquire_data(
start_block=earliest_block,
interfaces=list(interfaces),
db_session=chain.db_session,
lookback_block_limit=None,
backfill=True,
backfill_sample_period=backfill_sample_period,
backfill_sample_period=parsed_args.backfill_sample_period,
backfill_progress_bar=True,
)
analyze_data(
Expand All @@ -82,15 +78,16 @@ def main(argv: Sequence[str] | None = None) -> None:
db_session=chain.db_session,
calc_pnl=True,
backfill=True,
backfill_sample_period=backfill_sample_period,
backfill_sample_period=parsed_args.backfill_sample_period,
backfill_progress_bar=True,
)

# Loop forever, running db once an hour
while True:
print("Updating db")
logging.info("Syncing database")
# TODO to ensure these tables are synced, we want to
# add the block to add the data on.
# TODO add these functions to Hyperdrive object
acquire_data(
start_block=earliest_block,
interfaces=list(interfaces),
Expand All @@ -108,12 +105,15 @@ def main(argv: Sequence[str] | None = None) -> None:

chain.dump_db(db_dump_path)

time.sleep(3600)
time.sleep(parsed_args.dbsync_time)


class Args(NamedTuple):
"""Command line arguments for the script."""

backfill_sample_period: int
dbsync_time: int


# Placeholder for cli args
def namespace_to_args(namespace: argparse.Namespace) -> Args: # pylint: disable=unused-argument
Expand All @@ -129,7 +129,10 @@ def namespace_to_args(namespace: argparse.Namespace) -> Args: # pylint: disable
Args
Formatted arguments
"""
return Args()
return Args(
backfill_sample_period=namespace.backfill_sample_period,
dbsync_time=namespace.dbsync_time,
)


def parse_arguments(argv: Sequence[str] | None = None) -> Args:
Expand All @@ -147,6 +150,20 @@ def parse_arguments(argv: Sequence[str] | None = None) -> Args:
"""
parser = argparse.ArgumentParser(description="Populates a database with trade events on hyperdrive.")

parser.add_argument(
"--backfill-sample-period",
type=int,
default=1000,
help="The block sample period when backfilling data. Default is 1000 blocks.",
)

parser.add_argument(
"--dbsync-time",
type=int,
default=3600,
help="The time in seconds to sleep between db syncs. Default is 3600 seconds (1 hour).",
)

# Use system arguments if none were passed
if argv is None:
argv = sys.argv
Expand Down
2 changes: 0 additions & 2 deletions src/agent0/core/hyperdrive/interactive/local_hyperdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,6 @@ def sync_database(
force_backfill: bool, optional
If True, will force backfilling pool info data. Otherwise will look for
`chain.config.backfill_pool_info` in the chain config.
force_init: bool, optional
If True, will explicitly initialize the database with this pool's info.
"""
if start_block is None:
data_start_block = self._data_start_block
Expand Down

0 comments on commit abaa8ed

Please sign in to comment.