feat: add rich token message renderer with conditional rows
Introduce unified rich token message rendering with fixed row order and conditional visibility based on available sources. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -2,13 +2,15 @@ package view
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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 {
|
||||
@@ -63,6 +65,69 @@ func RenderOnAlphaPriceMessage(symbol string, price float64, change24h float64)
|
||||
)
|
||||
}
|
||||
|
||||
type RichTokenMessageInput struct {
|
||||
Token string
|
||||
|
||||
HasSpot bool
|
||||
SpotPrice float64
|
||||
|
||||
HasFuture bool
|
||||
FuturePrice float64
|
||||
FundingRate float64
|
||||
FundingTimeMs int64
|
||||
|
||||
HasAlpha bool
|
||||
AlphaPrice float64
|
||||
|
||||
HasAlpha24h bool
|
||||
Alpha24hChange float64
|
||||
|
||||
HasMarginAPR bool
|
||||
MarginAPRPercent float64
|
||||
}
|
||||
|
||||
func RenderRichTokenMessage(in RichTokenMessageInput) string {
|
||||
rows := []string{fmt.Sprintf("🪙 %s", strings.ToUpper(in.Token))}
|
||||
|
||||
if in.HasSpot {
|
||||
rows = append(rows, fmt.Sprintf("💵 Spot: $%s", RenderPrice(in.SpotPrice)))
|
||||
}
|
||||
if in.HasFuture {
|
||||
rows = append(rows, fmt.Sprintf("📈 Future: $%s", RenderPrice(in.FuturePrice)))
|
||||
}
|
||||
if in.HasAlpha {
|
||||
rows = append(rows, fmt.Sprintf("🅰️ Alpha: $%s", RenderPrice(in.AlphaPrice)))
|
||||
}
|
||||
|
||||
if in.HasSpot && in.HasFuture && in.SpotPrice > 0 {
|
||||
delta := in.FuturePrice - in.SpotPrice
|
||||
deltaPct := delta / in.SpotPrice * 100
|
||||
rows = append(rows, fmt.Sprintf("🧭 Basis: $%+s (%+.2f%%)", RenderPrice(delta), deltaPct))
|
||||
}
|
||||
|
||||
if in.HasFuture {
|
||||
rows = append(rows, fmt.Sprintf("💸 Funding: %.4f%% %s in %s",
|
||||
in.FundingRate*100,
|
||||
IconOfFundingFeeDirection(in.FundingRate),
|
||||
timex.CdMinuteStringTime(time.UnixMilli(in.FundingTimeMs)),
|
||||
))
|
||||
}
|
||||
|
||||
if in.HasMarginAPR {
|
||||
rows = append(rows, fmt.Sprintf("🏦 Margin: %.3f%% APR", in.MarginAPRPercent))
|
||||
}
|
||||
|
||||
if in.HasAlpha24h {
|
||||
icon := "🟢"
|
||||
if in.Alpha24hChange < 0 {
|
||||
icon = "🔴"
|
||||
}
|
||||
rows = append(rows, fmt.Sprintf("📊 Alpha 24h: %s%+.2f%%", icon, in.Alpha24hChange))
|
||||
}
|
||||
|
||||
return strings.Join(rows, "\n")
|
||||
}
|
||||
|
||||
func RenderOnGetTopPricesMessage(symbols []string, price []float64, fundRate []float64) string {
|
||||
t := helper.NewNoBorderTableWriter("", "Price", "Fund")
|
||||
mFmt := message.NewPrinter(language.AmericanEnglish)
|
||||
|
||||
Reference in New Issue
Block a user