oracle

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcCurrencyPairRates added in v0.1.4

func CalcCurrencyPairRates(
	candles types.AggregatedProviderCandles,
	tickers types.AggregatedProviderPrices,
	deviationThresholds map[string]math.LegacyDec,
	currencyPairs []types.CurrencyPair,
	logger zerolog.Logger,
) (types.CurrencyPairDec, error)

CalcCurrencyPairRates filters the candles and tickers to the currency pair list provided, then filters candles/tickers outside of the deviation threshold, and finally computes the rates for the given currency pairs using TVWAP for candles and VWAP for tickers. It will first compute rates with candles and then attempt to fill in any missing prices with ticker data.

func ComputeTVWAP

ComputeTVWAP computes the time volume weighted average price for all points for each exchange pair. Filters out any candles that did not occur within timePeriod. The provided prices argument reflects a mapping of provider => {<base> => <TickerPrice>, ...}.

Ref : https://en.wikipedia.org/wiki/Time-weighted_average_price

func ComputeTvwapsByProvider

func ComputeTvwapsByProvider(prices types.AggregatedProviderCandles) (types.CurrencyPairDecByProvider, error)

ComputeTvwapsByProvider computes the tvwap prices from candles for each provider separately and returns them in a map separated by provider name

func ComputeVWAP

ComputeVWAP computes the volume weighted average price for all price points for each ticker/exchange pair. The provided prices argument reflects a mapping of provider => {<base> => <TickerPrice>, ...}.

Ref: https://en.wikipedia.org/wiki/Volume-weighted_average_price

func ComputeVwapsByProvider

func ComputeVwapsByProvider(prices types.AggregatedProviderPrices) types.CurrencyPairDecByProvider

ComputeVwapsByProvider computes the vwap prices from tickers for each provider separately and returns them in a map separated by provider name

func Connect

func Connect(protoAddr string) (net.Conn, error)

Connect dials the given address and returns a net.Conn. The protoAddr argument should be prefixed with the protocol, eg. "tcp://127.0.0.1:8080" or "unix:///tmp/test.sock".

func ConvertAggregatedCandles added in v0.1.4

func ConvertAggregatedCandles(
	candles types.AggregatedProviderCandles,
	rates types.CurrencyPairDec,
) types.AggregatedProviderCandles

ConvertAggregatedCandles converts the candles to USD and updates the currency pair with a USD quote. If no conversion exists the rate is omitted in the return.

func ConvertAggregatedTickers added in v0.1.4

func ConvertAggregatedTickers(
	tickers types.AggregatedProviderPrices,
	rates types.CurrencyPairDec,
) types.AggregatedProviderPrices

ConvertAggregatedTickers converts the tickers to USD and updates the currency pair with a USD quote. If no conversion exists the rate is omitted in the return.

func ConvertRatesToUSD added in v0.1.4

func ConvertRatesToUSD(rates types.CurrencyPairDec) types.CurrencyPairDec

ConvertRatesToUSD converts the rates to USD and updates the currency pair with a USD quote. If no conversion exists the rate is omitted in the return.

func CreateDeviationsFromCurrencyDeviationThresholdList added in v0.2.0

func CreateDeviationsFromCurrencyDeviationThresholdList(
	deviationList oracletypes.CurrencyDeviationThresholdList,
) (map[string]math.LegacyDec, error)

CreateDeviationsFromCurrencyDeviationThresholdList will create the deviations map used by the price feeder Oracle from a CurrencyDeviationThresholdList defined by Ojo's oracle module.

func CreatePairProvidersFromCurrencyPairProvidersList added in v0.2.0

func CreatePairProvidersFromCurrencyPairProvidersList(
	currencyPairs oracletypes.CurrencyPairProvidersList,
) map[types.ProviderName][]types.CurrencyPair

CreatePairProvidersFromCurrencyPairProvidersList will create the pair providers map used by the price feeder Oracle from a CurrencyPairProvidersList defined by Ojo's oracle module.

func FilterCandleDeviations

func FilterCandleDeviations(
	logger zerolog.Logger,
	candles types.AggregatedProviderCandles,
	deviationThresholds map[string]math.LegacyDec,
) (types.AggregatedProviderCandles, error)

FilterCandleDeviations finds the standard deviations of the tvwaps of all assets, and filters out any providers that are not within 2𝜎 of the mean.

func FilterTickerDeviations

func FilterTickerDeviations(
	logger zerolog.Logger,
	prices types.AggregatedProviderPrices,
	deviationThresholds map[string]math.LegacyDec,
) (types.AggregatedProviderPrices, error)

FilterTickerDeviations finds the standard deviations of the prices of all assets, and filters out any providers that are not within 2𝜎 of the mean.

func GenerateExchangeRatesString

func GenerateExchangeRatesString(prices types.CurrencyPairDec) string

GenerateExchangeRatesString generates a canonical string representation of the aggregated exchange rates.

func GenerateSalt

func GenerateSalt(length int) (string, error)

GenerateSalt generates a random salt, size length/2, as a HEX encoded string.

func NewProvider

func NewProvider(
	ctx context.Context,
	providerName types.ProviderName,
	logger zerolog.Logger,
	endpoint provider.Endpoint,
	providerPairs ...types.CurrencyPair,
) (provider.Provider, error)

func ProtocolAndAddress

func ProtocolAndAddress(listenAddr string) (string, string)

