decred.org/dcrdex@v1.0.5/client/mm/libxc/binance_test.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package libxc
     5  
     6  import (
     7  	"testing"
     8  
     9  	_ "decred.org/dcrdex/client/asset/importall"
    10  )
    11  
    12  func TestSubscribeTradeUpdates(t *testing.T) {
    13  	bn := &binance{
    14  		tradeUpdaters: make(map[int]chan *Trade),
    15  	}
    16  	_, unsub0, _ := bn.SubscribeTradeUpdates()
    17  	_, _, id1 := bn.SubscribeTradeUpdates()
    18  	unsub0()
    19  	_, _, id2 := bn.SubscribeTradeUpdates()
    20  	if len(bn.tradeUpdaters) != 2 {
    21  		t.Fatalf("wrong number of updaters. wanted 2, got %d", len(bn.tradeUpdaters))
    22  	}
    23  	if id1 == id2 {
    24  		t.Fatalf("ids should be unique. got %d twice", id1)
    25  	}
    26  	if _, found := bn.tradeUpdaters[id1]; !found {
    27  		t.Fatalf("id1 not found")
    28  	}
    29  	if _, found := bn.tradeUpdaters[id2]; !found {
    30  		t.Fatalf("id2 not found")
    31  	}
    32  }
    33  
    34  func TestBinanceToDexSymbol(t *testing.T) {
    35  	tests := map[[2]string]string{
    36  		{"ETH", "ETH"}:    "eth",
    37  		{"ETH", "MATIC"}:  "weth.polygon",
    38  		{"USDC", "ETH"}:   "usdc.eth",
    39  		{"USDC", "MATIC"}: "usdc.polygon",
    40  		{"BTC", "BTC"}:    "btc",
    41  		{"WBTC", "ETH"}:   "wbtc.eth",
    42  		{"POL", "MATIC"}:  "polygon",
    43  	}
    44  
    45  	for test, expected := range tests {
    46  		dexSymbol := binanceCoinNetworkToDexSymbol(test[0], test[1])
    47  		if expected != dexSymbol {
    48  			t.Fatalf("expected %s but got %v", expected, dexSymbol)
    49  		}
    50  	}
    51  }
    52  
    53  func TestBncAssetCfg(t *testing.T) {
    54  	tests := map[uint32]*bncAssetConfig{
    55  		0: {
    56  			assetID:          0,
    57  			symbol:           "btc",
    58  			coin:             "BTC",
    59  			chain:            "BTC",
    60  			conversionFactor: 1e8,
    61  		},
    62  		2: {
    63  			assetID:          2,
    64  			symbol:           "ltc",
    65  			coin:             "LTC",
    66  			chain:            "LTC",
    67  			conversionFactor: 1e8,
    68  		},
    69  		3: {
    70  			assetID:          3,
    71  			symbol:           "doge",
    72  			coin:             "DOGE",
    73  			chain:            "DOGE",
    74  			conversionFactor: 1e8,
    75  		},
    76  		5: {
    77  			assetID:          5,
    78  			symbol:           "dash",
    79  			coin:             "DASH",
    80  			chain:            "DASH",
    81  			conversionFactor: 1e8,
    82  		},
    83  		60: {
    84  			assetID:          60,
    85  			symbol:           "eth",
    86  			coin:             "ETH",
    87  			chain:            "ETH",
    88  			conversionFactor: 1e9,
    89  		},
    90  		42: {
    91  			assetID:          42,
    92  			symbol:           "dcr",
    93  			coin:             "DCR",
    94  			chain:            "DCR",
    95  			conversionFactor: 1e8,
    96  		},
    97  		966001: {
    98  			assetID:          966001,
    99  			symbol:           "usdc.polygon",
   100  			coin:             "USDC",
   101  			chain:            "MATIC",
   102  			conversionFactor: 1e6,
   103  		},
   104  		966002: {
   105  			assetID:          966002,
   106  			symbol:           "weth.polygon",
   107  			coin:             "ETH",
   108  			chain:            "MATIC",
   109  			conversionFactor: 1e9,
   110  		},
   111  		966: {
   112  			assetID:          966,
   113  			symbol:           "polygon",
   114  			coin:             "POL",
   115  			chain:            "MATIC",
   116  			conversionFactor: 1e9,
   117  		},
   118  	}
   119  
   120  	for test, expected := range tests {
   121  		cfg, err := bncAssetCfg(test)
   122  		if err != nil {
   123  			t.Fatalf("error getting asset config: %v", err)
   124  		}
   125  		if *expected != *cfg {
   126  			t.Fatalf("expected %v but got %v", expected, cfg)
   127  		}
   128  	}
   129  }