62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
import json
|
|
import logging
|
|
from hyperliquid.info import Info
|
|
from hyperliquid.utils import constants
|
|
|
|
# Import the setup function from our new logging module
|
|
from logging_utils import setup_logging
|
|
|
|
def save_coin_precision_data():
|
|
"""
|
|
Connects to the Hyperliquid API, gets a list of all listed coins
|
|
and their trade size precision, and saves it to a JSON file.
|
|
"""
|
|
logging.info("Fetching asset information from Hyperliquid...")
|
|
|
|
try:
|
|
info = Info(constants.MAINNET_API_URL, skip_ws=True)
|
|
meta_data = info.meta_and_asset_ctxs()[0]
|
|
all_assets = meta_data.get("universe", [])
|
|
|
|
if not all_assets:
|
|
logging.error("Could not retrieve asset information from the meta object.")
|
|
return
|
|
|
|
# Create a dictionary mapping the coin name to its precision
|
|
coin_precision_map = {}
|
|
for asset in all_assets:
|
|
name = asset.get("name")
|
|
precision = asset.get("szDecimals")
|
|
|
|
if name is not None and precision is not None:
|
|
coin_precision_map[name] = precision
|
|
|
|
# Save the dictionary to a JSON file
|
|
file_name = "_data/coin_precision.json"
|
|
with open(file_name, 'w', encoding='utf-8') as f:
|
|
# indent=4 makes the file readable; sort_keys keeps it organized
|
|
json.dump(coin_precision_map, f, indent=4, sort_keys=True)
|
|
|
|
logging.info(f"Successfully saved coin precision data to '{file_name}'")
|
|
|
|
# Provide an example of how to use the generated file
|
|
# print("\n--- Example Usage in another script ---")
|
|
# print("import json")
|
|
# print("\n# Load the data from the file")
|
|
# print("with open('coin_precision.json', 'r') as f:")
|
|
# print(" precision_data = json.load(f)")
|
|
# print("\n# Access the precision for a specific coin")
|
|
# print("eth_precision = precision_data.get('ETH')")
|
|
# print("print(f'The size precision for ETH is: {eth_precision}')")
|
|
|
|
|
|
except Exception as e:
|
|
logging.error(f"An error occurred: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Setup logging with a specified level and process name
|
|
setup_logging('off', 'CoinLister')
|
|
save_coin_precision_data()
|
|
|