Add rich unified token price message #17
@@ -11,6 +11,45 @@ import (
|
|||||||
"me.thuanle/bbot/internal/services/tele/view"
|
"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) {
|
func showStickerMode(context telebot.Context, token string) {
|
||||||
token = strings.ToUpper(token)
|
token = strings.ToUpper(token)
|
||||||
stickerIdx, ok := tele.Token2StickerIdxMap[token]
|
stickerIdx, ok := tele.Token2StickerIdxMap[token]
|
||||||
@@ -25,37 +64,67 @@ func showStickerMode(context telebot.Context, token string) {
|
|||||||
|
|
||||||
func OnTokenInfoByToken(context telebot.Context, token string) error {
|
func OnTokenInfoByToken(context telebot.Context, token string) error {
|
||||||
token = strings.ToUpper(token)
|
token = strings.ToUpper(token)
|
||||||
|
|
||||||
// Check if it's an Alpha token first
|
|
||||||
if data.Market.IsAlphaToken(token) {
|
|
||||||
showStickerMode(context, token)
|
showStickerMode(context, token)
|
||||||
|
|
||||||
if alphaToken, exists := data.Market.GetAlphaToken(token); exists {
|
var (
|
||||||
sp := alphaToken.GetPrice()
|
hasAlpha, hasAlpha24h bool
|
||||||
change24h := alphaToken.GetPercentChange24h()
|
alphaPrice, alpha24h float64
|
||||||
_ = chat.ReplyMessage(context, view.RenderOnAlphaPriceMessage(token, sp, change24h))
|
hasFuture, hasSpot bool
|
||||||
}
|
futurePrice float64
|
||||||
return nil
|
fundingRate float64
|
||||||
|
fundingTime int64
|
||||||
|
spotPrice float64
|
||||||
|
)
|
||||||
|
|
||||||
|
if alphaToken, ok := data.Market.GetAlphaToken(token); ok {
|
||||||
|
hasAlpha = true
|
||||||
|
alphaPrice = alphaToken.GetPrice()
|
||||||
|
hasAlpha24h = true
|
||||||
|
alpha24h = alphaToken.GetPercentChange24h()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regular token handling
|
|
||||||
symbols := binancex.Token2Symbols(token)
|
symbols := binancex.Token2Symbols(token)
|
||||||
if len(symbols) == 0 {
|
if len(symbols) > 0 {
|
||||||
return nil
|
fp, fr, ft, ok := data.Market.GetFuturePrice(symbols[0])
|
||||||
}
|
if ok {
|
||||||
|
hasFuture = true
|
||||||
|
futurePrice = fp
|
||||||
|
fundingRate = fr
|
||||||
|
fundingTime = ft
|
||||||
|
|
||||||
showStickerMode(context, token)
|
|
||||||
|
|
||||||
fp, fundRate, fundTime, ok := data.Market.GetFuturePrice(symbols[0])
|
|
||||||
marginRates := data.Market.GetMarginInterestRates()
|
|
||||||
tokenInterestRate := marginRates[token]
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
sSymbol := binancex.Future2SpotSymbol(symbols[0])
|
sSymbol := binancex.Future2SpotSymbol(symbols[0])
|
||||||
sp, _ := data.Market.GetSpotPrice(sSymbol)
|
if sp, ok := data.Market.GetSpotPrice(sSymbol); ok {
|
||||||
|
hasSpot = true
|
||||||
|
spotPrice = sp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ = chat.ReplyMessage(context, view.RenderOnPriceMessage(symbols[0], sp, fp, fundRate, fundTime, tokenInterestRate))
|
marginRates := data.Market.GetMarginInterestRates()
|
||||||
|
marginAPR := marginRates[token] * 365 * 100
|
||||||
|
hasMarginAPR := marginRates[token] != 0
|
||||||
|
|
||||||
|
if !hasSpot && !hasFuture && !hasAlpha {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := view.RenderRichTokenMessage(buildRichTokenMessageInput(buildRichTokenMessageArgs{
|
||||||
|
Token: token,
|
||||||
|
HasSpot: hasSpot,
|
||||||
|
SpotPrice: spotPrice,
|
||||||
|
HasFuture: hasFuture,
|
||||||
|
FuturePrice: futurePrice,
|
||||||
|
FundingRate: fundingRate,
|
||||||
|
FundingTimeMs: fundingTime,
|
||||||
|
HasAlpha: hasAlpha,
|
||||||
|
AlphaPrice: alphaPrice,
|
||||||
|
HasAlpha24h: hasAlpha24h,
|
||||||
|
Alpha24h: alpha24h,
|
||||||
|
HasMarginAPR: hasMarginAPR,
|
||||||
|
MarginAPRPercent: marginAPR,
|
||||||
|
}))
|
||||||
|
|
||||||
|
_ = chat.ReplyMessage(context, msg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestBuildRichTokenMessageInput_AllSources(t *testing.T) {
|
||||||
|
in := buildRichTokenMessageInput(buildRichTokenMessageArgs{
|
||||||
|
Token: "ETH",
|
||||||
|
SpotPrice: 3245,
|
||||||
|
HasSpot: true,
|
||||||
|
FuturePrice: 3251,
|
||||||
|
FundingRate: 0.000123,
|
||||||
|
FundingTimeMs: 1740000000000,
|
||||||
|
HasFuture: true,
|
||||||
|
AlphaPrice: 3248,
|
||||||
|
HasAlpha: true,
|
||||||
|
Alpha24h: -3.21,
|
||||||
|
HasAlpha24h: true,
|
||||||
|
MarginAPRPercent: 7.665,
|
||||||
|
HasMarginAPR: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
if !in.HasSpot || !in.HasFuture || !in.HasAlpha || !in.HasAlpha24h || !in.HasMarginAPR {
|
||||||
|
t.Fatalf("expected all source flags true: %+v", in)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildRichTokenMessageInput_OnlyAlpha(t *testing.T) {
|
||||||
|
in := buildRichTokenMessageInput(buildRichTokenMessageArgs{
|
||||||
|
Token: "ABC",
|
||||||
|
HasAlpha: true,
|
||||||
|
AlphaPrice: 0.1234,
|
||||||
|
HasAlpha24h: true,
|
||||||
|
Alpha24h: 12.4,
|
||||||
|
})
|
||||||
|
|
||||||
|
if !in.HasAlpha || !in.HasAlpha24h {
|
||||||
|
t.Fatalf("expected alpha flags true: %+v", in)
|
||||||
|
}
|
||||||
|
if in.HasSpot || in.HasFuture {
|
||||||
|
t.Fatalf("did not expect spot/future flags true: %+v", in)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user