types

package
v0.0.0-...-5c7cbbe Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CoinValue

type CoinValue[D ValueDefinition] struct {
	// contains filtered or unexported fields
}

func NewCoinValue

func NewCoinValue[D ValueDefinition](value *big.Int) *CoinValue[D]

func NewCoinValueFromCoins

func NewCoinValueFromCoins[D ValueDefinition](value decimal.Decimal) *CoinValue[D]

func NewCoinValueFromScaled

func NewCoinValueFromScaled[D ValueDefinition](value decimal.Decimal, exp int32) *CoinValue[D]

func (*CoinValue[D]) Add

func (v *CoinValue[D]) Add(other Value) Value

Add adds the value of another CoinValue to the current CoinValue.

It takes a Value as a parameter and returns a Value. The function checks if the current CoinValue and the other Value have the same coin name. If they don't, it panics with the message "cannot add values of different coins". If they are the same, it creates a new CoinValue with the same definition and adds the units of the other Value to the current CoinValue's value. The function returns the new CoinValue.

Parameters: - other: the Value to add with.

Returns: - Value: the new CoinValue after the addition.

func (CoinValue[D]) CoinName

func (v CoinValue[D]) CoinName() string

CoinName returns the name of the coin associated with the CoinValue.

func (CoinValue[D]) Coins

func (v CoinValue[D]) Coins() decimal.Decimal

Coins returns the value of the CoinValue in whole coin units.

For example for Ethereum this would return the value denomitated in Ether.

Returns: - decimal.Decimal: The value of the CoinValue in whole coind units.

func (*CoinValue[D]) Div

func (v *CoinValue[D]) Div(other Value) Value

Div divides the value of a CoinValue by another Value.

It takes a Value as a parameter and returns a Value. The function checks if the current CoinValue and the other Value have the same coin name. If they don't, it panics with the message "cannot divide values of different coins". If they are the same, it creates a new CoinValue with the same definition and divides the units of the current CoinValue's value by the units of the other Value. The function returns the new CoinValue.

Parameters: - other: the Value to divide with.

Returns: - Value: the new CoinValue after the division.

func (*CoinValue[D]) DivScalar

func (v *CoinValue[D]) DivScalar(scalar *big.Int) Value

DivScalar divides the value of a CoinValue by a scalar value.

It takes a pointer to a big.Int as a parameter and returns a Value. The function creates a new CoinValue with the same definition and divides the units of the current CoinValue's value by the scalar value. The function returns the new CoinValue.

Parameters: - scalar: a pointer to a big.Int representing the scalar value to divide with.

Returns: - Value: the new CoinValue after the division.

func (*CoinValue[D]) Mul

func (v *CoinValue[D]) Mul(other Value) Value

Mul multiplies the value of another CoinValue with the current CoinValue.

It takes a Value as a parameter and returns a Value. The function checks if the current CoinValue and the other Value have the same coin name. If they don't, it panics with the message "cannot multiply values of different coins". If they are the same, it creates a new CoinValue with the same definition and multiplies the units of the other Value with the current CoinValue's value. The function returns the new CoinValue.

Parameters: - other: the Value to multiply with.

Returns: - Value: the new CoinValue after the multiplication.

func (*CoinValue[D]) MulScalar

func (v *CoinValue[D]) MulScalar(scalar *big.Int) Value

MulScalar multiplies the value of a CoinValue by a scalar value.

It takes a pointer to a big.Int as a parameter and returns a Value. The function creates a new CoinValue with the same definition and multiplies the units of the current CoinValue's value by the scalar value. The function returns the new CoinValue.

Parameters: - scalar: a pointer to a big.Int representing the scalar value to multiply with.

Returns: - Value: the new CoinValue after the multiplication.

func (CoinValue[D]) Same

func (v CoinValue[D]) Same(other Value) bool

Same checks if the CoinValue is the same as another Value by comparing their coin names.

Parameters: - other: the Value to compare with.

Returns: - bool: true if the coin names are the same, false otherwise.

func (CoinValue[D]) ScaledValue

func (v CoinValue[D]) ScaledValue(exp int32) decimal.Decimal

ScaledValue returns the value of the CoinValue in decimal form, scaled by the given exponent.

For example for Ethereum the exponent value 9 would return the value denomitated in Gwei.

Parameters: - exp: the exponent to scale the value by.

Returns: - decimal.Decimal: the scaled value of the CoinValue.

func (*CoinValue[D]) Sub

func (v *CoinValue[D]) Sub(other Value) Value

Sub subtracts the value of another CoinValue from the current CoinValue.

It takes a Value as a parameter and returns a Value. The function checks if the current CoinValue and the other Value have the same coin name. If they don't, it panics with the message "cannot subtract values of different coins". If they are the same, it creates a new CoinValue with the same definition and subtracts the units of the other Value from the current CoinValue's value. The function returns the new CoinValue.

Parameters: - other: the Value to substract with.

Returns: - Value: the new CoinValue after the subtraction.

func (CoinValue[D]) Units

func (v CoinValue[D]) Units() *big.Int

Units returns the value of the CoinValue in the smallest unit.

For example for Ethereum this would return the value denominated in wei.

Returns: - *big.Int: the value of the CoinValue in the smallest unit..

type Value

type Value interface {
	Units() *big.Int
	Coins() decimal.Decimal
	ScaledValue(exp int32) decimal.Decimal

	CoinName() string

	Same(other Value) bool

	Add(other Value) Value
	Sub(other Value) Value
	Mul(other Value) Value
	Div(other Value) Value

	MulScalar(scalar *big.Int) Value
	DivScalar(scalar *big.Int) Value
}

type ValueDefinition

type ValueDefinition interface {
	// returns the name of the coin
	// To be used to determine if we are talking about the same coin
	CoinName() string

	// returns the exp of the number of units in one coint
	// e.g. for Ethereum, 10^18 wei per Eth, so this should return 18
	UnitExp() int32
}

Jump to

Keyboard shortcuts

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