Documentation ¶
Overview ¶
Package quotes implements multiple price feed adapters.
Index ¶
- Variables
- func FetchPrices(tokens map[TokenNetwork]string, apiKey string) (map[TokenNetwork]decimal.Decimal, error)
- func TokenExists(tokenID string, tokens []Asset) bool
- type Asset
- type BinanceConfig
- type BitfakerConfig
- type ConfFunc
- type Config
- type DisabledFilter
- type Driver
- type DriverType
- type ExchangeType
- type FakeMarketConfig
- type Filter
- type FilterConfig
- type FilterType
- type HistoricalData
- type IndexConfig
- type KrakenConfig
- type LynexV2Config
- type LynexV3Config
- type Market
- func (m Market) ApplyMainQuote() Market
- func (m Market) Base() string
- func (m Market) IsEmpty() bool
- func (m Market) LegacyQuote() string
- func (m Market) MarshalJSON() ([]byte, error)
- func (m Market) Quote() string
- func (m Market) String() string
- func (m Market) StringWithoutMain() string
- func (m *Market) UnmarshalJSON(raw []byte) error
- type MexcConfig
- type OpendaxConfig
- type PriceCache
- type PriceDiffFilter
- type PriceDiffFilterConfig
- type Prices
- type QuickswapConfig
- type RpcConfig
- type SamplerFilterConfig
- type SectaV2Config
- type SectaV3Config
- type SyncswapConfig
- type TakerType
- type TokenNetwork
- type TradeEvent
- type UniswapV3Config
Constants ¶
This section is empty.
Variables ¶
var ( DriverBinance = DriverType{"binance"} DriverKraken = DriverType{"kraken"} DriverMexc = DriverType{"mexc"} DriverOpendax = DriverType{"opendax"} DriverBitfaker = DriverType{"bitfaker"} DriverUniswapV3 = DriverType{"uniswap_v3"} DriverSyncswap = DriverType{"syncswap"} DriverQuickswap = DriverType{"quickswap"} DriverSectaV2 = DriverType{"secta_v2"} DriverSectaV3 = DriverType{"secta_v3"} DriverLynexV2 = DriverType{"lynex_v2"} DriverLynexV3 = DriverType{"lynex_v3"} DriverInternal = DriverType{"internal"} // Internal trades )
var ( // TakerTypeUnknown represents the trade, // for which you can't determine its type, // and therefore the taker side cannot be deduced. TakerTypeUnknown = TakerType{""} // TakerTypeBuy represents a "buy" trade. // Its value is set to "sell" because the sell order it // was matched with was present in the order book before, // therefore the taker is the "sell" side. TakerTypeBuy = TakerType{"sell"} // TakerTypeSell represents a "sell" trade. // It's value is set to "buy" because the buy order it // was matched with was present in the order book before, // therefore the taker is the "buy" side. TakerTypeSell = TakerType{"buy"} )
var ( ErrNotStarted = errors.New("driver is not started; call `Start()` first or wait for it to finish") ErrAlreadyStarted = errors.New("driver is already started") ErrAlreadyStopped = errors.New("driver is already stopped") ErrInvalidWsUrl = errors.New("websocket URL must start with ws:// or wss://") ErrNotSubbed = errors.New("market not subscribed") ErrAlreadySubbed = errors.New("market already subscribed") ErrFailedSub = errors.New("failed to subscribe to market") ErrFailedUnsub = errors.New("failed to unsubscribe from market") ErrSwapParsing = errors.New("recovered in from panic during swap parsing") ErrMarketDisabled = errors.New("market is disabled") )
var MarketSubscriptions = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "price_feed_value", Help: "Current trades subscriptions by provider and market.", }, []string{"provider", "market"}, )
Functions ¶
func FetchPrices ¶ added in v0.0.109
func FetchPrices(tokens map[TokenNetwork]string, apiKey string) (map[TokenNetwork]decimal.Decimal, error)
FetchPrices fetches the current prices for a map of tokens from CoinGecko (map[address]CoinGeckoID).
func TokenExists ¶ added in v0.0.109
TokenExists checks if a token is in the list.
Types ¶
type Asset ¶ added in v0.0.109
func FetchTokens ¶ added in v0.0.109
FetchTokens fetches the list of all available tokens from CoinGecko.
type BinanceConfig ¶ added in v0.0.23
type BinanceConfig struct { USDCtoUSDT bool `yaml:"usdc_to_usdt" env:"USDC_TO_USDT" env-default:"true"` BatchPeriod time.Duration `yaml:"batch_period" env:"BATCH_PERIOD" env-default:"5s"` AssetsUpdatePeriod time.Duration `yaml:"assets_update_period" env:"ASSETS_UPDATE_PERIOD" env-default:"5m"` IdlePeriod time.Duration `yaml:"idle_period" env:"IDLE_PERIOD" env-default:"30m"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
type BitfakerConfig ¶ added in v0.0.23
type BitfakerConfig struct { Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` Markets map[string]FakeMarketConfig `yaml:"markets" env:"MARKETS"` }
type ConfFunc ¶ added in v0.0.76
type ConfFunc func(*indexStrategy)
func WithCustomWeights ¶ added in v0.0.76
func WithCustomWeights(driversWeights map[DriverType]decimal.Decimal) ConfFunc
WithCustomWeights configures custom drivers weights. Should be passed as an argument to the NewStrategy() constructor.
type Config ¶
type Config struct { Drivers []DriverType `yaml:"drivers" env:"QUOTES_DRIVERS" env-default:"binance,syncswap"` Rpc RpcConfig `yaml:"rpc" env-prefix:"QUOTES_RPC_"` Index IndexConfig `yaml:"index" env-prefix:"QUOTES_INDEX_"` Binance BinanceConfig `yaml:"binance" env-prefix:"QUOTES_BINANCE_"` Kraken KrakenConfig `yaml:"kraken" env-prefix:"QUOTES_KRAKEN_"` Mexc MexcConfig `yaml:"mexc" env-prefix:"QUOTES_MEXC_"` Opendax OpendaxConfig `yaml:"opendax" env-prefix:"QUOTES_OPENDAX_"` Bitfaker BitfakerConfig `yaml:"bitfaker" env-prefix:"QUOTES_BITFAKER_"` UniswapV3 UniswapV3Config `yaml:"uniswap_v3" env-prefix:"QUOTES_UNISWAP_V3_"` Syncswap SyncswapConfig `yaml:"syncswap" env-prefix:"QUOTES_SYNCSWAP_"` Quickswap QuickswapConfig `yaml:"quickswap" env-prefix:"QUOTES_QUICKSWAP_"` SectaV2 SectaV2Config `yaml:"secta_v2" env-prefix:"QUOTES_SECTA_V2_"` SectaV3 SectaV3Config `yaml:"secta_v3" env-prefix:"QUOTES_SECTA_V3_"` LynexV2 LynexV2Config `yaml:"lynex_v2" env-prefix:"QUOTES_LYNEX_V2_"` LynexV3 LynexV3Config `yaml:"lynex_v3" env-prefix:"QUOTES_LYNEX_V3_"` }
func NewConfigFromEnv ¶ added in v0.0.22
func NewConfigFromFile ¶ added in v0.0.24
type DisabledFilter ¶ added in v0.0.30
type DisabledFilter struct{}
func (DisabledFilter) Allow ¶ added in v0.0.30
func (f DisabledFilter) Allow(trade TradeEvent) bool
type Driver ¶
type Driver interface { // ActiveDrivers returns all configured data providers. ActiveDrivers() []DriverType // ExchangeType returns the type of the exchange. ExchangeType() ExchangeType // Start handles the initialization of the driver. // It should be called before any other method. Start() error // Stop handles the cleanup of the driver. // It unsubscribes from all markets and closes all open connections. // After calling Stop, the driver can't be used anymore // and needs to be again with Start method. Stop() error // Subscribe establishes a streaming connection to fetch trades for the given market. // The driver sends trades to the outbox channel configured in the constructor function. // If the market is already subscribed, this method returns an error. Subscribe(market Market) error // Unsubscribe closes the streaming connection for the given market. // After calling this method, the driver won't send any more trades for the given market. // If the market is not subscribed yet, this method returns an error. Unsubscribe(market Market) error HistoricalData }
Driver is an interface that represents trades adapter. It is used to stream trades from different exchanges and aggregate them into a single outbox channel.
func NewDriver ¶
func NewDriver(config Config, outbox chan<- TradeEvent, inbox <-chan TradeEvent, history HistoricalData) (Driver, error)
NewDriver creates a new instance of the driver.
If no drivers appear in the `config.Drivers` array, the constructor assumes no drives are configured and returns an error.
Index driver is configured automatically if at least one of the following conditions is met:
- `config.Drivers` contains multiple drivers;
- a valid non-nil `inbox` channel is provided;
Params:
- config: contains the configuration for the driver(s) to be created
- outbox: a channel where the driver sends aggregated trades
- inbox: an optional channel where the package user can send trades from his own source. If you don't { have / need } your own source, pass `nil` here.
- trades: an optional adapter to fetch historical data from instead of querying RPC provider, If you don't { have / need } a historical data adapter, pass `nil` here.
type DriverType ¶
type DriverType struct {
// contains filtered or unexported fields
}
DriverType is enum that represents all available quotes providers.
func ToDriverType ¶
func ToDriverType(raw string) (DriverType, error)
func (DriverType) MarshalJSON ¶
func (t DriverType) MarshalJSON() ([]byte, error)
func (DriverType) MarshalYAML ¶
func (t DriverType) MarshalYAML() (any, error)
func (*DriverType) SetValue ¶ added in v0.0.61
func (t *DriverType) SetValue(s string) error
func (DriverType) String ¶
func (d DriverType) String() string
func (*DriverType) UnmarshalJSON ¶
func (t *DriverType) UnmarshalJSON(raw []byte) error
func (*DriverType) UnmarshalYAML ¶
func (t *DriverType) UnmarshalYAML(value *yaml.Node) error
type ExchangeType ¶ added in v0.0.61
type ExchangeType string
ExchangeType is enum that represents the type of exchange. It is not implemented with struct based enums pattern because it's not used as an input parameter for any function or method.
var ( ExchangeTypeUnspecified ExchangeType = "" ExchangeTypeCEX ExchangeType = "cex" ExchangeTypeDEX ExchangeType = "dex" ExchangeTypeHybrid ExchangeType = "hybrid" )
type FakeMarketConfig ¶ added in v0.0.90
type FakeMarketConfig struct { StartPrice float64 `yaml:"start_price" env:"START_PRICE"` PriceVolatility float64 `yaml:"price_volatility" env:"PRICE_VOLATILITY"` StartAmount float64 `yaml:"start_pamount" env:"START_AMOUNT"` AmountVolatility float64 `yaml:"amount_volatility" env:"AMOUNT_VOLATILITY"` Period time.Duration `yaml:"period" env:"PERIOD"` }
type Filter ¶ added in v0.0.30
type Filter interface {
Allow(trade TradeEvent) bool
}
func NewFilter ¶ added in v0.0.41
func NewFilter(conf FilterConfig) Filter
type FilterConfig ¶ added in v0.0.30
type FilterConfig struct { FilterType FilterType `yaml:"filter_type" env:"TYPE" env-default:"disabled"` SamplerFilter SamplerFilterConfig `yaml:"sampler" env-prefix:"SAMPLER_"` PriceDiffFilter PriceDiffFilterConfig `yaml:"price_diff" env-prefix:"PRICE_DIFF_"` }
type FilterType ¶ added in v0.0.30
type FilterType string
const ( SamplerFilterType FilterType = "sampler" PriceDiffFilterType FilterType = "price_diff" DisabledFilterType FilterType = "disabled" )
type HistoricalData ¶ added in v0.0.95
type HistoricalData interface { // HistoricalData returns historical trade data for the given market. // The returned data is ordered from oldest to newest. // The window parameter defines the time range to fetch data for starting from now. HistoricalData(ctx context.Context, market Market, window time.Duration, limit uint64) ([]TradeEvent, error) }
type IndexConfig ¶ added in v0.0.26
type IndexConfig struct { TradesCached int `yaml:"trades_cached" env:"TRADES_CACHED" env-default:"20"` BufferSeconds int `yaml:"buffer_minutes" env:"BUFFER_MINUTES" env-default:"5"` MarketsMapping map[string][]string `yaml:"markets_mapping" env:"MARKETS_MAPPING"` // MaxPriceDiff has default of `0.2` because our default leverage is 5x, // and so if the user opens order on his full balance, he'll get liquidated on 20% price change. MaxPriceDiff string `yaml:"max_price_diff" env:"MAX_PRICE_DIFF" env-default:"0.2"` }
type KrakenConfig ¶ added in v0.0.23
type KrakenConfig struct { URL string `yaml:"url" env:"URL" env-default:"wss://ws.kraken.com"` ReconnectPeriod time.Duration `yaml:"period" env:"RECONNECT_PERIOD" env-default:"5s"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
TODO: add idle period
type LynexV2Config ¶ added in v0.0.95
type LynexV2Config struct { AssetsURL string `` /* 134-byte string literal not displayed */ MappingURL string `` /* 139-byte string literal not displayed */ MarketsURL string `` /* 137-byte string literal not displayed */ FactoryAddress string `yaml:"factory_address" env:"FACTORY_ADDRESS" env-default:"0xBc7695Fd00E3b32D08124b7a4287493aEE99f9ee"` StablePoolMarkets []string `yaml:"stable_pool_markets" env:"STABLE_POOL_MARKETS" env-default:"usdt/usdc"` // `env-default` tag value is a comma separated list of markets as in `usdt/usdc, usdc/dai` IdlePeriod time.Duration `yaml:"idle_period" env:"IDLE_PERIOD" env-default:"30m"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
type LynexV3Config ¶ added in v0.0.95
type LynexV3Config struct { AssetsURL string `` /* 134-byte string literal not displayed */ MappingURL string `` /* 139-byte string literal not displayed */ MarketsURL string `` /* 137-byte string literal not displayed */ FactoryAddress string `yaml:"factory_address" env:"FACTORY_ADDRESS" env-default:"0x622b2c98123D303ae067DB4925CD6282B3A08D0F"` IdlePeriod time.Duration `yaml:"idle_period" env:"IDLE_PERIOD" env-default:"30m"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
type Market ¶
type Market struct {
// contains filtered or unexported fields
}
func NewMarketDerived ¶ added in v0.0.77
func NewMarketFromString ¶ added in v0.0.26
NewMarketFromString returns a new Market from a string "btc/usdt" -> Market{btc, usdt} NOTE: string must contain "/" delimiter
func NewMarketWithMainQuote ¶ added in v0.0.63
func (Market) ApplyMainQuote ¶ added in v0.0.63
func (Market) LegacyQuote ¶ added in v0.0.63
func (Market) MarshalJSON ¶ added in v0.0.46
func (Market) String ¶ added in v0.0.26
String returns a string representation of the market. Example: `Market{btc, usdt}` -> "btc/usdt"
func (Market) StringWithoutMain ¶ added in v0.0.63
func (*Market) UnmarshalJSON ¶ added in v0.0.46
type MexcConfig ¶ added in v0.0.90
type MexcConfig struct { USDCtoUSDT bool `yaml:"usdc_to_usdt" env:"USDC_TO_USDT" env-default:"true"` BatchPeriod time.Duration `yaml:"batch_period" env:"BATCH_PERIOD" env-default:"5s"` AssetsUpdatePeriod time.Duration `yaml:"assets_update_period" env:"ASSETS_UPDATE_PERIOD" env-default:"5m"` URL string `yaml:"url" env:"URL" env-default:"wss://ws.mexc.com/ws"` ReconnectPeriod time.Duration `yaml:"period" env:"RECONNECT_PERIOD" env-default:"5s"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
TODO: add idle period
type OpendaxConfig ¶ added in v0.0.23
type OpendaxConfig struct { URL string `yaml:"url" env:"URL" env-default:"wss://alpha.yellow.org/api/v1/finex/ws"` ReconnectPeriod time.Duration `yaml:"period" env:"RECONNECT_PERIOD" env-default:"5s"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
type PriceCache ¶ added in v0.0.76
type PriceCache struct {
// contains filtered or unexported fields
}
func (*PriceCache) AddTrade ¶ added in v0.0.76
func (p *PriceCache) AddTrade(market Market, price, volume decimal.Decimal, timestamp time.Time, source DriverType)
AddTrade adds a new trade to the cache for a market.
func (*PriceCache) GetIndexPrice ¶ added in v0.0.76
func (p *PriceCache) GetIndexPrice(event *TradeEvent) (decimal.Decimal, bool)
type PriceDiffFilter ¶ added in v0.0.30
type PriceDiffFilter struct {
// contains filtered or unexported fields
}
func (*PriceDiffFilter) Allow ¶ added in v0.0.30
func (f *PriceDiffFilter) Allow(trade TradeEvent) bool
type PriceDiffFilterConfig ¶ added in v0.0.30
type PriceDiffFilterConfig struct {
Threshold string `yaml:"threshold" env:"THRESHOLD" env-default:"5"`
}
type QuickswapConfig ¶ added in v0.0.40
type QuickswapConfig struct { AssetsURL string `` /* 132-byte string literal not displayed */ MappingURL string `` /* 137-byte string literal not displayed */ MarketsURL string `` /* 135-byte string literal not displayed */ // PoolFactoryAddress is the address of the factory contract. // See docs at https://docs.quickswap.exchange/technical-reference/smart-contracts/v3/factory. // Note that the contract used in this lib is compiled from https://github.com/code-423n4/2022-09-quickswap. PoolFactoryAddress string `yaml:"pool_factory_address" env:"POOL_FACTORY_ADDRESS" env-default:"0x411b0fAcC3489691f28ad58c47006AF5E3Ab3A28"` IdlePeriod time.Duration `yaml:"idle_period" env:"IDLE_PERIOD" env-default:"30m"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
type SamplerFilterConfig ¶ added in v0.0.30
type SamplerFilterConfig struct {
Percentage int64 `yaml:"percentage" env:"PERCENTAGE" env-default:"5"`
}
type SectaV2Config ¶ added in v0.0.77
type SectaV2Config struct { AssetsURL string `` /* 134-byte string literal not displayed */ MappingURL string `` /* 139-byte string literal not displayed */ MarketsURL string `` /* 137-byte string literal not displayed */ FactoryAddress string `yaml:"factory_address" env:"FACTORY_ADDRESS" env-default:"0x8Ad39bf99765E24012A28bEb0d444DE612903C43"` IdlePeriod time.Duration `yaml:"idle_period" env:"IDLE_PERIOD" env-default:"30m"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
type SectaV3Config ¶ added in v0.0.77
type SectaV3Config struct { AssetsURL string `` /* 134-byte string literal not displayed */ MappingURL string `` /* 139-byte string literal not displayed */ MarketsURL string `` /* 137-byte string literal not displayed */ FactoryAddress string `yaml:"factory_address" env:"FACTORY_ADDRESS" env-default:"0x9BD425a416A276C72a13c13bBd8145272680Cf07"` IdlePeriod time.Duration `yaml:"idle_period" env:"IDLE_PERIOD" env-default:"30m"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
type SyncswapConfig ¶ added in v0.0.23
type SyncswapConfig struct { AssetsURL string `` /* 134-byte string literal not displayed */ MappingURL string `` /* 139-byte string literal not displayed */ MarketsURL string `` /* 137-byte string literal not displayed */ ClassicPoolFactoryAddress string `` /* 127-byte string literal not displayed */ StablePoolFactoryAddress string `yaml:"stable_pool_factory_address" env:"STABLE_POOL_FACTORY_ADDRESS" env-default:"0xE4CF807E351b56720B17A59094179e7Ed9dD3727"` StablePoolMarkets []string `yaml:"stable_pool_markets" env:"STABLE_POOL_MARKETS" env-default:"usdt/usdc"` // `env-default` tag value is a comma separated list of markets as in `usdt/usdc, usdc/dai` IdlePeriod time.Duration `yaml:"idle_period" env:"IDLE_PERIOD" env-default:"30m"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
type TakerType ¶
type TakerType struct {
// contains filtered or unexported fields
}
TakerType is enum that represents the side of taker in a trade.
func ToTakerType ¶
func (TakerType) MarshalJSON ¶
func (TakerType) MarshalYAML ¶
func (*TakerType) UnmarshalJSON ¶
func (*TakerType) UnmarshalYAML ¶
type TokenNetwork ¶ added in v0.0.113
type TradeEvent ¶
type TradeEvent struct { Source DriverType Market Market // e.g. `btc/usdt` Price decimal.Decimal Amount decimal.Decimal Total decimal.Decimal TakerType TakerType CreatedAt time.Time }
TradeEvent is a generic container for trades received from providers.
type UniswapV3Config ¶ added in v0.0.69
type UniswapV3Config struct { AssetsURL string `` /* 130-byte string literal not displayed */ MappingURL string `` /* 135-byte string literal not displayed */ MarketsURL string `` /* 133-byte string literal not displayed */ FactoryAddress string `yaml:"factory_address" env:"FACTORY_ADDRESS" env-default:"0x1F98431c8aD98523631AE4a59f267346ea31F984"` IdlePeriod time.Duration `yaml:"idle_period" env:"IDLE_PERIOD" env-default:"30m"` Filter FilterConfig `yaml:"filter" env-prefix:"FILTER_"` }
Source Files ¶
- base_dex.go
- binance.go
- bitfaker.go
- coingecko.go
- config.go
- enums.go
- error.go
- filter.go
- index.go
- index_strategy.go
- interface.go
- kraken.go
- lynex_v2.go
- lynex_v3.go
- market.go
- mexc.go
- once.go
- opendax.go
- price_cache.go
- price_diff_filter.go
- quickswap.go
- sampler_filter.go
- secta_v2.go
- secta_v3.go
- syncswap.go
- uniswap_v3.go
- websocket.go
Directories ¶
Path | Synopsis |
---|---|
Types and helpers for easy formatting and parsing open-finance protocol messages.
|
Types and helpers for easy formatting and parsing open-finance protocol messages. |
App for testing quotes drivers.
|
App for testing quotes drivers. |