price

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package price implements functions to ease working with stellar price values. At present, prices are only used within the offer system, and are represented by a fraction whose numberator and denominator are both 32-bit signed integers.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrDivisionByZero is returned when a price operation would result in a division by 0
	ErrDivisionByZero = errors.New("division by 0")
	// ErrOverflow is returned when a price operation would result in an integer overflow
	ErrOverflow = errors.New("overflow")
)

Functions

func ConvertToBuyingUnits

func ConvertToBuyingUnits(sellingOfferAmount int64, sellingUnitsNeeded int64, pricen int64, priced int64) (int64, int64, error)

ConvertToBuyingUnits uses special rounding logic to multiply the amount by the price and returns (buyingUnits, sellingUnits) that can be taken from the offer

offerSellingBound = (offer.price.n > offer.price.d)

? offer.amount : ceil(floor(offer.amount * offer.price) / offer.price)

pathPaymentAmountBought = min(offerSellingBound, pathPaymentBuyingBound) pathPaymentAmountSold = ceil(pathPaymentAmountBought * offer.price)

offer.amount = amount selling offerSellingBound = roundingCorrectedOffer pathPaymentBuyingBound = needed pathPaymentAmountBought = what we are consuming from offer pathPaymentAmountSold = amount we are giving to the buyer Sell units = pathPaymentAmountSold and buy units = pathPaymentAmountBought

this is how we do floor and ceiling in stellar-core: https://github.com/stellar/stellar-core/blob/9af27ef4e20b66f38ab148d52ba7904e74fe502f/src/util/types.cpp#L201

func MulFractionRoundDown

func MulFractionRoundDown(x int64, n int64, d int64) (int64, error)

MulFractionRoundDown sets x = (x * n) / d, which is a round-down operation see https://github.com/stellar/stellar-core/blob/9af27ef4e20b66f38ab148d52ba7904e74fe502f/src/util/types.cpp#L201

func Parse

func Parse(v string) (xdr.Price, error)

Parse calculates and returns the best rational approximation of the given real number price while still keeping both the numerator and the denominator of the resulting value within the precision limits of a 32-bit signed integer..

func StringFromFloat64

func StringFromFloat64(v float64) string

StringFromFloat64 will format a float64 to decimal representation with 7 digits after the decimal point

Types

This section is empty.

Jump to

Keyboard shortcuts

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