mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 23:16:52 +00:00
Currency package update (#247)
* Initial currency overhaul before service system implementation * Remove redundant currency string in orderbook.Base Unexport lastupdated field in orderbook.Base as it was being instantiated multiple times Add error handling for process orderbook * Remove redundant currency string in ticker.Price Unexport lastupdated field in ticker.Price Add error handling for process ticker function and fix tests * Phase Two Update * Update translations to use map type - thankyou to kempeng for spotting this * Change pair method name from Display -> Format for better readability * Fixes misspelling and tests * Implement requested changes from GloriousCode * Remove reduntant function and streamlined return in currency_translation.go * Revert pair method naming conventions * Change currency naming conventions * Changed code type to exported Item type with underlying string to reduce complexity * Added interim orderbook process method to orderbook.Base type * Changed feebuilder struct field to currency.Pair * Adds fall over system for backup fx providers * deprecate function and children and fix linter issue with btcmarkets * Fixed requested changes * Fix bug and move mtx for rates * Fixed after rebase oopsies * Fix linter issues * Fixes race conditions in testing functions * Final phase coinmarketcap update * fix linter issues * Implement requested changes * Adds configuration variables to increase/decrease time durations between updating currency file and fetching new currency rates * Add a collection of tests to improve codecov * After rebase oopsy fixes for btse * Fix requested changes * fix after rebase oopsies and add more efficient comparison checks within currency pair * Fix linter issues
This commit is contained in:
committed by
Adrian Gallagher
parent
ed760e184e
commit
0990f9d118
@@ -47,7 +47,7 @@ totalAsks, totalOrderbookVal := ob.CalculateTotalAsks()
|
||||
the package itself.
|
||||
|
||||
```go
|
||||
ob, err := orderbook.GetOrderbook(...)
|
||||
ob, err := orderbook.Get(...)
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
)
|
||||
|
||||
// Const values for orderbook package
|
||||
@@ -32,23 +32,23 @@ type Item struct {
|
||||
|
||||
// Base holds the fields for the orderbook base
|
||||
type Base struct {
|
||||
Pair pair.CurrencyPair `json:"pair"`
|
||||
CurrencyPair string `json:"CurrencyPair"`
|
||||
Bids []Item `json:"bids"`
|
||||
Asks []Item `json:"asks"`
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
AssetType string
|
||||
Pair currency.Pair `json:"pair"`
|
||||
Bids []Item `json:"bids"`
|
||||
Asks []Item `json:"asks"`
|
||||
LastUpdated time.Time `json:"lastUpdated"`
|
||||
AssetType string `json:"assetType"`
|
||||
ExchangeName string `json:"exchangeName"`
|
||||
}
|
||||
|
||||
// Orderbook holds the orderbook information for a currency pair and type
|
||||
type Orderbook struct {
|
||||
Orderbook map[pair.CurrencyItem]map[pair.CurrencyItem]map[string]Base
|
||||
Orderbook map[*currency.Item]map[*currency.Item]map[string]Base
|
||||
ExchangeName string
|
||||
}
|
||||
|
||||
// CalculateTotalBids returns the total amount of bids and the total orderbook
|
||||
// TotalBidsAmount returns the total amount of bids and the total orderbook
|
||||
// bids value
|
||||
func (o *Base) CalculateTotalBids() (amountCollated, total float64) {
|
||||
func (o *Base) TotalBidsAmount() (amountCollated, total float64) {
|
||||
for _, x := range o.Bids {
|
||||
amountCollated += x.Amount
|
||||
total += x.Amount * x.Price
|
||||
@@ -56,9 +56,9 @@ func (o *Base) CalculateTotalBids() (amountCollated, total float64) {
|
||||
return amountCollated, total
|
||||
}
|
||||
|
||||
// CalculateTotalAsks returns the total amount of asks and the total orderbook
|
||||
// TotalAsksAmount returns the total amount of asks and the total orderbook
|
||||
// asks value
|
||||
func (o *Base) CalculateTotalAsks() (amountCollated, total float64) {
|
||||
func (o *Base) TotalAsksAmount() (amountCollated, total float64) {
|
||||
for _, x := range o.Asks {
|
||||
amountCollated += x.Amount
|
||||
total += x.Amount * x.Price
|
||||
@@ -73,27 +73,27 @@ func (o *Base) Update(bids, asks []Item) {
|
||||
o.LastUpdated = time.Now()
|
||||
}
|
||||
|
||||
// GetOrderbook checks and returns the orderbook given an exchange name and
|
||||
// currency pair if it exists
|
||||
func GetOrderbook(exchange string, p pair.CurrencyPair, orderbookType string) (Base, error) {
|
||||
orderbook, err := GetOrderbookByExchange(exchange)
|
||||
// Get checks and returns the orderbook given an exchange name and currency pair
|
||||
// if it exists
|
||||
func Get(exchange string, p currency.Pair, orderbookType string) (Base, error) {
|
||||
orderbook, err := GetByExchange(exchange)
|
||||
if err != nil {
|
||||
return Base{}, err
|
||||
}
|
||||
|
||||
if !FirstCurrencyExists(exchange, p.FirstCurrency) {
|
||||
if !BaseCurrencyExists(exchange, p.Base) {
|
||||
return Base{}, errors.New(ErrPrimaryCurrencyNotFound)
|
||||
}
|
||||
|
||||
if !SecondCurrencyExists(exchange, p) {
|
||||
if !QuoteCurrencyExists(exchange, p) {
|
||||
return Base{}, errors.New(ErrSecondaryCurrencyNotFound)
|
||||
}
|
||||
|
||||
return orderbook.Orderbook[p.FirstCurrency][p.SecondCurrency][orderbookType], nil
|
||||
return orderbook.Orderbook[p.Base.Item][p.Quote.Item][orderbookType], nil
|
||||
}
|
||||
|
||||
// GetOrderbookByExchange returns an exchange orderbook
|
||||
func GetOrderbookByExchange(exchange string) (*Orderbook, error) {
|
||||
// GetByExchange returns an exchange orderbook
|
||||
func GetByExchange(exchange string) (*Orderbook, error) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for x := range Orderbooks {
|
||||
@@ -104,14 +104,14 @@ func GetOrderbookByExchange(exchange string) (*Orderbook, error) {
|
||||
return nil, errors.New(ErrOrderbookForExchangeNotFound)
|
||||
}
|
||||
|
||||
// FirstCurrencyExists checks to see if the first currency of the orderbook map
|
||||
// BaseCurrencyExists checks to see if the base currency of the orderbook map
|
||||
// exists
|
||||
func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool {
|
||||
func BaseCurrencyExists(exchange string, currency currency.Code) bool {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for _, y := range Orderbooks {
|
||||
if y.ExchangeName == exchange {
|
||||
if _, ok := y.Orderbook[currency]; ok {
|
||||
if _, ok := y.Orderbook[currency.Item]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -119,15 +119,15 @@ func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SecondCurrencyExists checks to see if the second currency of the orderbook
|
||||
// QuoteCurrencyExists checks to see if the quote currency of the orderbook
|
||||
// map exists
|
||||
func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool {
|
||||
func QuoteCurrencyExists(exchange string, p currency.Pair) bool {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for _, y := range Orderbooks {
|
||||
if y.ExchangeName == exchange {
|
||||
if _, ok := y.Orderbook[p.FirstCurrency]; ok {
|
||||
if _, ok := y.Orderbook[p.FirstCurrency][p.SecondCurrency]; ok {
|
||||
if _, ok := y.Orderbook[p.Base.Item]; ok {
|
||||
if _, ok := y.Orderbook[p.Base.Item][p.Quote.Item]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -137,53 +137,57 @@ func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool {
|
||||
}
|
||||
|
||||
// CreateNewOrderbook creates a new orderbook
|
||||
func CreateNewOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew Base, orderbookType string) Orderbook {
|
||||
func CreateNewOrderbook(exchangeName string, orderbookNew Base, orderbookType string) *Orderbook {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
orderbook := Orderbook{}
|
||||
orderbook.ExchangeName = exchangeName
|
||||
orderbook.Orderbook = make(map[pair.CurrencyItem]map[pair.CurrencyItem]map[string]Base)
|
||||
a := make(map[pair.CurrencyItem]map[string]Base)
|
||||
orderbook.Orderbook = make(map[*currency.Item]map[*currency.Item]map[string]Base)
|
||||
a := make(map[*currency.Item]map[string]Base)
|
||||
b := make(map[string]Base)
|
||||
b[orderbookType] = orderbookNew
|
||||
a[p.SecondCurrency] = b
|
||||
orderbook.Orderbook[p.FirstCurrency] = a
|
||||
a[orderbookNew.Pair.Quote.Item] = b
|
||||
orderbook.Orderbook[orderbookNew.Pair.Base.Item] = a
|
||||
Orderbooks = append(Orderbooks, orderbook)
|
||||
return orderbook
|
||||
return &orderbook
|
||||
}
|
||||
|
||||
// ProcessOrderbook processes incoming orderbooks, creating or updating the
|
||||
// Orderbook list
|
||||
func ProcessOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew Base, orderbookType string) {
|
||||
if orderbookNew.Pair.Pair() == "" {
|
||||
// set Pair if not set
|
||||
orderbookNew.Pair = p
|
||||
}
|
||||
orderbookNew.CurrencyPair = p.Pair().String()
|
||||
if orderbookNew.LastUpdated.IsZero() {
|
||||
orderbookNew.LastUpdated = time.Now()
|
||||
// Process processes incoming orderbooks, creating or updating the orderbook
|
||||
// list
|
||||
func (o Base) Process() error {
|
||||
if o.Pair.IsEmpty() {
|
||||
return errors.New("orderbook currency pair not populated")
|
||||
}
|
||||
|
||||
orderbook, err := GetOrderbookByExchange(exchangeName)
|
||||
if o.AssetType == "" {
|
||||
return errors.New("orderbook asset type not set")
|
||||
}
|
||||
|
||||
if o.LastUpdated.IsZero() {
|
||||
o.LastUpdated = time.Now()
|
||||
}
|
||||
|
||||
orderbook, err := GetByExchange(o.ExchangeName)
|
||||
if err != nil {
|
||||
CreateNewOrderbook(exchangeName, p, orderbookNew, orderbookType)
|
||||
return
|
||||
CreateNewOrderbook(o.ExchangeName, o, o.AssetType)
|
||||
return nil
|
||||
}
|
||||
|
||||
if FirstCurrencyExists(exchangeName, p.FirstCurrency) {
|
||||
if BaseCurrencyExists(o.ExchangeName, o.Pair.Base) {
|
||||
m.Lock()
|
||||
a := make(map[string]Base)
|
||||
a[orderbookType] = orderbookNew
|
||||
orderbook.Orderbook[p.FirstCurrency][p.SecondCurrency] = a
|
||||
a[o.AssetType] = o
|
||||
orderbook.Orderbook[o.Pair.Base.Item][o.Pair.Quote.Item] = a
|
||||
m.Unlock()
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
m.Lock()
|
||||
a := make(map[pair.CurrencyItem]map[string]Base)
|
||||
a := make(map[*currency.Item]map[string]Base)
|
||||
b := make(map[string]Base)
|
||||
b[orderbookType] = orderbookNew
|
||||
a[p.SecondCurrency] = b
|
||||
orderbook.Orderbook[p.FirstCurrency] = a
|
||||
b[o.AssetType] = o
|
||||
a[o.Pair.Quote.Item] = b
|
||||
orderbook.Orderbook[o.Pair.Base.Item] = a
|
||||
m.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,20 +7,20 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
log "github.com/thrasher-/gocryptotrader/logger"
|
||||
)
|
||||
|
||||
func TestCalculateTotalBids(t *testing.T) {
|
||||
t.Parallel()
|
||||
currency := pair.NewCurrencyPair("BTC", "USD")
|
||||
currency := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Bids: []Item{{Price: 100, Amount: 10}},
|
||||
LastUpdated: time.Now(),
|
||||
Pair: currency,
|
||||
Bids: []Item{{Price: 100, Amount: 10}},
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
a, b := base.CalculateTotalBids()
|
||||
a, b := base.TotalBidsAmount()
|
||||
if a != 10 && b != 1000 {
|
||||
t.Fatal("Test failed. TestCalculateTotalBids expected a = 10 and b = 1000")
|
||||
}
|
||||
@@ -28,15 +28,13 @@ func TestCalculateTotalBids(t *testing.T) {
|
||||
|
||||
func TestCalculateTotaAsks(t *testing.T) {
|
||||
t.Parallel()
|
||||
currency := pair.NewCurrencyPair("BTC", "USD")
|
||||
currency := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
LastUpdated: time.Now(),
|
||||
Pair: currency,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
}
|
||||
|
||||
a, b := base.CalculateTotalAsks()
|
||||
a, b := base.TotalAsksAmount()
|
||||
if a != 10 && b != 1000 {
|
||||
t.Fatal("Test failed. TestCalculateTotalAsks expected a = 10 and b = 1000")
|
||||
}
|
||||
@@ -44,14 +42,13 @@ func TestCalculateTotaAsks(t *testing.T) {
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
currency := pair.NewCurrencyPair("BTC", "USD")
|
||||
currency := currency.NewPairFromStrings("BTC", "USD")
|
||||
timeNow := time.Now()
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
LastUpdated: timeNow,
|
||||
Pair: currency,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
LastUpdated: timeNow,
|
||||
}
|
||||
|
||||
asks := []Item{{Price: 200, Amount: 101}}
|
||||
@@ -63,147 +60,141 @@ func TestUpdate(t *testing.T) {
|
||||
t.Fatal("test failed. TestUpdate expected LastUpdated to be greater then original time")
|
||||
}
|
||||
|
||||
a, b := base.CalculateTotalAsks()
|
||||
a, b := base.TotalAsksAmount()
|
||||
if a != 100 && b != 20200 {
|
||||
t.Fatal("Test failed. TestUpdate expected a = 100 and b = 20100")
|
||||
}
|
||||
|
||||
a, b = base.CalculateTotalBids()
|
||||
a, b = base.TotalBidsAmount()
|
||||
if a != 100 && b != 20100 {
|
||||
t.Fatal("Test failed. TestUpdate expected a = 100 and b = 20100")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrderbook(t *testing.T) {
|
||||
currency := pair.NewCurrencyPair("BTC", "USD")
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", currency, base, Spot)
|
||||
CreateNewOrderbook("Exchange", base, Spot)
|
||||
|
||||
result, err := GetOrderbook("Exchange", currency, Spot)
|
||||
result, err := Get("Exchange", c, Spot)
|
||||
if err != nil {
|
||||
t.Fatalf("Test failed. TestGetOrderbook failed to get orderbook. Error %s",
|
||||
err)
|
||||
}
|
||||
|
||||
if result.Pair.Pair() != currency.Pair() {
|
||||
if result.Pair.String() != c.String() {
|
||||
t.Fatal("Test failed. TestGetOrderbook failed. Mismatched pairs")
|
||||
}
|
||||
|
||||
_, err = GetOrderbook("nonexistent", currency, Spot)
|
||||
_, err = Get("nonexistent", c, Spot)
|
||||
if err == nil {
|
||||
t.Fatal("Test failed. TestGetOrderbook retrieved non-existent orderbook")
|
||||
}
|
||||
|
||||
currency.FirstCurrency = "blah"
|
||||
_, err = GetOrderbook("Exchange", currency, Spot)
|
||||
c.Base = currency.NewCode("blah")
|
||||
_, err = Get("Exchange", c, Spot)
|
||||
if err == nil {
|
||||
t.Fatal("Test failed. TestGetOrderbook retrieved non-existent orderbook using invalid first currency")
|
||||
}
|
||||
|
||||
newCurrency := pair.NewCurrencyPair("BTC", "AUD")
|
||||
_, err = GetOrderbook("Exchange", newCurrency, Spot)
|
||||
newCurrency := currency.NewPairFromStrings("BTC", "AUD")
|
||||
_, err = Get("Exchange", newCurrency, Spot)
|
||||
if err == nil {
|
||||
t.Fatal("Test failed. TestGetOrderbook retrieved non-existent orderbook using invalid second currency")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrderbookByExchange(t *testing.T) {
|
||||
currency := pair.NewCurrencyPair("BTC", "USD")
|
||||
currency := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
Pair: currency,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", currency, base, Spot)
|
||||
CreateNewOrderbook("Exchange", base, Spot)
|
||||
|
||||
_, err := GetOrderbookByExchange("Exchange")
|
||||
_, err := GetByExchange("Exchange")
|
||||
if err != nil {
|
||||
t.Fatalf("Test failed. TestGetOrderbookByExchange failed to get orderbook. Error %s",
|
||||
err)
|
||||
}
|
||||
|
||||
_, err = GetOrderbookByExchange("nonexistent")
|
||||
_, err = GetByExchange("nonexistent")
|
||||
if err == nil {
|
||||
t.Fatal("Test failed. TestGetOrderbookByExchange retrieved non-existent orderbook")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstCurrencyExists(t *testing.T) {
|
||||
currency := pair.NewCurrencyPair("BTC", "AUD")
|
||||
c := currency.NewPairFromStrings("BTC", "AUD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", currency, base, Spot)
|
||||
CreateNewOrderbook("Exchange", base, Spot)
|
||||
|
||||
if !FirstCurrencyExists("Exchange", currency.FirstCurrency) {
|
||||
if !BaseCurrencyExists("Exchange", c.Base) {
|
||||
t.Fatal("Test failed. TestFirstCurrencyExists expected first currency doesn't exist")
|
||||
}
|
||||
|
||||
var item pair.CurrencyItem = "blah"
|
||||
if FirstCurrencyExists("Exchange", item) {
|
||||
var item = currency.NewCode("blah")
|
||||
if BaseCurrencyExists("Exchange", item) {
|
||||
t.Fatal("Test failed. TestFirstCurrencyExists unexpected first currency exists")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecondCurrencyExists(t *testing.T) {
|
||||
currency := pair.NewCurrencyPair("BTC", "USD")
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", currency, base, Spot)
|
||||
CreateNewOrderbook("Exchange", base, Spot)
|
||||
|
||||
if !SecondCurrencyExists("Exchange", currency) {
|
||||
if !QuoteCurrencyExists("Exchange", c) {
|
||||
t.Fatal("Test failed. TestSecondCurrencyExists expected first currency doesn't exist")
|
||||
}
|
||||
|
||||
currency.SecondCurrency = "blah"
|
||||
if SecondCurrencyExists("Exchange", currency) {
|
||||
c.Quote = currency.NewCode("blah")
|
||||
if QuoteCurrencyExists("Exchange", c) {
|
||||
t.Fatal("Test failed. TestSecondCurrencyExists unexpected first currency exists")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNewOrderbook(t *testing.T) {
|
||||
currency := pair.NewCurrencyPair("BTC", "USD")
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", currency, base, Spot)
|
||||
CreateNewOrderbook("Exchange", base, Spot)
|
||||
|
||||
result, err := GetOrderbook("Exchange", currency, Spot)
|
||||
result, err := Get("Exchange", c, Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestCreateNewOrderbook failed to create new orderbook")
|
||||
}
|
||||
|
||||
if result.Pair.Pair() != currency.Pair() {
|
||||
if result.Pair.String() != c.String() {
|
||||
t.Fatal("Test failed. TestCreateNewOrderbook result pair is incorrect")
|
||||
}
|
||||
|
||||
a, b := result.CalculateTotalAsks()
|
||||
a, b := result.TotalAsksAmount()
|
||||
if a != 10 && b != 1000 {
|
||||
t.Fatal("Test failed. TestCreateNewOrderbook CalculateTotalAsks value is incorrect")
|
||||
}
|
||||
|
||||
a, b = result.CalculateTotalBids()
|
||||
a, b = result.TotalBidsAmount()
|
||||
if a != 10 && b != 2000 {
|
||||
t.Fatal("Test failed. TestCreateNewOrderbook CalculateTotalBids value is incorrect")
|
||||
}
|
||||
@@ -211,54 +202,72 @@ func TestCreateNewOrderbook(t *testing.T) {
|
||||
|
||||
func TestProcessOrderbook(t *testing.T) {
|
||||
Orderbooks = []Orderbook{}
|
||||
currency := pair.NewCurrencyPair("BTC", "USD")
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
CurrencyPair: currency.Pair().String(),
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
ExchangeName: "Exchange",
|
||||
AssetType: Spot,
|
||||
}
|
||||
|
||||
ProcessOrderbook("Exchange", currency, base, Spot)
|
||||
err := base.Process()
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
|
||||
result, err := GetOrderbook("Exchange", currency, Spot)
|
||||
result, err := Get("Exchange", c, Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to create new orderbook")
|
||||
}
|
||||
|
||||
if result.Pair.Pair() != currency.Pair() {
|
||||
if result.Pair.String() != c.String() {
|
||||
t.Fatal("Test failed. TestProcessOrderbook result pair is incorrect")
|
||||
}
|
||||
|
||||
currency = pair.NewCurrencyPair("BTC", "GBP")
|
||||
base.Pair = currency
|
||||
ProcessOrderbook("Exchange", currency, base, Spot)
|
||||
c = currency.NewPairFromStrings("BTC", "GBP")
|
||||
base.Pair = c
|
||||
|
||||
result, err = GetOrderbook("Exchange", currency, Spot)
|
||||
err = base.Process()
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
|
||||
result, err = Get("Exchange", c, Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
||||
}
|
||||
|
||||
if result.Pair.Pair() != currency.Pair() {
|
||||
if result.Pair.String() != c.String() {
|
||||
t.Fatal("Test failed. TestProcessOrderbook result pair is incorrect")
|
||||
}
|
||||
|
||||
base.Asks = []Item{{Price: 200, Amount: 200}}
|
||||
ProcessOrderbook("Exchange", currency, base, "monthly")
|
||||
base.AssetType = "monthly"
|
||||
err = base.Process()
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
|
||||
result, err = GetOrderbook("Exchange", currency, "monthly")
|
||||
result, err = Get("Exchange", c, "monthly")
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
||||
}
|
||||
|
||||
a, b := result.CalculateTotalAsks()
|
||||
a, b := result.TotalAsksAmount()
|
||||
if a != 200 && b != 40000 {
|
||||
t.Fatal("Test failed. TestProcessOrderbook CalculateTotalsAsks incorrect values")
|
||||
}
|
||||
|
||||
base.Bids = []Item{{Price: 420, Amount: 200}}
|
||||
ProcessOrderbook("Blah", currency, base, "quarterly")
|
||||
result, err = GetOrderbook("Blah", currency, "quarterly")
|
||||
base.ExchangeName = "Blah"
|
||||
base.AssetType = "quarterly"
|
||||
err = base.Process()
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
|
||||
result, err = Get("Blah", c, "quarterly")
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to create new orderbook")
|
||||
}
|
||||
@@ -269,7 +278,7 @@ func TestProcessOrderbook(t *testing.T) {
|
||||
|
||||
type quick struct {
|
||||
Name string
|
||||
P pair.CurrencyPair
|
||||
P currency.Pair
|
||||
Bids []Item
|
||||
Asks []Item
|
||||
}
|
||||
@@ -281,36 +290,54 @@ func TestProcessOrderbook(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
var m sync.Mutex
|
||||
|
||||
var catastrophicFailure bool
|
||||
|
||||
for i := 0; i < 500; i++ {
|
||||
if catastrophicFailure {
|
||||
break
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
newName := "Exchange" + strconv.FormatInt(rand.Int63(), 10)
|
||||
newPairs := pair.NewCurrencyPair("BTC"+strconv.FormatInt(rand.Int63(), 10),
|
||||
"USD"+strconv.FormatInt(rand.Int63(), 10))
|
||||
newPairs := currency.NewPair(currency.NewCode("BTC"+strconv.FormatInt(rand.Int63(), 10)),
|
||||
currency.NewCode("USD"+strconv.FormatInt(rand.Int63(), 10)))
|
||||
|
||||
asks := []Item{{Price: rand.Float64(), Amount: rand.Float64()}}
|
||||
bids := []Item{{Price: rand.Float64(), Amount: rand.Float64()}}
|
||||
base := Base{
|
||||
base := &Base{
|
||||
Pair: newPairs,
|
||||
CurrencyPair: newPairs.Pair().String(),
|
||||
Asks: asks,
|
||||
Bids: bids,
|
||||
ExchangeName: newName,
|
||||
AssetType: Spot,
|
||||
}
|
||||
|
||||
ProcessOrderbook(newName, newPairs, base, Spot)
|
||||
m.Lock()
|
||||
err = base.Process()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
catastrophicFailure = true
|
||||
return
|
||||
}
|
||||
|
||||
testArray = append(testArray, quick{Name: newName, P: newPairs, Bids: bids, Asks: asks})
|
||||
m.Unlock()
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
if catastrophicFailure {
|
||||
t.Fatal("Test Failed - Process() error", err)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
for _, test := range testArray {
|
||||
wg.Add(1)
|
||||
fatalErr := false
|
||||
go func(test quick) {
|
||||
result, err := GetOrderbook(test.Name, test.P, Spot)
|
||||
result, err := Get(test.Name, test.P, Spot)
|
||||
if err != nil {
|
||||
fatalErr = true
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user