Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

currency: Adds matching lookup table built from available pairs. #1312

Merged
merged 25 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions currency/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,13 @@ func (p *PairsManager) Match(symbol string, a asset.Item) (Pair, error) {
if symbol == "" {
return EMPTYPAIR, errSymbolStringEmpty
}
symbol = strings.ToLower(symbol)
p.m.RLock()
defer p.m.RUnlock()
if p.matcher == nil {
return EMPTYPAIR, errPairMatcherIsNil
}
assets, ok := p.matcher[a]
if !ok {
return EMPTYPAIR, fmt.Errorf("%w: %v", asset.ErrNotSupported, a)
}
pair, ok := assets[strings.ToLower(symbol)]
pair, ok := p.matcher[key{symbol, a}]
if !ok {
return EMPTYPAIR, ErrPairNotFound
gloriousCode marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -90,28 +87,35 @@ func (p *PairsManager) Store(a asset.Item, ps *PairStore) error {
if err != nil {
return err
}
matcher := make(map[string]*Pair)
for x := range cpy.Available {
matcher[cpy.Available[x].Base.Lower().String()+cpy.Available[x].Quote.Lower().String()] = &cpy.Available[x]
}
p.m.Lock()
if p.Pairs == nil {
p.Pairs = make(map[asset.Item]*PairStore)
}
p.Pairs[a] = cpy
if p.matcher == nil {
p.matcher = make(map[asset.Item]map[string]*Pair)
p.matcher = make(map[key]*Pair)
}
for x := range cpy.Available {
p.matcher[key{
Symbol: cpy.Available[x].Base.Lower().String() + cpy.Available[x].Quote.Lower().String(),
Asset: a}] = &cpy.Available[x]
}
p.matcher[a] = matcher
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()
vals, ok := p.Pairs[a]
if !ok {
p.m.Unlock()
return
}
for x := range vals.Available {
delete(p.matcher, key{Symbol: vals.Available[x].Base.Lower().String() + vals.Available[x].Quote.Lower().String(), Asset: a})
}
delete(p.Pairs, a)
delete(p.matcher, a)
p.m.Unlock()
}

Expand Down Expand Up @@ -214,15 +218,14 @@ func (p *PairsManager) StorePairs(a asset.Item, pairs Pairs, enabled bool) error
} else {
pairStore.Available = cpy

matcher := make(map[string]*Pair)
for x := range pairStore.Available {
matcher[pairStore.Available[x].Base.Lower().String()+pairStore.Available[x].Quote.Lower().String()] = &pairStore.Available[x]
}

if p.matcher == nil {
p.matcher = make(map[asset.Item]map[string]*Pair)
p.matcher = make(map[key]*Pair)
}
for x := range pairStore.Available {
p.matcher[key{
Symbol: pairStore.Available[x].Base.Lower().String() + pairStore.Available[x].Quote.Lower().String(),
Asset: a}] = &pairStore.Available[x]
}
p.matcher[a] = matcher
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions currency/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func TestPairsManagerMatch(t *testing.T) {
p = initTest(t)

_, err = p.Match("sillyBilly", 1337)
if !errors.Is(err, asset.ErrNotSupported) {
t.Fatalf("received: '%v' but expected: '%v'", err, asset.ErrNotSupported)
if !errors.Is(err, ErrPairNotFound) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrPairNotFound)
}

_, err = p.Match("sillyBilly", asset.Spot)
Expand Down
20 changes: 13 additions & 7 deletions currency/manager_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

// PairsManager manages asset pairs
type PairsManager struct {
BypassConfigFormatUpgrades bool `json:"bypassConfigFormatUpgrades"`
RequestFormat *PairFormat `json:"requestFormat,omitempty"`
ConfigFormat *PairFormat `json:"configFormat,omitempty"`
UseGlobalFormat bool `json:"useGlobalFormat,omitempty"`
LastUpdated int64 `json:"lastUpdated,omitempty"`
Pairs FullStore `json:"pairs"`
matcher map[asset.Item]map[string]*Pair `json:"-"`
BypassConfigFormatUpgrades bool `json:"bypassConfigFormatUpgrades"`
RequestFormat *PairFormat `json:"requestFormat,omitempty"`
ConfigFormat *PairFormat `json:"configFormat,omitempty"`
UseGlobalFormat bool `json:"useGlobalFormat,omitempty"`
LastUpdated int64 `json:"lastUpdated,omitempty"`
Pairs FullStore `json:"pairs"`
matcher map[key]*Pair `json:"-"`
gloriousCode marked this conversation as resolved.
Show resolved Hide resolved
m sync.RWMutex
}

Expand All @@ -38,3 +38,9 @@ type PairFormat struct {
Separator string `json:"separator,omitempty"`
Index string `json:"index,omitempty"`
}

// key is used to store the asset type and symbol in a map
type key struct {
gloriousCode marked this conversation as resolved.
Show resolved Hide resolved
Symbol string
Asset asset.Item
}
3 changes: 2 additions & 1 deletion currency/pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ func (p Pairs) GetRandomPair() (Pair, error) {
}

// DeriveFrom matches symbol string to the available pairs list when no
// delimiter is supplied.
// delimiter is supplied. WARNING: This is not optimised and should only be used
// for one off processes.
func (p Pairs) DeriveFrom(symbol string) (Pair, error) {
if len(p) == 0 {
return EMPTYPAIR, errPairsEmpty
Expand Down
20 changes: 16 additions & 4 deletions engine/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var (
errGRPCShutdownSignalIsNil = errors.New("cannot shutdown, gRPC shutdown channel is nil")
errInvalidStrategy = errors.New("invalid strategy")
errSpecificPairNotEnabled = errors.New("specified pair is not enabled")
errPairNotEnabled = errors.New("pair is not enabled")
)

// RPCServer struct
Expand Down Expand Up @@ -4680,15 +4681,20 @@ func (s *RPCServer) GetFundingRates(ctx context.Context, r *gctrpc.GetFundingRat
return nil, err
}

pairs, err := exch.GetEnabledPairs(a)
cp, err := exch.MatchSymbolWithAvailablePairs(r.Pair.Base+r.Pair.Quote, a, false)
if err != nil {
return nil, err
}
cp, err := pairs.DeriveFrom(r.Pair.Base + r.Pair.Quote)

pairs, err := exch.GetEnabledPairs(a)
if err != nil {
return nil, err
}

if !pairs.Contains(cp, true) {
return nil, fmt.Errorf("%w %v", errPairNotEnabled, cp)
}

funding, err := exch.GetFundingRates(ctx, &fundingrate.RatesRequest{
Asset: a,
Pair: cp,
Expand Down Expand Up @@ -4773,14 +4779,20 @@ func (s *RPCServer) GetLatestFundingRate(ctx context.Context, r *gctrpc.GetLates
return nil, fmt.Errorf("%s %w", a, order.ErrNotFuturesAsset)
}

pairs, err := exch.GetEnabledPairs(a)
cp, err := exch.MatchSymbolWithAvailablePairs(r.Pair.Base+r.Pair.Quote, a, false)
if err != nil {
return nil, err
}
cp, err := pairs.DeriveFrom(r.Pair.Base + r.Pair.Quote)

pairs, err := exch.GetEnabledPairs(a)
if err != nil {
return nil, err
}

if !pairs.Contains(cp, true) {
return nil, fmt.Errorf("%w %v", errPairNotEnabled, cp)
}

funding, err := exch.GetLatestFundingRate(ctx, &fundingrate.LatestRateRequest{
Asset: a,
Pair: cp,
Expand Down
7 changes: 5 additions & 2 deletions exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,12 @@ func (b *Bitfinex) UpdateTickers(ctx context.Context, a asset.Item) error {
}

for key, val := range tickerNew {
pair, err := enabled.DeriveFrom(strings.Replace(key, ":", "", 1)[1:])
pair, err := b.MatchSymbolWithAvailablePairs(key[1:], a, true)
if err != nil {
// GetTickerBatch returns all pairs in call across all asset types.
return err
}

if !enabled.Contains(pair, true) {
continue
}

Expand Down
23 changes: 16 additions & 7 deletions exchanges/bitmex/bitmex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ instruments:
if tick[j].Typ != futuresID {
continue instruments
}
pair, err = enabled.DeriveFrom(tick[j].Symbol)
pair, err = b.MatchSymbolWithAvailablePairs(tick[j].Symbol, a, false)
if err != nil {
return err
}
case asset.Index:
switch tick[j].Typ {
case bitMEXBasketIndexID,
Expand All @@ -390,23 +393,29 @@ instruments:
// contain an underscore. Calling DeriveFrom will then error and
// the instruments will be missed.
tick[j].Symbol = strings.Replace(tick[j].Symbol, currency.UnderscoreDelimiter, "", 1)
pair, err = enabled.DeriveFrom(tick[j].Symbol)
pair, err = b.MatchSymbolWithAvailablePairs(tick[j].Symbol, a, false)
if err != nil {
return err
}
case asset.PerpetualContract:
if tick[j].Typ != perpetualContractID {
continue instruments
}
pair, err = enabled.DeriveFrom(tick[j].Symbol)
pair, err = b.MatchSymbolWithAvailablePairs(tick[j].Symbol, a, false)
if err != nil {
return err
}
case asset.Spot:
if tick[j].Typ != spotID {
continue instruments
}
tick[j].Symbol = strings.Replace(tick[j].Symbol, currency.UnderscoreDelimiter, "", 1)
pair, err = enabled.DeriveFrom(tick[j].Symbol)
}
if err != nil {
if !errors.Is(err, currency.ErrPairNotFound) {
pair, err = b.MatchSymbolWithAvailablePairs(tick[j].Symbol, a, false)
gloriousCode marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
}
if !enabled.Contains(pair, true) {
continue
}

Expand Down
12 changes: 6 additions & 6 deletions exchanges/bybit/bybit_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (by *Bybit) wsHandleData(respRaw []byte) error {
if err != nil {
return err
}
p, err := by.extractCurrencyPair(data.OBData.Symbol, asset.Spot)
p, err := by.MatchSymbolWithAvailablePairs(data.OBData.Symbol, asset.Spot, false)
if err != nil {
return err
}
Expand All @@ -286,7 +286,7 @@ func (by *Bybit) wsHandleData(respRaw []byte) error {
return err
}

p, err := by.extractCurrencyPair(data.Parameters.Symbol, asset.Spot)
p, err := by.MatchSymbolWithAvailablePairs(data.Parameters.Symbol, asset.Spot, false)
if err != nil {
return err
}
Expand All @@ -313,7 +313,7 @@ func (by *Bybit) wsHandleData(respRaw []byte) error {
return err
}

p, err := by.extractCurrencyPair(data.Ticker.Symbol, asset.Spot)
p, err := by.MatchSymbolWithAvailablePairs(data.Ticker.Symbol, asset.Spot, false)
if err != nil {
return err
}
Expand All @@ -334,7 +334,7 @@ func (by *Bybit) wsHandleData(respRaw []byte) error {
return err
}

p, err := by.extractCurrencyPair(data.Kline.Symbol, asset.Spot)
p, err := by.MatchSymbolWithAvailablePairs(data.Kline.Symbol, asset.Spot, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -425,7 +425,7 @@ func (by *Bybit) wsHandleData(respRaw []byte) error {
}
}

p, err := by.extractCurrencyPair(data[j].Symbol, asset.Spot)
p, err := by.MatchSymbolWithAvailablePairs(data[j].Symbol, asset.Spot, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -475,7 +475,7 @@ func (by *Bybit) wsHandleData(respRaw []byte) error {
}
}

p, err := by.extractCurrencyPair(data[j].Symbol, asset.Spot)
p, err := by.MatchSymbolWithAvailablePairs(data[j].Symbol, asset.Spot, false)
gloriousCode marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
Loading