File size: 3,219 Bytes
d87b18a
 
 
 
 
 
 
 
 
80df99b
 
 
 
 
 
 
 
65ccdcd
80df99b
 
 
 
 
 
 
 
 
d87b18a
740fcec
 
 
80df99b
740fcec
 
 
80df99b
740fcec
80df99b
740fcec
80df99b
740fcec
80df99b
740fcec
80df99b
740fcec
80df99b
740fcec
 
 
d87b18a
 
 
 
 
 
 
 
 
 
80df99b
d87b18a
 
80df99b
d87b18a
80df99b
d87b18a
80df99b
d87b18a
 
80df99b
 
d87b18a
80df99b
 
 
48f60d7
d87b18a
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Cross-Exchange Symbol Resolver
==============================
Handles ticker format discrepancies and multiplier prefixes (1000, 10000, 1000000, k)
across Binance, Bybit, OKX, and Hyperliquid.
"""

import re

# Known meme assets where uppercase 'K' denotes a 1000x contract prefix (e.g. Hyperliquid kPEPE -> KPEPE)
_KNOWN_K_MULTIPLIER_BASES = frozenset({"PEPE", "SHIB", "BONK", "FLOKI", "LUNC", "NEIRO", "DOGS"})


def _strip_multiplier_from_base(raw_base: str) -> str:
    """Strip exchange multiplier prefixes without mangling tickers that start with 'K' (e.g. KAVA, KAS, KSM)."""
    if not raw_base:
        return ""
    stripped = re.sub(r'^(1000000000|1000000|100000|10000|1000|1M|1m)(?=.)', '', raw_base)
    if stripped != raw_base:
        return stripped.upper()
    if raw_base.startswith("k") and len(raw_base) > 1 and raw_base[1].isupper():
        return raw_base[1:].upper()
    upper_base = raw_base.upper()
    if upper_base.startswith("K") and upper_base[1:] in _KNOWN_K_MULTIPLIER_BASES:
        return upper_base[1:]
    return upper_base


def strip_multiplier_prefix(symbol: str) -> str:
    """
    Strip exchange contract multiplier prefixes (1000, 10000, 1000000, 1000000000, k/K)
    from normalized symbols such as 1000PEPEUSDT -> PEPEUSDT, while preserving tokens like 1INCHUSDT and KAVAUSDT.
    """
    if not symbol:
        return ""
    raw = str(symbol).strip().replace("/", "").replace("-", "").replace("_", "").replace(" ", "")
    quote = ""
    raw_upper = raw.upper()
    for q in ("USDT", "USDC", "USD", "BUSD", "EUR"):
        if raw_upper.endswith(q) and len(raw) > len(q):
            quote = q
            raw = raw[:-len(q)]
            break
    bare = _strip_multiplier_from_base(raw)
    if not bare:
        bare = raw.upper()
    return f"{bare}{quote}"


def get_symbol_variations(symbol: str) -> list[str]:
    """
    Generate candidate ticker variations across exchanges.
    Examples:
    - '1000PEPEUSDT' -> ['1000PEPEUSDT', 'PEPEUSDT', 'kPEPEUSDT', 'PEPE', 'kPEPE', '1000PEPE']
    - 'PEPE'         -> ['PEPEUSDT', '1000PEPEUSDT', 'kPEPEUSDT', 'PEPE', '1000PEPE', 'kPEPE']
    - '1000SATSUSDT' -> ['1000SATSUSDT', '10000SATSUSDT', 'SATSUSDT', 'SATS', ...]
    """
    if not symbol:
        return []
    raw = str(symbol).strip().replace("/", "").replace("-", "").replace("_", "").replace(" ", "")

    quote = "USDT"
    raw_upper = raw.upper()
    for q in ("USDT", "USDC", "USD", "BUSD", "EUR"):
        if raw_upper.endswith(q) and len(raw) > len(q):
            quote = q
            raw = raw[:-len(q)]
            break

    upper_base = raw.upper()
    bare = _strip_multiplier_from_base(raw)

    bases = [upper_base, bare]
    if raw != upper_base and raw not in bases:
        bases.append(raw)
    bases.extend([f"1000{bare}", f"10000{bare}", f"100000{bare}", f"1000000{bare}", f"1M{bare}", f"k{bare}", f"K{bare}"])
    unique_bases = []
    for b in bases:
        if b and b not in unique_bases:
            unique_bases.append(b)

    variations = []
    for b in unique_bases:
        variations.append(f"{b}{quote}")
        variations.append(b)
        if quote != "USDT":
            variations.append(f"{b}USDT")

    return variations