Documentation
¶
Index ¶
- Constants
- Variables
- func AddrsToStrings(addrs ...sdk.AccAddress) []string
- func BankersRound(quo, rem, halfPrecision *big.Int) *big.Int
- func BigIntPow10(power int64) *big.Int
- func ChopPrecisionAndRound(d *big.Int) *big.Int
- func Clamp(value sdkmath.LegacyDec, clampValue sdkmath.LegacyDec) sdkmath.LegacyDec
- func CombineErrors(errs ...error) (outErr error)
- func CombineErrorsFromStrings(strs ...string) (err error)
- func CombineErrorsGeneric(errAnySlice any) (out error, ok bool)
- func MustSqrtBigInt(i *big.Int) *big.Int
- func MustSqrtDec(dec sdkmath.LegacyDec) sdkmath.LegacyDec
- func ParseNibiBalance(wei sdkmath.Int) (amtUnibi, amtWei sdkmath.Int)
- func ParseNibiBalanceFromParts(unibi, wei sdkmath.Int) (amtUnibi, amtWei sdkmath.Int)
- func ParsePagination(pageReq *sdkquery.PageRequest) (newPageReq *sdkquery.PageRequest, page int, err error)
- func SqrtBigInt(i *big.Int) (*big.Int, error)
- func SqrtDec(dec sdkmath.LegacyDec) (sdkmath.LegacyDec, error)
- func StringsToAddrs(strs ...string) []sdk.AccAddress
- func ToError(v any) (out error, ok bool)
- func TryCatch(callback func()) func() error
- func U256SafeFromBig(x *big.Int) (*uint256.Int, error)
- func U256SafeFromSdkInt(x sdkmath.Int) (*uint256.Int, error)
- func UnsafeBytesToStr(b []byte) string
- func UnsafeStrToBytes(s string) []byte
- func WeiPerUnibiU256() *uint256.Int
Constants ¶
const ( TreasuryPoolModuleAccount = "treasury_pool" // TO_MICRO: multiplier for converting between units and micro-units. TO_MICRO = int64(1_000_000) NIBIRU_TEAM = "nibi1l8dxzwz9d4peazcqjclnkj2mhvtj7mpnkqx85mg0ndrlhwrnh7gskkzg0v" )
const ( // number of decimal places PrecisionExponent = 18 // bits required to represent the above precision // Ceiling[Log2[10^Precision - 1]] DecimalPrecisionBits = 60 MaxDecBitLen = maxBitLen + decimalTruncateBits )
const DefaultPageItemsLimit uint64 = 50
const (
PRECISION = 18
)
Variables ¶
var ( // LegacyDecValue represents a collections.ValueCodec to work with LegacyDec. LegacyDecValue collcodec.ValueCodec[sdkmath.LegacyDec] = legacyDecValueCodec{} SdkIntKey collcodec.KeyCodec[sdkmath.Int] = mathIntKeyCodec{} AccAddressValue collcodec.ValueCodec[sdk.AccAddress] = accAddressValueCodec{} )
var ( PRECISION_MULT = calcPrecisionMultiplier(0) PRECISION_SQRT = int64(PRECISION / 2) )
var ErrNilGrpcMsg = grpcstatus.Errorf(grpccodes.InvalidArgument, "nil msg")
var StringValueEncoder collections.ValueEncoder[string] = stringValueEncoder{}
TODO: (realu) Move to collections library
var WeiPerUnibi = sdkmath.NewIntFromBigInt( new(big.Int).Exp(big.NewInt(10), big.NewInt(12), nil), )
WeiPerUnibi is a big.Int for 10^{12}. Each "unibi" (micronibi) is 10^{12} wei because 1 NIBI = 10^{18} wei.
Functions ¶
func AddrsToStrings ¶
func AddrsToStrings(addrs ...sdk.AccAddress) []string
func BankersRound ¶
BankersRound: Banker's rounding is a method commonly used in banking and accounting to reduce roudning bias when processing large volumes of rounded numbers.
1. If the remainder < half precision, round down 2. If the remainder > half precision, round up 3. If remainder == half precision, round to the nearest even number
The name comes from the idea that it provides egalitarian rounding that doesn't consistently favor one party over another (e.g. always rounding up). With this method, rounding errors tend to cancel out rather than accumulating in one direction.
func BigIntPow10 ¶
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 ¶
ChopPrecisionAndRound: 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 Clamp ¶
Clamp return the value if it is within the clampValue, otherwise return the clampValue. e.g. Clamp(1.5, 1) = 1, Clamp(-1.5, 1) = -1, Clamp(0.5, 1) = 0.5
func CombineErrors ¶
Combines errors into single error. Error descriptions are ordered the same way they're passed to the function.
func CombineErrorsGeneric ¶
func MustSqrtBigInt ¶
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 ¶
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 ParseNibiBalance ¶
ParseNibiBalance splits a NIBI amount in wei into unibi (bank units) and wei remainder. Used by keeper to normalize the dual-balance model at the 10^12 boundary.
- This is the inverse of GetWeiBalance aggregation: (unibi × 10^12) + wei.
- Example: ParseNibiBalance(2×10^12 + 3) → (2, 3)
### Returns:
- amtUnibi: bank balance in unibi (micro-NIBI; 10^{-6} NIBI)
- amtWei: remainder in wei, where 0 ≤ amtWei < 10^{12}
func ParseNibiBalanceFromParts ¶
ParseNibiBalanceFromParts normalizes (unibi, wei) into canonical form. Computes total wei as (unibi × 10^{12}) + wei, then splits into normalized parts.
Returns:
- amtUnibi: normalized unibi (micro-NIBI; 10^-6 NIBI)
- amtWei: normalized remainder, where 0 ≤ amtWei < 10^{12}
Used by keeper to carry/borrow across the 10^{12} boundary, keeping bank and wei-store synchronized.
Example: ParseNibiBalanceFromParts(5, 2×10^{12} + 3) → (7, 3)
func ParsePagination ¶
func ParsePagination( pageReq *sdkquery.PageRequest, ) (newPageReq *sdkquery.PageRequest, page int, err error)
ParsePagination: Validates and cleans a PageRequest to make setting values less error-prone and use Nibiru-specific defaults.
- This fn is intended to be used with sdkquery.Paginate, which paginates all of the results in a PrefixStore based on the provided PageRequest.
- This fn is panic-safe, so it can be used freely throughout the base app.
- A "page" value of -1 means that a key is given for the prefix store. This means that the PageRequest.Offset will be ignored.
func SqrtBigInt ¶
SqrtBigInt is the panic-safe version of MustSqrtBigInt
func SqrtDec ¶
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 ¶
func StringsToAddrs(strs ...string) []sdk.AccAddress
func ToError ¶
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 ¶
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.
func U256SafeFromBig ¶
U256SafeFromBig converts a big.Int to uint256.Int, returning an error if x < 0 or x.BitLen() > 256. Nil or zero is treated as zero.
func U256SafeFromSdkInt ¶
U256SafeFromSdkInt converts sdkmath.Int to *uint256.Int safely. Returns for non-positive value or overflow.
func UnsafeBytesToStr ¶
UnsafeBytesToStr is meant to make a zero allocation conversion from []byte -> string to speed up operations, it is not meant to be used generally, but for a specific pattern to delete keys from a map. This is from file "cosmos-sdk/internal/conv/string.go".
func UnsafeStrToBytes ¶
UnsafeStrToBytes uses unsafe to convert string into byte array. Returned bytes must not be altered after this function is called as it will cause a segmentation fault. This is from file "cosmos-sdk/internal/conv/string.go".
func WeiPerUnibiU256 ¶
WeiPerUnibiU256 is a uint256.Int for 10^{12}. Each "unibi" (micronibi) is 10^{12} wei because 1 NIBI = 10^{18} wei.
Types ¶
This section is empty.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package omap defines a generic-based type for creating ordered maps.
|
Package omap defines a generic-based type for creating ordered maps. |
|
Package nullify provides methods to init nil values structs for test assertion.
|
Package nullify provides methods to init nil values structs for test assertion. |
|
testnetwork
Package "testnetwork" implements and exposes a fully operational in-process Tendermint test network that consists of at least one or potentially many validators.
|
Package "testnetwork" implements and exposes a fully operational in-process Tendermint test network that consists of at least one or potentially many validators. |