Files
crypto-price-bot/internal/helper/binancex/symbol.go
T
thuanle 440fa3bacd Fix concurrent map read/write race condition in MarketData
Add sync.RWMutex to protect future and spot price maps accessed by
WebSocket handlers (writers) and Telegram bot handlers (readers).
Also adds Alpha token support with caching.

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

104 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 {
_, _, _, test := data.Market.GetFuturePrice(sym)
return test
}
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
}