Skip to content

Commit

Permalink
libxc/binance: Fix polygon symbol conversion
Browse files Browse the repository at this point in the history
Since the conversion from MATIC -> POL, the symbol conversion for polygon
and polygon tokens was not working properly. This is fixed.
  • Loading branch information
martonp committed Dec 11, 2024
1 parent 0030e49 commit 6e30960
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 30 deletions.
55 changes: 25 additions & 30 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2130,21 +2131,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)
}
}
}

0 comments on commit 6e30960

Please sign in to comment.