balance

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2019 License: Apache-2.0 Imports: 16 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")
)

Functions

This section is empty.

Types

type Balance

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

Wrap the amount with owner information

func GetBalanceFromDb

func GetBalanceFromDb(db storage.Store, accountKey storage.StoreKey) (*Balance, error)

GetBalanceFromDb takes a datastore with GetSetter interface and initializes a new Balance from the data.

func NewBalance

func NewBalance() *Balance

balance Generators start here

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 for balance start here

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

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) Primitive

func (bd *BalanceData) Primitive() serialize.DataAdapter

Primitive gives the source object of the data through the adapter interface. Useful if you want to access the source object after deserialization.

func (*BalanceData) SerialTag

func (bd *BalanceData) SerialTag() string

type Coin

type Coin struct {
	Currency Currency `json:"currency"`
	Amount   *big.Int `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) 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

Minus two coins

func (Coin) Multiply

func (coin Coin) Multiply(value Coin) Coin

Multiply one coin by another

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

Plus two coins

func (Coin) Quotient

func (coin Coin) Quotient(value Coin) Coin

Quotient of one coin by another (divide without remainder, modulus, etc)

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 {
	CurName    string     `json:"curr_name"`
	CurChain   chain.Type `json:"curr_chain"`
	CurDecimal int64      `json:"curr_decimal"`

	Amount []byte `json:"amt"`
}

CoinData is a flattening of coin map in a balance data type

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 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) 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) 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 []byte) 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 []byte, balance Balance) error

Jump to

Keyboard shortcuts

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