mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-16 23:16:48 +00:00
* initial concept of a nice validation tester for exchanges * adds some datahandler design * expand testing * more tests and fixes * minor end of day fix for bithumb * fixes implementation issues * more test coverage and improvements, but not sure if i should continue * fix more wrapper implementations * adds error type, more fixes * changes signature, fixes implementations * fixes more wrapper implementations * one more bit * more cleanup * WOW things work? * lintle 1/1337 * mini bump * fixes all linting * neaten * GetOrderInfo+ asset pair fixes+improvements * adds new websocket test * expand ws testing * fix bug, expand tests, improve implementation * code coverage of a lot of new codes * fixes everything * reverts accidental changes * minor fixes from reviewing code * removes Bitfinex cancelBatchOrder implementation * fixes dumb baby typo for babies * mini nit fixes * so many nits to address * addresses all the nits * Titlecase * switcheroo * removes websocket testing for now * fix appveyor, minor test fix * fixes typo, re-kindles killed kode * skip binance wrapper tests when running CI * expired context, huobi okx fixes * kodespull * fix ordering * time fix because why not * fix exmo, others * hopefully this fixes all of my life's problems * last thing today * huobi, more like hypotrophy * golangci-lint, more like mypooroldknee-splint * fix huobi times by removing them * should fix okx currency issues * blocks the application * adds last little contingency for pairs * addresses most nits and new problems * lovely fixed before seeing why okx sucks * fixes issues with okx websocket * the classic receieieivaier * lintle * adds test and fixes existing tests * expands error handling messages during setup * fixes dumb okx bugs introduced * quick fix for lint and exmo * fixes nixes * fix exmo deposit issue * lint * fixes issue with extra asset runs missing * fix surprise race * all the lint and merge fixes * fixes surprise bugs in OKx * fixes issues with times and chains * fixing all the merge stuff * merge fix * rm logs and a panic potential * lovely lint lament * an easy demonstration of scenario, but not of initial purpose * put it in the bin * Revert "put it in the bin" This reverts commit 15c6490f713233d43f10957367fcbf18e3818bdd. * re-add after immediate error popup * fix mini poor test design * okx okay * merge fixes * fixes issues discovered in lovely test * I FORGOT TO COMMIT THIS * nit fixaroonaboo * forgoetten test fix * revert old okx asset intrument work * fixes * revert problems I didnt understand. update bybit * fix merge bugs * test cleanup * further improvements * reshuffle and lint * rm redundant CI_TEST by rm the CI_TEST field that is redundant * path fix * move to its own section, dont run on 32 bit + appveyor * lint * fix lbank * address nits * let it rip * fix failing test time range * niteroo boogaloo * mod tidy, use common.SimpleTimeFormat
452 lines
11 KiB
Go
452 lines
11 KiB
Go
package currency
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/common/convert"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
)
|
|
|
|
var (
|
|
// ErrAssetAlreadyEnabled defines an error for the pairs management system
|
|
// that declares the asset is already enabled.
|
|
ErrAssetAlreadyEnabled = errors.New("asset already enabled")
|
|
// ErrPairAlreadyEnabled returns when enabling a pair that is already enabled
|
|
ErrPairAlreadyEnabled = errors.New("pair already enabled")
|
|
// ErrPairNotFound is returned when a currency pair is not found
|
|
ErrPairNotFound = errors.New("pair not found")
|
|
// ErrAssetIsNil is an error when the asset has not been populated by the
|
|
// configuration
|
|
ErrAssetIsNil = errors.New("asset is nil")
|
|
// ErrPairNotContainedInAvailablePairs defines an error when a pair is not
|
|
// contained in the available pairs list and is not supported by the
|
|
// exchange for that asset type.
|
|
ErrPairNotContainedInAvailablePairs = errors.New("pair not contained in available pairs")
|
|
|
|
errPairStoreIsNil = errors.New("pair store is nil")
|
|
errPairFormatIsNil = errors.New("pair format is nil")
|
|
errAssetNotFound = errors.New("asset type not found")
|
|
)
|
|
|
|
// GetAssetTypes returns a list of stored asset types
|
|
func (p *PairsManager) GetAssetTypes(enabled bool) asset.Items {
|
|
p.m.RLock()
|
|
defer p.m.RUnlock()
|
|
assetTypes := make(asset.Items, 0, len(p.Pairs))
|
|
for k, ps := range p.Pairs {
|
|
if enabled && (ps.AssetEnabled == nil || !*ps.AssetEnabled) {
|
|
continue
|
|
}
|
|
assetTypes = append(assetTypes, k)
|
|
}
|
|
return assetTypes
|
|
}
|
|
|
|
// Get gets the currency pair config based on the asset type
|
|
func (p *PairsManager) Get(a asset.Item) (*PairStore, error) {
|
|
if !a.IsValid() {
|
|
return nil, fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
|
|
p.m.RLock()
|
|
defer p.m.RUnlock()
|
|
c, ok := p.Pairs[a]
|
|
if !ok {
|
|
return nil,
|
|
fmt.Errorf("cannot get pair store, %v %w", a, asset.ErrNotSupported)
|
|
}
|
|
return c.copy()
|
|
}
|
|
|
|
// Store stores a new currency pair config based on its asset type
|
|
func (p *PairsManager) Store(a asset.Item, ps *PairStore) error {
|
|
if !a.IsValid() {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
cpy, err := ps.copy()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.m.Lock()
|
|
if p.Pairs == nil {
|
|
p.Pairs = make(map[asset.Item]*PairStore)
|
|
}
|
|
p.Pairs[a] = cpy
|
|
p.m.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// Delete deletes a map entry based on the supplied asset type
|
|
func (p *PairsManager) Delete(a asset.Item) {
|
|
p.m.Lock()
|
|
delete(p.Pairs, a)
|
|
p.m.Unlock()
|
|
}
|
|
|
|
// GetPairs gets a list of stored pairs based on the asset type and whether
|
|
// they're enabled or not
|
|
func (p *PairsManager) GetPairs(a asset.Item, enabled bool) (Pairs, error) {
|
|
if !a.IsValid() {
|
|
return nil, fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
|
|
p.m.RLock()
|
|
defer p.m.RUnlock()
|
|
pairStore, ok := p.Pairs[a]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
|
|
if !enabled {
|
|
availPairs := make(Pairs, len(pairStore.Available))
|
|
copy(availPairs, pairStore.Available)
|
|
return availPairs, nil
|
|
}
|
|
|
|
lenCheck := len(pairStore.Enabled)
|
|
if lenCheck == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
// NOTE: enabledPairs is declared before the next check for comparison
|
|
// reasons within exchange update pairs functionality.
|
|
enabledPairs := make(Pairs, lenCheck)
|
|
copy(enabledPairs, pairStore.Enabled)
|
|
|
|
err := pairStore.Available.ContainsAll(pairStore.Enabled, true)
|
|
if err != nil {
|
|
err = fmt.Errorf("%w of asset type %s", err, a)
|
|
}
|
|
return enabledPairs, err
|
|
}
|
|
|
|
// StoreFormat stores a new format for request or config format.
|
|
func (p *PairsManager) StoreFormat(a asset.Item, pFmt *PairFormat, config bool) error {
|
|
if !a.IsValid() {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
if pFmt == nil {
|
|
return errPairFormatIsNil
|
|
}
|
|
|
|
cpy := *pFmt
|
|
|
|
p.m.Lock()
|
|
defer p.m.Unlock()
|
|
|
|
if p.Pairs == nil {
|
|
p.Pairs = make(map[asset.Item]*PairStore)
|
|
}
|
|
|
|
pairStore, ok := p.Pairs[a]
|
|
if !ok {
|
|
pairStore = new(PairStore)
|
|
p.Pairs[a] = pairStore
|
|
}
|
|
|
|
if config {
|
|
pairStore.ConfigFormat = &cpy
|
|
} else {
|
|
pairStore.RequestFormat = &cpy
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// StorePairs stores a list of pairs based on the asset type and whether
|
|
// they're enabled or not
|
|
func (p *PairsManager) StorePairs(a asset.Item, pairs Pairs, enabled bool) error {
|
|
if !a.IsValid() {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
|
|
// NOTE: Length check not needed in this scenario as it has the ability to
|
|
// remove the entire stored list if needed.
|
|
cpy := make(Pairs, len(pairs))
|
|
copy(cpy, pairs)
|
|
|
|
p.m.Lock()
|
|
defer p.m.Unlock()
|
|
|
|
if p.Pairs == nil {
|
|
p.Pairs = make(map[asset.Item]*PairStore)
|
|
}
|
|
|
|
pairStore, ok := p.Pairs[a]
|
|
if !ok {
|
|
pairStore = new(PairStore)
|
|
p.Pairs[a] = pairStore
|
|
}
|
|
|
|
if enabled {
|
|
pairStore.Enabled = cpy
|
|
} else {
|
|
pairStore.Available = cpy
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// EnsureOnePairEnabled not all assets have pairs, eg options
|
|
// search for an asset that does and enable one if none are enabled
|
|
// error if no currency pairs found for an entire exchange
|
|
// returns the asset and pair of a pair if it has been enabled
|
|
func (p *PairsManager) EnsureOnePairEnabled() (Pair, asset.Item, error) {
|
|
if p == nil {
|
|
return EMPTYPAIR, asset.Empty, common.ErrNilPointer
|
|
}
|
|
p.m.Lock()
|
|
defer p.m.Unlock()
|
|
for _, v := range p.Pairs {
|
|
if v.AssetEnabled == nil ||
|
|
!*v.AssetEnabled ||
|
|
len(v.Available) == 0 {
|
|
continue
|
|
}
|
|
if len(v.Enabled) > 0 {
|
|
return EMPTYPAIR, asset.Empty, nil
|
|
}
|
|
}
|
|
for k, v := range p.Pairs {
|
|
if v.AssetEnabled == nil ||
|
|
!*v.AssetEnabled ||
|
|
len(v.Available) == 0 {
|
|
continue
|
|
}
|
|
rp, err := v.Available.GetRandomPair()
|
|
if err != nil {
|
|
return EMPTYPAIR, asset.Empty, err
|
|
}
|
|
p.Pairs[k].Enabled = v.Enabled.Add(rp)
|
|
return rp, k, nil
|
|
}
|
|
return EMPTYPAIR, asset.Empty, ErrCurrencyPairsEmpty
|
|
}
|
|
|
|
// DisablePair removes the pair from the enabled pairs list if found
|
|
func (p *PairsManager) DisablePair(a asset.Item, pair Pair) error {
|
|
if !a.IsValid() {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
|
|
if pair.IsEmpty() {
|
|
return ErrCurrencyPairEmpty
|
|
}
|
|
|
|
p.m.Lock()
|
|
defer p.m.Unlock()
|
|
|
|
pairStore, err := p.getPairStoreRequiresLock(a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
enabled, err := pairStore.Enabled.Remove(pair)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pairStore.Enabled = enabled
|
|
return nil
|
|
}
|
|
|
|
// EnablePair adds a pair to the list of enabled pairs if it exists in the list
|
|
// of available pairs and isn't already added
|
|
func (p *PairsManager) EnablePair(a asset.Item, pair Pair) error {
|
|
if !a.IsValid() {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
|
|
if pair.IsEmpty() {
|
|
return ErrCurrencyPairEmpty
|
|
}
|
|
|
|
p.m.Lock()
|
|
defer p.m.Unlock()
|
|
|
|
pairStore, err := p.getPairStoreRequiresLock(a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if pairStore.Enabled.Contains(pair, true) {
|
|
return fmt.Errorf("%s %w", pair, ErrPairAlreadyEnabled)
|
|
}
|
|
|
|
if !pairStore.Available.Contains(pair, true) {
|
|
return fmt.Errorf("%s %w in the list of available pairs",
|
|
pair, ErrPairNotFound)
|
|
}
|
|
pairStore.Enabled = pairStore.Enabled.Add(pair)
|
|
return nil
|
|
}
|
|
|
|
// IsAssetPairEnabled checks if a pair is enabled for an enabled asset type
|
|
func (p *PairsManager) IsAssetPairEnabled(a asset.Item, pair Pair) error {
|
|
if !a.IsValid() {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
|
|
if pair.IsEmpty() {
|
|
return ErrCurrencyPairEmpty
|
|
}
|
|
|
|
p.m.RLock()
|
|
defer p.m.RUnlock()
|
|
|
|
pairStore, err := p.getPairStoreRequiresLock(a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if pairStore.AssetEnabled == nil {
|
|
return fmt.Errorf("%s %w", a, ErrAssetIsNil)
|
|
}
|
|
if !*pairStore.AssetEnabled {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotEnabled)
|
|
}
|
|
if !pairStore.Enabled.Contains(pair, true) {
|
|
return fmt.Errorf("%s %w", pair, ErrPairNotFound)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsAssetEnabled checks to see if an asset is enabled
|
|
func (p *PairsManager) IsAssetEnabled(a asset.Item) error {
|
|
if !a.IsValid() {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
|
|
p.m.RLock()
|
|
defer p.m.RUnlock()
|
|
|
|
pairStore, err := p.getPairStoreRequiresLock(a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if pairStore.AssetEnabled == nil {
|
|
return fmt.Errorf("%s %w", a, ErrAssetIsNil)
|
|
}
|
|
|
|
if !*pairStore.AssetEnabled {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotEnabled)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetAssetEnabled sets if an asset is enabled or disabled for first run
|
|
func (p *PairsManager) SetAssetEnabled(a asset.Item, enabled bool) error {
|
|
if !a.IsValid() {
|
|
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
|
|
}
|
|
|
|
p.m.Lock()
|
|
defer p.m.Unlock()
|
|
|
|
pairStore, err := p.getPairStoreRequiresLock(a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if pairStore.AssetEnabled == nil {
|
|
pairStore.AssetEnabled = convert.BoolPtr(enabled)
|
|
return nil
|
|
}
|
|
|
|
if !*pairStore.AssetEnabled && !enabled {
|
|
return errors.New("asset already disabled")
|
|
} else if *pairStore.AssetEnabled && enabled {
|
|
return ErrAssetAlreadyEnabled
|
|
}
|
|
|
|
*pairStore.AssetEnabled = enabled
|
|
return nil
|
|
}
|
|
|
|
func (p *PairsManager) getPairStoreRequiresLock(a asset.Item) (*PairStore, error) {
|
|
if p.Pairs == nil {
|
|
return nil, errors.New("pair manager not initialised")
|
|
}
|
|
|
|
pairStore, ok := p.Pairs[a]
|
|
if !ok {
|
|
return nil, fmt.Errorf("%w %v", errAssetNotFound, a)
|
|
}
|
|
|
|
if pairStore == nil {
|
|
return nil, errors.New("currency store is nil")
|
|
}
|
|
|
|
return pairStore, nil
|
|
}
|
|
|
|
// UnmarshalJSON implements the unmarshal json interface so that the key can be
|
|
// correctly unmarshalled from a string into a uint.
|
|
func (fs *FullStore) UnmarshalJSON(d []byte) error {
|
|
var temp map[string]*PairStore
|
|
err := json.Unmarshal(d, &temp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*fs = make(FullStore, len(temp))
|
|
for key, val := range temp {
|
|
ai, err := asset.New(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
(*fs)[ai] = val
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON implements the marshal json interface so that the key can be
|
|
// correctly marshalled from a uint.
|
|
func (fs FullStore) MarshalJSON() ([]byte, error) {
|
|
temp := make(map[string]*PairStore, len(fs))
|
|
for key, val := range fs {
|
|
temp[key.String()] = val
|
|
}
|
|
return json.Marshal(temp)
|
|
}
|
|
|
|
// copy copies and segregates pair store from internal and external calls.
|
|
func (ps *PairStore) copy() (*PairStore, error) {
|
|
if ps == nil {
|
|
return nil, errPairStoreIsNil
|
|
}
|
|
var assetEnabled *bool
|
|
if ps.AssetEnabled != nil {
|
|
assetEnabled = convert.BoolPtr(*ps.AssetEnabled)
|
|
}
|
|
|
|
enabled := make(Pairs, len(ps.Enabled))
|
|
copy(enabled, ps.Enabled)
|
|
|
|
avail := make(Pairs, len(ps.Available))
|
|
copy(avail, ps.Available)
|
|
|
|
var rFmt *PairFormat
|
|
if ps.RequestFormat != nil {
|
|
cpy := *ps.RequestFormat
|
|
rFmt = &cpy
|
|
}
|
|
|
|
var cFmt *PairFormat
|
|
if ps.ConfigFormat != nil {
|
|
cpy := *ps.ConfigFormat
|
|
cFmt = &cpy
|
|
}
|
|
|
|
return &PairStore{
|
|
AssetEnabled: assetEnabled,
|
|
Enabled: enabled,
|
|
Available: avail,
|
|
RequestFormat: rFmt,
|
|
ConfigFormat: cFmt,
|
|
}, nil
|
|
}
|