Files
thuanle 9c39423315 Rewrite price lookup from WebSocket to REST API
Replace unreliable WebSocket connections with on-demand REST API calls
for spot and futures prices. Add cached trading pair list (refreshed
hourly) for symbol validation, and /refresh command for manual updates.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-26 15:14:09 +07:00

103 lines
1.9 KiB
Go

package binancex
import (
"strings"
"me.thuanle/bbot/internal/configs/binance"
"me.thuanle/bbot/internal/data"
"me.thuanle/bbot/internal/utils/stringx"
)
func Symbol2Token(sym string) string {
token := strings.ToUpper(sym)
for _, prefix := range binance.SymbolPrefixList {
token, _ = strings.CutPrefix(token, prefix)
}
for suffix, abbr := range binance.SymbolSuffixMap {
var f bool
token, f = stringx.ReplaceSuffix(token, suffix, abbr)
if f {
break
}
}
return token
}
var (
checkingPrefixList = append(binance.SymbolPrefixList, "")
)
func testSym(sym string) bool {
return data.Market.IsFuturesPair(sym)
}
func Token2Symbols(token string) []string {
var syms []string
if !stringx.IsAlphaNumeric(token) {
return syms
}
token = strings.ToUpper(token)
for _, prefix := range checkingPrefixList {
prefix = strings.ToUpper(prefix)
s := prefix + token
//no suffix
if testSym(s) {
syms = append(syms, s)
continue
}
for suffix, abbr := range binance.SymbolSuffixMap {
suffix = strings.ToUpper(suffix)
abbr = strings.ToUpper(abbr)
//suffix
sym := s + suffix
if testSym(sym) {
syms = append(syms, sym)
continue
}
//suffix abbr
symAbr, found := stringx.ReplaceSuffix(s, abbr, suffix)
if found && testSym(symAbr) {
syms = append(syms, symAbr)
continue
}
}
}
return syms
}
func IsToken(s string) bool {
// First check regular symbols
if len(Token2Symbols(s)) > 0 {
return true
}
// Then check Alpha tokens
s = strings.ToUpper(s)
return data.Market.IsAlphaToken(s)
}
// Future2SpotSymbol convert future symbol to spot symbol
func Future2SpotSymbol(sym string) string {
for _, prefix := range binance.SymbolPrefixList {
var f bool
sym, f = strings.CutPrefix(sym, prefix)
if f {
break
}
}
spotSym, ok := binance.Future2SpotSymbolMap[sym]
if !ok {
return sym
}
return spotSym
}