Skip to content

Commit

Permalink
add lotsizes utility
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Sep 21, 2024
1 parent 40f9e6e commit bd6b3b0
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ client/cmd/translationsreport/translationsreport
client/cmd/translationsreport/worksheets
server/cmd/dexadm/dexadm
server/cmd/geogame/geogame
client/mm/binance/cmd/lotsizes/lotsizes
84 changes: 37 additions & 47 deletions client/mm/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,54 +336,25 @@ func (b *binanceOrderBook) midGap() uint64 {
return b.book.MidGap()
}

// TODO: check all symbols
var dexToBinanceSymbol = map[string]string{
"POLYGON": "MATIC",
"WETH": "ETH",
}

var binanceToDexSymbol = make(map[string]string)

func convertBnCoin(coin string) string {
symbol := strings.ToLower(coin)
if convertedSymbol, found := binanceToDexSymbol[strings.ToUpper(coin)]; found {
symbol = strings.ToLower(convertedSymbol)
switch coin {
case "MATIC", "POL":
return "polygon"
}
return symbol
}

func convertBnNetwork(network string) string {
symbol := convertBnCoin(network)
if symbol == "weth" {
return "eth"
}
return symbol
return strings.ToLower(coin)
}

// 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)
if symbol == "weth" && netSymbol == "eth" {
return "eth"
}
symbol, netSymbol := convertBnCoin(coin), convertBnCoin(network)
if symbol == netSymbol {
return symbol
}
return symbol + "." + netSymbol
}

func init() {
for key, value := range dexToBinanceSymbol {
binanceToDexSymbol[value] = key
}
}

