utils

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

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.

If its an out of gas panic, this function will also panic like in normal tx execution flow. This is still safe for beginblock / endblock code though, as they do not have out of gas panics.

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.

func CompositeKey added in v1.2.0

func CompositeKey(parts ...[]byte) []byte

func ComputeTokensFromShares added in v1.2.0

func ComputeTokensFromShares(shares sdk.DecCoins, delegatedTokens sdk.Coins, delegatorShares sdk.DecCoins) sdk.DecCoins

ComputeTokensFromShares calculates the token worth of provided shares.

func ComputeTokensFromSharesTruncated added in v1.2.0

func ComputeTokensFromSharesTruncated(shares sdk.DecCoins, delegatedTokens sdk.Coins, delegatorShares sdk.DecCoins) sdk.DecCoins

ComputeTokensFromSharesTruncated calculates the token worth of provided shares, truncated.

func Contains added in v1.3.0

func Contains[T comparable](slice []T, value T) bool

Contains returns true if the slice contains the given value.

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 DenomsSubsetOf added in v1.2.0

func DenomsSubsetOf(coins sdk.DecCoins, coinsB sdk.DecCoins) bool

DenomsSubsetOf returns true if receiver's denom set is subset of coinsB's denoms.

func FilterCoinsByDenom added in v1.2.0

func FilterCoinsByDenom(coins, coinsB sdk.Coins) sdk.Coins

FilterCoinsByDenom returns the intersection of two coins. e.g. FilterCoinsByDenom({2A, 3B}, {A}) = {2A} FilterCoinsByDenom({2A, 3B}, {5C}) = {} FilterCoinsByDenom({2A, 3B}, {A, B}) = {2A, 3B}

func FilterDecCoinsByDenom added in v1.2.0

func FilterDecCoinsByDenom(coins, coinsB sdk.DecCoins) sdk.DecCoins

FilterDecCoinsByDenom returns the intersection of two coins. e.g. FilterDecCoinsByDenom{2A, 3B}, {A}) = {2A} FilterDecCoinsByDenom({2A, 3B}, {5C}) = {} FilterDecCoinsByDenom({2A, 3B}, {A, B}) = {2A, 3B}

func FilterDepositRecords

func FilterDepositRecords(arr []recordstypes.DepositRecord, condition func(recordstypes.DepositRecord) bool) (ret []recordstypes.DepositRecord)

func FindDuplicate added in v1.2.0

func FindDuplicate[T comparable](slice []T) *T

FindDuplicate returns the first duplicate element in the slice. If no duplicates are found, it returns nil instead.

func FindDuplicateFunc added in v1.2.0

func FindDuplicateFunc[T any](slice []T, compare func(a T, b T) bool) *T

FindDuplicateFunc returns the first duplicate element in the slice. If no duplicates are found, it returns nil instead.

func FormatUint32Slice added in v1.2.0

func FormatUint32Slice(s []uint32) string

func GetFromBech32

func GetFromBech32(bech32str, prefix string) ([]byte, error)

GetFromBech32 decodes a bytestring from a Bech32 encoded string.

func GetSharesDenomFromTokenDenom added in v1.2.0

func GetSharesDenomFromTokenDenom(prefix string, id uint32, tokenDenom string) string

GetSharesDenomFromTokenDenom returns the shares denom from the token denom. The returned shares denom will be in the format "{prefix}/{id}/{tokenDenom}".

func GetTokenDenomFromSharesDenom added in v1.2.0

func GetTokenDenomFromSharesDenom(sharesDenom string) string

GetTokenDenomFromSharesDenom returns the token denom from the shares denom. It expects the shares denom to be in the format "{xxxxxx}/{xxxxxx}/{tokenDenom}".

func IsAllGT added in v1.2.0

func IsAllGT(coins, coinsB sdk.DecCoins) bool

IsAllGT returns true if for every denom in coinsB, the denom is present at a greater amount in coins.

func IsAllGTE added in v1.2.0

func IsAllGTE(coins, coinsB sdk.DecCoins) bool

IsAllGTE returns false if for any denom in coinsB, the denom is present at a smaller amount in coins; else returns true.

func IsAnyGT added in v1.2.0

func IsAnyGT(coins, coinsB sdk.DecCoins) bool

IsAnyGT returns true iff for any denom in coins, the denom is present at a greater amount in coinsB.

e.g. IsAnyGT({2A, 3B}, {A}) = true IsAnyGT({2A, 3B}, {5C}) = false IsAnyGT({}, {5C}) = false IsAnyGT({2A, 3B}, {}) = false

func IsAnyLT added in v1.2.0

func IsAnyLT(coins, coinsB sdk.DecCoins) bool

IsAnyLT returns true iff for any denom in coins, the denom is present at a smaller amount in coinsB.

func IsIcs20Packet added in v1.2.0

func IsIcs20Packet(packetData []byte) (isIcs20 bool, ics20data transfertypes.FungibleTokenPacketData)

IsIcs20Packet checks if the data is a ICS20 packet.

func IsInvalidExRate added in v1.2.0

func IsInvalidExRate(delegatedTokens sdk.Coins, delegatorsShares sdk.DecCoins) bool

IsInvalidExRate returns true if the delegated tokens are zero and the delegators shares are positive.

func IsOutOfGasError

func IsOutOfGasError(err any) (bool, string)

