mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 07:26:46 +00:00
Merge branch 'master' into engine
This commit is contained in:
@@ -9,11 +9,13 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// Const values for orderbook package
|
||||
// const values for orderbook package
|
||||
const (
|
||||
ErrOrderbookForExchangeNotFound = "ticker for exchange does not exist"
|
||||
ErrPrimaryCurrencyNotFound = "primary currency for orderbook not found"
|
||||
ErrSecondaryCurrencyNotFound = "secondary currency for orderbook not found"
|
||||
errExchangeOrderbookNotFound = "orderbook for exchange does not exist"
|
||||
errPairNotSet = "orderbook currency pair not set"
|
||||
errAssetTypeNotSet = "orderbook asset type not set"
|
||||
errBaseCurrencyNotFound = "orderbook base currency not found"
|
||||
errQuoteCurrencyNotFound = "orderbook quote currency not found"
|
||||
)
|
||||
|
||||
// Vars for the orderbook package
|
||||
@@ -81,11 +83,11 @@ func Get(exchange string, p currency.Pair, orderbookType asset.Item) (Base, erro
|
||||
}
|
||||
|
||||
if !BaseCurrencyExists(exchange, p.Base) {
|
||||
return Base{}, errors.New(ErrPrimaryCurrencyNotFound)
|
||||
return Base{}, errors.New(errBaseCurrencyNotFound)
|
||||
}
|
||||
|
||||
if !QuoteCurrencyExists(exchange, p) {
|
||||
return Base{}, errors.New(ErrSecondaryCurrencyNotFound)
|
||||
return Base{}, errors.New(errQuoteCurrencyNotFound)
|
||||
}
|
||||
|
||||
return orderbook.Orderbook[p.Base.Item][p.Quote.Item][orderbookType], nil
|
||||
@@ -100,7 +102,7 @@ func GetByExchange(exchange string) (*Orderbook, error) {
|
||||
return &Orderbooks[x], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New(ErrOrderbookForExchangeNotFound)
|
||||
return nil, errors.New(errExchangeOrderbookNotFound)
|
||||
}
|
||||
|
||||
// BaseCurrencyExists checks to see if the base currency of the orderbook map
|
||||
@@ -155,11 +157,11 @@ func CreateNewOrderbook(exchangeName string, orderbookNew *Base, orderbookType a
|
||||
// list
|
||||
func (o *Base) Process() error {
|
||||
if o.Pair.IsEmpty() {
|
||||
return errors.New("orderbook currency pair not populated")
|
||||
return errors.New(errPairNotSet)
|
||||
}
|
||||
|
||||
if o.AssetType == "" {
|
||||
return errors.New("orderbook asset type not set")
|
||||
return errors.New(errAssetTypeNotSet)
|
||||
}
|
||||
|
||||
if o.LastUpdated.IsZero() {
|
||||
|
||||
@@ -87,7 +87,7 @@ func TestGetOrderbook(t *testing.T) {
|
||||
t.Fatalf("Test failed. TestGetOrderbook failed to get orderbook. Error %s",
|
||||
err)
|
||||
}
|
||||
if result.Pair.String() != c.String() {
|
||||
if !result.Pair.Equal(c) {
|
||||
t.Fatal("Test failed. TestGetOrderbook failed. Mismatched pairs")
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ func TestCreateNewOrderbook(t *testing.T) {
|
||||
t.Fatal("Test failed. TestCreateNewOrderbook failed to create new orderbook")
|
||||
}
|
||||
|
||||
if result.Pair.String() != c.String() {
|
||||
if !result.Pair.Equal(c) {
|
||||
t.Fatal("Test failed. TestCreateNewOrderbook result pair is incorrect")
|
||||
}
|
||||
|
||||
@@ -205,41 +205,66 @@ func TestProcessOrderbook(t *testing.T) {
|
||||
Orderbooks = []Orderbook{}
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
ExchangeName: "Exchange",
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
// test for empty pair
|
||||
base.Pair = currency.Pair{}
|
||||
err := base.Process()
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
if err == nil {
|
||||
t.Error("empty pair should throw an err")
|
||||
}
|
||||
|
||||
// test for empty asset type
|
||||
base.Pair = c
|
||||
err = base.Process()
|
||||
if err == nil {
|
||||
t.Error("empty asset type should throw an err")
|
||||
}
|
||||
|
||||
// now process a valid orderbook
|
||||
base.AssetType = asset.Spot
|
||||
err = base.Process()
|
||||
if err != nil {
|
||||
t.Error("unexpcted result: ", err)
|
||||
}
|
||||
result, err := Get("Exchange", c, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to create new orderbook")
|
||||
}
|
||||
|
||||
if result.Pair.String() != c.String() {
|
||||
if !result.Pair.Equal(c) {
|
||||
t.Fatal("Test failed. TestProcessOrderbook result pair is incorrect")
|
||||
}
|
||||
|
||||
// now test for processing a pair with a different quote currency
|
||||
c = currency.NewPairFromStrings("BTC", "GBP")
|
||||
base.Pair = c
|
||||
|
||||
err = base.Process()
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
|
||||
result, err = Get("Exchange", c, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
||||
}
|
||||
if !result.Pair.Equal(c) {
|
||||
t.Fatal("Test failed. TestProcessOrderbook result pair is incorrect")
|
||||
}
|
||||
|
||||
if result.Pair.String() != c.String() {
|
||||
// now test for processing a pair which has a different base currency
|
||||
c = currency.NewPairFromStrings("LTC", "GBP")
|
||||
base.Pair = c
|
||||
err = base.Process()
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
result, err = Get("Exchange", c, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
||||
}
|
||||
if !result.Pair.Equal(c) {
|
||||
t.Fatal("Test failed. TestProcessOrderbook result pair is incorrect")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user