func mapDexToBinanceSymbol(symbol string) string {
if binanceSymbol, found := dexToBinanceSymbol[symbol]; found {
return binanceSymbol
if symbol == "eth" {
return "weth." + netSymbol
}
return symbol
return symbol + "." + netSymbol
}

type bncAssetConfig struct {
Expand All @@ -406,7 +377,7 @@ func bncAssetCfg(assetID uint32) (*bncAssetConfig, error) {
if err != nil {
return nil, err
}
coin := mapDexToBinanceSymbol(ui.Conventional.Unit)
coin := ui.Conventional.Unit
chain := coin
if tkn := asset.TokenInfo(assetID); tkn != nil {
pui, err := asset.UnitInfo(tkn.ParentID)
Expand All @@ -420,7 +391,7 @@ func bncAssetCfg(assetID uint32) (*bncAssetConfig, error) {
assetID: assetID,
symbol: dex.BipIDSymbol(assetID),
coin: coin,
chain: mapDexToBinanceSymbol(chain),
chain: chain,
conversionFactor: ui.Conventional.ConversionFactor,
}, nil
}
Expand Down Expand Up @@ -588,8 +559,6 @@ func (bnc *binance) refreshBalances(ctx context.Context) error {
return nil
}

// readCoins stores the token IDs for which deposits and withdrawals are
// enabled on binance and sets the minWithdraw map.
func (bnc *binance) readCoins(coins []*bntypes.CoinInfo) {
tokenIDs := make(map[string][]uint32)
minWithdraw := make(map[uint32]uint64)
Expand Down Expand Up @@ -622,12 +591,10 @@ func (bnc *binance) readCoins(coins []*bntypes.CoinInfo) {
// getCoinInfo retrieves binance configs then updates the user balances and
// the tokenIDs.
func (bnc *binance) getCoinInfo(ctx context.Context) error {
coins := make([]*bntypes.CoinInfo, 0)
err := bnc.getAPI(ctx, "/sapi/v1/capital/config/getall", nil, true, true, &coins)
if err != nil {
var coins []*bntypes.CoinInfo
if err := bnc.getAPI(ctx, "/sapi/v1/capital/config/getall", nil, true, true, &coins); err != nil {
return fmt.Errorf("error getting binance coin info: %w", err)
}

bnc.readCoins(coins)
return nil
}
Expand Down Expand Up @@ -1177,8 +1144,30 @@ func (bnc *binance) MatchedMarkets(ctx context.Context) (_ []*libxc.MarketMatch,
}
markets := make([]*libxc.MarketMatch, 0, len(bnMarkets))

lotSize := func(mkt *bntypes.Market) uint64 {
var assetID uint32
if tids := tokenIDs[mkt.BaseAsset]; len(tids) > 0 {
assetID = tids[0]
} else {
var found bool
if assetID, found = dex.BipSymbolID(convertBnCoin(mkt.BaseAsset)); !found {
return 0
}
}
ui, err := asset.UnitInfo(assetID)
if err != nil {
return 0
}
for _, filt := range mkt.Filters {
if filt.FilterType == "LOT_SIZE" {
return uint64(math.Round(filt.MinQty * float64(ui.Conventional.ConversionFactor)))
}
}
return 0
}

for _, mkt := range bnMarkets {
dexMarkets := binanceMarketToDexMarkets(mkt.BaseAsset, mkt.QuoteAsset, tokenIDs, bnc.isUS)
dexMarkets := binanceMarketToDexMarkets(mkt.BaseAsset, mkt.QuoteAsset, tokenIDs, lotSize(mkt), bnc.isUS)
markets = append(markets, dexMarkets...)
}

Expand Down Expand Up @@ -1911,7 +1900,7 @@ func assetDisabled(isUS bool, assetID uint32) bool {
// A symbol represents a single market on the CEX, but tokens on the DEX
// have a different assetID for each network they are on, therefore they will
// match multiple markets as defined using assetID.
func binanceMarketToDexMarkets(binanceBaseSymbol, binanceQuoteSymbol string, tokenIDs map[string][]uint32, isUS bool) []*libxc.MarketMatch {
func binanceMarketToDexMarkets(binanceBaseSymbol, binanceQuoteSymbol string, tokenIDs map[string][]uint32, lotSize uint64, isUS bool) []*libxc.MarketMatch {
var baseAssetIDs, quoteAssetIDs []uint32

baseAssetIDs = getDEXAssetIDs(binanceBaseSymbol, tokenIDs)
Expand All @@ -1935,6 +1924,7 @@ func binanceMarketToDexMarkets(binanceBaseSymbol, binanceQuoteSymbol string, tok
MarketID: dex.BipIDSymbol(baseID) + "_" + dex.BipIDSymbol(quoteID),
BaseID: baseID,
QuoteID: quoteID,
LotSize: lotSize,
})
}
}
Expand Down
76 changes: 25 additions & 51 deletions client/mm/binance/binance_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@ import (
)

var (
log = dex.StdOutLogger("T", dex.LevelTrace)
binanceUS = true
global bool
apiKey string
apiSecret string
minTradeSymbol string
log = dex.StdOutLogger("T", dex.LevelTrace)
binanceUS = true
net = dex.Mainnet
apiKey string
apiSecret string
)

func TestMain(m *testing.M) {
flag.StringVar(&minTradeSymbol, "symbol", "", "Market slug")
flag.BoolVar(&global, "global", false, "Use Binance global")
var global, testnet bool
flag.BoolVar(&global, "global", false, "use Binance global")
flag.BoolVar(&testnet, "testnet", false, "use testnet")
flag.Parse()

if global {
binanceUS = false
}
if testnet {
net = dex.Testnet
}

if s := os.Getenv("SECRET"); s != "" {
apiSecret = s
Expand All @@ -48,7 +51,7 @@ func TestMain(m *testing.M) {
m.Run()
}

func tNewBinance(t *testing.T, net dex.Network) *binance {
func tNewBinance() *binance {
cfg := &libxc.CEXConfig{
Net: net,
APIKey: apiKey,
Expand Down Expand Up @@ -84,7 +87,7 @@ func (drv *spoofDriver) Info() *asset.WalletInfo {
}

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

Expand All @@ -109,7 +112,7 @@ func TestConnect(t *testing.T) {
// This may fail due to balance being to low. You can try switching the side
// of the trade or the qty.
func TestTrade(t *testing.T) {
bnc := tNewBinance(t, dex.Testnet)
bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()
_, err := bnc.Connect(ctx)
Expand Down Expand Up @@ -160,7 +163,7 @@ func TestTrade(t *testing.T) {
func TestCancelTrade(t *testing.T) {
tradeID := "42641326270691d752e000000001"

bnc := tNewBinance(t, dex.Testnet)
bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()
_, err := bnc.Connect(ctx)
Expand All @@ -175,7 +178,7 @@ func TestCancelTrade(t *testing.T) {
}

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

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

func TestVWAP(t *testing.T) {
bnc := tNewBinance(t, dex.Mainnet)
bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()
_, err := bnc.Connect(ctx)
Expand Down Expand Up @@ -260,7 +263,7 @@ func TestVWAP(t *testing.T) {
}

func TestSubscribeMarket(t *testing.T) {
bnc := tNewBinance(t, dex.Testnet)
bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()
wg, err := bnc.Connect(ctx)
Expand All @@ -277,7 +280,7 @@ func TestSubscribeMarket(t *testing.T) {
}

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

Expand All @@ -296,7 +299,7 @@ func TestWithdrawal(t *testing.T) {
}

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

Expand All @@ -310,7 +313,7 @@ func TestConfirmDeposit(t *testing.T) {
}

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

Expand All @@ -328,7 +331,7 @@ func TestGetDepositAddress(t *testing.T) {
}

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

Expand All @@ -346,7 +349,7 @@ func TestBalances(t *testing.T) {
}

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

Expand Down Expand Up @@ -381,7 +384,7 @@ func TestGetCoinInfo(t *testing.T) {
}

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

Expand All @@ -400,7 +403,7 @@ func TestTradeStatus(t *testing.T) {

func TestMarkets(t *testing.T) {
// Need keys for getCoinInfo
bnc := tNewBinance(t, dex.Testnet)
bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

Expand All @@ -417,32 +420,3 @@ func TestMarkets(t *testing.T) {
b, _ := json.MarshalIndent(mkts, "", " ")
fmt.Println("##### Market Data:", string(b))
}

func TestGetMinTrade(t *testing.T) {
// e.g.
// go test -tags bnclive -run TestGetMinTrade --symbol DCRBTC --global

if minTradeSymbol == "" {
t.Fatal("No market symbol provided")
}
bnc := tNewBinance(t, dex.Testnet)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var xcInfo bntypes.ExchangeInfo
if err := bnc.getAPI(ctx, "/api/v3/exchangeInfo", nil, false, false, &xcInfo); err != nil {
t.Fatal(err)
}
for _, mkt := range xcInfo.Symbols {
if mkt.Symbol != minTradeSymbol {
continue
}
for _, filt := range mkt.Filters {
if filt.FilterType == "LOT_SIZE" {
fmt.Printf("Market %s min trade = %f %s \n", mkt.Symbol, filt.MinQty, mkt.BaseAsset)
return
}
}
}
t.Fatal("Market not found")
}
Loading

0 comments on commit bd6b3b0

Please sign in to comment.