Files
crypto-price-bot/internal/services/tele/commands/token.go
thuanle 06c40ef30f refactor: separate direct and related spot resolvers
Keep IsToken checks explicit across futures and direct spot symbols, move futures-derived spot mapping to a clearly named helper, and update token data collection to use related spot resolution.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-27 04:10:06 +07:00

125 lines
3.0 KiB
Go

package commands
import (
"strings"
"gopkg.in/telebot.v3"
"me.thuanle/bbot/internal/configs/tele"
"me.thuanle/bbot/internal/data"
"me.thuanle/bbot/internal/helper/binancex"
"me.thuanle/bbot/internal/services/tele/chat"
"me.thuanle/bbot/internal/services/tele/view"
)
type buildRichTokenMessageArgs struct {
Token string
HasSpot bool
SpotPrice float64
HasFuture bool
FuturePrice float64
FundingRate float64
FundingTimeMs int64
HasAlpha bool
AlphaPrice float64
HasAlpha24h bool
Alpha24h float64
HasMarginAPR bool
MarginAPRPercent float64
}
func buildRichTokenMessageInput(a buildRichTokenMessageArgs) view.RichTokenMessageInput {
return view.RichTokenMessageInput{
Token: a.Token,
HasSpot: a.HasSpot,
SpotPrice: a.SpotPrice,
HasFuture: a.HasFuture,
FuturePrice: a.FuturePrice,
FundingRate: a.FundingRate,
FundingTimeMs: a.FundingTimeMs,
HasAlpha: a.HasAlpha,
AlphaPrice: a.AlphaPrice,
HasAlpha24h: a.HasAlpha24h,
Alpha24hChange: a.Alpha24h,
HasMarginAPR: a.HasMarginAPR,
MarginAPRPercent: a.MarginAPRPercent,
}
}
func showStickerMode(context telebot.Context, token string) {
token = strings.ToUpper(token)
stickerIdx, ok := tele.Token2StickerIdxMap[token]
if !ok {
return
}
stickers, err := context.Bot().StickerSet(tele.StickerSet)
if err == nil && stickers != nil {
_ = context.Reply(&stickers.Stickers[stickerIdx])
}
}
func collectRichTokenData(token string) buildRichTokenMessageArgs {
a := buildRichTokenMessageArgs{Token: token}
if alphaToken, ok := data.Market.GetAlphaToken(token); ok {
a.HasAlpha = true
a.HasAlpha24h = true
a.Alpha24h = alphaToken.GetPercentChange24h()
if alphaPrice, ok := data.Market.GetAlphaPrice(token + "USDT"); ok {
a.AlphaPrice = alphaPrice
} else {
a.AlphaPrice = alphaToken.GetPrice()
}
}
futureSymbols := binancex.Token2FutureSymbols(token)
for _, futureSymbol := range futureSymbols {
if fp, fr, ft, ok := data.Market.GetFuturePrice(futureSymbol); ok {
a.HasFuture = true
a.FuturePrice = fp
a.FundingRate = fr
a.FundingTimeMs = ft
break
}
}
spotSymbols := binancex.Token2RelatedSpotSymbols(token)
for _, spotSymbol := range spotSymbols {
if sp, ok := data.Market.GetSpotPrice(spotSymbol); ok {
a.HasSpot = true
a.SpotPrice = sp
break
}
}
marginRates := data.Market.GetMarginInterestRates()
a.MarginAPRPercent = marginRates[token] * 365 * 100
a.HasMarginAPR = marginRates[token] != 0
return a
}
func OnTokenInfoByToken(context telebot.Context, token string) error {
token = strings.ToUpper(token)
showStickerMode(context, token)
args := collectRichTokenData(token)
if !args.HasSpot && !args.HasFuture && !args.HasAlpha {
return nil
}
msg := view.RenderRichTokenMessage(buildRichTokenMessageInput(args))
_ = chat.ReplyMessage(context, msg)
return nil
}
func OnTokenInfo(context telebot.Context) error {
text := strings.ToLower(context.Text())
return OnTokenInfoByToken(context, text)
}