ProtocolAndAddress splits an address into the protocol and address components. For instance, "tcp://127.0.0.1:8080" will be split into "tcp" and "127.0.0.1:8080". If the address has no protocol prefix, the default is "tcp".

func SetProviderTickerPricesAndCandles

func SetProviderTickerPricesAndCandles(
	providerName types.ProviderName,
	providerPrices types.AggregatedProviderPrices,
	providerCandles types.AggregatedProviderCandles,
	prices types.CurrencyPairTickers,
	candles types.CurrencyPairCandles,
	pair types.CurrencyPair,
) (success bool)

SetProviderTickerPricesAndCandles flattens and collects prices for candles and tickers based on the base currency per provider. Returns true if at least one of price or candle exists.

func StandardDeviation

StandardDeviation returns maps of the standard deviations and means of assets. Will skip calculating for an asset if there are less than 3 prices.

Types

type Oracle

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

Oracle implements the core component responsible for fetching exchange rates for a given set of currency pairs and determining the correct exchange rates to submit to the on-chain price oracle adhering the oracle specification.

func New

func New(
	logger zerolog.Logger,
	oc client.OracleClient,
	providerPairs map[types.ProviderName][]types.CurrencyPair,
	providerTimeout time.Duration,
	deviations map[string]sdkmath.LegacyDec,
	endpoints map[types.ProviderName]provider.Endpoint,
	chainConfig bool,
) *Oracle

func (*Oracle) GetComputedPrices

func (o *Oracle) GetComputedPrices(
	providerCandles types.AggregatedProviderCandles,
	providerPrices types.AggregatedProviderPrices,
) (types.CurrencyPairDec, error)

func (*Oracle) GetLastPriceSyncTimestamp

func (o *Oracle) GetLastPriceSyncTimestamp() time.Time

GetLastPriceSyncTimestamp returns the latest timestamp at which prices where fetched from the oracle's set of exchange rate providers.

func (*Oracle) GetParamCache

func (o *Oracle) GetParamCache(ctx context.Context, currentBlockHeight int64) (oracletypes.Params, error)

GetParamCache returns the last updated parameters of the x/oracle module if the current ParamCache is outdated or a param update event was found, the cache is updated.

func (*Oracle) GetParams

func (o *Oracle) GetParams(ctx context.Context) (oracletypes.Params, error)

GetParams returns the current on-chain parameters of the x/oracle module.

func (*Oracle) GetPrices

func (o *Oracle) GetPrices() types.CurrencyPairDec

GetPrices returns a copy of the current prices fetched from the oracle's set of exchange rate providers.

func (*Oracle) GetTvwapPrices

func (o *Oracle) GetTvwapPrices() types.CurrencyPairDecByProvider

GetTvwapPrices returns a copy of the tvwapsByProvider map

func (*Oracle) GetVwapPrices

func (o *Oracle) GetVwapPrices() types.CurrencyPairDecByProvider

GetVwapPrices returns the vwapsByProvider map using a read lock

func (*Oracle) LoadProviderPairsAndDeviations added in v0.1.7

func (o *Oracle) LoadProviderPairsAndDeviations(ctx context.Context) error

LoadProviderPairsAndDeviations loads the on chain pair providers and deviations from the oracle params.

func (*Oracle) RequiredRates added in v0.1.4

func (o *Oracle) RequiredRates() []types.CurrencyPair

func (*Oracle) SetPrices

func (o *Oracle) SetPrices(ctx context.Context) error

SetPrices retrieves all the prices and candles from our set of providers as determined in the config. If candles are available, uses TVWAP in order to determine prices. If candles are not available, uses the most recent prices with VWAP. Warns the the user of any missing prices, and filters out any faulty providers which do not report prices or candles within 2𝜎 of the others.

func (*Oracle) Start

func (o *Oracle) Start(ctx context.Context) error

Start starts the oracle process in a blocking fashion.

func (*Oracle) StartClientless added in v0.2.0

func (o *Oracle) StartClientless(
	ctx context.Context,
	params oracletypes.Params,
	tickSleep time.Duration,
) error

Starts oracle process without a client for running on an Ojo node that runs a price feeder natively.

func (*Oracle) Stop

func (o *Oracle) Stop()

Stop stops the oracle process and waits for it to gracefully exit.

type ParamCache

type ParamCache struct {
	Logger zerolog.Logger
	// contains filtered or unexported fields
}

ParamCache is used to cache oracle param data for an amount of blocks, defined by paramsCacheInterval.

func (*ParamCache) Initialize added in v0.1.7

func (paramCache *ParamCache) Initialize(
	ctx context.Context,
	client client.CometRPC,
	logger zerolog.Logger,
) error

Initialize initializes a ParamCache struct that starts a new goroutine subscribed to param update events. It is also updated every paramsCacheInterval incase param update events events are missed.

func (*ParamCache) IsOutdated

func (paramCache *ParamCache) IsOutdated(currentBlockHeight int64) bool

IsOutdated checks whether or not the current param data was fetched in the last 200 blocks.

func (*ParamCache) UpdateParamCache added in v0.1.7

func (paramCache *ParamCache) UpdateParamCache(currentBlockHeight int64, params oracletypes.Params, err error)

Update retrieves the most recent oracle params and updates the instance.

type PreviousPrevote

type PreviousPrevote struct {
	ExchangeRates     string
	Salt              string
	SubmitBlockHeight int64
}

PreviousPrevote defines a structure for defining the previous prevote submitted on-chain.

func NewPreviousPrevote

func NewPreviousPrevote() *PreviousPrevote

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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