coins

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package coins is a generated protocol buffer package.

It is generated from these files:

x/coins/codec.proto

It has these top-level messages:

Coin
Set
SendMsg
FeeInfo

Index

Constants

View Source
const (
	CodeInvalidCurrency   uint32 = 30
	CodeInvalidCoin              = 31
	CodeInsufficientFees         = 32
	CodeInsufficientFunds        = 33
	CodeInvalidAmount            = 34
	CodeInvalidMemo              = 35
	CodeEmptyAccount             = 36
)

ABCI Response Codes x/coins reserves 30 ~ 39.

Variables

View Source
var (
	ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowCodec   = fmt.Errorf("proto: integer overflow")
)

Functions

func ErrEmptyAccount

func ErrEmptyAccount(addr []byte) error

func ErrInsufficientFees

func ErrInsufficientFees(coin Coin) error

func ErrInsufficientFunds

func ErrInsufficientFunds() error

func ErrInvalidAmount

func ErrInvalidAmount(reason string) error

func ErrInvalidCurrency

func ErrInvalidCurrency(cur string, other ...string) error

ErrInvalidCurrency takes one or two currencies that are not proper

func ErrInvalidMemo

func ErrInvalidMemo(reason string) error

func ErrInvalidWallet

func ErrInvalidWallet(msg string) error

func ErrMismatchedSign

func ErrMismatchedSign(coin Coin) error

func ErrOutOfRange

func ErrOutOfRange(coin Coin) error

func IsEmpty

func IsEmpty(c *Coin) bool

IsEmpty returns true on null or zero amount

func IsEmptyAccountErr

func IsEmptyAccountErr(err error) bool

func IsInsufficientFeesErr

func IsInsufficientFeesErr(err error) bool

func IsInsufficientFundsErr

func IsInsufficientFundsErr(err error) bool

func IsInvalidAmountErr

func IsInvalidAmountErr(err error) bool

func IsInvalidCoinErr

func IsInvalidCoinErr(err error) bool

func IsInvalidCurrencyErr

func IsInvalidCurrencyErr(err error) bool

func IsInvalidMemoErr

func IsInvalidMemoErr(err error) bool

func IssueCoins

func IssueCoins(store weave.KVStore, dest weave.Address,
	amount Coin) error

IssueCoins attempts to add the given amount of coins to the destination address. Fails if it overflows the wallet.

Note the amount may also be negative: "the lord giveth and the lord taketh away"

func MoveCoins

func MoveCoins(store weave.KVStore, src weave.Address,
	dest weave.Address, amount Coin) error

MoveCoins moves the given amount from src to dest. If src doesn't exist, or doesn't have sufficient coins, it fails.

func RegisterRoutes

func RegisterRoutes(r weave.Registry, auth weave.AuthFunc)

RegisterRoutes will instantiate and register all handlers in this package

Types

type Coin

type Coin struct {
	// Whole coins, -10^9 < integer < 10^9
	Integer int32 `protobuf:"varint,1,opt,name=integer,proto3" json:"integer,omitempty"`
	// Billionth of coins. 0 <= abs(fractional) < 10^9
	// If fractional != 0, must have same sign as integer
	Fractional int32 `protobuf:"varint,2,opt,name=fractional,proto3" json:"fractional,omitempty"`
	// CurrencyCode is 3-4 upper-case letters and
	// all Coins of the same currency can be combined
	CurrencyCode string `protobuf:"bytes,3,opt,name=currency_code,json=currencyCode,proto3" json:"currency_code,omitempty"`
	// Issuer is optional string, maybe chain_id? maybe custodian name?
	// can be empty. tokens are only fungible if CurrencyCode and
	// Issuer both match.
	Issuer string `protobuf:"bytes,4,opt,name=issuer,proto3" json:"issuer,omitempty"`
}

Coin can hold any amount between -1 billion and +1 billion at steps of 10^-9. It is a fixed-point decimal representation and uses integers to avoid rounding associated with floats.

Every code has a denomination, which is just a

If you want anything more complex, you should write your own type, possibly borrowing from this code.

func NewCoin

func NewCoin(integer int32, fractional int32,
	currencyCode string) Coin

NewCoin creates a new coin object

func (Coin) Add

func (c Coin) Add(o Coin) (Coin, error)

