Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c8be4c4bfd | |||
| 1486681ef9 | |||
| a4644f5982 | |||
| 82b0f2f1a8 | |||
| 6e8a2e8f68 |
@@ -13,6 +13,7 @@ type IMarket interface {
|
||||
// Alpha token methods
|
||||
IsAlphaToken(symbol string) bool
|
||||
GetAlphaToken(symbol string) (market.AlphaTokenInfo, bool)
|
||||
GetAlphaPrice(symbol string) (float64, bool)
|
||||
|
||||
// Trading pair methods
|
||||
IsSpotPair(symbol string) bool
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -60,6 +61,17 @@ type AlphaTokenResponse struct {
|
||||
Data []AlphaTokenInfo `json:"data"`
|
||||
}
|
||||
|
||||
type AlphaTickerData struct {
|
||||
Price string `json:"price"`
|
||||
}
|
||||
|
||||
type AlphaTickerResponse struct {
|
||||
Code string `json:"code"`
|
||||
Message *string `json:"message"`
|
||||
MessageDetail *string `json:"messageDetail"`
|
||||
Data AlphaTickerData `json:"data"`
|
||||
}
|
||||
|
||||
// GetPrice returns the price as float64
|
||||
func (a *AlphaTokenInfo) GetPrice() float64 {
|
||||
price, err := strconv.ParseFloat(a.Price, 64)
|
||||
@@ -150,3 +162,37 @@ func (ms *MarketData) IsAlphaToken(symbol string) bool {
|
||||
_, exists := ms.GetAlphaToken(symbol)
|
||||
return exists
|
||||
}
|
||||
|
||||
func (ms *MarketData) GetAlphaPrice(symbol string) (float64, bool) {
|
||||
const alphaTickerURL = "https://www.binance.com/bapi/defi/v1/public/alpha-trade/ticker"
|
||||
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
endpoint := alphaTickerURL + "?" + url.Values{"symbol": []string{symbol}}.Encode()
|
||||
|
||||
resp, err := client.Get(endpoint)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("symbol", symbol).Msg("Failed to fetch Alpha ticker")
|
||||
return 0, false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
var tickerResp AlphaTickerResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&tickerResp); err != nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
if tickerResp.Code != "000000" {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
price, err := strconv.ParseFloat(tickerResp.Data.Price, 64)
|
||||
if err != nil || price <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return price, true
|
||||
}
|
||||
|
||||
@@ -67,9 +67,13 @@ func collectRichTokenData(token string) buildRichTokenMessageArgs {
|
||||
|
||||
if alphaToken, ok := data.Market.GetAlphaToken(token); ok {
|
||||
a.HasAlpha = true
|
||||
a.AlphaPrice = alphaToken.GetPrice()
|
||||
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)
|
||||
@@ -109,7 +113,6 @@ func OnTokenInfoByToken(context telebot.Context, token string) error {
|
||||
}
|
||||
|
||||
msg := view.RenderRichTokenMessage(buildRichTokenMessageInput(args))
|
||||
|
||||
_ = chat.ReplyMessage(context, msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,45 +7,6 @@ import (
|
||||
"me.thuanle/bbot/internal/data/market"
|
||||
)
|
||||
|
||||
type marketStub struct {
|
||||
spotPairs map[string]bool
|
||||
futuresPairs map[string]bool
|
||||
spotPrices map[string]float64
|
||||
futurePrices map[string]struct {
|
||||
price float64
|
||||
rate float64
|
||||
time int64
|
||||
}
|
||||
alphaTokens map[string]market.AlphaTokenInfo
|
||||
marginRates map[string]float64
|
||||
}
|
||||
|
||||
func (m *marketStub) GetFuturePrice(symbol string) (float64, float64, int64, bool) {
|
||||
v, ok := m.futurePrices[symbol]
|
||||
if !ok {
|
||||
return 0, 0, 0, false
|
||||
}
|
||||
return v.price, v.rate, v.time, true
|
||||
}
|
||||
func (m *marketStub) GetAllPremiumIndex() (map[string]market.PremiumIndex, error) { return nil, nil }
|
||||
func (m *marketStub) GetAllFundRate() (map[string]float64, map[string]int64) { return nil, nil }
|
||||
func (m *marketStub) GetSpotPrice(symbol string) (float64, bool) {
|
||||
v, ok := m.spotPrices[symbol]
|
||||
return v, ok
|
||||
}
|
||||
func (m *marketStub) GetMarginInterestRates() map[string]float64 { return m.marginRates }
|
||||
func (m *marketStub) IsAlphaToken(symbol string) bool {
|
||||
_, ok := m.alphaTokens[symbol]
|
||||
return ok
|
||||
}
|
||||
func (m *marketStub) GetAlphaToken(symbol string) (market.AlphaTokenInfo, bool) {
|
||||
v, ok := m.alphaTokens[symbol]
|
||||
return v, ok
|
||||
}
|
||||
func (m *marketStub) IsSpotPair(symbol string) bool { return m.spotPairs[symbol] }
|
||||
func (m *marketStub) IsFuturesPair(symbol string) bool { return m.futuresPairs[symbol] }
|
||||
func (m *marketStub) RefreshTradingPairCache() error { return nil }
|
||||
|
||||
func TestBuildRichTokenMessageInput_AllSources(t *testing.T) {
|
||||
in := buildRichTokenMessageInput(buildRichTokenMessageArgs{
|
||||
Token: "ETH",
|
||||
@@ -85,6 +46,50 @@ func TestBuildRichTokenMessageInput_OnlyAlpha(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type marketStub struct {
|
||||
spotPairs map[string]bool
|
||||
futuresPairs map[string]bool
|
||||
spotPrices map[string]float64
|
||||
futurePrices map[string]struct {
|
||||
price float64
|
||||
rate float64
|
||||
time int64
|
||||
}
|
||||
alphaTokens map[string]market.AlphaTokenInfo
|
||||
alphaPrices map[string]float64
|
||||
marginRates map[string]float64
|
||||
}
|
||||
|
||||
func (m *marketStub) GetFuturePrice(symbol string) (float64, float64, int64, bool) {
|
||||
v, ok := m.futurePrices[symbol]
|
||||
if !ok {
|
||||
return 0, 0, 0, false
|
||||
}
|
||||
return v.price, v.rate, v.time, true
|
||||
}
|
||||
func (m *marketStub) GetAllPremiumIndex() (map[string]market.PremiumIndex, error) { return nil, nil }
|
||||
func (m *marketStub) GetAllFundRate() (map[string]float64, map[string]int64) { return nil, nil }
|
||||
func (m *marketStub) GetSpotPrice(symbol string) (float64, bool) {
|
||||
v, ok := m.spotPrices[symbol]
|
||||
return v, ok
|
||||
}
|
||||
func (m *marketStub) GetMarginInterestRates() map[string]float64 { return m.marginRates }
|
||||
func (m *marketStub) IsAlphaToken(symbol string) bool {
|
||||
_, ok := m.alphaTokens[symbol]
|
||||
return ok
|
||||
}
|
||||
func (m *marketStub) GetAlphaToken(symbol string) (market.AlphaTokenInfo, bool) {
|
||||
v, ok := m.alphaTokens[symbol]
|
||||
return v, ok
|
||||
}
|
||||
func (m *marketStub) GetAlphaPrice(symbol string) (float64, bool) {
|
||||
v, ok := m.alphaPrices[symbol]
|
||||
return v, ok
|
||||
}
|
||||
func (m *marketStub) IsSpotPair(symbol string) bool { return m.spotPairs[symbol] }
|
||||
func (m *marketStub) IsFuturesPair(symbol string) bool { return m.futuresPairs[symbol] }
|
||||
func (m *marketStub) RefreshTradingPairCache() error { return nil }
|
||||
|
||||
func TestCollectRichTokenData_SpotOnlyReachable(t *testing.T) {
|
||||
orig := data.Market
|
||||
defer func() { data.Market = orig }()
|
||||
@@ -194,21 +199,25 @@ func TestCollectRichTokenData_PrefixFutureMapsToSpot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectRichTokenData_AlphaIndependent(t *testing.T) {
|
||||
func TestCollectRichTokenData_AlphaUsesSymbolPrice(t *testing.T) {
|
||||
orig := data.Market
|
||||
defer func() { data.Market = orig }()
|
||||
|
||||
data.Market = &marketStub{
|
||||
alphaTokens: map[string]market.AlphaTokenInfo{
|
||||
"ABC": {Symbol: "ABC", PercentChange24h: "12.4", Price: "0.1234"},
|
||||
"ABC": {Symbol: "ABC", PercentChange24h: "12.4", Price: "0.1"},
|
||||
},
|
||||
alphaPrices: map[string]float64{"ABCUSDT": 0.1234},
|
||||
}
|
||||
|
||||
args := collectRichTokenData("ABC")
|
||||
if !args.HasAlpha {
|
||||
t.Fatalf("expected alpha source available, got %+v", args)
|
||||
t.Fatalf("expected alpha to be available: %+v", args)
|
||||
}
|
||||
if args.HasSpot || args.HasFuture {
|
||||
t.Fatalf("expected alpha independent from spot/future, got %+v", args)
|
||||
if !args.HasAlpha24h {
|
||||
t.Fatalf("expected alpha 24h to be available: %+v", args)
|
||||
}
|
||||
if args.AlphaPrice != 0.1234 {
|
||||
t.Fatalf("expected alpha price from symbol lookup, got %.4f", args.AlphaPrice)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user