Documentation ¶
Index ¶
- Constants
- 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) *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 BoolToString(b bool) string
- func BytesSliceToBytes32(b []byte) [32]byte
- func BytesToInt32(b []byte) int32
- func BytesToUint32(b []byte) uint32
- func ChangeRateUint64(originalV uint64, newV uint64) (float32, error)
- func ContainsDuplicates[V comparable](values []V) bool
- func ContainsValue[V comparable](values []V, value V) bool
- func ConvertBigFloatSliceToUint64Slice(values []*big.Float) ([]uint64, error)
- func ConvertBigFloatToUint64(value *big.Float) (uint64, error)
- func ConvertMapToSliceOfKeys[K comparable, V any](m map[K]V) []K
- 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 Int32ToBytes(i int32) []byte
- func Int32ToString(i int32) string
- func Int64MulPpm(x int64, ppm uint32) int64
- func Int64ToBytesForState(id int64) []byte
- 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 MaxUint32(x, y uint32) uint32
- func MedianInt32(input []int32) (int32, error)
- func MedianUint64(input []uint64) (uint64, error)
- func Min[T constraints.Ordered](x, y T) T
- func MustConvertBigIntToInt32(n *big.Int) int32
- func MustConvertIntegerToUint32[T constraints.Integer](value T) uint32
- func MustGetMedianInt32(input []int32) int32
- func MustGetValue[V any](values []V, index uint) V
- func MustParseDuration(s string) time.Duration
- func MustRemoveIndex[V any](values []V, index uint) []V
- func QuoteToBaseQuantums(bigQuoteQuantums *big.Int, baseCurrencyAtomicResolution int32, ...) (bigNotional *big.Int)
- func RandomBool() bool
- 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 StringToUint32(s string) (uint32, error)
- func TxMode(ctx sdk.Context) string
- func Uint32ToBytes(i uint32) []byte
- func Uint32ToBytesForState(id uint32) []byte
- func Uint32ToString(i uint32) string
- func Uint64LinearInterpolate(v0 uint64, v1 uint64, cPpm uint32) (uint64, error)
- func Uint64MulPpm(x uint64, ppm uint32) uint64
- func Uint64ToString(i uint64) string
- type FileHandler
- type FileHandlerImpl
- type GrpcClient
- type GrpcClientImpl
- type GrpcServer
- type Medianizer
- type MedianizerImpl
- type MsgRouter
- type RequestHandler
- type RequestHandlerImpl
- type TimeProvider
- type TimeProviderImpl
- 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) UsdcAssetId = uint32(0) ZeroUint64 = uint64(0) )
const (
AvgInt32MaxArrayLength = 2 << 31
)
Variables ¶
This section is empty.
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 BoolToString ¶
func BytesSliceToBytes32 ¶
func BytesToInt32 ¶
func BytesToUint32 ¶
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 ContainsValue ¶
func ContainsValue[V comparable](values []V, value V) bool
ContainsValue returns true if the slice contains the provided value, 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 ConvertMapToSliceOfKeys ¶
func ConvertMapToSliceOfKeys[K comparable, V any](m map[K]V) []K
ConvertMapToSliceOfKeys converts a map to a slice of keys. Note that this method returns the keys in a non-deterministic order.
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 Int32ToBytes ¶
func Int32ToString ¶
func Int64MulPpm ¶
func Int64ToBytesForState ¶
Int64ToBytesForState converts the int64 to 8 bytes + '/' to be used as state key prefixes.
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 MedianInt32 ¶
MedianInt32 returns the median value of the input slice. Note that if the input has an even number of elements, then the returned median is rounded towards positive/negative infinity, to the nearest int32. For example, 6.5 is rounded to 7 and -4.5 is rounded to -5.
func MedianUint64 ¶
MedianUint64 returns the median value of the input slice. Note that if the input has an even number of elements, then the returned median is rounded up the nearest uint64. For example, 6.5 is rounded up to 7.
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 MustGetMedianInt32 ¶
MustGetMedianInt32 is a wrapper around `MedianInt32` that panics if input length is zero.
func MustGetValue ¶
MustGetValue returns the element at `index` position in a slice and panics if `index` is greater than or equal to slice length.
func MustParseDuration ¶
MustParseDuration turns a string into a duration. Panics if the string cannot be parsed.
func MustRemoveIndex ¶
MustRemoveIndex returns a copy of the provided slice with the value at `index` removed. This function will not change the ordering of other elements within the original slice. Note that function will panic if `index >= len(values)`.
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 RandomBool ¶
func RandomBool() bool
func RandomBytesBetween ¶
RandomBytesBetween returns a random byte slice that is in the range [start, end] when compared lexicographically. Nil slices for start and end will be treated as empty byte slices. Will panic if:
- start compares lexicographically less 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 StringToUint32 ¶
func TxMode ¶
TxMode returns a textual representation of the tx mode, one of `CheckTx`, `ReCheckTx`, or `DeliverTx`.
func Uint32ToBytes ¶
func Uint32ToBytesForState ¶
Uint32ToBytesForState converts the uint32 to 4 bytes + '/' to be used as state key prefixes
func Uint32ToString ¶
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.
func Uint64ToString ¶
Types ¶
type FileHandler ¶
FileHandler is an interface that encapsulates the os function `RemoveAll`.
type FileHandlerImpl ¶
type FileHandlerImpl struct{}
FileHandlerImpl is the struct that implements the `FileHandler` interface.
func (*FileHandlerImpl) RemoveAll ¶
func (r *FileHandlerImpl) RemoveAll(path string) error
RemoveAll wraps `os.RemoveAll` which removes everything at a given path.
type GrpcClient ¶
type GrpcClient interface { NewGrpcConnection(ctx context.Context, socketAddress string) (*grpc.ClientConn, error) NewTcpConnection(ctx context.Context, endpoint string) (*grpc.ClientConn, error) CloseConnection(grpcConn *grpc.ClientConn) error }
GrpcClient is an interface that encapsulates the `NewGrpcConnection` function and `CloseConnection`.
type GrpcClientImpl ¶
type GrpcClientImpl struct{}
GrpcClientImpl is the struct that implements the `GrpcClient` interface.
func (*GrpcClientImpl) CloseConnection ¶
func (g *GrpcClientImpl) CloseConnection(grpcConn *grpc.ClientConn) error
CloseConnection calls `grpc.ClientConn.Close()` to close a grpc connection.
func (*GrpcClientImpl) NewGrpcConnection ¶
func (g *GrpcClientImpl) NewGrpcConnection( ctx context.Context, socketAddress string, ) (*grpc.ClientConn, error)
NewGrpcConnection calls `grpc.Dial` with custom parameters to create a secure connection with context that blocks until the underlying connection is up.
func (*GrpcClientImpl) NewTcpConnection ¶
func (g *GrpcClientImpl) NewTcpConnection( ctx context.Context, endpoint string, ) (*grpc.ClientConn, error)
NewTcpConnection calls `grpc.Dial` to create an insecure tcp connection.
type GrpcServer ¶
type GrpcServer interface { Serve(lis net.Listener) error Stop() RegisterService(sd *grpc.ServiceDesc, ss interface{}) }
GrpcServer is an interface that encapsulates a `Grpc.Server` object.
type Medianizer ¶
Medianizer is an interface that encapsulates the lib.math function `MedianUint64`.
type MedianizerImpl ¶
type MedianizerImpl struct{}
MedianizerImpl is the struct that implements the `Medianizer` interface.
func (*MedianizerImpl) MedianUint64 ¶
func (r *MedianizerImpl) MedianUint64(input []uint64) (uint64, error)
MedianUint64 wraps `lib.MedianUint64` which gets the median of a uint64 slice.
type RequestHandler ¶
RequestHandler is an interface that handles making HTTP requests.
type RequestHandlerImpl ¶
type RequestHandlerImpl struct {
// contains filtered or unexported fields
}
RequestHandlerImpl is the struct that implements the `RequestHandler` interface.
func NewRequestHandlerImpl ¶
func NewRequestHandlerImpl(client *http.Client) *RequestHandlerImpl
NewRequestHandlerImpl creates a new RequestHandlerImpl. It manages making HTTP requests.
type TimeProvider ¶
TimeProvider is an interface that provides time.
type TimeProviderImpl ¶
type TimeProviderImpl struct{}
TimeProviderImpl implements TimeProvider interface.