fromhyperliquid_authimportHyperliquidAuth# Should now be found
fromhyperliquid.utilsimportconstants# For network constants
logger=logging.getLogger(__name__)
# Global auth instance cache (one per network to support mainnet/testnet switching if needed)
_auth_instances:dict[str,HyperliquidAuth]={}
defsetup(base_url=None,skip_ws=False,perp_dexs=None):# skip_ws and perp_dexs are not used by HyperliquidAuth directly but kept for signature compatibility
# Default to mainnet if base_url is not provided or not recognized
ifbase_url==constants.TESTNET_API_URL:
network="testnet"
elifbase_url==constants.MAINNET_API_URL:
network="mainnet"
elifbase_urlisNone:# If base_url is explicitly None, default to mainnet
network="mainnet"
logger.info(f"example_utils.setup: base_url not provided, defaulting to MAINNET_API_URL for network {network}")
else:# Unknown base_url, default to mainnet and log warning
network="mainnet"# Fallback for unrecognized URLs
logger.warning(f"example_utils.setup: Unknown base_url '{base_url}', defaulting to MAINNET_API_URL for network {network}. This might cause issues if a testnet URL was intended but not recognized.")
# Initialize auth for the network if it doesn't exist or if network has changed for the instance
logger.warning(f"Re-initializing HyperliquidAuth for {network} due to base_url mismatch. Expected {current_expected_base_url}, got {_auth_instances[network].base_url}.")
_auth_instances[network]=HyperliquidAuth(
config_dir=project_root,
network=network,# This network string is derived from the current base_url
auto_reconnect=True
)
auth_instance=_auth_instances[network]
try:
# The connect method of HyperliquidAuth handles agent vs direct logic.
address,info,exchange=auth_instance.connect()
# The skip_ws parameter for Info client is handled internally by HyperliquidAuth
# (_connect_with_agent, _connect_direct set skip_ws=True for their Info instances)
# Handle perp_dexs parameter (if needed and possible post-init)
# This is tricky as Info objects are usually configured at init.
# For robust perp_dexs handling, HyperliquidAuth would need to accept it and pass to Info constructor.
ifperp_dexsisnotNone:
ifhasattr(info,'perp_dexs'):
try:
# This assumes info.perp_dexs is a settable property or attribute.
# This might not be standard or might require re-initialization of Info.
logger.debug(f"perp_dexs parameter was provided to example_utils.setup. The Info object from HyperliquidAuth may or may not support post-init setting of perp_dexs. This parameter might be ignored or require changes in HyperliquidAuth.")
# If critical, HyperliquidAuth should be modified to pass perp_dexs to its Info() calls.
exceptAttributeError:# Should not happen if hasattr is true, but safeguard
logger.warning("Could not set perp_dexs on the Info object post-initialization. This parameter might be ignored.")
else:
logger.warning("Info object from HyperliquidAuth does not have a 'perp_dexs' attribute. Parameter ignored.")
logger.info(f"example_utils.setup: Successfully connected via HyperliquidAuth for network {network}. Address: {address}")
returnaddress,info,exchange
exceptExceptionase:
logger.error(f"example_utils.setup: Failed to connect to Hyperliquid on {network} network: {e}",exc_info=True)