fiat

package
v0.2.13-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 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,
	priceCfg *PriceSourceConfig) (
	map[time.Time]*Price, error)

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

func MsatToFiat

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

MsatToFiat converts a msat amount to fiat. Note that this function converts 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 Price

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

	// Price is the fiat price for the given currency for 1 BTC at the
	// given timestamp.
	Price decimal.Decimal

	// Currency is the code of the currency that the Price is quoted in.
	Currency string
}

Price represents the Bitcoin price in the given currency at a certain time.

func GetPrice

func GetPrice(prices []*Price, timestamp time.Time) (*Price, 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.

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

	// CustomPriceBackend uses user provided fiat price data.
	CustomPriceBackend

	// CoinGeckoPriceBackend uses CoinGecko's API for fiat price data.
	CoinGeckoPriceBackend
)

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(cfg *PriceSourceConfig) (*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) ([]*Price, error)

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

type PriceSourceConfig

type PriceSourceConfig struct {
	// Backend is the PriceBackend to be used for fetching price data.
	Backend PriceBackend

	// Granularity specifies the level of granularity with which we want to
	// get fiat prices. This option is only used for the CoinCap
	// PriceBackend.
	Granularity *Granularity

	// PricePoints is a set of price points that is used for fiat related
	// queries if the PriceBackend being used is the CustomPriceBackend.
	PricePoints []*Price
}

PriceSourceConfig is a struct holding various config options used for initialising a new PriceSource.

Jump to

Keyboard shortcuts

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