Files
gocryptotrader/exchanges/account/account.go
Ryan O'Hara-Reid a32d16e1f5 Expose auth validator functionality for wrapper (#416)
* expose auth validator functionality for wrapper

* Add REST validation after keys set, package account types for future syncing

* Add transient error checking for initial creddemtial validation

* fix command types

* Addressed nits from glorious person

* Amalgamate body within error when not between 2xx status, added btcmarket specific auth error check

* nit fix for glorious person

* Format fix

* removed unused code

* check transient first then validate if its an exchange specific authentication error, all others will be disregarded

* Addressed glorious nits

* Addressed glorious nits

* Moved account processing to updateaccountinfo func and added in fetch account info

* Add GRPC Account streaming (NOTE: could not complete until sync item added)

* RM exchange check

* Address xtda nits

* RM comment code

* Fix linter issues

* used most recent protoc version

* lbank linter issues fixed

* Addressed nits and changed len check to range in for loops

* Fixed timeout issue

* thrasher nits addressed

* add string holdings
2020-01-31 12:09:24 +11:00

87 lines
1.7 KiB
Go

package account
import (
"errors"
"fmt"
"strings"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/dispatch"
)
func init() {
service = new(Service)
service.accounts = make(map[string]*Account)
service.mux = dispatch.GetNewMux()
}
// SubscribeToExchangeAccount subcribes to your exchange account
func SubscribeToExchangeAccount(exchange string) (dispatch.Pipe, error) {
exchange = strings.ToLower(exchange)
service.Lock()
acc, ok := service.accounts[exchange]
if !ok {
service.Unlock()
return dispatch.Pipe{},
fmt.Errorf("%s exchange account holdings not found", exchange)
}
defer service.Unlock()
return service.mux.Subscribe(acc.ID)
}
// Process processes new account holdings updates
func Process(h *Holdings) error {
if h == nil {
return errors.New("cannot be nil")
}
if h.Exchange == "" {
return errors.New("exchange name unset")
}
return service.Update(h)
}
// GetHoldings returns full holdings for an exchange
func GetHoldings(exch string) (Holdings, error) {
if exch == "" {
return Holdings{}, errors.New("exchange name unset")
}
exch = strings.ToLower(exch)
service.Lock()
h, ok := service.accounts[exch]
if !ok {
service.Unlock()
return Holdings{}, errors.New("exchange account holdings not found")
}
defer service.Unlock()
return *h.h, nil
}
// Update updates holdings with new account info
func (s *Service) Update(a *Holdings) error {
exch := strings.ToLower(a.Exchange)
s.Lock()
acc, ok := s.accounts[exch]
if !ok {
id, err := s.mux.GetID()
if err != nil {
s.Unlock()
return err
}
s.accounts[exch] = &Account{h: a, ID: id}
s.Unlock()
return nil
}
acc.h.Accounts = a.Accounts
defer s.Unlock()
return s.mux.Publish([]uuid.UUID{acc.ID}, acc.h)
}