osmoutils

package
v13.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Functions

func ApplyFuncIfNoError

func ApplyFuncIfNoError(ctx sdk.Context, f func(ctx sdk.Context) error) (err error)

This function lets you run the function f, but if theres an error or panic drop the state machine change and log the error. If there is no error, proceeds as normal (but with some slowdown due to SDK store weirdness) Try to avoid usage of iterators in f.

func BinarySearch

func BinarySearch(f func(input sdk.Int) (sdk.Int, error),
	lowerbound sdk.Int,
	upperbound sdk.Int,
	targetOutput sdk.Int,
	errTolerance ErrTolerance,
	maxIterations int,
) (sdk.Int, error)

Binary search inputs between [lowerbound, upperbound] to a monotonic increasing function f. We stop once f(found_input) meets the ErrTolerance constraints. If we perform more than maxIterations (or equivalently lowerbound = upperbound), we return an error.

func BinarySearchBigDec

func BinarySearchBigDec(f func(input osmomath.BigDec) (osmomath.BigDec, error),
	lowerbound osmomath.BigDec,
	upperbound osmomath.BigDec,
	targetOutput osmomath.BigDec,
	errTolerance ErrTolerance,
	maxIterations int,
) (osmomath.BigDec, error)

BinarySearchBigDec takes as input: * an input range [lowerbound, upperbound] * an increasing function f * a target output x * max number of iterations (for gas control / handling does-not-converge cases)

It binary searches on the input range, until it finds an input y s.t. f(y) meets the err tolerance constraints for how close it is to x. If we perform more than maxIterations (or equivalently lowerbound = upperbound), we return an error.

func CanCreateModuleAccountAtAddr

func CanCreateModuleAccountAtAddr(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) error

CanCreateModuleAccountAtAddr tells us if we can safely make a module account at a given address. By collision resistance of the address (given API safe construction), the only way for an account to be already be at this address is if its claimed by the same pre-image from the correct module, or some SDK command breaks assumptions and creates an account at designated address. This function checks if there is an account at that address, and runs some safety checks to be extra-sure its not a user account (e.g. non-zero sequence, pubkey, of fore-seen account types). If there is no account, or if we believe its not a user-spendable account, we allow module account creation at the address. else, we do not.

TODO: This is generally from an SDK design flaw code based off wasmd code: https://github.com/CosmWasm/wasmd/pull/996 Its _mandatory_ that the caller do the API safe construction to generate a module account addr, namely, address.Module(ModuleName, {key})

func CoinsDenoms

func CoinsDenoms(coins sdk.Coins) []string

TODO: Get this into the SDK https://github.com/cosmos/cosmos-sdk/issues/12538

func CreateModuleAccount

func CreateModuleAccount(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) error

CreateModuleAccount creates a module account at the provided address. It overrides an account if it exists at that address, with a non-zero sequence number & pubkey Contract: addr is derived from `address.Module(ModuleName, key)`

func DefaultFeeString

func DefaultFeeString(cfg network.Config) string

func Filter

func Filter[T interface{}](filter func(T) bool, s []T) []T

func FormatFixedLengthU64

func FormatFixedLengthU64(d uint64) string

func FormatTimeString

func FormatTimeString(t time.Time) string

func GatherAllKeysFromStore

func GatherAllKeysFromStore(storeObj store.KVStore) []string

func GatherValuesFromStore

func GatherValuesFromStore[T any](storeObj store.KVStore, keyStart []byte, keyEnd []byte, parseValue func([]byte) (T, error)) ([]T, error)

func GatherValuesFromStorePrefix

func GatherValuesFromStorePrefix[T any](storeObj store.KVStore, prefix []byte, parseValue func([]byte) (T, error)) ([]T, error)

func Get

func Get(store store.KVStore, key []byte, result proto.Message) (found bool, err error)

Get returns a value at key by mutating the result parameter. Returns true if the value was found and the result mutated correctly. If the value is not in the store, returns false. Returns error only when database or serialization errors occur. (And when an error occurs, returns false)

func GetFirstValueAfterPrefixInclusive

func GetFirstValueAfterPrefixInclusive[T any](storeObj store.KVStore, keyStart []byte, parseValue func([]byte) (T, error)) (T, error)

func GetFirstValueInRange

func GetFirstValueInRange[T any](storeObj store.KVStore, keyStart []byte, keyEnd []byte, reverseIterate bool, parseValue func([]byte) (T, error)) (T, error)

func GetIterValuesWithStop

func GetIterValuesWithStop[T any](
	storeObj store.KVStore,
	keyStart []byte,
	keyEnd []byte,
	reverse bool,
	stopFn func([]byte) bool,
	parseValue func([]byte) (T, error),
) ([]T, error)

func GetValuesUntilDerivedStop

func GetValuesUntilDerivedStop[T any](storeObj store.KVStore, keyStart []byte, stopFn func([]byte) bool, parseValue func([]byte) (T, error)) ([]T, error)

func IsAckError

func IsAckError(acknowledgement []byte) bool

