balance

package
v0.10.9 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2019 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TagBalanceData = "data_balance_data"
	TagBalance     = "data_balance"
)

Variables

View Source
var (
	ErrWrongBalanceAdapter = errors.New("error in asserting to BalanceAdapter")
	ErrDuplicateCurrency   = errors.New("provided currency has already been registered")
	ErrMismatchingCurrency = errors.New("mismatching currencies")

	ErrInsufficientBalance = errors.New("insufficient balance")
)
View Source
var ErrNoBalanceFoundForThisAddress = errors.New("no balance found for the address")

Functions

func PrintDecimal added in v0.10.8

func PrintDecimal(i *big.Int, decimal int) string

Types

type Amount added in v0.10.8

type Amount struct {
	Int big.Int `json:"bint"`
}

Amount represents an amount of a currency

func NewAmount added in v0.10.8

func NewAmount(x int64) *Amount

func NewAmountFromInt added in v0.10.8

func NewAmountFromInt(x int64) *Amount

func NewAmountFromString added in v0.10.8

func NewAmountFromString(x string, base int) (*Amount, error)

NewAmountFromString parses the amount as a string with the given base. For example, if base is 10, then it expects the given string to be base-10 notation. If the base is 16, then it expects the string in hexadecimal notation. If the base is set to 16, it ignores any "0x" prefix if present.

func (Amount) MarshalJSON added in v0.10.8

func (a Amount) MarshalJSON() ([]byte, error)

func (Amount) MarshalText added in v0.10.8

func (a Amount) MarshalText() ([]byte, error)

func (*Amount) UnmarshalJSON added in v0.10.8

func (a *Amount) UnmarshalJSON(b []byte) error

func (*Amount) UnmarshalText added in v0.10.8

func (a *Amount) UnmarshalText(b []byte) error

type Balance

type Balance struct {
	Amounts map[string]Coin `json:"amounts"`
}

Wrap the amount with owner information

func NewBalance

func NewBalance() *Balance

Generators

func (*Balance) AddCoin

func (b *Balance) AddCoin(coin Coin) *Balance

Add a new or existing coin

func (*Balance) Data

func (b *Balance) Data() serialize.Data

Data creates a BalanceData from a given Balance object, the coins are flattened to a list in the generator itself ideally there should be no change done to a data after this step. This datatype can go straight to serialization.

func (*Balance) FindCoin

func (b *Balance) FindCoin(currency Currency) *Coin

methods

func (*Balance) GetCoin

func (b *Balance) GetCoin(currency Currency) Coin

func (Balance) IsEnoughBalance

func (b Balance) IsEnoughBalance(balance Balance) bool

func (*Balance) MinusCoin

func (b *Balance) MinusCoin(coin Coin) (*Balance, error)

func (*Balance) NewDataInstance

func (b *Balance) NewDataInstance() serialize.Data

func (*Balance) SetData

func (b *Balance) SetData(obj interface{}) error

SetData sets the balance object back from a BalanceData object

func (Balance) String

func (b Balance) String() string

String method used in fmt and Dump

type BalanceData

type BalanceData struct {
	Coins []*CoinData `json:"coins"`
	Tag   string      `json:"tag"` // Tag is a field used to identify the type after ser/deser

}

BalanceData is an easy to serialize representation of a Balance object. A full Balance object can be recostructed from a BalanceAdapter object and vice versa. There is a map flattening of course for Coins

func (*BalanceData) SerialTag

func (bd *BalanceData) SerialTag() string

type Coin

type Coin struct {
	Currency Currency `json:"currency"`
	Amount   *Amount  `json:"amount,string"`
}
Coin starts here

Coin is the basic amount, specified in integers, at the smallest increment (i.e. a satoshi, not a bitcoin)

func (*Coin) Data

func (c *Coin) Data() serialize.Data

func (Coin) Divide

func (coin Coin) Divide(value int) Coin

func (Coin) Equals

func (coin Coin) Equals(value Coin) bool

Equals another coin

func (Coin) Humanize added in v0.10.8

func (coin Coin) Humanize() string

