origin

package
v0.11.0-dev.4 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const TickGenericHTTPLoggerTag = "TICK_GENERIC_HTTP_ORIGIN"
View Source
const TickGenericJQLoggerTag = "TICK_GENERIC_JQ_ORIGIN"

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPCallback

type HTTPCallback func(ctx context.Context, pairs []Pair, data io.Reader) map[any]data.Point

type Origin

type Origin interface {
	// FetchDataPoints fetches data points for the given list of queries.
	//
	// A query is an any type that can be used to query the origin for a data
	// point. For example, a query could be a pair of assets.
	//
	// Note that this method does not guarantee that data points will be
	// returned for all pairs nor in the same order as the pairs. The caller
	// must verify returned data.
	FetchDataPoints(ctx context.Context, query []any) (map[any]data.Point, error)
}

Origin provides dataPoint prices for a given set of pairs from an external source.

type Pair

type Pair struct {
	Base  string
	Quote string
}

Pair represents an asset pair.

func PairFromString

func PairFromString(s string) (p Pair, err error)

PairFromString returns a new Pair for given string. The string must be formatted as "BASE/QUOTE".

func (Pair) Empty

func (p Pair) Empty() bool

Empty returns true if the pair is empty. Pair is considered empty if either base or quote is empty.

func (Pair) Equal

func (p Pair) Equal(c Pair) bool

Equal returns true if the pair is equal to the given pair.

func (Pair) Invert

func (p Pair) Invert() Pair

Invert returns an inverted pair. For example, if the pair is "BTC/USD", then the inverted pair is "USD/BTC".

func (Pair) MarshalText

func (p Pair) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler interface.

func (Pair) String

func (p Pair) String() string

String returns a string representation of the pair.

func (*Pair) UnmarshalText

func (p *Pair) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler interface.

type Static

type Static struct{}

Static is an origin that returns the same value for all queries.

func NewStatic

func NewStatic() *Static

NewStatic creates a new static origin.

func (*Static) FetchDataPoints

func (s *Static) FetchDataPoints(_ context.Context, query []any) (map[any]data.Point, error)

FetchDataPoints implements the data.Type interface.

type StaticValue

type StaticValue struct {
	Value *bn.FloatNumber
}

StaticValue is a numeric value obtained from a static origin.

func (StaticValue) Number

func (s StaticValue) Number() *bn.FloatNumber

Number implements the data.NumericValue interface.

func (StaticValue) Print

func (s StaticValue) Print() string

Print implements the data.Value interface.

type Tick

type Tick struct {
	// Pair is an asset pair for which this price is calculated.
	Pair Pair

	// Price is a price for the given asset pair.
	// Depending on the provider implementation, this price can be
	// a last trade price, an average of bid and ask prices, etc.
	//
	// Price is always non-nil if there is no error.
	Price *bn.FloatNumber

	// Volume24h is a 24h volume for the given asset pair presented in the
	// base currency.
	//
	// May be nil if the provider does not provide volume.
	Volume24h *bn.FloatNumber
}

Tick contains a price, volume and other information for a given asset pair at a given time.

Before using this data, you should check if it is valid by calling Tick.Validate() method.

func (Tick) MarshalJSON

func (t Tick) MarshalJSON() ([]byte, error)

func (Tick) Number

func (t Tick) Number() *bn.FloatNumber

Number implements the data.NumericValue interface.

func (Tick) Print

func (t Tick) Print() string

Print implements the data.Value interface.

func (Tick) Validate

func (t Tick) Validate() error

Validate returns an error if the tick is invalid.

type TickGenericHTTP

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

TickGenericHTTP is a generic http price provider that can fetch prices from an HTTP endpoint. The callback function is used to parse the response body.

func NewTickGenericHTTP

func NewTickGenericHTTP(opts TickGenericHTTPOptions) (*TickGenericHTTP, error)

NewTickGenericHTTP creates a new TickGenericHTTP instance.

func (*TickGenericHTTP) FetchDataPoints

func (g *TickGenericHTTP) FetchDataPoints(ctx context.Context, query []any) (map[any]data.Point, error)

FetchDataPoints implements the Origin interface.

type TickGenericHTTPOptions

