db

package
v0.0.0-...-613d1fa Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2021 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDBFilledWithPinnedOrders = errors.New("the database is full of pinned orders; no orders can be removed in order to make space")
	ErrMetadataAlreadyExists    = errors.New("metadata already exists in the database (use UpdateMetadata instead?)")
	ErrNotFound                 = errors.New("could not find existing model or row in database")
	ErrClosed                   = errors.New("database is already closed")
)

Functions

func NewPostgreSQLQueriesForTable

func NewPostgreSQLQueriesForTable(tableName string) *postgreSQLQueries

func NewSqliteQueriesForTable

func NewSqliteQueriesForTable(tableName string) *sqliteQueries

func ParseContractAddressesAndTokenIdsFromAssetData

func ParseContractAddressesAndTokenIdsFromAssetData(assetDataDecoder *zeroex.AssetDataDecoder, assetData []byte, contractAddresses ethereum.ContractAddresses) ([]*types.SingleAssetData, error)

Types

type DB

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

DB instantiates the DB connection and creates all the collections used by the application

func New

func New(ctx context.Context, opts *Options) (*DB, error)

New creates a new connection to the database. The connection will be automatically closed when the given context is canceled.

func (*DB) AddMiniHeaders

func (db *DB) AddMiniHeaders(miniHeaders []*types.MiniHeader) (added []*types.MiniHeader, removed []*types.MiniHeader, err error)

func (*DB) AddOrders

func (db *DB) AddOrders(orders []*types.OrderWithMetadata) (alreadyStored []common.Hash, added []*types.OrderWithMetadata, removed []*types.OrderWithMetadata, err error)

func (*DB) CountOrders

func (db *DB) CountOrders(query *OrderQuery) (count int, err error)

func (*DB) DHTStore

func (db *DB) DHTStore() ds.Batching

func (*DB) DeleteMiniHeader

func (db *DB) DeleteMiniHeader(hash common.Hash) error

func (*DB) DeleteMiniHeaders

func (db *DB) DeleteMiniHeaders(query *MiniHeaderQuery) (deleted []*types.MiniHeader, err error)

func (*DB) DeleteOrder

func (db *DB) DeleteOrder(hash common.Hash) error

func (*DB) DeleteOrders

func (db *DB) DeleteOrders(query *OrderQuery) (deleted []*types.OrderWithMetadata, err error)

func (*DB) FindMiniHeaders

func (db *DB) FindMiniHeaders(query *MiniHeaderQuery) (miniHeaders []*types.MiniHeader, err error)

func (*DB) FindOrders

func (db *DB) FindOrders(query *OrderQuery) (orders []*types.OrderWithMetadata, err error)

func (*DB) GetCurrentMaxExpirationTime

func (db *DB) GetCurrentMaxExpirationTime() (*big.Int, error)

GetCurrentMaxExpirationTime returns the maximum expiration time for non-pinned orders stored in the database. If there are no non-pinned orders in the database, it returns constants.UnlimitedExpirationTime.

func (*DB) GetLatestMiniHeader

func (db *DB) GetLatestMiniHeader() (*types.MiniHeader, error)

GetLatestMiniHeader is a helper method for getting the latest MiniHeader. It returns ErrNotFound if there are no MiniHeaders in the database.

func (*DB) GetMetadata

func (db *DB) GetMetadata() (*types.Metadata, error)

GetMetadata returns the metadata (or db.ErrNotFound if no metadata has been saved).

func (*DB) GetMiniHeader

func (db *DB) GetMiniHeader(hash common.Hash) (miniHeader *types.MiniHeader, err error)

func (*DB) GetOldestMiniHeader

func (db *DB) GetOldestMiniHeader() (*types.MiniHeader, error)

GetOldestMiniHeader is a helper method for getting the oldest MiniHeader. It returns ErrNotFound if there are no MiniHeaders in the database.

func (*DB) GetOrder

func (db *DB) GetOrder(hash common.Hash) (order *types.OrderWithMetadata, err error)

func (*DB) GetOrderStatuses

func (db *DB) GetOrderStatuses(hashes []common.Hash) (statuses []*StoredOrderStatus, err error)

func (*DB) PeerStore

func (db *DB) PeerStore() ds.Batching

func (*DB) ReadWriteTransactionalContext

func (db *DB) ReadWriteTransactionalContext(ctx context.Context, opts *sql.TxOptions, f func(tx *sqlz.Tx) error) error

ReadWriteTransactionalContext acquires a write lock, executes the transaction, then immediately releases the lock.