IsAckError checks an IBC acknowledgement to see if it's an error. This is a replacement for ack.Success() which is currently not working on some circumstances

func MakeNew added in v13.1.1

func MakeNew[T any]() T

MakeNew makes a new instance of generic T. if T is a pointer, makes a new instance of the underlying struct via reflection, and then a pointer to it.

func MinCoins

func MinCoins(coinsA sdk.Coins, coinsB sdk.Coins) sdk.Coins

MinCoins returns the minimum of each denom between both coins. For now it assumes they have the same denoms. TODO: Replace with method in SDK once we update our version

func MustExtractDenomFromPacketOnRecv

func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string

MustExtractDenomFromPacketOnRecv takes a packet with a valid ICS20 token data in the Data field and returns the denom as represented in the local chain. If the data cannot be unmarshalled this function will panic

func MustGet

func MustGet(store store.KVStore, key []byte, result proto.Message)

MustGet gets key from store by mutating result Panics on any error.

func MustGetDec

func MustGetDec(store store.KVStore, key []byte) sdk.Dec

MustGetDec gets dec value from store at key. Panics on any error.

func MustSet

func MustSet(storeObj store.KVStore, key []byte, value proto.Message)

MustSet runs store.Set(key, proto.Marshal(value)) but panics on any error.

func MustSetDec

func MustSetDec(store store.KVStore, key []byte, value sdk.Dec)

MustSetDec sets dec value to store at key. Panics on any error.

func ParseSdkIntFromString

func ParseSdkIntFromString(s string, separator string) ([]sdk.Int, error)

func ParseTimeString

func ParseTimeString(s string) (time.Time, error)

Parses a string encoded using FormatTimeString back into a time.Time

func ParseUint64SliceFromString

func ParseUint64SliceFromString(s string, separator string) ([]uint64, error)

func PrintPanicRecoveryError

func PrintPanicRecoveryError(ctx sdk.Context, recoveryError interface{})

PrintPanicRecoveryError error logs the recoveryError, along with the stacktrace, if it can be parsed. If not emits them to stdout.

func ReverseSlice

func ReverseSlice[T any](s []T) []T

ReverseSlice reverses the input slice in place. Does mutate argument.

func SortSlice

func SortSlice[T constraints.Ordered](s []T)

SortSlice sorts a slice of type T elements that implement constraints.Ordered. Mutates input slice s

func Uint64ToBytes added in v13.1.2

func Uint64ToBytes(i uint64) []byte

func Uint64ToString added in v13.1.2

func Uint64ToString(i uint64) string

Types

type AccountKeeper

type AccountKeeper interface {
	NewAccount(sdk.Context, authtypes.AccountI) authtypes.AccountI

	GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI
	SetAccount(ctx sdk.Context, acc authtypes.AccountI)
}

type ErrTolerance

type ErrTolerance struct {
	AdditiveTolerance       sdk.Int
	MultiplicativeTolerance sdk.Dec
	RoundingDir             osmomath.RoundingDirection
}

ErrTolerance is used to define a compare function, which checks if two ints are within a certain error tolerance of one another, and (optionally) that they are rounding in the correct direction. ErrTolerance.Compare(a, b) returns true iff: * RoundingMode = RoundUp, then b >= a * RoundingMode = RoundDown, then b <= a * |a - b| <= AdditiveTolerance * |a - b| / min(a, b) <= MultiplicativeTolerance

Each check is respectively ignored if the entry is nil. So AdditiveTolerance = sdk.Int{} or sdk.ZeroInt() MultiplicativeTolerance = sdk.Dec{} RoundingDir = RoundUnconstrained. Note that if AdditiveTolerance == 0, then this is equivalent to a standard compare.

func (ErrTolerance) Compare

func (e ErrTolerance) Compare(expected sdk.Int, actual sdk.Int) int

Compare returns if actual is within errTolerance of expected. returns 0 if it is returns 1 if not, and expected > actual. returns -1 if not, and expected < actual

func (ErrTolerance) CompareBigDec

func (e ErrTolerance) CompareBigDec(expected osmomath.BigDec, actual osmomath.BigDec) int

CompareBigDec validates if actual is within errTolerance of expected. returns 0 if it is returns 1 if not, and expected > actual. returns -1 if not, and expected < actual

type Proposal

type Proposal struct {
	Title       string
	Description string
	Deposit     string
}

func ParseProposalFlags

func ParseProposalFlags(fs *pflag.FlagSet) (*Proposal, error)

type SdkDec

type SdkDec[D any] interface {
	Add(SdkDec[D]) SdkDec[D]
	Quo(SdkDec[D]) SdkDec[D]
	QuoRaw(int64) SdkDec[D]
}

SdkDec

Directories

Path Synopsis
package partialord allows one to define partial orderings, and derive a total ordering
package partialord allows one to define partial orderings, and derive a total ordering
internal/dag
Package dag implements a simple Directed Acyclical Graph (DAG) for deterministic topological sorts
Package dag implements a simple Directed Acyclical Graph (DAG) for deterministic topological sorts

Jump to

Keyboard shortcuts

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