types

package
v0.3.0-dev1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2023 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExchangeConfigJson

type ExchangeConfigJson struct {
	Exchanges []ExchangeMarketConfigJson `json:"exchanges"`
}

ExchangeConfigJson demarshals the exchange configuration json for a particular market. The result is a list of parameters that define how the market is resolved on each supported exchange.

This struct stores data in an intermediate form as it's being assigned to various `ExchangeMarketConfig` objects, which are keyed by exchange id. These objects are not kept past the time the `GetAllMarketParams` API response is parsed, and do not contain an id because the id is expected to be known at the time the object is in use.

func (*ExchangeConfigJson) Validate

func (ecj *ExchangeConfigJson) Validate(
	exchangeNames []ExchangeId,
	marketNames map[string]MarketId,
) error

Validate validates the exchange configuration json, checking that required fields are defined and that market and exchange names correspond to valid markets and exchanges.

type ExchangeConfigUpdater

type ExchangeConfigUpdater interface {
	GetExchangeId() ExchangeId
	// UpdateMutableExchangeConfig notifies the object that the mutable exchange market configuration
	// for this object's exchange has been updated with a new configuration. It also provides
	// the current market configs for all supported markets on the exchange.
	UpdateMutableExchangeConfig(
		newExchangeConfig *MutableExchangeMarketConfig,
		newMarketConfigs []*MutableMarketConfig,
	) error
}

ExchangeConfigUpdater is the interface that wraps the UpdateMutableExchangeConfig method. ExchangeConfigUpdater objects are keyed by exchange id and receive updates notifying them that the mutable exchange market configuration has been updated, along with the all new configs. This interface is added to avoid import loops that occur when importing the `PriceFetcher` type directly into `PricefeedMutableMarketConfigs`.

type ExchangeId

type ExchangeId = string

ExchangeId is the unique id for an `Exchange` in the `Prices` module. The id will be matched against each exchange's `exchangeName` in the `MarketParam`'s `exchange_config_json`.

type ExchangeMarketConfigJson

type ExchangeMarketConfigJson struct {
	ExchangeName   string `json:"exchangeName"`
	Ticker         string `json:"ticker"`
	AdjustByMarket string `json:"adjustByMarket,omitempty"`
	Invert         bool   `json:"invert,omitempty"`
}

ExchangeMarketConfigJson captures per-exchange information for resolving a market, including the ticker and conversion details. It demarshals JSON parameters from the chain for a particular market on a specific exchange.

func (*ExchangeMarketConfigJson) Validate

func (emcj *ExchangeMarketConfigJson) Validate(
	exchangeIds []ExchangeId,
	marketNames map[string]MarketId,
) error

Validate validates the exchange market configuration json. It returns an error if the configuration is invalid.

type ExchangeQueryDetails

type ExchangeQueryDetails struct {
	Exchange ExchangeId
	Url      string // url to query exchange
	// function to get a map of tickers to prices from an exchange's response
	PriceFunction func(
		response *http.Response,
		tickerToPriceExponent map[string]int32,
		resolver types.Resolver,
	) (
		tickerToPrice map[string]uint64,
		unavailableTickers map[string]error,
		err error,
	)
	IsMultiMarket bool
}

ExchangeQueryDetails represents the information needed to query a specific exchange.

type ExchangeStartupConfig

type ExchangeStartupConfig struct {
	ExchangeId ExchangeId
	IntervalMs uint32
	TimeoutMs  uint32
	MaxQueries uint32
}

ExchangeStartupConfig contains configuration values for querying an exchange, passed in on startup. The configuration values include

  1. `ExchangeId`
  2. `IntervalMs` delay between task-loops where each task-loop sends API requests to an exchange
  3. `TimeoutMs` max time to wait on an API call to an exchange
  4. `MaxQueries` max number of API calls made to an exchange per task-loop. This parameter is used for rate limiting requests to the exchange.

For single-market API exchanges, the price fetcher will send approximately MaxQueries API responses into the exchange's buffered channel once every IntervalMs milliseconds. Note: the `ExchangeStartupConfig` will be used in the map of `{ exchangeId, `ExchangeStartupConfig` }` that dictates how the pricefeed client queries for market prices.

type ExchangeToMarketPrices

type ExchangeToMarketPrices interface {
	UpdatePrice(
		exchangeId ExchangeId,
		marketPriceTimestamp *MarketPriceTimestamp,
	)
	GetAllPrices() map[ExchangeId][]MarketPriceTimestamp
	GetIndexPrice(
		marketId MarketId,
		cutoffTime time.Time,
		resolver types.Resolver,
	) (
		medianPrice uint64,
		numPricesMedianized int,
	)
}

