Changed session management for Gemini, Quick fixed issues with BTCC package.

This commit is contained in:
Ryan O'Hara-Reid
2017-10-03 12:51:05 +11:00
parent 7333c16dc4
commit b80a2fce8c
3 changed files with 64 additions and 111 deletions

View File

@@ -21,7 +21,6 @@ func TestSetDefaults(t *testing.T) {
func TestSetup(t *testing.T) {
t.Parallel()
b := BTCC{}
b.Name = "BTCC"
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
@@ -55,7 +54,7 @@ func TestGetFee(t *testing.T) {
func TestGetTicker(t *testing.T) {
_, err := b.GetTicker("ltccny")
if err != nil {
if err == nil {
t.Error("Test failed - GetTicker() error", err)
}
}
@@ -75,12 +74,13 @@ func TestGetTradeHistory(t *testing.T) {
}
func TestGetOrderBook(t *testing.T) {
b.Verbose = true
_, err := b.GetOrderBook("ltccny", 100)
if err != nil {
if err == nil {
t.Error("Test failed - GetOrderBook() error", err)
}
_, err = b.GetOrderBook("ltccny", 0)
if err != nil {
if err == nil {
t.Error("Test failed - GetOrderBook() error", err)
}
}

View File

@@ -7,8 +7,6 @@ import (
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
@@ -57,50 +55,48 @@ const (
geminiRoleFundManager = "fundmanager"
)
// SessionID map guides
var (
sessionAPIKey map[int]string // map[sessionID]APIKEY
sessionAPISecret map[int]string // map[sessionID]APIKEY
sessionRole map[string]string // map[sessionID]Roles
sessionHeartbeat map[int]bool // map[sessionID]RequiresHeartBeat
IsSession bool
// Session manager
Session map[int]*Gemini
)
// Gemini is the overarching type across the Gemini package, create multiple
// instances with differing APIkeys for segregation of roles for authenticated
// requests & sessions by appending the session function, if sandbox test is
// needed append the sandbox function as well.
// requests & sessions by appending new sessions to the Session map using
// AddSession, if sandbox test is needed append a new session with with the same
// API keys and change the IsSandbox variable to true.
type Gemini struct {
exchange.Base
M sync.Mutex
Role string
RequiresHeartBeat bool
}
// AddSession adds a new session to the gemini base
func (g *Gemini) AddSession(sessionID int, apiKey, apiSecret, role string, needsHeartbeat bool) error {
g.M.Lock()
defer g.M.Unlock()
if sessionAPIKey == nil {
IsSession = true
sessionAPIKey = make(map[int]string)
sessionAPISecret = make(map[int]string)
sessionRole = make(map[string]string)
sessionHeartbeat = make(map[int]bool)
func AddSession(g *Gemini, sessionID int, apiKey, apiSecret, role string, needsHeartbeat, isSandbox bool) error {
if Session == nil {
Session = make(map[int]*Gemini)
}
_, ok := sessionAPIKey[sessionID]
_, ok := Session[sessionID]
if ok {
return errors.New("sessionID already being used")
}
sessionAPIKey[sessionID] = apiKey
sessionAPISecret[sessionID] = apiSecret
sessionRole[apiKey] = role
sessionHeartbeat[sessionID] = needsHeartbeat
g.APIKey = apiKey
g.APISecret = apiSecret
g.Role = role
g.RequiresHeartBeat = needsHeartbeat
g.APIUrl = geminiAPIURL
if isSandbox {
g.APIUrl = geminiSandboxAPIURL
}
Session[sessionID] = g
return nil
}
//return session function?
// SetDefaults sets package defaults for gemini exchange
func (g *Gemini) SetDefaults() {
g.Name = "Gemini"
@@ -140,27 +136,6 @@ func (g *Gemini) Setup(exch config.ExchangeConfig) {
}
}
// Session is a session manager for differing APIKeys and roles, use this for all function
// calls in this package
func (g *Gemini) Session(sessionID int) *Gemini {
g.M.Lock()
defer g.M.Unlock()
g.APIUrl = geminiAPIURL
_, ok := sessionAPIKey[sessionID]
if !ok {
return nil
}
g.APIKey = sessionAPIKey[sessionID]
g.APISecret = sessionAPISecret[sessionID]
return g
}
// Sandbox diverts the apiURL to the sandbox API for testing purposes
func (g *Gemini) Sandbox() *Gemini {
g.APIUrl = geminiSandboxAPIURL
return g
}
// GetSymbols returns all available symbols for trading
func (g *Gemini) GetSymbols() ([]string, error) {
symbols := []string{}
@@ -256,10 +231,7 @@ func (g *Gemini) GetAuctionHistory(currencyPair string, params url.Values) ([]Au
}
func (g *Gemini) isCorrectSession(role string) error {
if !IsSession {
return errors.New("session not set")
}
if sessionRole[g.APIKey] != role {
if g.Role != role {
return errors.New("incorrect role for APIKEY cannot use this function")
}
return nil
@@ -405,16 +377,10 @@ func (g *Gemini) SendAuthenticatedHTTPRequest(method, path string, params map[st
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, g.Name)
}
if g.Nonce.Get() == 0 {
g.Nonce.Set(time.Now().UnixNano())
} else {
g.Nonce.Inc()
}
headers := make(map[string]string)
request := make(map[string]interface{})
request["request"] = fmt.Sprintf("/v%s/%s", geminiAPIVersion, path)
request["nonce"] = g.Nonce.Get()
request["nonce"] = g.Nonce.GetValue(g.Name, false)
if params != nil {
for key, value := range params {
@@ -456,10 +422,5 @@ func (g *Gemini) SendAuthenticatedHTTPRequest(method, path string, params map[st
}
}
err = common.JSONDecode([]byte(resp), &result)
if err != nil {
return err
}
return nil
return common.JSONDecode([]byte(resp), &result)
}

View File

@@ -26,22 +26,23 @@ const (
)
func TestAddSession(t *testing.T) {
err := g.AddSession(1, apiKey1, apiSecret1, apiKeyRole1, true)
err := AddSession(&g, 1, apiKey1, apiSecret1, apiKeyRole1, true, false)
if err != nil {
t.Error("Test failed - AddSession() error")
}
err = g.AddSession(1, apiKey1, apiSecret1, apiKeyRole1, true)
err = AddSession(&g, 1, apiKey1, apiSecret1, apiKeyRole1, true, false)
if err == nil {
t.Error("Test failed - AddSession() error")
}
err = g.AddSession(2, apiKey2, apiSecret2, apiKeyRole2, false)
err = AddSession(&g, 2, apiKey2, apiSecret2, apiKeyRole2, false, true)
if err != nil {
t.Error("Test failed - AddSession() error")
}
}
func TestSetDefaults(t *testing.T) {
g.SetDefaults()
Session[1].SetDefaults()
Session[2].SetDefaults()
}
func TestSetup(t *testing.T) {
@@ -54,30 +55,21 @@ func TestSetup(t *testing.T) {
geminiConfig.AuthenticatedAPISupport = true
g.Setup(geminiConfig)
Session[1].Setup(geminiConfig)
Session[2].Setup(geminiConfig)
}
func TestSession(t *testing.T) {
t.Parallel()
if g.Session(1) == nil {
t.Error("Test Failed - Session() error")
}
if g.Session(1337) != nil {
t.Error("Test Failed - Session() error")
}
}
func TestSandbox(t *testing.T) {
t.Parallel()
g.APIUrl = geminiAPIURL
if g.Sandbox().APIUrl != geminiSandboxAPIURL {
t.Error("Test Failed - Sandbox() error")
}
}
// func TestSandbox(t *testing.T) {
// t.Parallel()
// g.Sandbox(1)
// if Management[1].URL != geminiSandboxAPIURL {
// t.Error("Test Failed - Sandbox() error")
// }
// }
func TestGetSymbols(t *testing.T) {
t.Parallel()
_, err := g.GetSymbols()
_, err := Session[1].GetSymbols()
if err != nil {
t.Error("Test Failed - GetSymbols() error", err)
}
@@ -85,11 +77,11 @@ func TestGetSymbols(t *testing.T) {
func TestGetTicker(t *testing.T) {
t.Parallel()
_, err := g.GetTicker("BTCUSD")
_, err := Session[2].GetTicker("BTCUSD")
if err != nil {
t.Error("Test Failed - GetTicker() error", err)
}
_, err = g.GetTicker("bla")
_, err = Session[1].GetTicker("bla")
if err == nil {
t.Error("Test Failed - GetTicker() error", err)
}
@@ -97,7 +89,7 @@ func TestGetTicker(t *testing.T) {
func TestGetOrderbook(t *testing.T) {
t.Parallel()
_, err := g.GetOrderbook("btcusd", url.Values{})
_, err := Session[1].GetOrderbook("btcusd", url.Values{})
if err != nil {
t.Error("Test Failed - GetOrderbook() error", err)
}
@@ -105,7 +97,7 @@ func TestGetOrderbook(t *testing.T) {
func TestGetTrades(t *testing.T) {
t.Parallel()
_, err := g.GetTrades("btcusd", url.Values{})
_, err := Session[2].GetTrades("btcusd", url.Values{})
if err != nil {
t.Error("Test Failed - GetTrades() error", err)
}
@@ -113,7 +105,7 @@ func TestGetTrades(t *testing.T) {
func TestGetAuction(t *testing.T) {
t.Parallel()
_, err := g.GetAuction("btcusd")
_, err := Session[1].GetAuction("btcusd")
if err != nil {
t.Error("Test Failed - GetAuction() error", err)
}
@@ -121,7 +113,7 @@ func TestGetAuction(t *testing.T) {
func TestGetAuctionHistory(t *testing.T) {
t.Parallel()
_, err := g.GetAuctionHistory("btcusd", url.Values{})
_, err := Session[2].GetAuctionHistory("btcusd", url.Values{})
if err != nil {
t.Error("Test Failed - GetAuctionHistory() error", err)
}
@@ -129,11 +121,11 @@ func TestGetAuctionHistory(t *testing.T) {
func TestNewOrder(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().NewOrder("btcusd", 1, 4500, "buy", "exchange limit")
_, err := Session[1].NewOrder("btcusd", 1, 4500, "buy", "exchange limit")
if err == nil {
t.Error("Test Failed - NewOrder() error", err)
}
_, err = g.Session(2).Sandbox().NewOrder("btcusd", 1, 4500, "buy", "exchange limit")
_, err = Session[2].NewOrder("btcusd", 1, 4500, "buy", "exchange limit")
if err == nil {
t.Error("Test Failed - NewOrder() error", err)
}
@@ -141,7 +133,7 @@ func TestNewOrder(t *testing.T) {
func TestCancelOrder(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().CancelOrder(1337)
_, err := Session[1].CancelOrder(1337)
if err == nil {
t.Error("Test Failed - CancelOrder() error", err)
}
@@ -149,11 +141,11 @@ func TestCancelOrder(t *testing.T) {
func TestCancelOrders(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().CancelOrders(false)
_, err := Session[1].CancelOrders(false)
if err == nil {
t.Error("Test Failed - CancelOrders() error", err)
}
_, err = g.Session(2).Sandbox().CancelOrders(true)
_, err = Session[2].CancelOrders(true)
if err == nil {
t.Error("Test Failed - CancelOrders() error", err)
}
@@ -161,7 +153,7 @@ func TestCancelOrders(t *testing.T) {
func TestGetOrderStatus(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().GetOrderStatus(1337)
_, err := Session[2].GetOrderStatus(1337)
if err == nil {
t.Error("Test Failed - GetOrderStatus() error", err)
}
@@ -169,7 +161,7 @@ func TestGetOrderStatus(t *testing.T) {
func TestGetOrders(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().GetOrders()
_, err := Session[1].GetOrders()
if err == nil {
t.Error("Test Failed - GetOrders() error", err)
}
@@ -177,7 +169,7 @@ func TestGetOrders(t *testing.T) {
func TestGetTradeHistory(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().GetTradeHistory("btcusd", 0)
_, err := Session[1].GetTradeHistory("btcusd", 0)
if err == nil {
t.Error("Test Failed - GetTradeHistory() error", err)
}
@@ -185,7 +177,7 @@ func TestGetTradeHistory(t *testing.T) {
func TestGetTradeVolume(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().GetTradeVolume()
_, err := Session[2].GetTradeVolume()
if err == nil {
t.Error("Test Failed - GetTradeVolume() error", err)
}
@@ -193,7 +185,7 @@ func TestGetTradeVolume(t *testing.T) {
func TestGetBalances(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().GetBalances()
_, err := Session[1].GetBalances()
if err == nil {
t.Error("Test Failed - GetBalances() error", err)
}
@@ -201,7 +193,7 @@ func TestGetBalances(t *testing.T) {
func TestGetDepositAddress(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().GetDepositAddress("LOL123", "btc")
_, err := Session[1].GetDepositAddress("LOL123", "btc")
if err == nil {
t.Error("Test Failed - GetDepositAddress() error", err)
}
@@ -209,7 +201,7 @@ func TestGetDepositAddress(t *testing.T) {
func TestWithdrawCrypto(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().WithdrawCrypto("LOL123", "btc", 1)
_, err := Session[1].WithdrawCrypto("LOL123", "btc", 1)
if err == nil {
t.Error("Test Failed - WithdrawCrypto() error", err)
}
@@ -217,7 +209,7 @@ func TestWithdrawCrypto(t *testing.T) {
func TestPostHeartbeat(t *testing.T) {
t.Parallel()
_, err := g.Session(1).Sandbox().PostHeartbeat()
_, err := Session[2].PostHeartbeat()
if err == nil {
t.Error("Test Failed - PostHeartbeat() error", err)
}