utils

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ShareScalar   = math.NewInt(1_000_000) // 1 asset = 1e6 shares (neutral precision target)
	VirtualAssets = math.NewInt(1)         // 1 base unit of underlying
	VirtualShares = ShareScalar
)

Fixed precision / virtual-offset parameters.

ShareScalar sets the "neutral" precision: 1 asset unit -> ShareScalar shares. VirtualAssets and VirtualShares are added to totals in all conversions to harden against first-depositor inflation/rounding attacks.

IMPORTANT: - VirtualAssets is ONE base unit of the underlying asset. - VirtualShares equals ShareScalar. - Do NOT divide by ShareScalar in redemption: the share supply is already scaled.

View Source
var ErrZeroAssetsWithSharesOutstanding = errors.New("total assets are zero while shares are outstanding")

ErrZeroAssetsWithSharesOutstanding is returned by CalculateSharesProRata when zero assets back outstanding shares, collapsing the pro-rata divisor to VirtualAssets alone. Classify with errors.Is.

Functions

func CalculateRedeemProRata added in v1.2.0

func CalculateRedeemProRata(shares math.Int, totalShares math.Int, totalAssets math.Int, payoutDenom string) (sdk.Coin, error)

CalculateRedeemProRata computes the payout amount for redeeming shares into the vault's underlying asset using a single-floor, pro-rata formula with virtual offsets. Vaults are single-denom, so the payout is always in underlying units and no price conversion applies.

Arguments:

  • shares is the number of vault shares being redeemed.
  • totalShares / totalAssets are the vault-wide totals prior to redemption.
  • payoutDenom is the underlying asset denom for the resulting coin.

Behavior:

  • Applies virtual offsets: ta' = totalAssets + VirtualAssets, ts' = totalShares + VirtualShares.
  • Computes: payout = floor( shares * ta' / ts' ).
  • Returns sdk.Coin(payoutDenom, payout).

Errors if any input is nil or negative.

func CalculateSharesProRata added in v1.2.0

func CalculateSharesProRata(
	amount math.Int,
	totalAssets math.Int,
	totalShares math.Int,
	shareDenom string,
) (sdk.Coin, error)

CalculateSharesProRata computes minted shares for a deposit of the vault's underlying asset using a single-floor, pro-rata formula with virtual offsets. Vaults are single-denom, so the deposit amount is already in underlying units and no price conversion applies.

Arguments:

  • amount is the deposit amount in underlying units.
  • totalAssets / totalShares are the vault-wide totals prior to the deposit.
  • shareDenom is the vault share denom for the resulting coin.

Behavior:

  • Applies virtual offsets: ta' = totalAssets + VirtualAssets, ts' = totalShares + VirtualShares.
  • First deposit mints amount * ShareScalar.
  • Otherwise: shares = floor( amount * ts' / ta' ).

Errors if any input is nil or negative, or with ErrZeroAssetsWithSharesOutstanding when zero assets back outstanding shares.

func ExpDec

func ExpDec(x math.LegacyDec, terms int) (result math.LegacyDec, err error)

ExpDec calculates e^x using a Maclaurin series expansion of `terms` terms. Fully deterministic and safe for on-chain use.

e^x = 1 + x + x^2/2! + x^3/3! + ... + x^n/n!

The truncated series is only accurate for small |x|, so x is first range-reduced by repeated halving until |x| <= 1 and the series result is squared back up (e^x = (e^(x/2^k))^(2^k)). Without that reduction the series diverges for large positive x and sign-inverts to a negative e^x below x ~= -5.61.

Note: x is cosmosmath.LegacyDec; higher `terms` -> greater accuracy.

A large positive x overflows the LegacyDec accumulator, which panics; that overflow is recovered and returned as an error instead. A large negative x underflows to zero, which is the closest LegacyDec representation of e^x in that regime.

func Filter

func Filter[S any](s []S, fn func(S) bool) iter.Seq[S]

func GenerateRandomBytes

func GenerateRandomBytes(n int) []byte

func Map

func Map[S any, T any](s []S, fn func(S) T) iter.Seq[T]

Types

type Address

type Address struct {
	Bytes  []byte
	Bech32 string
}

func TestAddress

func TestAddress() Address

func TestProvlabsAddress

func TestProvlabsAddress() Address

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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