ExchangeToMarketPrices maintains price info for multiple exchanges. Each exchange can support prices from multiple market sources. Methods are goroutine safe in the underlying MarketToPrice objects.

func NewExchangeToMarketPrices

func NewExchangeToMarketPrices(exchangeIds []ExchangeId) (ExchangeToMarketPrices, error)

NewExchangeToMarketPrices creates a new ExchangeToMarketPrices. Since `ExchangeToMarketPrices` is not goroutine safe to write to, all key-value store creation is done on initialization. Validation is also done to verify `exchangeIds` is a valid input.

type ExchangeToMarketPricesImpl

type ExchangeToMarketPricesImpl struct {
	// {k: exchangeId, v: market prices, read-write lock}
	ExchangeMarketPrices map[ExchangeId]*MarketToPrice
}

func (*ExchangeToMarketPricesImpl) GetAllPrices

func (exchangeToMarketPrices *ExchangeToMarketPricesImpl) GetAllPrices() map[ExchangeId][]MarketPriceTimestamp

GetAllPrices returns a map of exchangeIds to a list of all `MarketPriceTimestamps` for the exchange.

func (*ExchangeToMarketPricesImpl) GetIndexPrice

func (exchangeToMarketPrices *ExchangeToMarketPricesImpl) GetIndexPrice(
	marketId MarketId,
	cutoffTime time.Time,
	resolver types.Resolver,
) (
	medianPrice uint64,
	numPricesMedianized int,
)

GetIndexPrice returns the index price for a given marketId, disallowing prices that are older than cutoffTime. If no valid prices are found, an error is returned.

func (*ExchangeToMarketPricesImpl) UpdatePrice

func (exchangeToMarketPrices *ExchangeToMarketPricesImpl) UpdatePrice(
	exchangeId ExchangeId,
	marketPriceTimestamp *MarketPriceTimestamp,
)

UpdatePrice updates a price for a market for an exchange. Prices are only updated if the timestamp on the updates are greater than the timestamp on existing prices. NOTE: `UpdatePrice` will only ever read from `ExchangeMarketPrices` and calls a goroutine-safe method on the fetched `MarketToPrice`. Note: if an invalid `exchangeId` is being written to the `UpdatePrice` it is possible the underlying map was corrupted or the priceDaemon logic is invalid. Therefore, `UpdatePrice` will panic.

type Exponent

type Exponent = int32

Exponent denotes the number of decimals a value should be shifted.

type MarketConfig

type MarketConfig struct {
	// Ticker specifies the string to use to query the relevant ticker price for a market on this exchange.
	Ticker string

	// AdjustByMarket optionally identifies the appropriate market that should be used to adjust
	// the price of the market ticker to arrive at the final USD price of the market. This is used in
	// cases where we choose to query a price for a market on a particular exchange in a different quote
	// currency - perhaps because that market is more robust - and convert the price back to the quote
	// currency of the original market with another market's price.
	//
	// For example, for resolving the BTC-USD price on this exchange, we may use a "BTC-USDT" ticker with
	// an adjust-by market of USDT-USD, and compute BTC-USD as
	//
	// BTC-USD = BTC-USDT * USDT-USD
	//
	// If this field is nil, then the market has no adjust-by market.
	AdjustByMarket *MarketId

	// Invert specifies the inversion strategy to use when converting the price of the market's ticker to arrive
	// at the final USD price of the market. The application of the inversion strategy depends on whether an adjust-by
	// market is defined for this market.
	//
	// If an adjust-by market is defined for this market, then the inversion strategy is applied with respect
	// to the adjustment market. For example, say we use a "BTC-USDT" ticker for USDT-USD on this exchange, with
	// an adjust-by market of BTC-USD, and an inversion value of true. In this case, we are describing that
	// we will derive the BTC-USD price by multiplying the BTC-USD index price by the inverse of the BTC-USDT ticker
	// price:
	//
	// USDT-USD = BTC-USD / BTC-USDT
	//
	// If an adjust-by market is not defined for this market, then the inversion strategy is applied to the ticker
	// price itself. For example, for BTC, say we use "USD-BTC" as the BTC-USD ticker on this exchange with an
	// inversion value of true. In that case, we would derive the BTC-USD price by taking the inverse of the
	// USD-BTC price:
	//
	// BTC-USD = 1 / USD-BTC
	Invert bool
}

