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 ¶
- Variables
- func ConvertToBuyingUnits(sellingOfferAmount int64, sellingUnitsNeeded int64, pricen int64, priced int64) (int64, int64, error)
- func MulFractionRoundDown(x int64, n int64, d int64) (int64, error)
- func MustParse(v string) xdr.Price
- func Parse(v string) (xdr.Price, error)
- func StringFromFloat64(v float64) string
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
StringFromFloat64 will format a float64 to decimal representation with 7 digits after the decimal point
Types ¶
This section is empty.