Files
crypto-price-bot/internal/services/tele/view/price.go
T
thuanle ced55e5f16
Build Docker Image / build (amd64) (push) Successful in 1m31s
Add dedicated Alpha token price display with 24h change
Alpha tokens now show a simplified format with price and 24h change
instead of the regular token format with empty futures/funding data.

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

78 lines
2.0 KiB
Go

package view
import (
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
"golang.org/x/text/language"
"golang.org/x/text/message"
"me.thuanle/bbot/internal/helper/binancex"
"me.thuanle/bbot/internal/services/tele/view/helper"
"me.thuanle/bbot/internal/utils/timex"
"time"
)
func RenderPrice(price float64) string {
mFmt := message.NewPrinter(language.AmericanEnglish)
switch {
case price > 1000:
return mFmt.Sprintf("%0.0f", price)
case price > 100:
return mFmt.Sprintf("%0.1f", price)
case price > 10:
return mFmt.Sprintf("%0.2f", price)
case price > 1:
return mFmt.Sprintf("%0.3f", price)
case price > 0.1:
return mFmt.Sprintf("%0.4f", price)
case price > 0.01:
return mFmt.Sprintf("%0.5f", price)
case price > 0.001:
return mFmt.Sprintf("%0.6f", price)
case price > 0.0001:
return mFmt.Sprintf("%0.7f", price)
default:
return mFmt.Sprintf("%0.8f", price)
}
}
func RenderOnPriceMessage(symbol string, spotPrice float64, futurePrice float64, fundrate float64, fundtime int64, marginRate float64) string {
return fmt.Sprintf(
"%s\n"+
" Spot: %s - Future: %s \n"+
" Fund rate: %.4f%% %s in %s\n"+
" Margin rate: %.3f%%",
RenderSymbolInfo(symbol),
RenderPrice(spotPrice), RenderPrice(futurePrice),
fundrate*100, IconOfFundingFeeDirection(fundrate), timex.CdMinuteStringTime(time.UnixMilli(fundtime)),
marginRate*365*100,
)
}
func RenderOnAlphaPriceMessage(symbol string, price float64, change24h float64) string {
icon := "🟢"
if change24h < 0 {
icon = "🔴"
}
return fmt.Sprintf(
"🅰️ %s\n"+
" Price: $%s\n"+
" 24h: %s%.2f%%",
symbol,
RenderPrice(price),
icon, change24h,
)
}
func RenderOnGetTopPricesMessage(symbols []string, price []float64, fundRate []float64) string {
t := helper.NewNoBorderTableWriter("", "Price", "Fund")
mFmt := message.NewPrinter(language.AmericanEnglish)
for i, symbol := range symbols {
t.AppendRow(table.Row{
binancex.Symbol2Token(symbol),
mFmt.Sprintf("%.2f", price[i]),
mFmt.Sprintf("%.3f%%", fundRate[i]*100),
})
}
return t.Render()
}