mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 07:26:46 +00:00
* Exchanges: Remove Pair upgrade handling Now redundant behind #1401. These paths should never be met. Several legacy coin upgrade paths being deprecated as well: ZUSD and CNY Expecting any users with bad config from 3+ years ago would have to reset anyway. Also: At the time the intention of this was to upgrade the config format. However now, instead, it'd mostly serve to reset enabled pairs if there's a config mistake, which doesn't feel right. * Kraken: Fix typo in Kraken type struct * Exchanges: Abstract exchange Start() and Run() * Exchanges: Add test for abstracted Start * Exchanges: Move Start to Bootstrap * Simplify waitgroup usage * Add call to exchange.Bootstrap to allow overide or supplementation * Exchanges: Concurrent common bootstap actions * Gateio: Remove incorrect Run in test * GateIO: Fix pair dependencies in tests This ensures that the pairs are initialised no more than needed and kind-of just-in-time. Better pattern might be to use a function to get these pairs when we need them. * Exchanges: Complete UpdatePairs before ExecLims If we're going to update pairs, it needs to complete before we check for limits to avoid errors on old pairs * Exchanges: Remove Start and Run from tmpl Since they're replaced by bootstrap now and shouldn't need customisation normally * Alphapoint: Move Start to Bootstrap * GateIO: Fix linter shadow var
227 lines
5.2 KiB
Go
227 lines
5.2 KiB
Go
package exchange
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/engine"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
)
|
|
|
|
// change these if you wish to test another exchange and/or currency pair
|
|
const (
|
|
exchName = "BTC Markets" // change to test on another exchange
|
|
exchAPIKEY = ""
|
|
exchAPISECRET = ""
|
|
exchClientID = ""
|
|
pairs = "BTC-AUD" // change to test another currency pair
|
|
delimiter = "-"
|
|
assetType = asset.Spot
|
|
orderID = "1234"
|
|
orderType = order.Limit
|
|
orderSide = order.Buy
|
|
orderClientID = ""
|
|
orderPrice = 1
|
|
orderAmount = 1
|
|
)
|
|
|
|
var (
|
|
settings = engine.Settings{
|
|
CoreSettings: engine.CoreSettings{EnableDryRun: true},
|
|
ConfigFile: filepath.Join("..", "..", "..", "..", "testdata", "configtest.json"),
|
|
DataDir: filepath.Join("..", "..", "..", "..", "testdata", "gocryptotrader"),
|
|
}
|
|
exchangeTest = Exchange{}
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
var t int
|
|
if err := setupEngine(); err != nil {
|
|
fmt.Printf("Failed to configure exchange test cannot continue: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
t = m.Run()
|
|
cleanup()
|
|
os.Exit(t)
|
|
}
|
|
|
|
func TestExchange_Exchanges(t *testing.T) {
|
|
t.Parallel()
|
|
if x := exchangeTest.Exchanges(false); len(x) != 1 {
|
|
t.Fatalf("expected 1 received %v", x)
|
|
}
|
|
}
|
|
|
|
func TestExchange_GetExchange(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := exchangeTest.GetExchange(exchName)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = exchangeTest.GetExchange("hello world")
|
|
if err == nil {
|
|
t.Fatal("unexpected error message received nil")
|
|
}
|
|
}
|
|
|
|
func TestExchange_IsEnabled(t *testing.T) {
|
|
t.Parallel()
|
|
x := exchangeTest.IsEnabled(exchName)
|
|
if !x {
|
|
t.Fatal("expected return to be true")
|
|
}
|
|
x = exchangeTest.IsEnabled("fake_exchange")
|
|
if x {
|
|
t.Fatal("expected return to be false")
|
|
}
|
|
}
|
|
|
|
func TestExchange_Ticker(t *testing.T) {
|
|
t.Parallel()
|
|
c, err := currency.NewPairDelimiter(pairs, delimiter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = exchangeTest.Ticker(context.Background(), exchName, c, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_Orderbook(t *testing.T) {
|
|
t.Parallel()
|
|
c, err := currency.NewPairDelimiter(pairs, delimiter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = exchangeTest.Orderbook(context.Background(), exchName, c, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_Pairs(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := exchangeTest.Pairs(exchName, false, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = exchangeTest.Pairs(exchName, true, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_AccountInformation(t *testing.T) {
|
|
if !configureExchangeKeys() {
|
|
t.Skip("no exchange configured test skipped")
|
|
}
|
|
_, err := exchangeTest.AccountInformation(context.Background(),
|
|
exchName, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_QueryOrder(t *testing.T) {
|
|
if !configureExchangeKeys() {
|
|
t.Skip("no exchange configured test skipped")
|
|
}
|
|
t.Parallel()
|
|
_, err := exchangeTest.QueryOrder(context.Background(),
|
|
exchName, orderID, currency.EMPTYPAIR, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_SubmitOrder(t *testing.T) {
|
|
if !configureExchangeKeys() {
|
|
t.Skip("no exchange configured test skipped")
|
|
}
|
|
|
|
t.Parallel()
|
|
c, err := currency.NewPairDelimiter(pairs, delimiter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tempOrder := &order.Submit{
|
|
Pair: c,
|
|
Type: orderType,
|
|
Side: orderSide,
|
|
Price: orderPrice,
|
|
Amount: orderAmount,
|
|
ClientID: orderClientID,
|
|
Exchange: exchName,
|
|
AssetType: asset.Spot,
|
|
}
|
|
_, err = exchangeTest.SubmitOrder(context.Background(), tempOrder)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_CancelOrder(t *testing.T) {
|
|
if !configureExchangeKeys() {
|
|
t.Skip("no exchange configured test skipped")
|
|
}
|
|
t.Parallel()
|
|
cp := currency.NewPair(currency.BTC, currency.USD)
|
|
a := asset.Spot
|
|
_, err := exchangeTest.CancelOrder(context.Background(),
|
|
exchName, orderID, cp, a)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestOHLCV(t *testing.T) {
|
|
t.Parallel()
|
|
cp := currency.NewPair(currency.BTC, currency.AUD)
|
|
cp.Delimiter = currency.DashDelimiter
|
|
calvinKline, err := exchangeTest.OHLCV(context.Background(), exchName, cp, assetType, time.Now().Add(-time.Hour*24).UTC(), time.Now().UTC(), kline.OneHour)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if calvinKline.Exchange != exchName {
|
|
t.Error("unexpected response")
|
|
}
|
|
}
|
|
|
|
func setupEngine() (err error) {
|
|
engine.Bot, err = engine.NewFromSettings(&settings, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
em := engine.NewExchangeManager()
|
|
engine.Bot.ExchangeManager = em
|
|
|
|
return engine.Bot.LoadExchange(exchName)
|
|
}
|
|
|
|
func cleanup() {
|
|
err := os.RemoveAll(settings.DataDir)
|
|
if err != nil {
|
|
fmt.Printf("Clean up failed to remove file: %v manual removal may be required", err)
|
|
}
|
|
}
|
|
|
|
func configureExchangeKeys() bool {
|
|
ex, err := engine.Bot.GetExchangeByName(exchName)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
b := ex.GetBase()
|
|
b.SetCredentials(exchAPIKEY, exchAPISECRET, exchClientID, "", "", "")
|
|
b.SkipAuthCheck = true
|
|
return b.AreCredentialsValid(context.Background())
|
|
}
|