fix: independent token source lookups #19
@@ -13,6 +13,7 @@ type IMarket interface {
|
|||||||
// Alpha token methods
|
// Alpha token methods
|
||||||
IsAlphaToken(symbol string) bool
|
IsAlphaToken(symbol string) bool
|
||||||
GetAlphaToken(symbol string) (market.AlphaTokenInfo, bool)
|
GetAlphaToken(symbol string) (market.AlphaTokenInfo, bool)
|
||||||
|
GetAlphaPrice(symbol string) (float64, bool)
|
||||||
|
|
||||||
// Trading pair methods
|
// Trading pair methods
|
||||||
IsSpotPair(symbol string) bool
|
IsSpotPair(symbol string) bool
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -54,10 +55,21 @@ type AlphaTokenInfo struct {
|
|||||||
|
|
||||||
// AlphaTokenResponse represents the API response structure
|
// AlphaTokenResponse represents the API response structure
|
||||||
type AlphaTokenResponse struct {
|
type AlphaTokenResponse struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Message *string `json:"message"`
|
Message *string `json:"message"`
|
||||||
|
MessageDetail *string `json:"messageDetail"`
|
||||||
|
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"`
|
MessageDetail *string `json:"messageDetail"`
|
||||||
Data []AlphaTokenInfo `json:"data"`
|
Data AlphaTickerData `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPrice returns the price as float64
|
// GetPrice returns the price as float64
|
||||||
@@ -150,3 +162,37 @@ func (ms *MarketData) IsAlphaToken(symbol string) bool {
|
|||||||
_, exists := ms.GetAlphaToken(symbol)
|
_, exists := ms.GetAlphaToken(symbol)
|
||||||
return exists
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -66,9 +66,13 @@ func collectRichTokenData(token string) buildRichTokenMessageArgs {
|
|||||||
|
|
||||||
if alphaToken, ok := data.Market.GetAlphaToken(token); ok {
|
if alphaToken, ok := data.Market.GetAlphaToken(token); ok {
|
||||||
a.HasAlpha = true
|
a.HasAlpha = true
|
||||||
a.AlphaPrice = alphaToken.GetPrice()
|
|
||||||
a.HasAlpha24h = true
|
a.HasAlpha24h = true
|
||||||
a.Alpha24h = alphaToken.GetPercentChange24h()
|
a.Alpha24h = alphaToken.GetPercentChange24h()
|
||||||
|
if alphaPrice, ok := data.Market.GetAlphaPrice(token + "USDT"); ok {
|
||||||
|
a.AlphaPrice = alphaPrice
|
||||||
|
} else {
|
||||||
|
a.AlphaPrice = alphaToken.GetPrice()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
futureSymbol := token + "USDT"
|
futureSymbol := token + "USDT"
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ type marketStub struct {
|
|||||||
time int64
|
time int64
|
||||||
}
|
}
|
||||||
alphaTokens map[string]market.AlphaTokenInfo
|
alphaTokens map[string]market.AlphaTokenInfo
|
||||||
|
alphaPrices map[string]float64
|
||||||
marginRates map[string]float64
|
marginRates map[string]float64
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +82,10 @@ func (m *marketStub) GetAlphaToken(symbol string) (market.AlphaTokenInfo, bool)
|
|||||||
v, ok := m.alphaTokens[symbol]
|
v, ok := m.alphaTokens[symbol]
|
||||||
return v, ok
|
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) IsSpotPair(symbol string) bool { return m.spotPairs[symbol] }
|
||||||
func (m *marketStub) IsFuturesPair(symbol string) bool { return m.futuresPairs[symbol] }
|
func (m *marketStub) IsFuturesPair(symbol string) bool { return m.futuresPairs[symbol] }
|
||||||
func (m *marketStub) RefreshTradingPairCache() error { return nil }
|
func (m *marketStub) RefreshTradingPairCache() error { return nil }
|
||||||
@@ -126,3 +131,26 @@ func TestCollectRichTokenData_FutureFailureStillKeepsSpot(t *testing.T) {
|
|||||||
t.Fatalf("did not expect future when future lookup fails: %+v", args)
|
t.Fatalf("did not expect future when future lookup fails: %+v", args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.1"},
|
||||||
|
},
|
||||||
|
alphaPrices: map[string]float64{"ABCUSDT": 0.1234},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := collectRichTokenData("ABC")
|
||||||
|
if !args.HasAlpha {
|
||||||
|
t.Fatalf("expected alpha to be available: %+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