-
-
Notifications
You must be signed in to change notification settings - Fork 294
feat(perps-controller): human-readable market names & ranked search (TAT-2413) #9082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d84fd97
feat(perps-controller): add shared human-readable market names
abretonc7s f44f075
feat(perps-controller): add relevance-ranked market search helper
abretonc7s 25caf7c
Merge remote-tracking branch 'origin/main' into TAT-2413-feat-dev-tas…
abretonc7s dc03f5a
fix(perps-controller): satisfy CI lint and changelog checks
abretonc7s 5463e88
Merge remote-tracking branch 'origin/main' into TAT-2413-feat-dev-tas…
abretonc7s 7f1f028
Merge remote-tracking branch 'origin/main' into TAT-2413-feat-dev-tas…
abretonc7s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /** | ||
| * Market search ranking (TAT-2413). | ||
| * | ||
| * Provisional, standalone helper layered on the same match semantics as | ||
| * `filterMarketsByQuery` (case-insensitive substring on a market's ticker symbol | ||
| * and human-readable name). It adds the one thing `filterMarketsByQuery` does | ||
| * not: relevance ranking — exact matches first, then prefix, then substring; | ||
| * ties keep their input order (stable). No fuzzy/phonetic matching (out of scope | ||
| * for v1). | ||
| * | ||
| * Kept in its own file so it can be promoted or relocated later without touching | ||
| * the shared `marketUtils`. A market matches here (rank !== null) iff | ||
| * `filterMarketsByQuery` would include it, so the two stay behaviorally aligned. | ||
| * | ||
| * Portable: no platform-specific imports. | ||
| */ | ||
| import type { PerpsMarketData } from '../types'; | ||
|
|
||
| /** | ||
| * Relevance tier for a market/query match. Lower values sort first. | ||
| */ | ||
| export enum MarketMatchRank { | ||
| Exact = 0, | ||
| Prefix = 1, | ||
| Substring = 2, | ||
| } | ||
|
|
||
| /** | ||
| * Rank a single field value against a normalized query. | ||
| * | ||
| * @param value - Field value (e.g. symbol or name); may be undefined. | ||
| * @param query - Already trimmed, lower-cased, non-empty query. | ||
| * @returns The match tier, or null when the field does not match. | ||
| */ | ||
| function fieldRank( | ||
| value: string | undefined, | ||
| query: string, | ||
| ): MarketMatchRank | null { | ||
| if (!value) { | ||
| return null; | ||
| } | ||
| const normalized = value.toLowerCase(); | ||
| if (normalized === query) { | ||
| return MarketMatchRank.Exact; | ||
| } | ||
| if (normalized.startsWith(query)) { | ||
| return MarketMatchRank.Prefix; | ||
| } | ||
| if (normalized.includes(query)) { | ||
| return MarketMatchRank.Substring; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Compute the best (lowest) relevance rank for a market against a search query, | ||
| * considering both its ticker symbol and human-readable name. | ||
| * | ||
| * @param market - Market to score (uses `symbol` and `name`). | ||
| * @param searchQuery - User search text (trimmed/cased internally). | ||
| * @returns The match rank, or null when the market does not match (or the query | ||
| * is empty/whitespace). | ||
| */ | ||
| export function getMarketMatchRank( | ||
| market: Pick<PerpsMarketData, 'symbol' | 'name'>, | ||
| searchQuery: string, | ||
| ): MarketMatchRank | null { | ||
| if (!searchQuery?.trim()) { | ||
| return null; | ||
| } | ||
| const query = searchQuery.toLowerCase().trim(); | ||
| const ranks = [ | ||
| fieldRank(market.symbol, query), | ||
| fieldRank(market.name, query), | ||
| ].filter((rank): rank is MarketMatchRank => rank !== null); | ||
|
|
||
| return ranks.length > 0 ? Math.min(...ranks) : null; | ||
| } | ||
|
|
||
| /** | ||
| * Filter and rank markets by a search query, matching the human-readable name or | ||
| * ticker symbol. Exact matches sort first, then prefix, then substring; markets | ||
| * sharing a rank keep their input order (stable). An empty/whitespace query | ||
| * returns the markets unchanged (no filtering), matching `filterMarketsByQuery`. | ||
| * | ||
| * @param markets - Markets to search. | ||
| * @param searchQuery - User search text. | ||
| * @returns Matching markets ordered by relevance. | ||
| */ | ||
| export function rankMarketsByQuery( | ||
| markets: PerpsMarketData[], | ||
| searchQuery: string, | ||
| ): PerpsMarketData[] { | ||
| if (!searchQuery?.trim()) { | ||
| return markets; | ||
| } | ||
| const query = searchQuery.toLowerCase().trim(); | ||
|
|
||
| const matches: { market: PerpsMarketData; rank: MarketMatchRank }[] = []; | ||
| markets.forEach((market) => { | ||
| const rank = getMarketMatchRank(market, query); | ||
| if (rank !== null) { | ||
| matches.push({ market, rank }); | ||
| } | ||
| }); | ||
|
|
||
| // Stable sort by rank only; Array.prototype.sort is stable in modern engines, | ||
| // so equal-rank markets retain their original relative order. | ||
| matches.sort((a, b) => a.rank - b.rank); | ||
| return matches.map((match) => match.market); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HyperLiquid’s meta and perpDexs responses do not provide per asset human readable names, which matches the PR rationale. However, the online docs also list perpAnnotation / perpConciseAnnotations with optional displayName and keywords.
Did we choose the curated map because those annotations are incomplete, optional, too expensive to fetch, or not stable enough?