Skip to content

Commit

Permalink
libxc/binance: Add flags for binance live tests (#3107)
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp authored Jan 29, 2025
1 parent 0ac97e3 commit 8326b8e
Showing 1 changed file with 140 additions and 54 deletions.
194 changes: 140 additions & 54 deletions client/mm/libxc/binance_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package libxc
import (
"context"
"encoding/json"
"flag"
"fmt"
"math"
"os"
"os/user"
"strings"
"sync"
"testing"
Expand All @@ -17,16 +18,48 @@ import (
_ "decred.org/dcrdex/client/asset/importall"
"decred.org/dcrdex/client/mm/libxc/bntypes"
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/calc"
)

var (
log = dex.StdOutLogger("T", dex.LevelTrace)
u, _ = user.Current()
apiKey = ""
apiSecret = ""
binanceUS = true
net = dex.Mainnet
apiKey string
apiSecret string
baseID uint64
quoteID uint64
rate float64
qty float64
sell bool
tradeID string
assetID uint64
txID string
addr string
)

func TestMain(m *testing.M) {
var global, testnet bool
flag.BoolVar(&global, "global", false, "use Binance global")
flag.BoolVar(&testnet, "testnet", false, "use testnet")
flag.Uint64Var(&baseID, "base", ^uint64(0), "base asset ID")
flag.Uint64Var(&quoteID, "quote", ^uint64(0), "quote asset ID")
flag.Float64Var(&rate, "rate", -1, "rate")
flag.Float64Var(&qty, "qty", -1, "qty")
flag.StringVar(&tradeID, "trade", "", "trade ID")
flag.BoolVar(&sell, "sell", false, "sell")
flag.Uint64Var(&assetID, "asset", ^uint64(0), "asset ID")
flag.StringVar(&txID, "tx", "", "tx ID")
flag.StringVar(&addr, "addr", "", "address")
flag.Parse()

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

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

func tNewBinance(t *testing.T, net dex.Network) *binance {
func tNewBinance() *binance {
cfg := &CEXConfig{
Net: net,
APIKey: apiKey,
Expand All @@ -47,7 +80,6 @@ func tNewBinance(t *testing.T, net dex.Network) *binance {
log.Infof("Notification sent: %+v", n)
},
}
const binanceUS = true
return newBinance(cfg, binanceUS)
}

Expand All @@ -73,33 +105,12 @@ func (drv *spoofDriver) Info() *asset.WalletInfo {
}
}

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

_, err := bnc.Connect(ctx)
if err != nil {
t.Fatalf("Connect error: %v", err)
}

balance, err := bnc.Balance(60)
if err != nil {
t.Fatalf("Balance error: %v", err)
func TestPlaceTrade(t *testing.T) {
if baseID == ^uint64(0) || quoteID == ^uint64(0) || rate < 0 || qty < 0 {
t.Fatalf("baseID, quoteID, rate, or qty not set")
}
t.Logf("usdc balance: %v", balance)

balance, err = bnc.Balance(0)
if err != nil {
t.Fatalf("Balance error: %v", err)
}
t.Logf("btc balance: %v", balance)
}

// 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 @@ -131,7 +142,13 @@ func TestTrade(t *testing.T) {
}
}()

trade, err := bnc.Trade(ctx, 60, 60001, false, 3600e5, 1e7, updaterID)
baseUI, _ := asset.UnitInfo(uint32(baseID))
quoteUI, _ := asset.UnitInfo(uint32(quoteID))
msgRate := calc.MessageRate(rate, baseUI, quoteUI)
msgQty := uint64(math.Round(qty * float64(baseUI.Conventional.ConversionFactor)))

t.Logf("msgRate: %v, msgQty: %v", msgRate, msgQty)
trade, err := bnc.Trade(ctx, uint32(baseID), uint32(quoteID), false, msgRate, msgQty, updaterID)
if err != nil {
t.Fatalf("trade error: %v", err)
}
Expand All @@ -148,24 +165,29 @@ func TestTrade(t *testing.T) {
}

func TestCancelTrade(t *testing.T) {
tradeID := "42641326270691d752e000000001"
if tradeID == "" {
t.Fatalf("tradeID not set")
}
if baseID == ^uint64(0) || quoteID == ^uint64(0) {
t.Fatalf("baseID or quoteID not set")
}

bnc := tNewBinance(t, dex.Testnet)
bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()
_, err := bnc.Connect(ctx)
if err != nil {
t.Fatalf("Connect error: %v", err)
}

err = bnc.CancelTrade(ctx, 60, 0, tradeID)
err = bnc.CancelTrade(ctx, uint32(baseID), uint32(quoteID), tradeID)
if err != nil {
t.Fatalf("error cancelling trade: %v", err)
}
}

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 @@ -185,7 +207,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 @@ -250,15 +272,19 @@ func TestVWAP(t *testing.T) {
}

func TestSubscribeMarket(t *testing.T) {
bnc := tNewBinance(t, dex.Testnet)
if baseID == ^uint64(0) || quoteID == ^uint64(0) {
t.Fatalf("baseID or quoteID not set")
}

bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()
wg, err := bnc.Connect(ctx)
if err != nil {
t.Fatalf("Connect error: %v", err)
}

err = bnc.SubscribeMarket(ctx, 60, 0)
err = bnc.SubscribeMarket(ctx, uint32(baseID), uint32(quoteID))
if err != nil {
t.Fatalf("failed to subscribe to market: %v", err)
}
Expand All @@ -267,7 +293,17 @@ func TestSubscribeMarket(t *testing.T) {
}

func TestWithdrawal(t *testing.T) {
bnc := tNewBinance(t, dex.Mainnet)
if assetID == ^uint64(0) {
t.Fatalf("assetID not set")
}
if qty < 0 {
t.Fatalf("qty not set")
}
if addr == "" {
t.Fatalf("addr not set")
}

bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

Expand All @@ -276,7 +312,11 @@ func TestWithdrawal(t *testing.T) {
t.Fatalf("Connect error: %v", err)
}

withdrawalID, err := bnc.Withdraw(ctx, 966, 2e10, "")
ui, _ := asset.UnitInfo(uint32(assetID))
msgQty := uint64(math.Round(qty * float64(ui.Conventional.ConversionFactor)))

t.Logf("msgQty: %v", msgQty)
withdrawalID, err := bnc.Withdraw(ctx, uint32(assetID), msgQty, addr)
if err != nil {
fmt.Printf("withdrawal error: %v", err)
return
Expand All @@ -286,7 +326,14 @@ func TestWithdrawal(t *testing.T) {
}

func TestConfirmDeposit(t *testing.T) {
bnc := tNewBinance(t, dex.Mainnet)
if assetID == ^uint64(0) {
t.Fatalf("assetID not set")
}
if txID == "" {
t.Fatalf("txID not set")
}

bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

Expand All @@ -295,12 +342,20 @@ func TestConfirmDeposit(t *testing.T) {
t.Fatalf("Connect error: %v", err)
}

confirmed, amt := bnc.ConfirmDeposit(ctx, &DepositData{})
t.Logf("confirmed: %v, amt: %v", confirmed, amt)
confirmed, amt := bnc.ConfirmDeposit(ctx, &DepositData{
AssetID: uint32(assetID),
TxID: txID,
})

t.Logf("Confirmed: %v, Amt: %v", confirmed, amt)
}

func TestGetDepositAddress(t *testing.T) {
bnc := tNewBinance(t, dex.Mainnet)
if assetID == ^uint64(0) {
t.Fatalf("assetID not set")
}

bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

Expand All @@ -309,16 +364,20 @@ func TestGetDepositAddress(t *testing.T) {
t.Fatalf("Connect error: %v", err)
}

addr, err := bnc.GetDepositAddress(ctx, 966)
addr, err := bnc.GetDepositAddress(ctx, uint32(assetID))
if err != nil {
t.Fatalf("getDepositAddress error: %v", err)
}

t.Logf("deposit address: %v", addr)
t.Logf("Deposit Address: %v", addr)
}

func TestBalances(t *testing.T) {
bnc := tNewBinance(t, dex.Testnet)
func TestBalanceIndividually(t *testing.T) {
if assetID == ^uint64(0) {
t.Fatalf("assetID not set")
}

bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

Expand All @@ -327,16 +386,36 @@ func TestBalances(t *testing.T) {
t.Fatalf("Connect error: %v", err)
}

balance, err := bnc.Balance(0)
balance, err := bnc.Balance(uint32(assetID))
if err != nil {
t.Fatalf("balances error: %v", err)
}

t.Logf("%+v", balance)
}

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

_, err := bnc.Connect(ctx)
if err != nil {
t.Fatalf("Connect error: %v", err)
}

balances, err := bnc.Balances(ctx)
if err != nil {
t.Fatalf("balances error: %v", err)
}

for assetID, b := range balances {
t.Logf("%s: %+v", dex.BipIDSymbol(assetID), b)
}
}

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 @@ -371,7 +450,14 @@ func TestGetCoinInfo(t *testing.T) {
}

func TestTradeStatus(t *testing.T) {
bnc := tNewBinance(t, dex.Testnet)
if tradeID == "" {
t.Fatalf("tradeID not set")
}
if baseID == ^uint64(0) || quoteID == ^uint64(0) {
t.Fatalf("baseID or quoteID not set")
}

bnc := tNewBinance()
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

Expand All @@ -380,7 +466,7 @@ func TestTradeStatus(t *testing.T) {
t.Fatalf("Connect error: %v", err)
}

trade, err := bnc.TradeStatus(ctx, "eb6b6e1177213643142700000001", 60, 60001)
trade, err := bnc.TradeStatus(ctx, tradeID, uint32(baseID), uint32(quoteID))
if err != nil {
t.Fatalf("trade status error: %v", err)
}
Expand All @@ -390,7 +476,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 Down

0 comments on commit 8326b8e

Please sign in to comment.