model

package
v0.0.0-...-08046e1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2018 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AssetMinScale is the minimal value for an asset scale.
	AssetMinScale int8 = 0
	// AssetMaxScale is the minimal value for an asset scale.
	AssetMaxScale int8 = 24
)

Variables

View Source
var AssetCodeRegexp = regexp.MustCompile("^[A-Z0-9\\-]{1,64}$")

AssetCodeRegexp is used to validate asset codes at creation.

View Source
var MaxAssetAmount = new(big.Int).Exp(
	new(big.Int).SetInt64(2), new(big.Int).SetInt64(128), nil)

MaxAssetAmount is the maximum amount for an asset (2^128).

Functions

func NewAssetResource

func NewAssetResource(
	ctx context.Context,
	asset *Asset,
) mint.AssetResource

NewAssetResource generates a new resource.

func NewBalanceResource

func NewBalanceResource(
	ctx context.Context,
	balance *Balance,
) mint.BalanceResource

NewBalanceResource generates a new resource.

func NewCrossingResource

func NewCrossingResource(
	ctx context.Context,
	crossing *Crossing,
) mint.CrossingResource

NewCrossingResource generates a new resource.

func NewOfferResource

func NewOfferResource(
	ctx context.Context,
	offer *Offer,
) mint.OfferResource

NewOfferResource generates a new resource.

func NewOperationResource

func NewOperationResource(
	ctx context.Context,
	operation *Operation,
) mint.OperationResource

NewOperationResource generates a new resource.

func NewTransactionResource

func NewTransactionResource(
	ctx context.Context,
	transaction *Transaction,
	operations []*Operation,
	crossings []*Crossing,
) mint.TransactionResource

NewTransactionResource generates a new resource.

Types

type Amount

type Amount big.Int

Amount extends big.Int to implement sql.Scanner and driver.Valuer.

func (*Amount) Scan

func (b *Amount) Scan(src interface{}) error

Scan implements sql.Scanner.

func (Amount) Value

func (b Amount) Value() (value driver.Value, err error)

Value implements driver.Valuer.

type Asset

type Asset struct {
	Owner       string
	Token       string
	Created     time.Time
	Propagation mint.PgType

	Code  string // Asset code.
	Scale int8   // Asset scale.
}

Asset represents an asset object. Asset are created by users (issuer).

func CreateCanonicalAsset

func CreateCanonicalAsset(
	ctx context.Context,
	owner string,
	code string,
	scale int8,
) (*Asset, error)

CreateCanonicalAsset creates and stores a new Asset object.

func LoadAssetListByOwner

func LoadAssetListByOwner(
	ctx context.Context,
	createdBefore time.Time,
	limit uint,
	owner string,
) ([]Asset, error)

LoadAssetListByOwner loads an asset list by owner.

func LoadCanonicalAssetByName

func LoadCanonicalAssetByName(
	ctx context.Context,
	name string,
) (*Asset, error)

LoadCanonicalAssetByName attempts to load an asset by its name.

func LoadCanonicalAssetByOwnerCodeScale

func LoadCanonicalAssetByOwnerCodeScale(
	ctx context.Context,
	owner string,
	code string,
	scale int8,
) (*Asset, error)

LoadCanonicalAssetByOwnerCodeScale attempts to load an asset by its owner address, code and scale.

type Balance

type Balance struct {
	Owner       string
	Token       string
	Created     time.Time
	Propagation mint.PgType

	Asset  string // Asset name.
	Holder string // Holder address.
	Value  Amount
}

Balance represents a user balance for a given asset. Balances are updated as operations are created and then propagated.

  • Canonical balances are stored on the mint of the balance's asset owner (which acts as source of truth on its state).
  • Propagated balances are indicatively stored on the mints of the balances's holders.

func CreateCanonicalBalance

func CreateCanonicalBalance(
	ctx context.Context,
	owner string,
	asset string,
	holder string,
	value Amount,
) (*Balance, error)

CreateCanonicalBalance creates and store a new Balance object. Only one balance can exist for an asset, holder pair.

func CreatePropagatedBalance

func CreatePropagatedBalance(
	ctx context.Context,
	owner string,
	token string,
	created time.Time,
	asset string,
	holder string,
	value Amount,
) (*Balance, error)

CreatePropagatedBalance creates and store a new propagated Balance object.

func LoadBalanceListByAsset

func LoadBalanceListByAsset(
	ctx context.Context,
	createdBefore time.Time,
	limit uint,
	asset string,
) ([]Balance, error)

LoadBalanceListByAsset loads a balance list by asset.

func LoadBalanceListByHolder

func LoadBalanceListByHolder(
	ctx context.Context,
	createdBefore time.Time,
	limit uint,
	holder string,
) ([]Balance, error)

LoadBalanceListByHolder loads a balance list by holder.