func (*DB) ResetMiniHeaders

func (db *DB) ResetMiniHeaders(newMiniHeaders []*types.MiniHeader) (err error)

ResetMiniHeaders deletes all of the existing miniheaders and then stores new miniheaders in the database.

func (*DB) SaveMetadata

func (db *DB) SaveMetadata(metadata *types.Metadata) (err error)

SaveMetadata inserts the metadata into the database, overwriting any existing metadata. It returns ErrMetadataAlreadyExists if the metadata has already been saved in the database.

func (*DB) UpdateMetadata

func (db *DB) UpdateMetadata(updateFunc func(oldmetadata *types.Metadata) (newMetadata *types.Metadata)) (err error)

UpdateMetadata updates the metadata in the database via a transaction. It accepts a callback function which will be provided with the old metadata and should return the new metadata to save.

func (*DB) UpdateOrder

func (db *DB) UpdateOrder(hash common.Hash, updateFunc func(existingOrder *types.OrderWithMetadata) (updatedOrder *types.OrderWithMetadata, err error)) (err error)

type Database

type Database interface {
	AddOrders(orders []*types.OrderWithMetadata) (alreadyStored []common.Hash, added []*types.OrderWithMetadata, removed []*types.OrderWithMetadata, err error)
	GetOrder(hash common.Hash) (*types.OrderWithMetadata, error)
	GetOrderStatuses(hashes []common.Hash) (statuses []*StoredOrderStatus, err error)
	FindOrders(opts *OrderQuery) ([]*types.OrderWithMetadata, error)
	CountOrders(opts *OrderQuery) (int, error)
	DeleteOrder(hash common.Hash) error
	DeleteOrders(opts *OrderQuery) ([]*types.OrderWithMetadata, error)
	UpdateOrder(hash common.Hash, updateFunc func(existingOrder *types.OrderWithMetadata) (updatedOrder *types.OrderWithMetadata, err error)) error
	AddMiniHeaders(miniHeaders []*types.MiniHeader) (added []*types.MiniHeader, removed []*types.MiniHeader, err error)
	GetMiniHeader(hash common.Hash) (*types.MiniHeader, error)
	FindMiniHeaders(opts *MiniHeaderQuery) ([]*types.MiniHeader, error)
	DeleteMiniHeader(hash common.Hash) error
	DeleteMiniHeaders(opts *MiniHeaderQuery) ([]*types.MiniHeader, error)
	ResetMiniHeaders(miniHeaders []*types.MiniHeader) error
	GetMetadata() (*types.Metadata, error)
	SaveMetadata(metadata *types.Metadata) error
	UpdateMetadata(updateFunc func(oldmetadata *types.Metadata) (newMetadata *types.Metadata)) error
	PeerStore() ds.Batching
	DHTStore() ds.Batching
}

type FilterKind

type FilterKind string
const (
	Equal          FilterKind = "="
	NotEqual       FilterKind = "!="
	Less           FilterKind = "<"
	Greater        FilterKind = ">"
	LessOrEqual    FilterKind = "<="
	GreaterOrEqual FilterKind = ">="
	Contains       FilterKind = "CONTAINS"
)

type MiniHeaderField

type MiniHeaderField string
const (
	MFHash      MiniHeaderField = "hash"
	MFParent    MiniHeaderField = "parent"
	MFNumber    MiniHeaderField = "number"
	MFTimestamp MiniHeaderField = "timestamp"
	MFLogs      MiniHeaderField = "logs"
)

type MiniHeaderFilter

type MiniHeaderFilter struct {
	Field MiniHeaderField `json:"field"`
	Kind  FilterKind      `json:"kind"`
	Value interface{}     `json:"value"`
}

type MiniHeaderQuery

type MiniHeaderQuery struct {
	Filters []MiniHeaderFilter `json:"filters"`
	Sort    []MiniHeaderSort   `json:"sort"`
	Limit   uint               `json:"limit"`
	Offset  uint               `json:"offset"`
}

type MiniHeaderSort

type MiniHeaderSort struct {
	Field     MiniHeaderField `json:"field"`
	Direction SortDirection   `json:"direction"`
}

type Options

type Options struct {
	DriverName     string `json:"driverName"`
	DataSourceName string `json:"dataSourceName"`
	MaxOrders      int    `json:"maxOrders"`
	MaxMiniHeaders int    `json:"maxMiniHeaders"`
}

func TestOptions

func TestOptions() *Options

TestOptions returns a set of options suitable for testing.

