Price

Enables the safe handling of prices.

GitHub File: price.ts

Extends

Properties

Public

PropertyTypeModifierDescription

baseCurrency

TBase

readonly

The input currency (i.e. denominator) from which to construct the price.

quoteCurrency

TQoute

readonly

The output currency (i.e. numerator) to which the price is constructed.

scalar

readonly

Used to adjust the raw fraction with regards to the decimals of the base and quote Token.

Constructor

Parameters

ParamsTypeDescription

args

Object

An object containing either: - The array literally detailing the Currency classes and amounts; or - A second array that contains the Currency details in the CurrencyAmount instances

Methods

invert() - public

Flips the base and quote currency to display the inverted price.

Returns

TypeDescription

Price<TQuote, TBase>

The inverted price of the Price instance.


multiply() - public

Multiplies the Price instance with the provided price and returns a new price. The provided price must have the same base currency as the Price's quote currency.

Parameters

ParamsTypeDescription

other

Price<TQuote, TBase>

The price to be multiplied with the Price instance.

Returns

TypeDescription

Price<TBase, TOtherQuote>

The final Price after multiplying the two prices.


quote() - public

Returns the amount of quote currency corresponding to a given amount of the base currency. That is, what is the provided currency amount when quoted in currency corresponding to the Price instance.

Parameters

ParamsTypeDescription

currencyAmount

currencyAmount<TBase>

The base currency amount to convert.

Returns

TypeDescription

CurrencyAmount<TQuote>

The amount of quote currency equivalent to the given base currency amount.


adjustedForDecimals() - private

Scale the Price value by the scalar amount to prepare the price for further formatting

Returns

TypeDescription

Fraction

The Price value adjusted for the decimal difference between base and quote currencies.


toSignificant() - public

Rounds the Price to the significant digits specified.

Parameters

ParamsTypeDescription

significantDigits

number

The number of significant digits to round to. Default is 6.

format

object

Configure output format per toformat specifications. Optional.

rounding

Specifies the rounding logic used. Default is ROUND_HALF_UP which rounds to the nearest neighbour and rounds up if equidistant.

Returns

TypeDescription

string

The final price value rounded to the specified significant digits.


toFixed() - public

Rounds the Price to the fixed number of decimals specified.

Parameters

ParamsTypeDescription

decimalPlaces

number

The number of decimal places to round to. Default is 4.

format

object

Configure output format per toformat specifications. Optional.

rounding

Specifies the rounding logic used. Default is ROUND_HALF_UP which rounds to the nearest neighbour and rounds up if equidistant.

Returns

TypeDescription

string

The final price value rounded to the decimal places specified.

Last updated