func LoadCanonicalBalanceByAssetHolder

func LoadCanonicalBalanceByAssetHolder(
	ctx context.Context,
	asset string,
	holder string,
) (*Balance, error)

LoadCanonicalBalanceByAssetHolder attempts to load a balance for the given holder address and asset name.

func LoadCanonicalBalanceByID

func LoadCanonicalBalanceByID(
	ctx context.Context,
	id string,
) (*Balance, error)

LoadCanonicalBalanceByID attempts to load the canonical balanceffer for the given id.

func LoadCanonicalBalanceByOwnerToken

func LoadCanonicalBalanceByOwnerToken(
	ctx context.Context,
	owner string,
	token string,
) (*Balance, error)

LoadCanonicalBalanceByOwnerToken attempts to load a canonical balance for the given owner and token.

func LoadOrCreateCanonicalBalanceByAssetHolder

func LoadOrCreateCanonicalBalanceByAssetHolder(
	ctx context.Context,
	owner string,
	asset string,
	holder string,
) (*Balance, error)

LoadOrCreateCanonicalBalanceByAssetHolder loads an existing balance for the specified asset and holder or creates one (with a 0 value) if it does not exist.

func LoadPropagatedBalanceByOwnerToken

func LoadPropagatedBalanceByOwnerToken(
	ctx context.Context,
	owner string,
	token string,
) (*Balance, error)

LoadPropagatedBalanceByOwnerToken attempts to load a propagated balance for the given owner and token.

func (*Balance) ID

func (b *Balance) ID() string

ID returns the ID of the object.

func (*Balance) Save

func (b *Balance) Save(
	ctx context.Context,
) error

Save updates the object database representation with the in-memory values.

type Crossing

type Crossing struct {
	Owner       string
	Token       string
	Created     time.Time
	Propagation mint.PgType

	Offer  string
	Amount Amount

	Status      mint.TxStatus
	Transaction string `db:"txn"`
	Hop         int8   `db:"hop"`
}

Crossing represents a transaction crossing an offer and consuming some of its amount. Crossings are not propagated.

func CreateCanonicalCrossing

func CreateCanonicalCrossing(
	ctx context.Context,
	owner string,
	offer string,
	amount Amount,
	status mint.TxStatus,
	transaction string,
	hop int8,
) (*Crossing, error)

CreateCanonicalCrossing creates and stores a new Crossing object.

func LoadCanonicalCrossingByTransactionHop

func LoadCanonicalCrossingByTransactionHop(
	ctx context.Context,
	transaction string,
	hop int8,
) (*Crossing, error)

LoadCanonicalCrossingByTransactionHop attempts to load the crossing for the given transaction and hop.

func LoadCanonicalCrossingsByTransaction

func LoadCanonicalCrossingsByTransaction(
	ctx context.Context,
	transaction string,
) ([]*Crossing, error)

LoadCanonicalCrossingsByTransaction loads all crossings that are associated with the specified transaction.

func (*Crossing) ID

func (c *Crossing) ID() string

ID returns the ID of the object.

func (*Crossing) Save

func (c *Crossing) Save(
	ctx context.Context,
) error

Save updates the object database representation with the in-memory values.

type ErrUniqueConstraintViolation

type ErrUniqueConstraintViolation struct {
	Err error
}

ErrUniqueConstraintViolation is returned when a object insertion violates a unique constraint.

func (ErrUniqueConstraintViolation) Error

type OfPath

type OfPath []string

OfPath is an offer path and implemets sql.Scanner and dirver.Valuer for easy serialization.

func (*OfPath) Scan

func (p *OfPath) Scan(src interface{}) error

Scan implements sql.Scanner.

func (OfPath) Value

func (p OfPath) Value() (value driver.Value, err error)

Value implements driver.Valuer.

type Offer

type Offer struct {
	Owner       string
	Token       string
	Created     time.Time
	Propagation mint.PgType

	BaseAsset  string `db:"base_asset"`  // BaseAsset name.
	QuoteAsset string `db:"quote_asset"` // QuoteAsset name.
	BasePrice  Amount `db:"base_price"`
	QuotePrice Amount `db:"quote_price"`
	Amount     Amount

	Status    mint.OfStatus
	Remainder Amount
}

Offer represents an offer for an asset pair.

  • Offers are always represented as asks (ask on pair A/B offer to sell A (base asset) for B (quote asset)). Amounts are expressed in quote asset.
  • Canonical offers are stored on the mint of the offer's owner (which acts as source of truth on its state).
  • Propagated offers are indicatively stored on the mints of the offers's assets, to compute order books.

func CreateCanonicalOffer

func CreateCanonicalOffer(
	ctx context.Context,
	owner string,
	baseAsset string,
	quoteAsset string,
	basePrice Amount,
	quotePrice Amount,
	amount Amount,
	status mint.OfStatus,
	remainder Amount,
) (*Offer, error)

CreateCanonicalOffer creates and stores a new canonical Offer object.

func CreatePropagatedOffer

func CreatePropagatedOffer(
	ctx context.Context,
	owner string,
	token string,
	created time.Time,
	baseAsset string,
	quoteAsset string,
	basePrice Amount,
	quotePrice Amount,
	amount Amount,
	status mint.OfStatus,
	remainder Amount,
) (*Offer, error)

CreatePropagatedOffer creates and stores a new propagated Offer object.

func LoadCanonicalOfferByID

func LoadCanonicalOfferByID(
	ctx context.Context,
	id string,
) (*Offer, error)

LoadCanonicalOfferByID attempts to load the canonical offer for the given id.

func LoadCanonicalOfferByOwnerToken

func LoadCanonicalOfferByOwnerToken(
	ctx context.Context,
	owner string,
	token string,
) (*Offer, error)

LoadCanonicalOfferByOwnerToken attempts to load the canonical offer for the given owner and token.

func LoadOfferListByBaseAsset

func LoadOfferListByBaseAsset(
	ctx context.Context,
	createdBefore time.Time,
	limit uint,
	asset string,
) ([]Offer, error)

LoadOfferListByBaseAsset loads a balance list by base asset.

func LoadOfferListByQuoteAsset

func LoadOfferListByQuoteAsset(
	ctx context.Context,
	createdBefore time.Time,
	limit uint,
	asset string,
) ([]Offer, error)

LoadOfferListByQuoteAsset loads a balance list by quote asset.

func LoadPropagatedOfferByID

func LoadPropagatedOfferByID(
	ctx context.Context,
	id string,
) (*Offer, error)

LoadPropagatedOfferByID attempts to load the propagated offer for the given id.

func LoadPropagatedOfferByOwnerToken

func LoadPropagatedOfferByOwnerToken(
	ctx context.Context,
	owner string,
	token string,
) (*Offer, error)

LoadPropagatedOfferByOwnerToken attempts to load the propagated offer for the given owner and token.

func (*Offer) ID

func (o *Offer) ID() string

ID returns the ID of the object.

func (*Offer) Save

func (o *Offer) Save(
	ctx context.Context,
) error

Save updates the object database representation with the in-memory values.

type Operation

type Operation struct {
	Owner       string // Owner address.
	Token       string
	Created     time.Time
	Propagation mint.PgType

	Asset       string // Asset name.
	Source      string // Source address (if owner, issuance).
	Destination string // Destination addres (if owner, annihilation).
	Amount      Amount

	Status      mint.TxStatus
	Transaction *string `db:"txn"`
	Hop         *int8   `db:"hop"`
}

Operation represents a movement of an asset. Asset owners cannot hold a balance in their own assets so operations referring to the asset owner are either issuing or annihilating the asset.

  • Canonical operations are stored on the mint of the operation's owner (which acts as source of truth on its state).
  • Propagated operations are stored on the mints of the operation's source or destination, for retrieval by impacted users (only settled operations are reserved).
  • When part of a transaction, an operation refers the transaction and hop.

func CreateCanonicalOperation

func CreateCanonicalOperation(
	ctx context.Context,
	owner string,
	asset string,
	source string,
	destination string,
	amount Amount,
	status mint.TxStatus,
	transaction *string,
	hop *int8,
) (*Operation, error)

CreateCanonicalOperation creates and stores a new Operation.

func CreatePropagatedOperation

func CreatePropagatedOperation(
	ctx context.Context,
	owner string,
	token string,
	created time.Time,
	asset string,
	source string,
	destination string,
	amount Amount,
	status mint.TxStatus,
	transaction *string,
	hop *int8,
) (*Operation, error)

CreatePropagatedOperation creates and stores a new Operation.

func LoadCanonicalOperationByID

func LoadCanonicalOperationByID(
	ctx context.Context,
	id string,
) (*Operation, error)

LoadCanonicalOperationByID attempts to load the canonical operation for the given id.

func LoadCanonicalOperationByOwnerToken

func LoadCanonicalOperationByOwnerToken(
	ctx context.Context,
	owner string,
	token string,
) (*Operation, error)

LoadCanonicalOperationByOwnerToken attempts to load the canonical operation for the given owner and token.

func LoadCanonicalOperationByTransactionHop

func LoadCanonicalOperationByTransactionHop(
	ctx context.Context,
	transaction string,
	hop int8,
) (*Operation, error)

LoadCanonicalOperationByTransactionHop attempts to load the canonical operation for the given transaction and hop.

func LoadCanonicalOperationsByTransaction

func LoadCanonicalOperationsByTransaction(
	ctx context.Context,
	transaction string,
) ([]*Operation, error)

LoadCanonicalOperationsByTransaction loads all operations that are associated with the specified transaction.

func LoadPropagatedOperationByOwnerToken

func LoadPropagatedOperationByOwnerToken(
	ctx context.Context,
	owner string,
	token string,
) (*Operation, error)

LoadPropagatedOperationByOwnerToken attempts to load the propagated operation for the given owner and token.

func (*Operation) ID

func (o *Operation) ID() string

ID returns the ID of the object.

func (*Operation) Save

func (o *Operation) Save(
	ctx context.Context,
) error

Save updates the object database representation with the in-memory values.

type Task

type Task struct {
	Token   string
	Created time.Time

	Name    mint.TkName
	Subject string

	Status mint.TkStatus
	Retry  uint
}

Task represents a task object.

func CreateTask

func CreateTask(
	ctx context.Context,
	created time.Time,
	name mint.TkName,
	subject string,
	status mint.TkStatus,
	retry uint,
) (*Task, error)

CreateTask creates and stores a new Task.

func LoadPendingTasks

func LoadPendingTasks(
	ctx context.Context,
) ([]*Task, error)

LoadPendingTasks loads all tasks that are marked as pending.

func (*Task) Save

func (o *Task) Save(
	ctx context.Context,
) error

Save updates the object database representation with the in-memory values.

type Transaction

type Transaction struct {
	Owner       string
	Token       string
	Created     time.Time
	Propagation mint.PgType

	BaseAsset   string `db:"base_asset"`  // BaseAsset name.
	QuoteAsset  string `db:"quote_asset"` // QuoteAsset name.
	Amount      Amount
	Destination string
	Path        OfPath

	Status mint.TxStatus

	Lock   string
	Secret *string
}

Transaction represents a transaction across a chain of offers.

func CreateCanonicalTransaction

func CreateCanonicalTransaction(
	ctx context.Context,
	owner string,
	baseAsset string,
	quoteAsset string,
	amount Amount,
	destination string,
	path []string,
	status mint.TxStatus,
) (*Transaction, error)

CreateCanonicalTransaction creates and stores a new canonical Transaction object.

func CreatePropagatedTransaction

func CreatePropagatedTransaction(
	ctx context.Context,
	token string,
	created time.Time,
	owner string,
	baseAsset string,
	quoteAsset string,
	amount Amount,
	destination string,
	path []string,
	status mint.TxStatus,
	lock string,
) (*Transaction, error)

CreatePropagatedTransaction creates and stores a new propagated Transaction object.

func LoadCanonicalTransactionByOwnerToken

func LoadCanonicalTransactionByOwnerToken(
	ctx context.Context,
	owner string,
	token string,
) (*Transaction, error)

LoadCanonicalTransactionByOwnerToken attempts to load the canonical transaction for the given owner and token.

func LoadPropagatedTransactionByOwnerToken

func LoadPropagatedTransactionByOwnerToken(
	ctx context.Context,
	owner string,
	token string,
) (*Transaction, error)

LoadPropagatedTransactionByOwnerToken attempts to load the propagated transaction for the given owner and token.

func LoadTransactionByID

func LoadTransactionByID(
	ctx context.Context,
	id string,
) (*Transaction, error)

LoadTransactionByID attempts to load the transaction (canonical or propagated) for the given owner and token.

func (*Transaction) ID

func (t *Transaction) ID() string

ID returns the ID of the object.

func (*Transaction) Save

func (t *Transaction) Save(
	ctx context.Context,
) error

Save updates the object database representation with the in-memory values.

type User

type User struct {
	Token   string
	Created time.Time

	Username     string
	PasswordHash string `db:"password_hash"`
}

User represents a user object. User objects are not managed by the mint and solely accesed in read-only mode, leaving user management to an external system with access to the same underlying mintDB.

func CreateUser

func CreateUser(
	ctx context.Context,
	username string,
	password string,
) (*User, error)

CreateUser creates and stores a new User object.

func LoadUserByToken

func LoadUserByToken(
	ctx context.Context,
	token string,
) (*User, error)

LoadUserByToken attempts to load a user with the given user token.

func LoadUserByUsername

func LoadUserByUsername(
	ctx context.Context,
	username string,
) (*User, error)

LoadUserByUsername attempts to load a user with the given username.

func (*User) CheckPassword

func (u *User) CheckPassword(
	ctx context.Context,
	password string,
) error

CheckPassword checks if the provided password matches the password hash associated with that user.

func (*User) Save

func (u *User) Save(
	ctx context.Context,
) error

Save updates the object database representation with the in-memory values.

func (*User) UpdatePassword

func (u *User) UpdatePassword(
	ctx context.Context,
	password string,
) error

UpdatePassword updates the password hash in memory using the provided password value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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