type OrderField

type OrderField string
const (
	OFHash                     OrderField = "hash"
	OFChainID                  OrderField = "chainID"
	OFExchangeAddress          OrderField = "exchangeAddress"
	OFMakerAddress             OrderField = "makerAddress"
	OFMakerAssetData           OrderField = "makerAssetData"
	OFMakerFeeAssetData        OrderField = "makerFeeAssetData"
	OFMakerAssetAmount         OrderField = "makerAssetAmount"
	OFMakerFee                 OrderField = "makerFee"
	OFTakerAddress             OrderField = "takerAddress"
	OFTakerAssetData           OrderField = "takerAssetData"
	OFTakerFeeAssetData        OrderField = "takerFeeAssetData"
	OFTakerAssetAmount         OrderField = "takerAssetAmount"
	OFTakerFee                 OrderField = "takerFee"
	OFSenderAddress            OrderField = "senderAddress"
	OFFeeRecipientAddress      OrderField = "feeRecipientAddress"
	OFExpirationTimeSeconds    OrderField = "expirationTimeSeconds"
	OFSalt                     OrderField = "salt"
	OFSignature                OrderField = "signature"
	OFLastUpdated              OrderField = "lastUpdated"
	OFFillableTakerAssetAmount OrderField = "fillableTakerAssetAmount"
	OFIsRemoved                OrderField = "isRemoved"
	OFIsPinned                 OrderField = "isPinned"
	OFParsedMakerAssetData     OrderField = "parsedMakerAssetData"
	OFParsedMakerFeeAssetData  OrderField = "parsedMakerFeeAssetData"
	OFLastValidatedBlockNumber OrderField = "lastValidatedBlockNumber"
)

type OrderFilter

type OrderFilter struct {
	Field OrderField  `json:"field"`
	Kind  FilterKind  `json:"kind"`
	Value interface{} `json:"value"`
}

func MakerAssetIncludesTokenAddress

func MakerAssetIncludesTokenAddress(tokenAddress common.Address) OrderFilter

MakerAssetIncludesTokenAddress is a helper method which returns a filter that will match orders that include the token address (and any token id, including null) in MakerAssetData.

func MakerAssetIncludesTokenAddressAndTokenID

func MakerAssetIncludesTokenAddressAndTokenID(tokenAddress common.Address, tokenID *big.Int) OrderFilter

MakerAssetIncludesTokenAddressAndTokenID is a helper method which returns a filter that will match orders that include the token address and token ID in MakerAssetData.

func MakerFeeAssetIncludesTokenAddress

func MakerFeeAssetIncludesTokenAddress(tokenAddress common.Address) OrderFilter

MakerFeeAssetIncludesTokenAddress is a helper method which returns a filter that will match orders that include the token address (and any token id, including null) in MakerFeeAssetData.

func MakerFeeAssetIncludesTokenAddressAndTokenID

func MakerFeeAssetIncludesTokenAddressAndTokenID(tokenAddress common.Address, tokenID *big.Int) OrderFilter

MakerFeeAssetIncludesTokenAddressAndTokenID is a helper method which returns a filter that will match orders that include the token address and token ID in MakerFeeAssetData.

type OrderQuery

type OrderQuery struct {
	Filters []OrderFilter `json:"filters"`
	Sort    []OrderSort   `json:"sort"`
	Limit   uint          `json:"limit"`
	Offset  uint          `json:"offset"`
}

type OrderSort

type OrderSort struct {
	Field     OrderField    `json:"field"`
	Direction SortDirection `json:"direction"`
}

type PostgreSQLOptions

type PostgreSQLOptions struct {
	Host     string
	Port     string
	User     string
	Password string
	Database string
	Table    string
}

Options are the postgres datastore options, reexported here for convenience.

func (*PostgreSQLOptions) CreatePostgres

func (opts *PostgreSQLOptions) CreatePostgres() (*sqlds.Datastore, error)

Create returns a datastore connected to postgres initialized with a table

type Selector

type Selector interface {
	Select(cols ...string) *sqlz.SelectStmt
}

type SortDirection

type SortDirection string
const (
	Ascending  SortDirection = "ASC"
	Descending SortDirection = "DESC"
)

type StoredOrderStatus

type StoredOrderStatus struct {
	IsStored                 bool     `json:"isStored"`
	IsMarkedRemoved          bool     `json:"isMarkedRemoved"`
	FillableTakerAssetAmount *big.Int `json:"fillableTakerAssetAmount"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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