Files
gocryptotrader/engine/exchange_manager_test.go
Scott 86d3724507 Futures order position tracking & FTX scaled collateral calculation (#868)
* implements futures functions and GRPC functions on new branch

* lint and test fixes

* Fix uneven split pnl. Adds collateral weight test. docs. New clear func

* Test protection if someone has zero collateral

* Uses string instead of double for accuracy

* Fixes old code panic

* context, match, docs

* Addresses Shazniterinos, var names, expanded tests

* Returns subaccount name, provides USD values when offlinecalc

* Fixes oopsie

* Fixes cool bug which allowed made up subaccount results

* Subaccount override on FTX, subaccount results for collateral

* Strenghten collateral account info checks. Improve FTX test

* English is my first language

* Fixes oopsies

* Fixes for unrealised PNL & collateral rendering

* Fixes lint and tests

* Shaznit fixes

* Secret Shaznit

* Updates account information across wrappers to include more fields

* Updates online collateral calculations. Updates RPC data

* Accurately calculates collateral offline and online minus testing

* Tests and lint chocolate

* Simplifies accountinfo results

* Fixes shaznits

* Adds new func

* Increases collateral accuracy again again again x 200

* Increases accuracy of collateral rendering

* Fixes minor merge/test issues

* Linterino

* Fixes ws test. Improves collateral calculations and rendering

* Make it prettier

* Removes the lock I put on 👀

* Adds `additional_collateral_used` field, renders orig currency

* Fixes unrelated test

* Fix test

* Correctly calculate spot margin borrow collateral

* Address fun lint surprise

See https://github.com/golangci/golangci-lint/issues/741#issuecomment-1017014331

* Strange lint fixing x2

* Continued lint journey

* Nolint the nolint to not lint the lint

* Adds two new fields to response

* More linting issues arising

* fIX3s_c4s|NG

* Fixes command flags' incorrect numbering

* FairMarket = Won
2022-02-28 16:39:36 +11:00

130 lines
3.3 KiB
Go

package engine
import (
"errors"
"fmt"
"strings"
"testing"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitfinex"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
)
func TestSetupExchangeManager(t *testing.T) {
t.Parallel()
m := SetupExchangeManager()
if m == nil { //nolint:staticcheck,nolintlint // SA5011 Ignore the nil warnings
t.Fatalf("unexpected response")
}
if m.exchanges == nil { //nolint:staticcheck,nolintlint // SA5011 Ignore the nil warnings
t.Error("unexpected response")
}
}
func TestExchangeManagerAdd(t *testing.T) {
t.Parallel()
m := SetupExchangeManager()
b := new(bitfinex.Bitfinex)
b.SetDefaults()
m.Add(b)
exchanges, err := m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if exchanges[0].GetName() != "Bitfinex" {
t.Error("unexpected exchange name")
}
}
func TestExchangeManagerGetExchanges(t *testing.T) {
t.Parallel()
m := SetupExchangeManager()
exchanges, err := m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if exchanges != nil {
t.Error("unexpected value")
}
b := new(bitfinex.Bitfinex)
b.SetDefaults()
m.Add(b)
exchanges, err = m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if exchanges[0].GetName() != "Bitfinex" {
t.Error("unexpected exchange name")
}
}
func TestExchangeManagerRemoveExchange(t *testing.T) {
t.Parallel()
m := SetupExchangeManager()
if err := m.RemoveExchange("Bitfinex"); err != ErrNoExchangesLoaded {
t.Error("no exchanges should be loaded")
}
b := new(bitfinex.Bitfinex)
b.SetDefaults()
m.Add(b)
err := m.RemoveExchange("Bitstamp")
if !errors.Is(err, ErrExchangeNotFound) {
t.Errorf("received: %v but expected: %v", err, ErrExchangeNotFound)
}
if err := m.RemoveExchange("BiTFiNeX"); err != nil {
t.Error("exchange should have been removed")
}
if m.Len() != 0 {
t.Error("exchange manager len should be 0")
}
}
func TestNewExchangeByName(t *testing.T) {
m := SetupExchangeManager()
exchanges := []string{"binance", "bitfinex", "bitflyer", "bithumb", "bitmex", "bitstamp", "bittrex", "btc markets", "btse", "coinut", "exmo", "coinbasepro", "ftx", "gateio", "gemini", "hitbtc", "huobi", "itbit", "kraken", "lbank", "localbitcoins", "okcoin international", "okex", "poloniex", "yobit", "zb", "fake"}
for i := range exchanges {
exch, err := m.NewExchangeByName(exchanges[i])
if err != nil && exchanges[i] != "fake" {
t.Fatal(err)
}
if err == nil {
exch.SetDefaults()
if !strings.EqualFold(exch.GetName(), exchanges[i]) {
t.Error("did not load expected exchange")
}
}
}
}
type ExchangeBuilder struct{}
func (n ExchangeBuilder) NewExchangeByName(name string) (exchange.IBotExchange, error) {
var exch exchange.IBotExchange
switch name {
case "customex":
exch = new(sharedtestvalues.CustomEx)
default:
return nil, fmt.Errorf("%s, %w", name, ErrExchangeNotFound)
}
return exch, nil
}
func TestNewCustomExchangeByName(t *testing.T) {
m := SetupExchangeManager()
m.Builder = ExchangeBuilder{}
name := "customex"
exch, err := m.NewExchangeByName(name)
if err != nil {
t.Fatal(err)
}
if err == nil {
exch.SetDefaults()
if !strings.EqualFold(exch.GetName(), name) {
t.Error("did not load expected exchange")
}
}
}