Add combines two coins. Returns error if they are of different currencies, or if the combination would cause an overflow

To subtract:

c.Add(o.Negative())

func (*Coin) Clone

func (c *Coin) Clone() *Coin

Clone provides an independent copy of a coin pointer

func (Coin) Compare

func (c Coin) Compare(o Coin) int

Compare will check values of two coins, without inspecting the currency code. It is up to the caller to determine if they want to check this. It also assumes they were already normalized.

Returns 1 if c is larger, -1 if o is larger, 0 if equal

func (*Coin) Descriptor

func (*Coin) Descriptor() ([]byte, []int)

func (Coin) Equals

func (c Coin) Equals(o Coin) bool

Equals returns true if all fields are identical

func (*Coin) GetCurrencyCode

func (m *Coin) GetCurrencyCode() string

func (*Coin) GetFractional

func (m *Coin) GetFractional() int32

func (*Coin) GetInteger

func (m *Coin) GetInteger() int32

func (*Coin) GetIssuer

func (m *Coin) GetIssuer() string

func (Coin) ID

func (c Coin) ID() string

ID returns a unique identifier. If issuer is empty, then just the CurrencyCode. If issuer is present, then <Issuer>/<CurrencyCode>

func (Coin) IsGTE

func (c Coin) IsGTE(o Coin) bool

IsGTE returns true if c is same type and at least as large as o. It assumes they were already normalized.

func (Coin) IsNonNegative

func (c Coin) IsNonNegative() bool

IsNonNegative returns true if the value is 0 or higher

func (Coin) IsPositive

func (c Coin) IsPositive() bool

IsPositive returns true if the value is greater than 0

func (Coin) IsZero

func (c Coin) IsZero() bool

IsZero returns true amounts are 0

func (*Coin) Marshal

func (m *Coin) Marshal() (dAtA []byte, err error)

func (*Coin) MarshalTo

func (m *Coin) MarshalTo(dAtA []byte) (int, error)

func (Coin) Negative

func (c Coin) Negative() Coin

Negative returns the opposite coins value

c.Add(c.Negative()).IsZero() == true

func (*Coin) ProtoMessage

func (*Coin) ProtoMessage()

func (*Coin) Reset

func (m *Coin) Reset()

func (Coin) SameType

func (c Coin) SameType(o Coin) bool

SameType returns true if they have the same currency

func (*Coin) Size

func (m *Coin) Size() (n int)

func (*Coin) String

func (m *Coin) String() string

func (*Coin) Unmarshal

func (m *Coin) Unmarshal(dAtA []byte) error

func (Coin) Validate

func (c Coin) Validate() error

Validate ensures that the coin is in the valid range and valid currency code. It accepts negative values, so you may want to make other checks in your business logic

func (Coin) WithIssuer

func (c Coin) WithIssuer(issuer string) Coin

WithIssuer sets the Issuer on a coin. Returns new coin, so this can be chained on constructor

type FeeDecorator

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

FeeDecorator ensures that the fee can be deducted from the account. All deducted fees are send to the collector, which can be set to an address controlled by another extension ("smart contract").

If minFee is zero, no fees required, but will speed processing. If a currency is set on minFee, then all fees must be paid in that currency

It uses auth to verify the sender

func NewFeeDecorator

func NewFeeDecorator(auth weave.AuthFunc, min Coin) FeeDecorator

NewFeeDecorator returns a FeeDecorator with the given minimum fee, and all collected fees going to a default address.

func (FeeDecorator) Check

func (d FeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx,
	next weave.Checker) (weave.CheckResult, error)

Check verifies and deducts fees before calling down the stack

func (FeeDecorator) Deliver

func (d FeeDecorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx,
	next weave.Deliverer) (weave.DeliverResult, error)

Deliver verifies and deducts fees before calling down the stack

func (FeeDecorator) WithCollector

func (d FeeDecorator) WithCollector(addr weave.Address) FeeDecorator

WithCollector allows you to set the collector in app setup

type FeeInfo

type FeeInfo struct {
	Payer []byte `protobuf:"bytes,1,opt,name=payer,proto3" json:"payer,omitempty"`
	Fees  *Coin  `protobuf:"bytes,2,opt,name=fees" json:"fees,omitempty"`
}

FeeInfo records who pays what fees to have this message processed

func (*FeeInfo) DefaultPayer

func (f *FeeInfo) DefaultPayer(addr []byte) *FeeInfo

DefaultPayer makes sure there is a payer. If it was already set, returns f. If none was set, returns a new FeeInfo, with the New address set

func (*FeeInfo) Descriptor

func (*FeeInfo) Descriptor() ([]byte, []int)

func (*FeeInfo) GetFees

func (m *FeeInfo) GetFees() *Coin

func (*FeeInfo) GetPayer

func (m *FeeInfo) GetPayer() []byte

func (*FeeInfo) Marshal

func (m *FeeInfo) Marshal() (dAtA []byte, err error)

func (*FeeInfo) MarshalTo

func (m *FeeInfo) MarshalTo(dAtA []byte) (int, error)

func (*FeeInfo) ProtoMessage

func (*FeeInfo) ProtoMessage()

func (*FeeInfo) Reset

func (m *FeeInfo) Reset()

func (*FeeInfo) Size

func (m *FeeInfo) Size() (n int)

func (*FeeInfo) String

func (m *FeeInfo) String() string

func (*FeeInfo) Unmarshal

func (m *FeeInfo) Unmarshal(dAtA []byte) error

func (*FeeInfo) Validate

func (f *FeeInfo) Validate() error

Validate makes sure that this is sensible. Note that fee must be present, even if 0

type FeeTx

type FeeTx interface {
	GetFees() *FeeInfo
}

FeeTx exposes information about the fees that should be paid

type GenesisAccount

type GenesisAccount struct {
	Address weave.Address `json:"address"`
	Set
}

GenesisAccount is used to parse the json from genesis file use weave.Address, so address in hex, not base64

type Initializer

type Initializer struct{}

Initializer fulfils the InitStater interface to load data from the genesis file

func (Initializer) FromGenesis

func (Initializer) FromGenesis(opts weave.Options, kv weave.KVStore) error

FromGenesis will parse initial account info from genesis and save it to the database

type Key

type Key []byte

Key is the primary key we use to distinguish users This should be []byte, in order to index with our KVStore. Any structure to these bytes should be defined by the constructor.

Question: allow objects with a Marshal method???

func NewKey

func NewKey(addr weave.Address) Key

NewKey constructs the coin key from a key hash, by appending a prefix.

type SendHandler

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

SendHandler will handle sending coins

func NewSendHandler

func NewSendHandler(auth weave.AuthFunc) SendHandler

NewSendHandler creates a handler for SendMsg

func (SendHandler) Check

func (h SendHandler) Check(ctx weave.Context, store weave.KVStore,
	tx weave.Tx) (weave.CheckResult, error)

Check just verifies it is properly formed and returns the cost of executing it

func (SendHandler) Deliver

func (h SendHandler) Deliver(ctx weave.Context, store weave.KVStore,
	tx weave.Tx) (weave.DeliverResult, error)

Deliver moves the tokens from sender to receiver if all preconditions are met

type SendMsg

type SendMsg struct {
	Src    []byte `protobuf:"bytes,1,opt,name=src,proto3" json:"src,omitempty"`
	Dest   []byte `protobuf:"bytes,2,opt,name=dest,proto3" json:"dest,omitempty"`
	Amount *Coin  `protobuf:"bytes,3,opt,name=amount" json:"amount,omitempty"`
	// max length 128 character
	Memo string `protobuf:"bytes,4,opt,name=memo,proto3" json:"memo,omitempty"`
	// max length 64 bytes
	Ref []byte `protobuf:"bytes,5,opt,name=ref,proto3" json:"ref,omitempty"`
}

SendMsg is a request to move these coins from the given source to the given destination address. memo is an optional human-readable message ref is optional binary data, that can refer to another eg. tx hash

func (*SendMsg) DefaultSource

func (s *SendMsg) DefaultSource(addr []byte) *SendMsg

DefaultSource makes sure there is a payer. If it was already set, returns s. If none was set, returns a new SendMsg with the source set

func (*SendMsg) Descriptor

func (*SendMsg) Descriptor() ([]byte, []int)

func (*SendMsg) GetAmount

func (m *SendMsg) GetAmount() *Coin

func (*SendMsg) GetDest

func (m *SendMsg) GetDest() []byte

func (*SendMsg) GetMemo

func (m *SendMsg) GetMemo() string

func (*SendMsg) GetRef

func (m *SendMsg) GetRef() []byte

func (*SendMsg) GetSrc

func (m *SendMsg) GetSrc() []byte

func (*SendMsg) Marshal

func (m *SendMsg) Marshal() (dAtA []byte, err error)

func (*SendMsg) MarshalTo

func (m *SendMsg) MarshalTo(dAtA []byte) (int, error)

func (SendMsg) Path

func (SendMsg) Path() string

Path returns the routing path for this message

func (*SendMsg) ProtoMessage

func (*SendMsg) ProtoMessage()

func (*SendMsg) Reset

func (m *SendMsg) Reset()

func (*SendMsg) Size

func (m *SendMsg) Size() (n int)

func (*SendMsg) String

func (m *SendMsg) String() string

func (*SendMsg) Unmarshal

func (m *SendMsg) Unmarshal(dAtA []byte) error

func (*SendMsg) Validate

func (s *SendMsg) Validate() error

Validate makes sure that this is sensible

type Set

type Set struct {
	Coins []*Coin `protobuf:"bytes,1,rep,name=coins" json:"coins,omitempty"`
}

Set may contain Coin of many different currencies. It handles adding and subtracting sets of currencies.

func NewSet

func NewSet(cs ...Coin) (Set, error)

NewSet creates a Set containing all given coins. It will sort them and combine duplicates to produce a normalized form regardless of input.

func (*Set) Add

func (s *Set) Add(c Coin) error

Add modifies the set, to increase the holdings by c

func (Set) Clone

func (s Set) Clone() Set

Clone returns a copy that can be safely modified

func (Set) Combine

func (s Set) Combine(o Set) (Set, error)

Combine will create a new Set adding all the coins of s and o together.

func (Set) Contains

func (s Set) Contains(c Coin) bool

Contains returns true if there is at least that much coin in the Set. If it returns true, then:

s.Remove(c).IsNonNegative() == true

func (Set) Count

func (s Set) Count() int

Count returns the number of unique currencies in the Set

func (*Set) Descriptor

func (*Set) Descriptor() ([]byte, []int)

func (Set) Equals

func (s Set) Equals(o Set) bool

Equals returns true if both sets contain same coins

func (*Set) GetCoins

func (m *Set) GetCoins() []*Coin

func (Set) IsEmpty

func (s Set) IsEmpty() bool

IsEmpty returns if nothing is in the set

func (Set) IsNonNegative

func (s Set) IsNonNegative() bool

IsNonNegative returns true if all coins are positive, but also accepts an empty set

func (Set) IsPositive

func (s Set) IsPositive() bool

IsPositive returns true there is at least one coin and all coins are positive

func (*Set) Marshal

func (m *Set) Marshal() (dAtA []byte, err error)

func (*Set) MarshalTo

func (m *Set) MarshalTo(dAtA []byte) (int, error)

func (*Set) ProtoMessage

func (*Set) ProtoMessage()

func (*Set) Reset

func (m *Set) Reset()

func (*Set) Size

func (m *Set) Size() (n int)

func (*Set) String

func (m *Set) String() string

func (*Set) Subtract

func (s *Set) Subtract(c Coin) error

Subtract modifies the set, to decrease the holdings by c. The resulting set may have negative amounts

func (*Set) Unmarshal

func (m *Set) Unmarshal(dAtA []byte) error

func (Set) Validate

func (s Set) Validate() error

Validate requires that all coins are in alphabetical order and that each coin is valid in it's own right

Zero amounts should not be present

type Wallet

type Wallet struct {
	Set
	// contains filtered or unexported fields
}

Wallet is the actual object that we want to pass around in our code. It contains a set of coins, as well as logic to handle loading and saving the data to/from the persistent store, and helpers to manipulate state.

func GetOrCreateWallet

func GetOrCreateWallet(store weave.KVStore, key Key) *Wallet

GetOrCreateWallet loads this Wallet if present, or initializes a new Wallet with this key if not present.

func GetWallet

func GetWallet(store weave.KVStore, key Key) *Wallet

GetWallet loads this Wallet if present, or returns nil if missing

func (*Wallet) Save

func (u *Wallet) Save()

Save writes the current Wallet state to the backing store panics if invalid state

Jump to

Keyboard shortcuts

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