Files
gocryptotrader/engine/rpcserver_test.go
Adam 504c2fad6d Feature: Implement funding rates, futures and coin margin (exchange API coverage) (#530)
* ALMOST THERE

* more api wips

* more api thingz

* testing n more api wipz

* more apiz

* more wips

* what is goin on

* more wips

* whip n testing

* testing

* testing

no keys

* remove log

* kraken is broken

ugh

* still broken

* fixing auth funcs + usdtm api docs

* wip

* api stuffs

* whip

* more wips

* whip

* more wip

* api wip n testing

* wip

* wip

* unsaved

* wip n testing

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* whip

* wrapper authenticated functions

* adding asset type and fixing dependencies

* wip

* binance auth wrapper start

* wrapper functionality

* wip

* wip

* wip

* wrapper cancel functions

* order submission for wrappers

* wip

* more error fixing and nits

* websocket beginning n error fix

* wip

* WOW

* glorious n shazzy nits

* useless nits

* wip

* fixing things

* merge stuffs

* crapveyor

* crapveyor rebuild

* probably broke more things than he fixed

* rm lns n other thangs

* hope

* please

* stop it

* done

* ofcourse

* rm vb

* fix lbank

* appveyor please

* float lev

* DONT ASK RYAN FOR HELP EVER

* wip

* wip

* endpoint upgrades continued

* path upgrade

* NeeeNeeeNeeeNeeeNING

* fix stuffs

* fixing time issue

* fixing broken funcs

* glorious nits

* shaz changes

* fixing errors for fundmon

* more error fixing for fundmon

* test running past 30s

* basic changes

* THX AGAIN SHAZBERT

* path system upgrade

* config upgrade

* unsaved stuffs

* broken wip config upgrade

* path system upgrade contd.

* path system upgrade contd

* path upgrade ready for review

* testing verbose removed

* linter stuffs

* appveyor stuffs

* appveyor stuff

* fixed?

* bugfix

* wip

* broken stuff

* fix test

* wierd hack fix

* appveyor pls stop

* error found

* more useless nits

* bitmex err

* broken wip

* broken wip path upgrade change to uint32

* changed url lookups to uint

* WOW

* ready4review

* config fixed HOPEFULLY

* config fix and glorious changes

* efficient way of getting orders and open orders

* binance wrapper logic fixing

* testing, adding tests and fixing lot of errrrrs

* merge master

* appveyor stuffs

* appveyor stuffs

* fmt

* test

* octalLiteral issue fix?

* octalLiteral fix?

* rm vb

* prnt ln to restart

* adding testz

* test fixzzz

* READY FOR REVIEW

* Actually ready now

* FORMATTING

* addressing shazzy n glorious nits

* crapveyor

* rm vb

* small change

* fixing err

* shazbert nits

* review changes

* requested changes

* more requested changes

* noo

* last nit fixes

* restart appveyor

* improving test cov

* Update .golangci.yml

* shazbert changes

* moving pair formatting

* format pair update wip

* path upgrade complete

* error fix

* appveyor linters

* more linters

* remove testexch

* more formatting changes

* changes

* shazbert changes

* checking older requested changes to ensure completion

* wip

* fixing broken code

* error fix

* all fixed

* additional changes

* more changes

* remove commented code

* ftx margin api

* appveyor fixes

* more appveyor issues + test addition

* more appveyor issues + test addition

* remove unnecessary

* testing

* testing, fixing okex api, error fix

* git merge fix

* go sum

* glorious changes and error fix

* rm vb

* more glorious changes and go mod tidy

* fixed now

* okex testing upgrade

* old config migration and batch fetching fix

* added test

* glorious requested changes WIP

* tested and fixed

* go fmted

* go fmt and test fix

* additional funcs and tests for fundingRates

* OKEX tested and fixed

* appveyor fixes

* ineff assign

* 1 glorious change

* error fix

* typo

* shazbert changes

* glorious code changes and path fixing huobi WIP

* adding assetType to accountinfo functions

* fixing panic

* panic fix and updating account info wrappers WIP

* updateaccountinfo updated

* testing WIP binance USDT n Coin Margined and Kraken Futures

* auth functions tested and fixed

* added test

* config reverted

* shazbert and glorious changes

* shazbert and glorious changes

* latest changes and portfolio update

* go fmt change:

* remove commented codes

* improved error checking

* index out of range fix

* rm ln

* critical nit

* glorious latest changes

* appveyor changes

* shazbert change

* easier readability

* latest glorious changes

* shadow dec

* assetstore updated

* last change

* another last change

* merge changes

* go mod tidy

* thrasher requested changes wip

* improving struct layouts

* appveyor go fmt

* remove unnecessary code

* shazbert changes

* small change

* oopsie

* tidy

* configtest reverted

* error fix

* oopsie

* for what

* test patch fix

* insecurities

* fixing tests

* fix config
2021-02-12 16:19:18 +11:00

821 lines
24 KiB
Go

package engine
import (
"context"
"errors"
"log"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/drivers"
"github.com/thrasher-corp/gocryptotrader/database/repository"
"github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
sqltrade "github.com/thrasher-corp/gocryptotrader/database/repository/trade"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/gctrpc"
"github.com/thrasher-corp/goose"
)
const (
unexpectedLackOfError = "unexpected lack of error"
migrationsFolder = "migrations"
databaseFolder = "database"
databaseName = "rpctestdb"
)
// Sets up everything required to run any function inside rpcserver
func RPCTestSetup(t *testing.T) *Engine {
database.DB.Mu.Lock()
var err error
dbConf := database.Config{
Enabled: true,
Driver: database.DBSQLite3,
ConnectionDetails: drivers.ConnectionDetails{
Database: databaseName,
},
}
engerino := new(Engine)
engerino.Config = &config.Config{}
err = engerino.Config.LoadConfig(config.TestFile, true)
if err != nil {
t.Fatalf("SetupTest: Failed to load config: %s", err)
}
if engerino.GetExchangeByName(testExchange) == nil {
err = engerino.LoadExchange(testExchange, false, nil)
if err != nil {
t.Fatalf("SetupTest: Failed to load exchange: %s", err)
}
}
engerino.Config.Database = dbConf
err = engerino.DatabaseManager.Start(engerino)
if err != nil {
log.Fatal(err)
}
path := filepath.Join("..", databaseFolder, migrationsFolder)
err = goose.Run("up", dbConn.SQL, repository.GetSQLDialect(), path, "")
if err != nil {
t.Fatalf("failed to run migrations %v", err)
}
uuider, _ := uuid.NewV4()
err = exchange.Insert(exchange.Details{Name: testExchange, UUID: uuider})
if err != nil {
t.Fatalf("failed to insert exchange %v", err)
}
database.DB.Mu.Unlock()
return engerino
}
func CleanRPCTest(t *testing.T, engerino *Engine) {
database.DB.Mu.Lock()
defer database.DB.Mu.Unlock()
err := engerino.DatabaseManager.Stop()
if err != nil {
t.Error(err)
return
}
err = os.Remove(filepath.Join(common.GetDefaultDataDir(runtime.GOOS), databaseFolder, databaseName))
if err != nil {
t.Error(err)
}
}
func TestGetSavedTrades(t *testing.T) {
engerino := RPCTestSetup(t)
defer CleanRPCTest(t, engerino)
s := RPCServer{Engine: engerino}
_, err := s.GetSavedTrades(context.Background(), &gctrpc.GetSavedTradesRequest{})
if err == nil {
t.Fatal(unexpectedLackOfError)
}
if !errors.Is(err, errInvalidArguments) {
t.Error(err)
}
_, err = s.GetSavedTrades(context.Background(), &gctrpc.GetSavedTradesRequest{
Exchange: "fake",
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
})
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if err != errExchangeNotLoaded {
t.Error(err)
}
_, err = s.GetSavedTrades(context.Background(), &gctrpc.GetSavedTradesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
})
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if err.Error() != "request for Bitstamp spot trade data between 2019-11-30 00:00:00 and 2020-01-01 01:01:01 and returned no results" {
t.Error(err)
}
err = sqltrade.Insert(sqltrade.Data{
Timestamp: time.Date(2020, 0, 0, 0, 0, 1, 0, time.UTC),
Exchange: testExchange,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
AssetType: asset.Spot.String(),
Price: 1337,
Amount: 1337,
Side: order.Buy.String(),
})
if err != nil {
t.Error(err)
return
}
_, err = s.GetSavedTrades(context.Background(), &gctrpc.GetSavedTradesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
})
if err != nil {
t.Error(err)
}
}
func TestConvertTradesToCandles(t *testing.T) {
engerino := RPCTestSetup(t)
defer CleanRPCTest(t, engerino)
s := RPCServer{Engine: engerino}
// bad param test
_, err := s.ConvertTradesToCandles(context.Background(), &gctrpc.ConvertTradesToCandlesRequest{})
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if !errors.Is(err, errInvalidArguments) {
t.Error(err)
}
// bad exchange test
_, err = s.ConvertTradesToCandles(context.Background(), &gctrpc.ConvertTradesToCandlesRequest{
Exchange: "faker",
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
})
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if err != errExchangeNotLoaded {
t.Error(err)
}
// no trades test
_, err = s.ConvertTradesToCandles(context.Background(), &gctrpc.ConvertTradesToCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 2, 2, 2, 2, 2, 2, time.UTC).Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
})
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if err.Error() != "no trades returned from supplied params" {
t.Error(err)
}
// add a trade
err = sqltrade.Insert(sqltrade.Data{
Timestamp: time.Date(2020, 1, 1, 1, 1, 2, 1, time.UTC),
Exchange: testExchange,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
AssetType: asset.Spot.String(),
Price: 1337,
Amount: 1337,
Side: order.Buy.String(),
})
if err != nil {
t.Error(err)
return
}
// get candle from one trade
var candles *gctrpc.GetHistoricCandlesResponse
candles, err = s.ConvertTradesToCandles(context.Background(), &gctrpc.ConvertTradesToCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 2, 2, 2, 2, 2, 2, time.UTC).Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
})
if err != nil {
t.Error(err)
}
if len(candles.Candle) == 0 {
t.Error("no candles returned")
}
// save generated candle to database
_, err = s.ConvertTradesToCandles(context.Background(), &gctrpc.ConvertTradesToCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 2, 2, 2, 2, 2, 2, time.UTC).Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
Sync: true,
})
if err != nil {
t.Error(err)
}
// forcefully remove previous candle and insert a new one
_, err = s.ConvertTradesToCandles(context.Background(), &gctrpc.ConvertTradesToCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 2, 2, 2, 2, 2, 2, time.UTC).Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
Sync: true,
Force: true,
})
if err != nil {
t.Error(err)
}
// load the saved candle to verify that it was overwritten
candles, err = s.GetHistoricCandles(context.Background(), &gctrpc.GetHistoricCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 2, 2, 2, 2, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
UseDb: true,
})
if err != nil {
t.Error(err)
}
if len(candles.Candle) != 1 {
t.Error("expected only one candle")
}
}
func TestGetHistoricCandles(t *testing.T) {
engerino := RPCTestSetup(t)
defer CleanRPCTest(t, engerino)
s := RPCServer{Engine: engerino}
// error checks
_, err := s.GetHistoricCandles(context.Background(), &gctrpc.GetHistoricCandlesRequest{
Exchange: "",
})
if err != nil && err.Error() != errExchangeNameUnset {
t.Error(err)
}
_, err = s.GetHistoricCandles(context.Background(), &gctrpc.GetHistoricCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{},
})
if err != nil && err.Error() != errCurrencyPairUnset {
t.Error(err)
}
_, err = s.GetHistoricCandles(context.Background(), &gctrpc.GetHistoricCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
})
if err != nil && err.Error() != errStartEndTimesUnset {
t.Error(err)
}
var results *gctrpc.GetHistoricCandlesResponse
defaultStart := time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC)
defaultEnd := time.Date(2020, 1, 2, 2, 2, 2, 2, time.UTC)
cp := currency.NewPair(currency.BTC, currency.USD)
// default run
results, err = s.GetHistoricCandles(context.Background(), &gctrpc.GetHistoricCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
Start: defaultStart.Format(common.SimpleTimeFormat),
End: defaultEnd.Format(common.SimpleTimeFormat),
AssetType: asset.Spot.String(),
TimeInterval: int64(kline.OneHour.Duration()),
})
if err != nil {
t.Error(err)
}
if len(results.Candle) == 0 {
t.Error("expected results")
}
// sync run
results, err = s.GetHistoricCandles(context.Background(), &gctrpc.GetHistoricCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
AssetType: asset.Spot.String(),
Start: defaultStart.Format(common.SimpleTimeFormat),
End: defaultEnd.Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
Sync: true,
ExRequest: true,
})
if err != nil {
t.Error(err)
}
if len(results.Candle) == 0 {
t.Error("expected results")
}
// db run
results, err = s.GetHistoricCandles(context.Background(), &gctrpc.GetHistoricCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
AssetType: asset.Spot.String(),
Start: defaultStart.Format(common.SimpleTimeFormat),
End: defaultEnd.Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
UseDb: true,
})
if err != nil {
t.Error(err)
}
if len(results.Candle) == 0 {
t.Error("expected results")
}
err = trade.SaveTradesToDatabase(trade.Data{
TID: "test123",
Exchange: testExchange,
CurrencyPair: cp,
AssetType: asset.Spot,
Price: 1337,
Amount: 1337,
Side: order.Buy,
Timestamp: time.Date(2020, 1, 2, 3, 1, 1, 7, time.UTC),
})
if err != nil {
t.Error(err)
return
}
// db run including trades
results, err = s.GetHistoricCandles(context.Background(), &gctrpc.GetHistoricCandlesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
AssetType: asset.Spot.String(),
Start: defaultStart.Format(common.SimpleTimeFormat),
End: time.Date(2020, 1, 2, 4, 2, 2, 2, time.UTC).Format(common.SimpleTimeFormat),
TimeInterval: int64(kline.OneHour.Duration()),
UseDb: true,
FillMissingWithTrades: true,
})
if err != nil {
t.Error(err)
}
if results.Candle[len(results.Candle)-1].Close != 1337 {
t.Error("expected fancy new candle based off fancy new trade data")
}
}
func TestFindMissingSavedTradeIntervals(t *testing.T) {
engerino := RPCTestSetup(t)
defer CleanRPCTest(t, engerino)
s := RPCServer{Engine: engerino}
// bad request checks
_, err := s.FindMissingSavedTradeIntervals(context.Background(), &gctrpc.FindMissingTradePeriodsRequest{})
if err == nil {
t.Error("expected error")
return
}
if !errors.Is(err, errInvalidArguments) {
t.Error(err)
return
}
cp := currency.NewPair(currency.BTC, currency.USD)
// no data found response
defaultStart := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).UTC()
defaultEnd := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC).UTC()
var resp *gctrpc.FindMissingIntervalsResponse
resp, err = s.FindMissingSavedTradeIntervals(context.Background(), &gctrpc.FindMissingTradePeriodsRequest{
ExchangeName: testExchange,
AssetType: asset.Spot.String(),
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
Start: defaultStart.UTC().Format(common.SimpleTimeFormat),
End: defaultEnd.UTC().Format(common.SimpleTimeFormat),
})
if err != nil {
t.Error(err)
}
if resp.Status == "" {
t.Errorf("expected a status message")
}
// one trade response
err = trade.SaveTradesToDatabase(trade.Data{
TID: "test1234",
Exchange: testExchange,
CurrencyPair: cp,
AssetType: asset.Spot,
Price: 1337,
Amount: 1337,
Side: order.Buy,
Timestamp: time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC),
})
if err != nil {
t.Error(err)
return
}
resp, err = s.FindMissingSavedTradeIntervals(context.Background(), &gctrpc.FindMissingTradePeriodsRequest{
ExchangeName: testExchange,
AssetType: asset.Spot.String(),
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
Start: defaultStart.In(time.UTC).Format(common.SimpleTimeFormat),
End: defaultEnd.In(time.UTC).Format(common.SimpleTimeFormat),
})
if err != nil {
t.Error(err)
}
if len(resp.MissingPeriods) != 2 {
t.Errorf("expected 2 missing period, received: %v", len(resp.MissingPeriods))
}
// two trades response
err = trade.SaveTradesToDatabase(trade.Data{
TID: "test123",
Exchange: testExchange,
CurrencyPair: cp,
AssetType: asset.Spot,
Price: 1337,
Amount: 1337,
Side: order.Buy,
Timestamp: time.Date(2020, 1, 1, 13, 0, 0, 0, time.UTC),
})
if err != nil {
t.Error(err)
return
}
resp, err = s.FindMissingSavedTradeIntervals(context.Background(), &gctrpc.FindMissingTradePeriodsRequest{
ExchangeName: testExchange,
AssetType: asset.Spot.String(),
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
Start: defaultStart.In(time.UTC).Format(common.SimpleTimeFormat),
End: defaultEnd.In(time.UTC).Format(common.SimpleTimeFormat),
})
if err != nil {
t.Error(err)
}
if len(resp.MissingPeriods) != 2 {
t.Errorf("expected 2 missing periods, received: %v", len(resp.MissingPeriods))
}
}
func TestFindMissingSavedCandleIntervals(t *testing.T) {
engerino := RPCTestSetup(t)
defer CleanRPCTest(t, engerino)
s := RPCServer{Engine: engerino}
// bad request checks
_, err := s.FindMissingSavedCandleIntervals(context.Background(), &gctrpc.FindMissingCandlePeriodsRequest{})
if err == nil {
t.Error("expected error")
return
}
if !errors.Is(err, errInvalidArguments) {
t.Error(err)
return
}
cp := currency.NewPair(currency.BTC, currency.USD)
// no data found response
defaultStart := time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC)
defaultEnd := time.Date(2020, 1, 2, 2, 2, 2, 2, time.UTC)
var resp *gctrpc.FindMissingIntervalsResponse
_, err = s.FindMissingSavedCandleIntervals(context.Background(), &gctrpc.FindMissingCandlePeriodsRequest{
ExchangeName: testExchange,
AssetType: asset.Spot.String(),
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
Interval: int64(time.Hour),
Start: defaultStart.Format(common.SimpleTimeFormat),
End: defaultEnd.Format(common.SimpleTimeFormat),
})
if err != nil && err.Error() != "no candle data found: Bitstamp BTC USD 3600 spot" {
t.Error(err)
return
}
// one candle missing periods response
_, err = kline.StoreInDatabase(&kline.Item{
Exchange: testExchange,
Pair: cp,
Asset: asset.Spot,
Interval: kline.OneHour,
Candles: []kline.Candle{
{
Time: time.Date(2020, 1, 1, 2, 1, 1, 1, time.UTC),
Open: 1337,
High: 1337,
Low: 1337,
Close: 1337,
Volume: 1337,
},
},
}, false)
if err != nil {
t.Error(err)
return
}
_, err = s.FindMissingSavedCandleIntervals(context.Background(), &gctrpc.FindMissingCandlePeriodsRequest{
ExchangeName: testExchange,
AssetType: asset.Spot.String(),
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
Interval: int64(time.Hour),
Start: defaultStart.Format(common.SimpleTimeFormat),
End: defaultEnd.Format(common.SimpleTimeFormat),
})
if err != nil {
t.Error(err)
}
// two candle missing periods response
_, err = kline.StoreInDatabase(&kline.Item{
Exchange: testExchange,
Pair: cp,
Asset: asset.Spot,
Interval: kline.OneHour,
Candles: []kline.Candle{
{
Time: time.Date(2020, 1, 1, 3, 1, 1, 1, time.UTC),
Open: 1337,
High: 1337,
Low: 1337,
Close: 1337,
Volume: 1337,
},
},
}, false)
if err != nil {
t.Error(err)
return
}
resp, err = s.FindMissingSavedCandleIntervals(context.Background(), &gctrpc.FindMissingCandlePeriodsRequest{
ExchangeName: testExchange,
AssetType: asset.Spot.String(),
Pair: &gctrpc.CurrencyPair{
Base: cp.Base.String(),
Quote: cp.Quote.String(),
},
Interval: int64(time.Hour),
Start: defaultStart.Format(common.SimpleTimeFormat),
End: defaultEnd.Format(common.SimpleTimeFormat),
})
if err != nil {
t.Error(err)
}
if len(resp.MissingPeriods) != 2 {
t.Errorf("expected 2 missing periods, received: %v", len(resp.MissingPeriods))
}
}
func TestSetExchangeTradeProcessing(t *testing.T) {
engerino := RPCTestSetup(t)
defer CleanRPCTest(t, engerino)
s := RPCServer{Engine: engerino}
_, err := s.SetExchangeTradeProcessing(context.Background(), &gctrpc.SetExchangeTradeProcessingRequest{Exchange: testExchange, Status: true})
if err != nil {
t.Error(err)
return
}
exch := s.GetExchangeByName(testExchange)
base := exch.GetBase()
if !base.IsSaveTradeDataEnabled() {
t.Error("expected true")
}
_, err = s.SetExchangeTradeProcessing(context.Background(), &gctrpc.SetExchangeTradeProcessingRequest{Exchange: testExchange, Status: false})
if err != nil {
t.Error(err)
return
}
exch = s.GetExchangeByName(testExchange)
base = exch.GetBase()
if base.IsSaveTradeDataEnabled() {
t.Error("expected false")
}
}
func TestGetRecentTrades(t *testing.T) {
engerino := RPCTestSetup(t)
defer CleanRPCTest(t, engerino)
s := RPCServer{Engine: engerino}
_, err := s.GetRecentTrades(context.Background(), &gctrpc.GetSavedTradesRequest{})
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if !errors.Is(err, errInvalidArguments) {
t.Error(err)
}
_, err = s.GetRecentTrades(context.Background(), &gctrpc.GetSavedTradesRequest{
Exchange: "fake",
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
})
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if err != errExchangeNotLoaded {
t.Error(err)
}
_, err = s.GetRecentTrades(context.Background(), &gctrpc.GetSavedTradesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
})
if err != nil {
t.Error(err)
}
}
func TestGetHistoricTrades(t *testing.T) {
engerino := RPCTestSetup(t)
defer CleanRPCTest(t, engerino)
s := RPCServer{Engine: engerino}
err := s.GetHistoricTrades(&gctrpc.GetSavedTradesRequest{}, nil)
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if !errors.Is(err, errInvalidArguments) {
t.Error(err)
}
err = s.GetHistoricTrades(&gctrpc.GetSavedTradesRequest{
Exchange: "fake",
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
}, nil)
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if err != errExchangeNotLoaded {
t.Error(err)
}
err = s.GetHistoricTrades(&gctrpc.GetSavedTradesRequest{
Exchange: testExchange,
Pair: &gctrpc.CurrencyPair{
Delimiter: currency.DashDelimiter,
Base: currency.BTC.String(),
Quote: currency.USD.String(),
},
AssetType: asset.Spot.String(),
Start: time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC).Format(common.SimpleTimeFormat),
End: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC).Format(common.SimpleTimeFormat),
}, nil)
if err == nil {
t.Error(unexpectedLackOfError)
return
}
if err != common.ErrFunctionNotSupported {
t.Error(err)
}
}
func TestGetAccountInfo(t *testing.T) {
bot := SetupTestHelpers(t)
s := RPCServer{Engine: bot}
r, err := s.GetAccountInfo(context.Background(), &gctrpc.GetAccountInfoRequest{Exchange: fakePassExchange, AssetType: asset.Spot.String()})
if err != nil {
t.Fatalf("TestGetAccountInfo: Failed to get account info: %s", err)
}
if r.Accounts[0].Currencies[0].TotalValue != 10 {
t.Fatal("TestGetAccountInfo: Unexpected value of the 'TotalValue'")
}
}
func TestUpdateAccountInfo(t *testing.T) {
bot := SetupTestHelpers(t)
s := RPCServer{Engine: bot}
getResponse, err := s.GetAccountInfo(context.Background(), &gctrpc.GetAccountInfoRequest{Exchange: fakePassExchange, AssetType: asset.Spot.String()})
if err != nil {
t.Fatalf("TestGetAccountInfo: Failed to get account info: %s", err)
}
updateResponse, err := s.UpdateAccountInfo(context.Background(), &gctrpc.GetAccountInfoRequest{Exchange: fakePassExchange, AssetType: asset.Futures.String()})
if err != nil {
t.Fatalf("TestGetAccountInfo: Failed to update account info: %s", err)
}
if getResponse.Accounts[0].Currencies[0].TotalValue == updateResponse.Accounts[0].Currencies[0].TotalValue {
t.Fatalf("TestGetAccountInfo: Unexpected value of the 'TotalValue'")
}
}