MarketConfig specifies the exchange-specific market configuration used to resolve a market's price on a particular exchange.

func (*MarketConfig) Copy

func (mc *MarketConfig) Copy() MarketConfig

Copy returns a deep copy of the MarketConfig.

func (*MarketConfig) Equal

func (mc *MarketConfig) Equal(other MarketConfig) bool

Equal returns true if the two MarketConfigs are equal.

func (*MarketConfig) Validate

func (mc *MarketConfig) Validate() error

Validate validates the market configuration.

type MarketId

type MarketId = uint32

TODO(DEC-663): replace all values with their real value from the prices module. MarketId is the unique id for a `Market` in the `Prices` module. The id must match the `Prices` module's `Market.Id`.

type MarketPriceTimestamp

type MarketPriceTimestamp struct {
	MarketId      uint32
	Price         uint64
	LastUpdatedAt time.Time
}

MarketPriceTimestamp maintains a `MarketId`, `Price` and `LastUpdatedAt`.

type MarketToPrice

type MarketToPrice struct {
	sync.RWMutex                                            // reader-writer lock
	MarketToPriceTimestamp map[uint32]*types.PriceTimestamp // {k: market id, v: PriceTimestamp}
}

MarketToPrice maintains multiple prices for different markets for the same exchange, along with the last time that each market price was updated. Methods are goroutine safe.

func NewMarketToPrice

func NewMarketToPrice() *MarketToPrice

NewMarketToPrice creates a new MarketToPrice.

func (*MarketToPrice) GetAllPrices

func (mtp *MarketToPrice) GetAllPrices() []MarketPriceTimestamp

GetAllPrices returns a list of all `MarketPriceTimestamps` for an exchange.

func (*MarketToPrice) GetValidPriceForMarket

func (mtp *MarketToPrice) GetValidPriceForMarket(marketId MarketId, cutoffTime time.Time) (uint64, bool)

GetValidPriceForMarket returns the most recent valid price for a market for an exchange.

func (*MarketToPrice) UpdatePrice

func (mtp *MarketToPrice) UpdatePrice(
	marketPriceTimestamp *MarketPriceTimestamp,
)

UpdatePrice updates a price for a market for an exchange. Prices are only updated if the timestamp on the updates are greater than the timestamp on existing prices.

type MutableExchangeMarketConfig

type MutableExchangeMarketConfig struct {
	Id ExchangeId
	// We use the keys of MarketToMarketConfig to infer which markets are supported
	// by the exchange.
	MarketToMarketConfig map[MarketId]MarketConfig
}

MutableExchangeMarketConfig stores all mutable market configuration per exchange.

func (*MutableExchangeMarketConfig) Copy

Copy returns a copy of the MutableExchangeMarketConfig.

func (*MutableExchangeMarketConfig) Equal

Equal returns true if the two MutableExchangeMarketConfig objects are equal.

func (*MutableExchangeMarketConfig) GetMarketIds

func (memc *MutableExchangeMarketConfig) GetMarketIds() []MarketId

GetMarketIds returns the ordered list of market ids supported by the exchange. This set is currently implicitly defined by the keys of the MarketToTicker map.

func (*MutableExchangeMarketConfig) Validate

func (memc *MutableExchangeMarketConfig) Validate(marketConfigs []*MutableMarketConfig) error

type MutableMarketConfig

type MutableMarketConfig struct {
	Id           MarketId
	Pair         string
	Exponent     Exponent
	MinExchanges uint32
}

MutableMarketConfig stores the metadata that is common to a market across exchanges.

func (*MutableMarketConfig) Copy

Copy returns a copy of the MutableMarketConfig.

func (*MutableMarketConfig) Validate

func (mmc *MutableMarketConfig) Validate() error

type PricefeedMutableMarketConfigs

type PricefeedMutableMarketConfigs interface {
	AddPriceFetcher(updater ExchangeConfigUpdater)
	AddPriceEncoder(updater ExchangeConfigUpdater)
	UpdateMarkets(marketParams []types.MarketParam) error
	GetExchangeMarketConfigCopy(
		id ExchangeId,
	) (
		mutableExchangeMarketConfig *MutableExchangeMarketConfig,
		err error,
	)
	GetMarketConfigCopies(
		markets []MarketId,
	) (
		mutableMarketConfigs []*MutableMarketConfig,
		err error,
	)
}

