dydx

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 41 Imported by: 0

README

dYdX Market Params API

To learn more about the price aggregation that pairs with this API as well as the structure of the side-car's market config (Market Map), please refer to the Median Index Price Aggregator readme.

The dYdX Market Params API provides a list of all market parameters for the dYdX protocol. Specifically, this includes all markets that the dYdX protocol supports, the exchanges that need to provide prices along with the relevant translations, and a set of operations that convert all markets into a common set of prices. The side-car utilizes this API to update the price providers with all relevant market parameters, such that it can support the dYdX protocol out of the box.

Market Map Adapter

In order to utilize the market params API, the dYdX market map provider includes a custom adapter that can translate the market parameters into a format the side-car can understand.

Ticker Conversions

The ticker conversions are used to convert the specific parameters associated with a given market. Example:

{
    "id": 1000000,
    "pair": "USDT-USD",
    "exponent": -9,
    "min_exchanges": 3,
    "min_price_change_ppm": 1000,
    "exchange_config_json": "{\"exchanges\":[{\"exchangeName\":\"CoinbasePro\",\"ticker\":\"BTC-USD\"}]}"
},
  • id - This is currently not applicable to the side car and is not utilized.

  • pair - This is the pair that the market is associated with. There is a direct conversion here assuming the delimiter is - across all markets.

  • exponent - In the side-car we represent all prices in Decimals, which is the absolute value of the exponent.

  • min_exchanges - This is the minimum number of exchanges that need to provide prices for the market to be considered valid. This is a direct mapping to MinProviderCount in the side-car.

  • min_price_change_ppm - This is currently not applicable to the side car and is not utilized.

  • exchange_config_json - This is a list of exchanges that need to provide prices. We have a very similar structure in the side-car, but we use a different format. The side-car uses a MarketMap to store the exchange and ticker pairs.

Additional Considerations
MEXC Ticker Conversions

The dYdX Mexc API uses the Spot V2 endpoint which has a different representation of tickers relative to the Spot V3 websocket API. V2 includes underscores that we omit in the V3 API.

Bitstamp Ticker Conversions

The dYdX Bitstamp API uses the Ticker endpoint which has a different representation of tickers relative to the Websocket API. The ticker endpoint uses a / delimiter, while the websocket connection does not have a delimiter and lowercases the ticker.

Documentation

Index

Constants

View Source
const (
	// Name is the name of the MarketMap provider.
	Name = "dydx_api"

	// SwitchOverAPIHandlerName is the name of the dYdX switch over API.
	SwitchOverAPIHandlerName = "dydx_migration_api"

	// ResearchAPIHandlerName is the name of the dYdX research json API.
	ResearchAPIHandlerName = "dydx_research_json_api"

	// ResearchCMCAPIHandlerName is the name of the dYdX research json API that only returns CoinMarketCap markets.
	ResearchCMCAPIHandlerName = "dydx_research_coinmarketcap_api"

	// ChainID is the chain ID for the dYdX market map provider.
	ChainID = "dydx-node"

	// Endpoint is the endpoint for the dYdX market map API.
	Endpoint = "%s/dydxprotocol/prices/params/market?pagination.limit=10000"

	// Delimiter is the delimiter used to separate the base and quote assets in a pair.
	Delimiter = "-"

	// UniswapV3TickerFields is the number of fields to expect to parse from a UniswapV3 ticker.
	UniswapV3TickerFields = 3

	// UniswapV3TickerSeparator is the separator for fields contained within a ticker for a uniswapv3_api provider.
	UniswapV3TickerSeparator = Delimiter

	// RaydiumTickerFields is the minimum number of fields to expect the raydium exchange ticker to have.
	RaydiumTickerFields = 8

	// RaydiumTickerSeparator is the separator for fields contained within a ticker for the raydium provider.
	RaydiumTickerSeparator = Delimiter
)

Variables

View Source
var (
	DYDXChain = mmclient.Chain{
		ChainID: ChainID,
	}
)
View Source
var DefaultAPIConfig = config.APIConfig{
	Name:             Name,
	Atomic:           true,
	Enabled:          true,
	Timeout:          20 * time.Second,
	Interval:         10 * time.Second,
	ReconnectTimeout: 2000 * time.Millisecond,
	MaxQueries:       1,
	Endpoints:        []config.Endpoint{{URL: "http://localhost:1317"}},
}

DefaultAPIConfig returns the default configuration for the dYdX market map API.

View Source
var DefaultResearchAPIConfig = config.APIConfig{
	Name:             ResearchAPIHandlerName,
	Atomic:           true,
	Enabled:          true,
	Timeout:          20 * time.Second,
	Interval:         10 * time.Second,
	ReconnectTimeout: 2000 * time.Millisecond,
	MaxQueries:       1,
	Endpoints: []config.Endpoint{
		{
			URL: "https://dydx-api.lavenderfive.com",
		},
		{
			URL: "https://raw.githubusercontent.com/dydxprotocol/v4-web/main/public/configs/otherMarketData.json",
		},
	},
}

DefaultResearchAPIConfig returns the default configuration for the dYdX market map API.

View Source
var DefaultResearchCMCAPIConfig = config.APIConfig{
	Name:             ResearchCMCAPIHandlerName,
	Atomic:           true,
	Enabled:          true,
	Timeout:          20 * time.Second,
	Interval:         10 * time.Second,
	ReconnectTimeout: 2000 * time.Millisecond,
	MaxQueries:       1,
	Endpoints: []config.Endpoint{
		{
			URL: "https://dydx-api.lavenderfive.com",
		},
		{
			URL: "https://raw.githubusercontent.com/dydxprotocol/v4-web/main/public/configs/otherMarketData.json",
		},
	},
}

DefaultResearchCMCAPIConfig returns the default configuration for the dYdX market map API that only returns CoinMarketCap markets.

View Source
var DefaultSwitchOverAPIConfig = config.APIConfig{
	Name:             SwitchOverAPIHandlerName,
	Atomic:           true,
	Enabled:          true,
	Timeout:          20 * time.Second,
	Interval:         10 * time.Second,
	ReconnectTimeout: 2000 * time.Millisecond,
	MaxQueries:       1,
	Endpoints: []config.Endpoint{
		{
			URL: "http://localhost:1317",
		},
		{
			URL: "localhost:9090",
		},
	},
}

DefaultSwitchOverAPIConfig returns the default configuration for the dYdX switch over API provider.

View Source
var ProviderMapping = map[string]string{
	"Binance":              binance.Name,
	"BinanceUS":            binance.Name,
	"Bitfinex":             bitfinex.Name,
	"Kraken":               kraken.Name,
	"Gate":                 gate.Name,
	"Bitstamp":             bitstamp.Name,
	"Bybit":                bybit.Name,
	"CryptoCom":            cryptodotcom.Name,
	"Huobi":                huobi.Name,
	"Kucoin":               kucoin.Name,
	"Okx":                  okx.Name,
	"Mexc":                 mexc.Name,
	"CoinbasePro":          coinbase.Name,
	"TestVolatileExchange": volatile.Name,
	"Raydium":              raydium.Name,
	"UniswapV3-Ethereum":   uniswapv3.ProviderNames[constants.ETHEREUM],
	"UniswapV3-Base":       uniswapv3.ProviderNames[constants.BASE],
	coinmarketcap.Name:     coinmarketcap.Name,
}

ProviderMapping is referencing the different providers that are supported by the dYdX market params.

ref: https://github.com/dydxprotocol/v4-chain/blob/main/protocol/daemons/pricefeed/client/constants/exchange_common/exchange_id.go

Functions

func ConvertDenomByProvider

func ConvertDenomByProvider(denom string, exchange string) (string, error)

ConvertDenomByProvider converts a given denom to a format that is compatible with a given provider. Specifically, this is used to convert API to WebSocket representations of denoms where necessary.

func ConvertExchangeConfigJSON

func ConvertExchangeConfigJSON(
	config dydxtypes.ExchangeConfigJson,
) ([]mmtypes.ProviderConfig, error)

ConvertExchangeConfigJSON creates a set of paths and providers for a given ticker from a dYdX market. These paths represent the different ways to convert a currency pair using the dYdX market.

func ConvertMarketParamsToMarketMap

func ConvertMarketParamsToMarketMap(
	params dydxtypes.QueryAllMarketParamsResponse,
) (mmtypes.MarketMapResponse, error)

ConvertMarketParamsToMarketMap converts a dYdX market params response to a connect market map response.

func CreateCurrencyPairFromPair

func CreateCurrencyPairFromPair(pair string) (connecttypes.CurrencyPair, error)

CreateCurrencyPairFromPair creates a currency pair from a dYdX market.

func CreateTickerFromMarket

func CreateTickerFromMarket(market dydxtypes.MarketParam) (mmtypes.Ticker, error)

CreateTickerFromMarket creates a ticker from a dYdX market.

func ExtractMetadata

func ExtractMetadata(providerName string, cfg dydxtypes.ExchangeMarketConfigJson) (string, error)

ExtractMetadata extracts Metadata_JSON from ExchangeMarketConfigJson, based on the converted provider name.

func NewDefaultSwitchOverMarketMapFetcher

func NewDefaultSwitchOverMarketMapFetcher(
	logger *zap.Logger,
	api config.APIConfig,
	rh apihandlers.RequestHandler,
	metrics metrics.APIMetrics,
) (mmclient.MarketMapFetcher, error)

NewDefaultSwitchOverMarketMapFetcher returns a new SwitchOverProvider with the default dYdX x/prices and x/marketmap fetchers.

func NewSwitchOverFetcher

func NewSwitchOverFetcher(
	logger *zap.Logger,
	pricesFetcher mmclient.MarketMapFetcher,
	marketmapFetcher mmclient.MarketMapFetcher,
	metrics metrics.APIMetrics,
) (mmclient.MarketMapFetcher, error)

NewSwitchOverFetcher returns a new SwitchOverFetcher with the provided fetchers.

func RaydiumMetadataFromTicker

func RaydiumMetadataFromTicker(ticker string) (string, error)

RaydiumMetadataFromTicker extracts json-metadata from a ticker for Raydium. All raydium tickers on dydx will be formatted as follows (BASE-QUOTE-BASE_VAULT-BASE_DECIMALS-QUOTE_VAULT-QUOTE_DECIMALS-OPEN_ORDERS_ADDRESS-AMM_INFO_ADDRESS).

func UniswapV3MetadataFromTicker

func UniswapV3MetadataFromTicker(ticker string, invert bool) (string, error)

UniswapV3MetadataFromTicker returns the metadataJSON string for uniswapv3_api according to the dYdX encoding. This is PoolAddress-DecimalsBase-DecimalsQuote.

Types

type APIHandler

type APIHandler struct {
	// contains filtered or unexported fields
}

APIHandler implements the MarketMapAPIDataHandler interface for the dYdX prices module, which can be used by a base provider. This is specifically for fetching market data from the dYdX prices module, which is then translated to a market map.

func NewAPIHandler

func NewAPIHandler(
	logger *zap.Logger,
	api config.APIConfig,
) (*APIHandler, error)

NewAPIHandler returns a new MarketMap MarketMapAPIDataHandler.

func (*APIHandler) CreateURL

func (h *APIHandler) CreateURL(chains []types.Chain) (string, error)

CreateURL returns the URL that is used to fetch the latest market map data from the dYdX prices module.

func (*APIHandler) ParseResponse

func (h *APIHandler) ParseResponse(
	chains []types.Chain,
	resp *http.Response,
) types.MarketMapResponse

ParseResponse parses the response from the x/prices API and returns the resolved and unresolved market map data. The response from the MarketMap API is expected to be a a single market map object that was converted from the dYdX market params response.

type MultiMarketMapRestAPIFetcher

type MultiMarketMapRestAPIFetcher struct {
	// contains filtered or unexported fields
}

MultiMarketMapRestAPIFetcher is an implementation of a RestAPIFetcher that wraps two underlying Fetchers for fetching the market-map according to dydx mainnet and the additional markets that can be added according to the dydx research json.

func DefaultDYDXResearchMarketMapFetcher

func DefaultDYDXResearchMarketMapFetcher(
	rh apihandlers.RequestHandler,
	metrics metrics.APIMetrics,
	api config.APIConfig,
	logger *zap.Logger,
) (*MultiMarketMapRestAPIFetcher, error)

DefaultDYDXResearchMarketMapFetcher returns a MultiMarketMapFetcher composed of dydx mainnet + research apiDataHandlers.

func NewDYDXResearchMarketMapFetcher

func NewDYDXResearchMarketMapFetcher(
	mainnetFetcher, researchFetcher mmclient.MarketMapFetcher,
	logger *zap.Logger,
	isCMCOnly bool,
) *MultiMarketMapRestAPIFetcher

NewDYDXResearchMarketMapFetcher returns an aggregated market-map among the dydx mainnet and the dydx research json.

func (*MultiMarketMapRestAPIFetcher) Fetch

Fetch fetches the market map from the underlying fetchers and combines the results. If any of the underlying fetchers fetch for a chain that is different from the chain that the fetcher is initialized with, those responses will be ignored.

type ResearchAPIHandler

type ResearchAPIHandler struct {
	APIHandler
	// contains filtered or unexported fields
}

ResearchAPIHandler is a subclass of the dydx_chain.ResearchAPIHandler. It interprets the dydx ResearchJSON as a market-map.

func NewResearchAPIHandler

func NewResearchAPIHandler(
	logger *zap.Logger,
	api config.APIConfig,
) (*ResearchAPIHandler, error)

NewResearchAPIHandler returns a new MarketMap MarketMapAPIDataHandler.

func (*ResearchAPIHandler) CreateURL

func (h *ResearchAPIHandler) CreateURL(chains []types.Chain) (string, error)

CreateURL returns a static url (the url of the first configured endpoint). If the dydx chain is not configured in the request, an error is returned.

func (*ResearchAPIHandler) ParseResponse

func (h *ResearchAPIHandler) ParseResponse(
	chains []types.Chain,
	resp *http.Response,
) types.MarketMapResponse

ParseResponse parses the response from the dydx ResearchJSON API into a MarketMap, and unmarshals the market-map in accordance with the underlying dydx ResearchAPIHandler.

type SwitchOverFetcher

type SwitchOverFetcher struct {
	// contains filtered or unexported fields
}

SwitchOverFetcher is an implementation of a RestAPIFetcher that wraps a dydx x/prices market map fetcher and a x/marketmap fetcher. The fetcher operates by first fetching the market map from the x/prices API and then fetching the market map from the x/marketmap API. The fetcher will switch over to the x/marketmap API the first time it receives a non-nil market map from the x/marketmap API.

func (*SwitchOverFetcher) Fetch

Fetch fetches the market map from the x/prices API and then the x/marketmap API. The fetcher will switch over to the x/marketmap API the first time it receives a non-nil market map from the x/marketmap API.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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