common

package
v0.21.5 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

README

x/common

The x/common directory holds helper and utility functions to be utilized by other x/ cosmos-sdk modules.

Documentation

Index

Constants

View Source
const (
	TreasuryPoolModuleAccount = "treasury_pool"
	// TO_MICRO: multiplier for converting between units and micro-units.
	TO_MICRO = int64(1_000_000)
)
View Source
const (
	// number of decimal places
	Precision = 18

	// bits required to represent the above precision
	// Ceiling[Log2[10^Precision - 1]]
	DecimalPrecisionBits = 60

	MaxDecBitLen = maxBitLen + decimalTruncateBits
)
View Source
const (
	PRECISION = 18
)

Variables

View Source
var (
	PRECISION_MULT = calcPrecisionMultiplier(0)
	PRECISION_SQRT = int64(PRECISION / 2)
)

Functions

func AddrsToStrings added in v0.5.2

func AddrsToStrings(addrs ...sdk.AccAddress) []string

func BigIntPow10 added in v0.21.0

func BigIntPow10(power int64) *big.Int

BigIntPow10 returns a big int that is a power of 10, e.g. BigIngPow10(3) returns 1000. This function is useful for creating large numbers outside the range of an int64 or 18 decimal precision.

func ChopPrecisionAndRound added in v0.21.0

func ChopPrecisionAndRound(d *big.Int) *big.Int

Remove a Precision amount of rightmost digits and perform bankers rounding on the remainder (gaussian rounding) on the digits which have been removed.

Mutates the input. Use the non-mutative version if that is undesired

func CombineErrors added in v0.17.0

func CombineErrors(errs ...error) (outErr error)

Combines errors into single error. Error descriptions are ordered the same way they're passed to the function.

func CombineErrorsFromStrings added in v0.17.0

func CombineErrorsFromStrings(strs ...string) (err error)

func CombineErrorsGeneric added in v0.17.0

func CombineErrorsGeneric(errAnySlice any) (out error, ok bool)

func MustSqrtBigInt added in v0.21.0

func MustSqrtBigInt(i *big.Int) *big.Int

MustSqrtBigInt returns the square root of the input.

  • NOTE: MustSqrtBigInt panics if it is called on a negative number because it uses the `big.Int.Sqrt` from the "math/big" package.

func MustSqrtDec added in v0.21.0

func MustSqrtDec(dec sdk.Dec) sdk.Dec

MustSqrtDec computes the square root of the input decimal using its underlying big.Int. The big.Int.Sqrt method is part of the standard library, thoroughly tested, works at seemingly unbound precision (e.g. for numbers as large as 10**99.

  • NOTE, MustSqrtDec panics if it is called on a negative number, similar to the sdk.NewCoin and SqrtBigInt functions. A panic safe version of MustSqrtDec is available in the SqrtDec method.

func SqrtBigInt added in v0.21.0

func SqrtBigInt(i *big.Int) (*big.Int, error)

SqrtInt is the panic-safe version of MustSqrtBigInt

func SqrtDec added in v0.21.0

func SqrtDec(dec sdk.Dec) (sdk.Dec, error)

SqrtDec computes the square root of the input decimal using its underlying big.Int. SqrtDec is panic-safe and returns an error if the input decimal is negative.

The big.Int.Sqrt method is part of the standard library, thoroughly tested, works at seemingly unbound precision (e.g. for numbers as large as 10**99.

func StringsToAddrs added in v0.5.2

func StringsToAddrs(strs ...string) []sdk.AccAddress

func ToError added in v0.17.0

func ToError(v any) (out error, ok bool)

ToError converts a value to an error type if it: (1) is a string, (2) has a String() function (3) is already an error. (4) or is a slice of these cases I.e., the types supported are: string, []string, error, []error, fmt.Stringer, []fmt.Stringer

The type is inferred from try catch blocks at runtime.

func TryCatch added in v0.17.0

func TryCatch(callback func()) func() error

TryCatch is an implementation of the try-catch block from languages like C++ and JS. given a 'callback' function, TryCatch defers and recovers from any panics or errors, allowing one to safely handle multiple panics in succession.

Typically, you'll write something like: `err := TryCatch(aRiskyFunction)()`

Usage example:

var calmPanic error = TryCatch(func() {
  panic("something crazy")
})()
fmt.Println(calmPanic.Error()) // prints "something crazy"

Note that TryCatch doesn't return an error. It returns a function that returns an error. Only when you call the output of TryCatch will it "feel" like a try-catch block from other languages.

This means that TryCatch can be used to restart go routines that panic as well.

Types

This section is empty.

Directories

Path Synopsis
Package nullify provides methods to init nil values structs for test assertion.
Package nullify provides methods to init nil values structs for test assertion.
cli
Package network implements and exposes a fully operational in-process Tendermint test network that consists of at least one or potentially many validators.
Package network implements and exposes a fully operational in-process Tendermint test network that consists of at least one or potentially many validators.

Jump to

Keyboard shortcuts

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