diff --git a/client/mm/libxc/binance.go b/client/mm/libxc/binance.go index 28f1552a21..fc145b3028 100644 --- a/client/mm/libxc/binance.go +++ b/client/mm/libxc/binance.go @@ -338,23 +338,20 @@ func (b *binanceOrderBook) midGap() uint64 { // TODO: check all symbols var dexToBinanceSymbol = map[string]string{ - "POLYGON": "MATIC", - "WETH": "ETH", + "polygon": "MATIC", + "weth": "ETH", } var binanceToDexSymbol = make(map[string]string) -func convertBnCoin(coin string) string { +// convertBnCoin converts a binance coin symbol to a dex symbol. +// If allowWETH is false, then "weth" will be converted to "eth". +func convertBnCoin(coin string, allowWETH bool) string { symbol := strings.ToLower(coin) if convertedSymbol, found := binanceToDexSymbol[strings.ToUpper(coin)]; found { - symbol = strings.ToLower(convertedSymbol) + symbol = convertedSymbol } - return symbol -} - -func convertBnNetwork(network string) string { - symbol := convertBnCoin(network) - if symbol == "weth" { + if !allowWETH && symbol == "weth" { return "eth" } return symbol @@ -363,7 +360,7 @@ func convertBnNetwork(network string) string { // binanceCoinNetworkToDexSymbol takes the coin name and its network name as // returned by the binance API and returns the DEX symbol. func binanceCoinNetworkToDexSymbol(coin, network string) string { - symbol, netSymbol := convertBnCoin(coin), convertBnNetwork(network) + symbol, netSymbol := convertBnCoin(coin, true), convertBnCoin(network, false) if symbol == "weth" && netSymbol == "eth" { return "eth" } @@ -380,10 +377,10 @@ func init() { } func mapDexToBinanceSymbol(symbol string) string { - if binanceSymbol, found := dexToBinanceSymbol[symbol]; found { + if binanceSymbol, found := dexToBinanceSymbol[strings.ToLower(symbol)]; found { return binanceSymbol } - return symbol + return strings.ToUpper(symbol) } type bncAssetConfig struct { @@ -406,21 +403,24 @@ func bncAssetCfg(assetID uint32) (*bncAssetConfig, error) { if err != nil { return nil, err } - coin := mapDexToBinanceSymbol(ui.Conventional.Unit) + + symbol := dex.BipIDSymbol(assetID) + if symbol == "" { + return nil, fmt.Errorf("no symbol found for asset ID %d", assetID) + } + + parts := strings.Split(symbol, ".") + coin := mapDexToBinanceSymbol(parts[0]) chain := coin - if tkn := asset.TokenInfo(assetID); tkn != nil { - pui, err := asset.UnitInfo(tkn.ParentID) - if err != nil { - return nil, err - } - chain = pui.Conventional.Unit + if len(parts) > 1 { + chain = mapDexToBinanceSymbol(parts[1]) } return &bncAssetConfig{ assetID: assetID, - symbol: dex.BipIDSymbol(assetID), + symbol: symbol, coin: coin, - chain: mapDexToBinanceSymbol(chain), + chain: chain, conversionFactor: ui.Conventional.ConversionFactor, }, nil } @@ -659,6 +659,7 @@ func (bnc *binance) getMarkets(ctx context.Context) (map[string]*bntypes.Market, marketsMap := make(map[string]*bntypes.Market, len(exchangeInfo.Symbols)) tokenIDs := bnc.tokenIDs.Load().(map[string][]uint32) + for _, market := range exchangeInfo.Symbols { dexMarkets := binanceMarketToDexMarkets(market.BaseAsset, market.QuoteAsset, tokenIDs, bnc.isUS) if len(dexMarkets) == 0 { @@ -2130,13 +2131,7 @@ func (bnc *binance) TradeStatus(ctx context.Context, tradeID string, baseID, quo } func getDEXAssetIDs(coin string, tokenIDs map[string][]uint32) []uint32 { - dexSymbol := convertBnCoin(coin) - - // Binance does not differentiate between eth and weth like we do. - dexNonTokenSymbol := dexSymbol - if dexNonTokenSymbol == "weth" { - dexNonTokenSymbol = "eth" - } + dexSymbol := convertBnCoin(coin, false) isRegistered := func(assetID uint32) bool { _, err := asset.UnitInfo(assetID) @@ -2144,7 +2139,7 @@ func getDEXAssetIDs(coin string, tokenIDs map[string][]uint32) []uint32 { } assetIDs := make([]uint32, 0, 1) - if assetID, found := dex.BipSymbolID(dexNonTokenSymbol); found { + if assetID, found := dex.BipSymbolID(dexSymbol); found { // Only registered assets. if isRegistered(assetID) { assetIDs = append(assetIDs, assetID) diff --git a/client/mm/libxc/binance_test.go b/client/mm/libxc/binance_test.go index 7c80842cfe..20e523646e 100644 --- a/client/mm/libxc/binance_test.go +++ b/client/mm/libxc/binance_test.go @@ -5,6 +5,8 @@ package libxc import ( "testing" + + _ "decred.org/dcrdex/client/asset/importall" ) func TestSubscribeTradeUpdates(t *testing.T) { @@ -47,3 +49,74 @@ func TestBinanceToDexSymbol(t *testing.T) { } } } + +func TestBncAssetCfg(t *testing.T) { + tests := map[uint32]*bncAssetConfig{ + 0: { + assetID: 0, + symbol: "btc", + coin: "BTC", + chain: "BTC", + conversionFactor: 1e8, + }, + 2: { + assetID: 2, + symbol: "ltc", + coin: "LTC", + chain: "LTC", + conversionFactor: 1e8, + }, + 3: { + assetID: 3, + symbol: "doge", + coin: "DOGE", + chain: "DOGE", + conversionFactor: 1e8, + }, + 5: { + assetID: 5, + symbol: "dash", + coin: "DASH", + chain: "DASH", + conversionFactor: 1e8, + }, + 60: { + assetID: 60, + symbol: "eth", + coin: "ETH", + chain: "ETH", + conversionFactor: 1e9, + }, + 42: { + assetID: 42, + symbol: "dcr", + coin: "DCR", + chain: "DCR", + conversionFactor: 1e8, + }, + 966001: { + assetID: 966001, + symbol: "usdc.polygon", + coin: "USDC", + chain: "MATIC", + conversionFactor: 1e6, + }, + 966002: { + assetID: 966002, + symbol: "weth.polygon", + coin: "ETH", + chain: "MATIC", + conversionFactor: 1e9, + }, + } + + for test, expected := range tests { + cfg, err := bncAssetCfg(test) + if err != nil { + t.Fatalf("error getting asset config: %v", err) + } + if *expected != *cfg { + t.Fatalf("expected %v but got %v", expected, cfg) + } + } +}