fiat

package
v0.2.5-alpha Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

View Source
const Subsystem = "FIAT"

Subsystem defines the logging code for this subsystem.

Variables

View Source
var (
	// GranularityMinute aggregates the bitcoin price over 1 minute.
	GranularityMinute = newGranularity(time.Minute, time.Hour*24, "m1")

	// Granularity5Minute aggregates the bitcoin price over 5 minute.
	Granularity5Minute = newGranularity(
		time.Minute*5, time.Hour*24*5, "m5",
	)

	// Granularity15Minute aggregates the bitcoin price over 15 minutes.
	Granularity15Minute = newGranularity(
		time.Minute*15, time.Hour*24*7, "m15",
	)

	// Granularity30Minute aggregates the bitcoin price over 30 minutes.
	Granularity30Minute = newGranularity(
		time.Minute*30, time.Hour*24*14, "m30",
	)

	// GranularityHour aggregates the bitcoin price over 1 hour.
	GranularityHour = newGranularity(
		time.Hour, time.Hour*24*30, "h1",
	)

	// Granularity6Hour aggregates the bitcoin price over 6 hours.
	Granularity6Hour = newGranularity(
		time.Hour*6, time.Hour*24*183, "h6",
	)

	// Granularity12Hour aggregates the bitcoin price over 12 hours.
	Granularity12Hour = newGranularity(
		time.Hour*12, time.Hour*24*365, "h12",
	)

	// GranularityDay aggregates the bitcoin price over one day.
	GranularityDay = newGranularity(
		time.Hour*24, time.Hour*24*7305, "d1",
	)
)
View Source
var ErrQueryTooLong = errors.New("period too long for coincap api, " +
	"please reduce")

ErrQueryTooLong is returned when we cannot get a granularity level for a period of time because it is too long.

Functions

func GetPrices

func GetPrices(ctx context.Context, timestamps []time.Time,
	backend PriceBackend, granularity Granularity) (
	map[time.Time]*USDPrice, error)

GetPrices gets a set of prices for a set of timestamps.

func MsatToUSD

func MsatToUSD(price decimal.Decimal, amt lnwire.MilliSatoshi) decimal.Decimal

MsatToUSD converts a msat amount to usd. Note that this function coverts values to Bitcoin values, then gets the fiat price for that BTC value.

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type Granularity

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

Granularity indicates the level of aggregation price information will be provided at.

func BestGranularity

func BestGranularity(duration time.Duration) (Granularity, error)

BestGranularity takes a period of time and returns the lowest granularity that we can query the coincap api in a single query. This helper is used to provide default granularity periods when they are not provided by requests.

type PriceBackend

type PriceBackend uint8

PriceBackend is an enum that indicates which backend we are using for fiat information.

const (
	// UnknownPriceBackend is used to indicate that no specific backend
	// was specified for fiat price data and that the defaults should
	// instead be used.
	UnknownPriceBackend PriceBackend = iota

	// CoinCapPriceBackend uses CoinCap's API for fiat price data.
	CoinCapPriceBackend

	// CoinDeskPriceBackend uses CoinDesk's API for fiat price data.
	CoinDeskPriceBackend
)

func (PriceBackend) String

func (p PriceBackend) String() string

String returns the string representation of a price backend.

type PriceRequest

type PriceRequest struct {
	// Identifier uniquely identifies the request.
	Identifier string

	// Value is the amount of BTC in msat.
	Value lnwire.MilliSatoshi

	// Timestamp is the time at which the price should be obtained.
	Timestamp time.Time
}

PriceRequest describes a request for price information.

type PriceSource

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

PriceSource holds a fiatBackend that can be used to fetch fiat price information.

func NewPriceSource

func NewPriceSource(backend PriceBackend, granularity *Granularity) (
	*PriceSource, error)

NewPriceSource returns a PriceSource which can be used to query price data.

func (PriceSource) GetPrices

func (p PriceSource) GetPrices(ctx context.Context, startTime,
	endTime time.Time) ([]*USDPrice, error)

GetPrices fetches price information using the given the PriceSource fiatBackend implementation. GetPrices also validates the time parameters and sorts the results.

type USDPrice

type USDPrice struct {
	// Timestamp is the time at which the BTC price is quoted.
	Timestamp time.Time

	// Price is the price in USD for 1 BTC at the given timestamp.
	Price decimal.Decimal
}

USDPrice represents the Bitcoin price in USD at a certain time.

func GetPrice

func GetPrice(prices []*USDPrice, timestamp time.Time) (*USDPrice, error)

GetPrice gets the price for a given time from a set of price data. This function expects the price data to be sorted with ascending timestamps and for first timestamp in the price data to be before any timestamp we are querying. The last datapoint's timestamp may be before the timestamp we are querying. If a request lies between two price points, we just return the earlier price.

Jump to

Keyboard shortcuts

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