type TickGenericHTTPOptions struct {
	// URL is an TickGenericHTTP endpoint that returns JSON data. It may contain
	// the following variables:
	//   - ${lcbase} - lower case base asset
	//   - ${ucbase} - upper case base asset
	//   - ${lcquote} - lower case quote asset
	//   - ${ucquote} - upper case quote asset
	//   - ${lcbases} - lower case base assets joined by commas
	//   - ${ucbases} - upper case base assets joined by commas
	//   - ${lcquotes} - lower case quote assets joined by commas
	//   - ${ucquotes} - upper case quote assets joined by commas
	URL string

	// Headers is a set of TickGenericHTTP headers that are sent with each request.
	Headers http.Header

	// Callback is a function that is used to parse the response body.
	Callback HTTPCallback

	// Client is an TickGenericHTTP client that is used to fetch data from the
	// TickGenericHTTP endpoint. If nil, http.DefaultClient is used.
	Client *http.Client

	// Logger is an TickGenericHTTP logger that is used to log errors. If nil,
	// null logger is used.
	Logger log.Logger
}

type TickGenericJQ

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

TickGenericJQ is a generic origin implementation that uses JQ to parse JSON data from an TickGenericHTTP endpoint.

func NewTickGenericJQ

func NewTickGenericJQ(opts TickGenericJQOptions) (*TickGenericJQ, error)

NewTickGenericJQ creates a new TickGenericJQ instance.

The client argument is an TickGenericHTTP client that is used to fetch data from the TickGenericHTTP endpoint.

The url argument is an TickGenericHTTP endpoint that returns JSON data. It may contain the following variables:

  • ${lcbase} - lower case base asset
  • ${ucbase} - upper case base asset
  • ${lcquote} - lower case quote asset
  • ${ucquote} - upper case quote asset
  • ${lcbases} - lower case base assets joined by commas
  • ${ucbases} - upper case base assets joined by commas
  • ${lcquotes} - lower case quote assets joined by commas
  • ${ucquotes} - upper case quote assets joined by commas

The jq argument is a JQ query that is used to parse JSON data. It must return a single value that will be used as a price or an object with the following fields:

  • price - a price
  • time - a timestamp (optional)
  • volume - a 24h volume (optional)

The JQ query may contain the following variables:

  • $lcbase - lower case base asset
  • $ucbase - upper case base asset
  • $lcquote - lower case quote asset
  • $ucquote - upper case quote asset

Price and volume must be a string that can be parsed as a number or a number.

Time must be a string that can be parsed as time or a number that represents a UNIX timestamp.

If JQ query returns multiple values, the dataPoint will be invalid.

func (*TickGenericJQ) FetchDataPoints

func (g *TickGenericJQ) FetchDataPoints(ctx context.Context, query []any) (map[any]data.Point, error)

FetchDataPoints implements the Origin interface.

type TickGenericJQOptions

type TickGenericJQOptions struct {
	// URL is an TickGenericHTTP endpoint that returns JSON data. It may contain
	// the following variables:
	//   - ${lcbase} - lower case base asset
	//   - ${ucbase} - upper case base asset
	//   - ${lcquote} - lower case quote asset
	//   - ${ucquote} - upper case quote asset
	//   - ${lcbases} - lower case base assets joined by commas
	//   - ${ucbases} - upper case base assets joined by commas
	//   - ${lcquotes} - lower case quote assets joined by commas
	//   - ${ucquotes} - upper case quote assets joined by commas
	URL string

	// Query is a JQ query that is used to parse JSON data. It must
	// return a single value that will be used as a price or an object with the
	// following fields:
	//   - price - a price
	//   - time - a timestamp (optional)
	//   - volume - a 24h volume (optional)
	//
	// The JQ query may contain the following variables:
	//   - $lcbase - lower case base asset
	//   - $ucbase - upper case base asset
	//   - $lcquote - lower case quote asset
	//   - $ucquote - upper case quote asset
	//
	// Price and volume must be a number or a string that can be parsed as a number.
	// Time must be a number or a string that can be parsed as a number or a string
	// that can be parsed as a time.
	Query string

	// Headers is a set of TickGenericHTTP headers that are sent with each request.
	Headers http.Header

	// Client is an TickGenericHTTP client that is used to fetch data from the
	// TickGenericHTTP endpoint. If nil, http.DefaultClient is used.
	Client *http.Client

	// Logger is an TickGenericHTTP logger that is used to log errors. If nil,
	// null logger is used.
	Logger log.Logger
}

Jump to

Keyboard shortcuts

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