Frustratingly, this has to return the error descriptor, not an actual error itself because the SDK errors here are not actually errors. (They don't implement error interface)

func IssueShares added in v1.2.0

func IssueShares(amount sdk.Coins, getShareDenom ShareDenomGetter, delegatedTokens sdk.Coins, delegatorsShares sdk.DecCoins) sdk.DecCoins

IssueShares calculates the shares to issue for a delegation of the given amount.

func JSONStringHasKey added in v1.2.0

func JSONStringHasKey(data, key string) (found bool, jsonObject map[string]interface{})

JSONStringHasKey parses the provided data as a json object and checks if it contains the provided key.

func LogHeader

func LogHeader(s string, a ...any) string

LogHeader returns a log header string with a dash padding on either side Ex:

------------------------------ string ------------------------------

func LogICACallbackStatusWithHostZone

func LogICACallbackStatusWithHostZone(chainID string, callbackID string, status icacallbacktypes.AckResponseStatus, packet channeltypes.Packet) string

LogICACallbackStatusWithHostZone returns a log string with a chain Id and icacallback as a prefix, and status of the callback Ex:

| COSMOSHUB-4   |  DELEGATE ICACALLBACK  |  ICA SUCCESS, Packet: ...

func LogICACallbackWithHostZone

func LogICACallbackWithHostZone(chainID string, callbackID string, s string, a ...any) string

LogICACallbackWithHostZone returns a log string with a chain Id and icacallback as a prefix Ex:

| COSMOSHUB-4   |  DELEGATE ICACALLBACK  |  string

func LogICQCallbackWithHostZone

func LogICQCallbackWithHostZone(chainID string, callbackID string, s string, a ...any) string

LogICQCallbackWithHostZone returns a log string with a chain Id and icqcallback as a prefix Ex:

| COSMOSHUB-4   |  WITHDRAWALHOSTBALANCE ICQCALLBACK  |  string

func LogWithHostZone

func LogWithHostZone(chainID string, s string, a ...any) string

LogWithHostZone returns a log string with a chainId and tab as the prefix Ex:

| COSMOSHUB-4   |   string

func Map added in v1.2.0

func Map[T, U any](slice []T, f func(T) U) []U

Map applies the given function to each element in the slice and returns a new slice with the results.

func MapGetOrDefault added in v1.3.0

func MapGetOrDefault[K, V any](
	ctx sdk.Context,
	collectionMap collections.Map[K, V],
	key K,
	defaultValueProvider func() V,
) (V, error)

MapGetOrDefault gets a value from the map with the given key. If the key is not found within the map, the default value is returned.

func Min

func Min(a int, b int) int

func MustParseCoin added in v1.2.0

func MustParseCoin(s string) sdk.Coin

func MustParseCoins added in v1.2.0

func MustParseCoins(s string) sdk.Coins

func MustParseDec added in v1.2.0

func MustParseDec(s string) sdkmath.LegacyDec

func MustParseDecCoins added in v1.2.0

func MustParseDecCoins(s string) sdk.DecCoins

func ParseUint32Slice added in v1.2.0

func ParseUint32Slice(s string) ([]uint32, 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 Remove added in v1.3.0

func Remove[T comparable](slice []T, value T) (newSlice []T, removed bool)

Remove removes the first instance of value from the provided slice.

func RemoveDuplicates added in v1.2.0

func RemoveDuplicates[T comparable](slice []T) []T

RemoveDuplicates removes all duplicate elements from the slice.

func SharesFromDecCoins added in v1.2.0

func SharesFromDecCoins(tokens sdk.DecCoins, getShareDenom ShareDenomGetter, delegatedTokens sdk.Coins, delegatorsShares sdk.DecCoins) (sdk.DecCoins, error)

SharesFromDecCoins returns the shares of a delegation given a bond amount.

func SharesFromTokens added in v1.2.0

func SharesFromTokens(tokens sdk.Coins, getShareDenom ShareDenomGetter, delegatedTokens sdk.Coins, delegatorsShares sdk.DecCoins) (sdk.DecCoins, error)

SharesFromTokens returns the shares of a delegation given a bond amount.

func SharesFromTokensTruncated added in v1.2.0

func SharesFromTokensTruncated(tokens sdk.Coins, getShareDenom ShareDenomGetter, delegatedTokens sdk.Coins, delegatorsShares sdk.DecCoins) (sdk.DecCoins, error)

SharesFromTokensTruncated returns the truncated shares of a delegation given a bond amount.

func StAssetDenomFromHostZoneDenom

func StAssetDenomFromHostZoneDenom(hostZoneDenom string) string

StAssetDenomFromHostZoneDenom returns the stDenom from a native denom by appending a st prefix

func StringMapKeys

func StringMapKeys[V any](m map[string]V) []string

func Uint32ToBigEndian added in v1.2.0

func Uint32ToBigEndian(i uint32) []byte

Uint32ToBigEndian marshals uint32 to a bigendian byte slice so it can be sorted

func VerifyAddressFormat

func VerifyAddressFormat(bz []byte) error

VerifyAddressFormat verifies that the provided bytes form a valid address according to the default address rules or a custom address verifier set by GetConfig().SetAddressVerifier(). TODO make an issue to get rid of global Config ref: https://github.com/cosmos/cosmos-sdk/issues/9690

func VerifyTxHash

func VerifyTxHash(txHash string) (err error)

VerifyTxHash verifies a tx hash is valid

Types

type AccAddress

type AccAddress []byte

AccAddress a wrapper around bytes meant to represent an account address. When marshaled to a string or JSON, it uses Bech32.

func AccAddressFromBech32

func AccAddressFromBech32(address string, bech32prefix string) (addr AccAddress, err error)

AccAddressFromBech32 creates an AccAddress from a Bech32 string.

type AccountKeeper

type AccountKeeper interface {
	NewAccount(context.Context, sdk.AccountI) sdk.AccountI
	GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
	SetAccount(ctx context.Context, acc sdk.AccountI)
}

type ShareDenomGetter added in v1.2.0

type ShareDenomGetter func(tokenDenom string) (shareDenom string)

ShareDenomGetter represents a function that returns the shares denom given a token denom.

Jump to

Keyboard shortcuts

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