WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
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
43 changes: 43 additions & 0 deletions hyperliquid/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Optional,
SpotMeta,
SpotMetaAndAssetCtxs,
SpotAssetCtx,
Subscription,
cast,
)
Expand Down Expand Up @@ -396,6 +397,48 @@ def spot_meta_and_asset_ctxs(self) -> SpotMetaAndAssetCtxs:
]
"""
return cast(SpotMetaAndAssetCtxs, self.post("/info", {"type": "spotMetaAndAssetCtxs"}))

def spot_asset_ctx(self, baseAsset: str, quoteAsset: str) -> SpotAssetCtx:
"""Retrieve exchange spot asset contexts for a given trading pair
Fetches all asset contexts and filters for the specified trading pair
POST /info
Args:
baseAsset (str): Base asset of the trading pair. I.e. UBTC for BTC/USDC
quoteAsset (str): Quote asset of the trading pair. Most commonly USDC.
Returns:
{
dayNtlVlm: float string,
markPx: float string,
midPx: Optional(float string),
prevDayPx: float string,
circulatingSupply: float string,
coin: str
}
"""
spot_metadata_and_ctxs = self.spot_meta_and_asset_ctxs()

# filter for token metadata by symbol, to obtain index information
token_infos = list(
filter(lambda token: token['name'] in [baseAsset, quoteAsset], spot_metadata_and_ctxs[0]["tokens"])
)
assert len(token_infos) == 2, "Tokens not found in Hyperliquid spot markets"

# filter markets for matching token indexes to obtain market index
markets = list(filter(
lambda market:
(market['tokens'][0] == token_infos[0]["index"] and market['tokens'][1] == token_infos[1]["index"])
or (market['tokens'][0] == token_infos[1]["index"] and market['tokens'][1] == token_infos[0]["index"]),
spot_metadata_and_ctxs[0]['universe']
))
assert len(markets) == 1, "Market not found in Hyperliquid spot markets"
market = markets[0]

# assumes that if a market exists, then a corresponding context exists one-to-one
ctx = list(filter(
lambda ctx: ctx['coin'] == market['name'],
spot_metadata_and_ctxs[1]
))[0]
return cast(SpotAssetCtx, ctx)

def funding_history(self, name: str, startTime: int, endTime: Optional[int] = None) -> Any:
"""Retrieve funding history for a given coin
Expand Down
Loading