Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libxc/binance: Fix polygon symbol conversion #3118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,23 +338,19 @@ 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.
func convertBnCoin(coin string, weth 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 !weth && symbol == "weth" {
return "eth"
}
return symbol
Expand All @@ -363,7 +359,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"
}
Expand All @@ -380,10 +376,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 {
Expand All @@ -406,21 +402,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
}
Expand Down Expand Up @@ -659,6 +658,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 {
Expand Down Expand Up @@ -2130,21 +2130,15 @@ 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)
return err == nil
}

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)
Expand Down
73 changes: 73 additions & 0 deletions client/mm/libxc/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package libxc

import (
"testing"

_ "decred.org/dcrdex/client/asset/importall"
)

func TestSubscribeTradeUpdates(t *testing.T) {
Expand Down Expand Up @@ -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)
}
}
}
Loading