PricefeedMutableMarketConfigs stores a single copy of all market state that can change dynamically and synchronizes access for running go routines within the daemon.

type PricefeedMutableMarketConfigsImpl

type PricefeedMutableMarketConfigsImpl struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

PricefeedMutableMarketConfigsImpl implements PricefeedMutableMarketConfigs.

func NewPriceFeedMutableMarketConfigs

func NewPriceFeedMutableMarketConfigs(
	canonicalExchangeIds []ExchangeId,
) *PricefeedMutableMarketConfigsImpl

NewPriceFeedMutableMarketConfigs creates a new PricefeedMutableMarketConfigsImpl with no markets assigned to any exchanges. Apply market settings by calling `UpdateMarkets`.

func (*PricefeedMutableMarketConfigsImpl) AddPriceEncoder

func (pfmmc *PricefeedMutableMarketConfigsImpl) AddPriceEncoder(
	priceEncoder ExchangeConfigUpdater,
)

AddPriceEncoder adds a new price encoder to the pricefeed mutable market configs. This method is synchronized.

func (*PricefeedMutableMarketConfigsImpl) AddPriceFetcher

func (pfmmc *PricefeedMutableMarketConfigsImpl) AddPriceFetcher(
	priceFetcher ExchangeConfigUpdater,
)

AddPriceFetcher adds a new price fetcher to the pricefeed mutable market configs. This method is synchronized.

func (*PricefeedMutableMarketConfigsImpl) GetExchangeMarketConfigCopy

func (pfmmc *PricefeedMutableMarketConfigsImpl) GetExchangeMarketConfigCopy(
	id ExchangeId,
) (
	mutableExchangeMarketConfig *MutableExchangeMarketConfig,
	err error,
)

GetExchangeMarketConfigCopy retrieves a copy of the current market-specific mutable configuration for all markets of an exchange, in order to maintain synchronization. Whenever a market is added or modified on an exchange, this data structure becomes stale.

func (*PricefeedMutableMarketConfigsImpl) GetMarketConfigCopies

func (pfmmc *PricefeedMutableMarketConfigsImpl) GetMarketConfigCopies(
	markets []MarketId,
) (
	mutableMarketConfigs []*MutableMarketConfig,
	err error,
)

GetMarketConfigCopies retrieves a copy of the current market-specific mutable configuration for the specified markets, in order to maintain synchronization. In the event of a market update, this data could become stale. MarketConfigs are returned in the same order as the input markets.

func (*PricefeedMutableMarketConfigsImpl) UpdateMarkets

func (pfmmc *PricefeedMutableMarketConfigsImpl) UpdateMarkets(marketParams []types.MarketParam) error

UpdateMarkets parses the market params, validates them, and updates the pricefeed mutable market configs, broadcasting updates to the price fetchers and encoders when necessary. This method is synchronized. 1. Validate and parse market params into a new set of MutableExchangeMarketConfig and MutableMarketConfig maps. 2. As a sanity check, validate all new configs have 2 entries: a price fetcher and encoder. 3. Pre-compute updates to send to updaters. 4. Take the writer lock on the pricefeed mutable market configs. 5. Swap in new markets and exchange configs. 6. For each changed exchange config, send an update to each updater.

func (*PricefeedMutableMarketConfigsImpl) ValidateAndTransformParams

func (pfmmc *PricefeedMutableMarketConfigsImpl) ValidateAndTransformParams(marketParams []types.MarketParam) (
	mutableExchangeConfigs map[ExchangeId]*MutableExchangeMarketConfig,
	mutableMarketConfigs map[MarketId]*MutableMarketConfig,
	err error,
)

ValidateAndTransformParams validates the market params and transforms them into the internal representation used by the PricefeedMutableMarketConfigsImpl. The method guarantees that the returned mutableExchangeConfigs will have an entry for all current exchange feeds. This method is exposed for testing.

type UpdateParameters

type UpdateParameters struct {
	ExchangeConfig *MutableExchangeMarketConfig
	MarketConfigs  []*MutableMarketConfig
}

UpdateParameters contains the parameters to send to an ExchangeConfigUpdater when the exchange config changes.

type UpdatersForExchange

type UpdatersForExchange struct {
	PriceFetcher ExchangeConfigUpdater
	PriceEncoder ExchangeConfigUpdater
}

UpdatersForExchange contains named references to all ExchangeConfigUpdaters for a single exchange.

func (*UpdatersForExchange) Validate

func (ufe *UpdatersForExchange) Validate() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL