Documentation ¶
Index ¶
- Constants
- Variables
- func AbsDiffUint64(x uint64, y uint64) uint64
- func AbsInt32(i int32) uint32
- func AbsInt64(i int64) uint64
- func AddUint32(a int64, b uint32) (int64, error)
- func AssertCheckTxMode(ctx sdk.Context)
- func AssertDeliverTxMode(ctx sdk.Context)
- func AvgInt32(nums []int32) int32
- func BaseToQuoteQuantums(bigBaseQuantums *big.Int, baseCurrencyAtomicResolution int32, ...) (bigNotional *big.Int)
- func BigFloat0() *big.Float
- func BigFloatMaxUint64() *big.Float
- func BigInt0() *big.Int
- func BigInt32Clamp(n *big.Int, lowerBound, upperBound int32) int32
- func BigIntClamp(n *big.Int, lowerBound *big.Int, upperBound *big.Int) *big.Int
- func BigIntMulPpm(input *big.Int, ppm uint32) *big.Int
- func BigIntMulSignedPpm(input *big.Int, ppm int32, roundUp bool) *big.Int
- func BigIntOneMillion() *big.Int
- func BigIntOneTrillion() *big.Int
- func BigIntRoundToMultiple(n *big.Int, multiple *big.Int, roundUp bool) *big.Int
- func BigMax(a, b *big.Int) *big.Int
- func BigMaxInt32() *big.Int
- func BigMin(a, b *big.Int) *big.Int
- func BigMulPow10(val *big.Int, exponent int32) (result *big.Rat)
- func BigNegMaxUint64() *big.Int
- func BigPow10(exponent uint64) *big.Int
- func BigRat0() *big.Rat
- func BigRat1() *big.Rat
- func BigRatClamp(n *big.Rat, lowerBound *big.Rat, upperBound *big.Rat) *big.Rat
- func BigRatMulPpm(input *big.Rat, ppm uint32) *big.Rat
- func BigRatOneMillion() *big.Rat
- func BigRatRound(n *big.Rat, roundUp bool) *big.Int
- func BigUint64Clamp(n *big.Int, lowerBound, upperBound uint64) uint64
- func ChangeRateUint64(originalV uint64, newV uint64) (float32, error)
- func ContainsDuplicates[V comparable](values []V) bool
- func ConvertBigFloatSliceToUint64Slice(values []*big.Float) ([]uint64, error)
- func ConvertBigFloatToUint64(value *big.Float) (uint64, error)
- func ConvertStringSliceToBigFloatSlice(values []string) ([]*big.Float, error)
- func DenomTraceToIBCDenom(denomTraceStr string) (ibcDenom string, err error)
- func DivisionUint32RoundUp(x, y uint32) uint32
- func FilterSlice[V any](values []V, filterFunc func(V) bool) []V
- func FundingRateToIndex(proratedFundingRate *big.Rat, baseCurrencyAtomicResolution int32, ...) (fundingIndex *big.Int)
- func GetSortedKeys[R interface{ ... }, K comparable, V any](m map[K]V) []K
- func Int64MulPpm(x int64, ppm uint32) int64
- func IntToString[T int | int32 | int64](i T) string
- func IsDeliverTxMode(ctx sdk.Context) bool
- func MapSlice[V any, E any](values []V, mapFunc func(V) E) []E
- func Max[T constraints.Ordered](x, y T) T
- func Median[V uint64 | uint32 | int64 | int32](input []V) (V, error)
- func MergeAllMapsMustHaveDistinctKeys[K comparable, V any](maps ...map[K]V) map[K]V
- func Min[T constraints.Ordered](x, y T) T
- func MustConvertBigIntToInt32(n *big.Int) int32
- func MustConvertIntegerToUint32[T constraints.Integer](value T) uint32
- func MustGetMedian[V uint64 | uint32 | int64 | int32](input []V) V
- func MustParseCoinsNormalized(coinStr string) sdk.Coins
- func QuoteToBaseQuantums(bigQuoteQuantums *big.Int, baseCurrencyAtomicResolution int32, ...) (bigNotional *big.Int)
- func RandomBytesBetween(start []byte, end []byte, rand *rand.Rand) []byte
- func RatPow10(exponent int32) *big.Rat
- func SliceToSet[K comparable](values []K) map[K]struct{}
- func TxMode(ctx sdk.Context) string
- func Uint32ToKey(i uint32) []byte
- func Uint64LinearInterpolate(v0 uint64, v1 uint64, cPpm uint32) (uint64, error)
- func Uint64MulPpm(x uint64, ppm uint32) uint64
- func UintToString[T uint | uint32 | uint64](i T) string
- type MsgRouter
- type Sortable
- type TxHash
Constants ¶
const ( OneMillion = uint32(1_000_000) TenThousand = uint32(10_000) OneHundred = uint32(100) MaxPriceChangePpm = uint32(10_000) // 10^6 quantums == 1 USD. QuoteCurrencyAtomicResolution = int32(-6) ZeroUint64 = uint64(0) // 10^BaseDenomExponent denotes how much full coin is represented by 1 base denom. BaseDenomExponent = -18 DefaultBaseDenom = "adv4tnt" )
const (
AvgInt32MaxArrayLength = 2 << 31
)
Variables ¶
var PowerReduction = sdkmath.NewIntFromBigInt( new(big.Int).SetUint64(1_000_000_000_000_000_000), )
PowerReduction defines the default power reduction value for staking. Use 1e18, since default stake denom is assumed to be 1e-18 of a full coin.
Functions ¶
func AbsDiffUint64 ¶
func AddUint32 ¶
AddUint32 returns the sum of a and b. If the sum underflows or overflows, this method returns an error.
func AssertCheckTxMode ¶
func AssertDeliverTxMode ¶
func BaseToQuoteQuantums ¶
func BaseToQuoteQuantums( bigBaseQuantums *big.Int, baseCurrencyAtomicResolution int32, priceValue uint64, priceExponent int32, ) (bigNotional *big.Int)
BaseToQuoteQuantums converts an amount denoted in base quantums, to an equivalent amount denoted in quote quantums. To determine the equivalent amount, an oracle price is used.
- `priceValue * 10^priceExponent` represents the conversion rate from one full coin of base currency to one full coin of quote currency.
- `10^baseCurrencyAtomicResolution` represents the amount of one full coin that a base quantum is equal to.
- `10^quoteCurrencyAtomicResolution` represents the amount of one full coin that a quote quantum is equal to.
To convert from base to quote quantums, we use the following equation:
quoteQuantums = (baseQuantums * 10^baseCurrencyAtomicResolution) * (priceValue * 10^priceExponent) / (10^quoteCurrencyAtomicResolution) = baseQuantums * priceValue * 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution)
The result is rounded down.
func BigFloatMaxUint64 ¶
BigFloatMaxUint64 returns a `big.Float` that is set to MaxUint64.
func BigInt32Clamp ¶
BigInt32Clamp takes a `big.Int` as input, and `int32` upper and lower bounds. It returns `int32` bounded within the upper and lower bound, inclusive. Note that if there is overlap between the bounds (`lower > upper`), this function will do the following: - If `n < lower`, the lower bound is returned. - Else, the upper bound is returned (since `n >= lower`, then `n > upper` must be true).
func BigIntClamp ¶
See `bigGenericClamp` for specification.
func BigIntMulPpm ¶
BigIntMulPpm takes a `big.Int` and returns the result of `input * ppm / 1_000_000`.
func BigIntMulSignedPpm ¶
BigIntMulSignedPpm takes a `big.Int` and returns the result of `input * ppm / 1_000_000`.
func BigIntOneMillion ¶
BigIntOneMillion returns a `big.Int` that is set to 1_000_000.
func BigIntOneTrillion ¶
BigIntOneTrillion returns a `big.Int` that is set to 1_000_000_000_000.
func BigIntRoundToMultiple ¶
BigIntRoundToMultiple takes an input, a multiple, and a direction to round (true for up, false for down). It returns a rounded result such that it is evenly divided by `multiple`. This function always expects the `multiple` parameter to be positive, otherwise it will panic.
func BigMaxInt32 ¶
BigMaxInt32 returns a `big.Int` that represents `MaxInt32`.
func BigMulPow10 ¶
BigMulPow10 returns the result of `val * 10^exponent`, in *big.Rat.
func BigNegMaxUint64 ¶
BigNegMaxUint64 returns a `big.Int` that is set to -math.MaxUint64.
func BigPow10 ¶
BigPow10 returns the result of `10^exponent`. Caches all calculated values and re-uses cached values in any following calls to BigPow10.
func BigRatClamp ¶
See `bigGenericClamp` for specification.
func BigRatMulPpm ¶
BigRatMulPpm takes a `big.Rat` and returns the result of `input * ppm / 1_000_000`.
func BigRatOneMillion ¶
BigRatOneMillion returns a `big.Rat` that is set to 1_000_000.
func BigRatRound ¶
BigRatRound takes an input and a direction to round (true for up, false for down). It returns the result rounded to a `*big.Int` in the specified direction.
func BigUint64Clamp ¶
BigUint64Clamp takes a `big.Int` as input, and `uint64` upper and lower bounds. It returns `uint64` bounded within the upper and lower bound, inclusive. Note that if there is overlap between the bounds (`lower > upper`), this function will do the following: - If `n < lower`, the lower bound is returned. - Else, the upper bound is returned (since `n >= lower`, then `n > upper` must be true).
func ChangeRateUint64 ¶
ChangeRateUint64 returns the rate of change between the original and the new values. result = (new - original) / original Note: the return value is truncated to fit float32 precision.
func ContainsDuplicates ¶
func ContainsDuplicates[V comparable](values []V) bool
ContainsDuplicates returns true if the slice contains duplicates, false if not.
func ConvertBigFloatSliceToUint64Slice ¶
ConvertBigFloatSliceToUint64Slice attempts to convert all values in a slice from big Float to uint64 and return an error if any conversions fail. Note: during conversion, will always round down.
func ConvertBigFloatToUint64 ¶
ConvertBigFloatToUint64 attempts to convert a big Float into a uint64 and returns an error if the conversion would fail. Note: during conversion, will always round down.
func ConvertStringSliceToBigFloatSlice ¶
ConvertStringSliceToBigFloatSlice attempts to convert all values in a slice from string to big Float and return an error if any conversions fail.
func DenomTraceToIBCDenom ¶
Given a denom trace string (e.g. `transfer/channel-141/uosmo`), output the IBC denom. For more information on IBC denom, see: https://interchainacademy.cosmos.network/tutorials/5-ibc-dev/#how-are-ibc-denoms-derived
func DivisionUint32RoundUp ¶
DivisionUint32RoundUp returns the result of x/y, rounded up.
func FilterSlice ¶
FilterSlice takes a function that returns a boolean on whether to include the element in the final result, and returns a slice of elements where the function returned true when called with each element.
func FundingRateToIndex ¶
func FundingRateToIndex( proratedFundingRate *big.Rat, baseCurrencyAtomicResolution int32, priceValue uint64, priceExponent int32, ) (fundingIndex *big.Int)
FundingRateToIndex converts funding rate (in ppm) to FundingIndex given the oracle price.
To get funding index from funding rate, we know that:
- `fundingPaymentQuoteQuantum = fundingRatePpm / 1_000_000 * time * quoteQuantums`
- Divide both sides by `baseQuantums`:
- Left side: `fundingPaymentQuoteQuantums / baseQuantums = fundingIndexDelta / 1_000_000`
- right side: ``` fundingRate * time * quoteQuantums / baseQuantums = fundingRatePpm / 1_000_000 * priceValue * 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution) ```
Hence, further multiplying both sides by 1_000_000, we have:
fundingIndexDelta = (fundingRatePpm * time) * priceValue * 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution)
Arguments:
proratedFundingRate: prorated funding rate adjusted by time delta, in parts-per-million timeSinceLastFunding: time (in seconds) since last funding index update baseCurrencyAtomicResolution: atomic resolution of the base currency priceValue: index price of the perpetual market according to the pricesKeeper priceExponent: priceExponent of the market according to the pricesKeeper
func GetSortedKeys ¶ added in v0.3.0
func GetSortedKeys[R interface { ~[]K sort.Interface }, K comparable, V any](m map[K]V) []K
GetSortedKeys returns the keys of the map in sorted order.
func Int64MulPpm ¶
func IntToString ¶ added in v0.4.0
IntToString converts any int type to a base-10 string.
func IsDeliverTxMode ¶
func MapSlice ¶
MapSlice takes a function and executes that function on each element of a slice, returning the result. Note the function must return one result for each element of the slice.
func Max ¶
func Max[T constraints.Ordered](x, y T) T
func Median ¶ added in v0.3.0
Median is a generic median calculator. If the input has an even number of elements, then the average of the two middle numbers is rounded away from zero.
func MergeAllMapsMustHaveDistinctKeys ¶ added in v0.3.0
func MergeAllMapsMustHaveDistinctKeys[K comparable, V any](maps ...map[K]V) map[K]V
MergeAllMapsMustHaveDistinctKeys merges all the maps into a single map. Panics if there are duplicate keys.
func Min ¶
func Min[T constraints.Ordered](x, y T) T
func MustConvertBigIntToInt32 ¶
`MustConvertBigIntToInt32` converts a `big.Int` to an `int32` and panics if the input value overflows or underflows `int32`.
func MustConvertIntegerToUint32 ¶
func MustConvertIntegerToUint32[T constraints.Integer](value T) uint32
func MustGetMedian ¶ added in v0.3.0
MustGetMedian is a wrapper around `Median` that panics if input length is zero.
func MustParseCoinsNormalized ¶ added in v0.4.0
MustParseCoinsNormalized parses a string of coins and panics on error.
func QuoteToBaseQuantums ¶
func QuoteToBaseQuantums( bigQuoteQuantums *big.Int, baseCurrencyAtomicResolution int32, priceValue uint64, priceExponent int32, ) (bigNotional *big.Int)
QuoteToBaseQuantums converts an amount denoted in quote quantums, to an equivalent amount denoted in base quantums. To determine the equivalent amount, an oracle price is used.
- `priceValue * 10^priceExponent` represents the conversion rate from one full coin of base currency to one full coin of quote currency.
- `10^baseCurrencyAtomicResolution` represents the amount of one full coin that a base quantum is equal to.
- `10^quoteCurrencyAtomicResolution` represents the amount of one full coin that a quote quantum is equal to.
To convert from quote to base quantums, we use the following equation:
baseQuantums = quoteQuantums / priceValue / 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution)
The result is rounded down.
func RandomBytesBetween ¶
RandomBytesBetween returns a random byte slice that is in the range [start, end] when compared lexicographically. The slice will have a length in the range [len(start), len(end)]. In the current implementation, all possible permutations are not equally likely. Nil slices for start and end will be treated as empty byte slices. Will panic if:
- start compares lexicographically greater than end
- nil rand is provided
func RatPow10 ¶
RatPow10 returns the result of `10^exponent`. Re-uses the cached values by calling bigPow10Helper.
func SliceToSet ¶
func SliceToSet[K comparable](values []K) map[K]struct{}
SliceToSet converts a slice to a set. Function will panic if there are duplicate values.
func TxMode ¶
TxMode returns a textual representation of the tx mode, one of `CheckTx`, `ReCheckTx`, or `DeliverTx`.
func Uint32ToKey ¶ added in v0.4.0
Uint32ToKey converts a uint32 to a 4-byte slice in big-endian format. The slices can be ordered lexicographically
func Uint64LinearInterpolate ¶
Uint64LinearInterpolate interpolates value v0 towards v1 by a small constant value c, typically expected to be between 0 and 1. Here, the input value of c is represented in ppm. In order to avoid overflows, if 0 <= cPpm <= 1_000_000 then an error is returned.
func Uint64MulPpm ¶
Uint64MulPpm multiplies a uint64 value by a scaling factor represented in ppm. If the integer overflows, this method panics.
Types ¶
type Sortable ¶ added in v0.3.0
type Sortable[K constraints.Ordered] []K
Sortable[K] attaches the methods of sort.Interface to []K, sorting in increasing order.