Skip to content

Commit

Permalink
Merge pull request #3689 from vegaprotocol/hotfix/remove-into-proto
Browse files Browse the repository at this point in the history
Do not rely on proto conversion for GetAsset
  • Loading branch information
jeremyletang committed Jul 5, 2021
2 parents c74ccc7 + 75e261f commit 59906ba
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions types/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package types

import (
"errors"

"code.vegaprotocol.io/vega/proto"
v1 "code.vegaprotocol.io/vega/proto/oracles/v1"
"code.vegaprotocol.io/vega/types/num"
Expand All @@ -18,6 +20,13 @@ const (
MARKET_TRADING_CONFIG_DISCRETE
)

var (
ErrNilTradableInstrument = errors.New("nil tradable instrument")
ErrNilInstrument = errors.New("nil instrument")
ErrNilProduct = errors.New("nil product")
ErrUnknownAsset = errors.New("unknown asset")
)

type MarketTimestamps struct {
Proposed int64
Pending int64
Expand Down Expand Up @@ -376,12 +385,20 @@ func (i Instrument_Future) IntoProto() *proto.Instrument_Future {
}
}

func (i Instrument_Future) getAsset() (string, error) {
if i.Future == nil {
return "", ErrUnknownAsset
}
return i.Future.SettlementAsset, nil
}

func (i Instrument_Future) iIntoProto() interface{} {
return i.IntoProto()
}

type iProto interface {
iIntoProto() interface{}
getAsset() (string, error)
}

type Instrument struct {
Expand Down Expand Up @@ -554,9 +571,11 @@ type Market struct {
State Market_State
MarketTimestamps *MarketTimestamps
tmc MarketTradingConfigType
asset string
}

func MarketFromProto(mkt *proto.Market) *Market {
asset, _ := mkt.GetAsset()
m := &Market{
Id: mkt.Id,
TradableInstrument: TradableInstrumentFromProto(mkt.TradableInstrument),
Expand All @@ -569,6 +588,7 @@ func MarketFromProto(mkt *proto.Market) *Market {
TradingMode: mkt.TradingMode,
State: mkt.State,
MarketTimestamps: MarketTimestampsFromProto(mkt.MarketTimestamps),
asset: asset,
}
m.tmc = m.TradingModeConfig.tmcType()
return m
Expand Down Expand Up @@ -630,9 +650,29 @@ func (m Market) GetId() string {
return m.Id
}

func (m Market) GetAsset() (string, error) {
// @TODO implement this in a better way
return m.IntoProto().GetAsset()
func (m *Market) getAsset() (string, error) {
if m.TradableInstrument == nil {
return "", ErrNilTradableInstrument
}
if m.TradableInstrument.Instrument == nil {
return "", ErrNilInstrument
}
if m.TradableInstrument.Instrument.Product == nil {
return "", ErrNilProduct
}

return m.TradableInstrument.Instrument.Product.getAsset()
}

func (m *Market) GetAsset() (string, error) {
if m.asset == "" {
asset, err := m.getAsset()
if err != nil {
return asset, err
}
m.asset = asset
}
return m.asset, nil
}

func (m Market) GetContinuous() *Market_Continuous {
Expand Down

0 comments on commit 59906ba

Please sign in to comment.