Skip to content

Commit

Permalink
avoid duplicate slugs in bnc ticker request
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Jul 12, 2024
1 parent 27c1254 commit ea5a479
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.
50 changes: 25 additions & 25 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"decred.org/dcrdex/dex/calc"
"decred.org/dcrdex/dex/dexnet"
"decred.org/dcrdex/dex/encode"
"decred.org/dcrdex/dex/utils"
)

// Binance API spot trading docs:
Expand Down Expand Up @@ -1049,18 +1050,16 @@ func (bnc *binance) Markets(ctx context.Context) (map[string]*Market, error) {
return bnc.marketSnapshot.m, nil
}

mktIDs, err := bnc.MatchedMarkets(ctx)
matches, err := bnc.MatchedMarkets(ctx)
if err != nil {
return nil, fmt.Errorf("error getting market list for market data request: %w", err)
}

slugs := make([]string, len(mktIDs))
mkts := make(map[string]*MarketMatch, len(slugs))
for i, m := range mktIDs {
slugs[i] = m.Slug
mkts[m.Slug] = m
mkts := make(map[string][]*MarketMatch, len(matches))
for _, m := range matches {
mkts[m.Slug] = append(mkts[m.Slug], m)
}
encSymbols, err := json.Marshal(slugs)
encSymbols, err := json.Marshal(utils.MapKeys(mkts))
if err != nil {
return nil, fmt.Errorf("error encoding symbold for market data request: %w", err)
}
Expand All @@ -1075,25 +1074,27 @@ func (bnc *binance) Markets(ctx context.Context) (map[string]*Market, error) {

m := make(map[string]*Market, len(ds))
for _, d := range ds {
mkt, found := mkts[d.Symbol]
ms, found := mkts[d.Symbol]
if !found {
bnc.log.Errorf("Market %s not returned in market data request", d.Symbol)
continue
}
m[mkt.MarketID] = &Market{
BaseID: mkt.BaseID,
QuoteID: mkt.QuoteID,
Day: &MarketDay{
Vol: d.Volume,
QuoteVol: d.QuoteVolume,
PriceChange: d.PriceChange,
PriceChangePct: d.PriceChangePercent,
AvgPrice: d.WeightedAvgPrice,
LastPrice: d.LastPrice,
OpenPrice: d.OpenPrice,
HighPrice: d.HighPrice,
LowPrice: d.LowPrice,
},
for _, mkt := range ms {
m[mkt.MarketID] = &Market{
BaseID: mkt.BaseID,
QuoteID: mkt.QuoteID,
Day: &MarketDay{
Vol: d.Volume,
QuoteVol: d.QuoteVolume,
PriceChange: d.PriceChange,
PriceChangePct: d.PriceChangePercent,
AvgPrice: d.WeightedAvgPrice,
LastPrice: d.LastPrice,
OpenPrice: d.OpenPrice,
HighPrice: d.HighPrice,
LowPrice: d.LowPrice,
},
}
}
}
bnc.marketSnapshot.m = m
Expand Down Expand Up @@ -1752,9 +1753,8 @@ func (bnc *binance) Book(baseID, quoteID uint32) (buys, sells []*core.MiniOrder,
return
}

// VWAP returns the volume weighted average price for a certain quantity of the
// base asset on a market. The sell parameter specifies the side of the market
// on which to get the average price. SubscribeMarket must be called, and the
// VWAP returns the volume weighted average price for a certain quantity
// of the base asset on a market. SubscribeMarket must be called, and the
// market must be synced before results can be expected.
func (bnc *binance) VWAP(baseID, quoteID uint32, sell bool, qty uint64) (avgPrice, extrema uint64, filled bool, err error) {
book, err := bnc.book(baseID, quoteID)
Expand Down
35 changes: 22 additions & 13 deletions client/mm/libxc/binance_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ func TestMain(m *testing.M) {
m.Run()
}

func tNewBinance(t *testing.T, network dex.Network) *binance {
return newBinance(apiKey, apiSecret, log, network, true)
func tNewBinance(t *testing.T, net dex.Network) *binance {
cfg := &CEXConfig{
Net: net,
APIKey: apiKey,
SecretKey: apiSecret,
Logger: log,
Notify: func(n interface{}) {
log.Infof("Notification sent: %+v", n)
},
}
const binanceUS = true
return newBinance(cfg, binanceUS)
}

type spoofDriver struct {
Expand Down Expand Up @@ -154,7 +164,7 @@ func TestCancelTrade(t *testing.T) {
}

func TestMatchedMarkets(t *testing.T) {
bnc := tNewBinance(t, dex.Testnet)
bnc := tNewBinance(t, dex.Mainnet)
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

Expand All @@ -169,7 +179,7 @@ func TestMatchedMarkets(t *testing.T) {
}

for _, market := range markets {
t.Logf("%v - %v", dex.BipIDSymbol(market.BaseID), dex.BipIDSymbol(market.QuoteID))
fmt.Printf("%s_%s \n", dex.BipIDSymbol(market.BaseID), dex.BipIDSymbol(market.QuoteID))
}
}

Expand Down Expand Up @@ -335,18 +345,17 @@ func TestGetCoinInfo(t *testing.T) {
t.Fatalf("error getting binance coin info: %v", err)
}

bcoins, err := json.MarshalIndent(coins, "", " ")
if err != nil {
t.Fatalf("error marshaling binance coin info: %v", err)
coinLookup := make(map[string]bool)
for _, a := range asset.Assets() {
coinLookup[a.Info.UnitInfo.Conventional.Unit] = true
for _, tkn := range a.Tokens {
coinLookup[tkn.UnitInfo.Conventional.Unit] = true
}
}

t.Logf("binance coin info:\n %v", string(bcoins))
return

for _, c := range coins {
if c.Coin == "USDC" {
b, _ := json.MarshalIndent(c, "", " ")
fmt.Println(string(b))
if !coinLookup[c.Coin] {
continue
}
networks := make([]string, 0)
for _, n := range c.NetworkList {
Expand Down
8 changes: 8 additions & 0 deletions dex/utils/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func MapItems[K comparable, V any](m map[K]V) []V {
return vs
}

func MapKeys[K comparable, V any](m map[K]V) []K {
ks := make([]K, 0, len(m))
for k := range m {
ks = append(ks, k)
}
return ks
}

func Min[I constraints.Ordered](m I, ns ...I) I {
min := m
for _, n := range ns {
Expand Down

0 comments on commit ea5a479

Please sign in to comment.