func (Coin) IsCurrency

func (coin Coin) IsCurrency(currencies ...string) bool

See if the coin is one of a list of currencies

func (Coin) IsValid

func (coin Coin) IsValid() bool

IsValid coin or is it broken

func (Coin) LessThanCoin

func (coin Coin) LessThanCoin(value Coin) bool

LessThan, for coins...

func (Coin) LessThanEqualCoin

func (coin Coin) LessThanEqualCoin(value Coin) bool

LessThanEqual, for coins...

func (Coin) Minus

func (coin Coin) Minus(value Coin) (Coin, error)

Minus two coins

func (Coin) MultiplyInt

func (coin Coin) MultiplyInt(value int) Coin

Multiply one coin by another

func (*Coin) NewDataInstance

func (c *Coin) NewDataInstance() serialize.Data

func (Coin) Plus

func (coin Coin) Plus(value Coin) (Coin, error)

Plus two coins

func (*Coin) SetData

func (c *Coin) SetData(a interface{}) error

func (Coin) String

func (coin Coin) String() string

Turn a coin into a readable, floating point string with the currency

type CoinData

type CoinData struct {
	Currency Currency `json:"currency"`
	Amount   []byte   `json:"amount"`
}

func (*CoinData) SerialTag added in v0.10.8

func (ad *CoinData) SerialTag() string

type Context

type Context struct {
	// contains filtered or unexported fields
}

func NewContext

func NewContext(logger *log.Logger, balances *Store, currencies *CurrencyList) *Context

func (*Context) Currencies

func (ctx *Context) Currencies() *CurrencyList

func (*Context) Store

func (ctx *Context) Store() *Store

type Currencies added in v0.10.8

type Currencies []Currency

func (Currencies) GetCurrencyList added in v0.10.8

func (cs Currencies) GetCurrencyList() *CurrencyList

type Currency

type Currency struct {
	Name  string     `json:"name"`
	Chain chain.Type `json:"chain"`

	Decimal int64 `json:"decimal"`
}

func (Currency) Base

func (c Currency) Base() *big.Int

func (Currency) Bytes

func (c Currency) Bytes() []byte

func (Currency) NewCoinFromAmount added in v0.10.8

func (c Currency) NewCoinFromAmount(a Amount) Coin

func (Currency) NewCoinFromBytes

func (c Currency) NewCoinFromBytes(amount []byte) Coin

Create a coin from bytes, the bytes must come from Big.Int.

func (Currency) NewCoinFromFloat64

func (c Currency) NewCoinFromFloat64(amount float64) Coin

TODO Create a coin from float

func (Currency) NewCoinFromInt

func (c Currency) NewCoinFromInt(amount int64) Coin

Create a coin from integer (not fractional)

func (Currency) StringKey

func (c Currency) StringKey() string

type CurrencyList

type CurrencyList struct {
	// contains filtered or unexported fields
}

func NewCurrencyList

func NewCurrencyList() *CurrencyList

func (CurrencyList) GetCurrencies added in v0.10.8

func (c CurrencyList) GetCurrencies() Currencies

func (*CurrencyList) GetCurrencyByName

func (cl *CurrencyList) GetCurrencyByName(name string) (Currency, bool)

func (*CurrencyList) GetCurrencyByStringKey

func (cl *CurrencyList) GetCurrencyByStringKey(key string) (Currency, bool)

func (CurrencyList) Len

func (cl CurrencyList) Len() int

func (*CurrencyList) Register

func (cl *CurrencyList) Register(c Currency) error

type Store

type Store struct {
	*storage.ChainState
}

func NewStore

func NewStore(name, dbDir, configDB string, typ storage.StorageType) *Store

func (*Store) Exists

func (st *Store) Exists(address keys.Address) bool

func (*Store) FindAll

func (st *Store) FindAll() map[string]*Balance

func (*Store) Get

func (st *Store) Get(address []byte, lastCommit bool) (bal *Balance, err error)

func (*Store) Set

func (st *Store) Set(address keys.Address, balance Balance) error

Jump to

Keyboard shortcuts

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