db

package
v0.0.0-...-b400eb5 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusOpen      = int64(0)
	StatusFilled    = int64(1)
	StatusUnfunded  = int64(2)
	StatusCancelled = int64(3)
)

Variables

This section is empty.

Functions

func CheckMask

func CheckMask(mask, hash []byte) bool

CheckMask verifies that a given hash matches the provided hashmask

func DefaultSha3

func DefaultSha3() []byte

func FindValidNonce

func FindValidNonce(text, timestamp string, mask []byte) <-chan []byte

FindValidNonce finds a valid hash for a given hash mask, Terms, and Timestamp, and returns the Nonce used to generate thas hashmask.

func GetCancellationEpoch

func GetCancellationEpoch(maker, sender *types.Address, db *gorm.DB) *types.Uint256

func GetDB

func GetDB(connectionString, passwordURI string) (*gorm.DB, error)

func OnesCount

func OnesCount(data []byte) int

OnesCount returns how many 1s appear in the binary representation of a string of bytes. Similar to math/bits.OnesCount, but works for arbitrarily sized byte slices.

func PopulateAssetMetadata

func PopulateAssetMetadata(orders []Order, db *gorm.DB)

Types

type AssetAttribute

type AssetAttribute struct {
	AssetData   types.AssetData `gorm:"primary_key"  json:"-"`
	Name        string          `gorm:"primary_key"  json:"name"`
	Type        string          `json:"type,omitempty"`
	Value       string          `json:"value,omitempty"`
	DisplayType string          `json:"display_type,omitempty"`
}

type AssetMetadata

type AssetMetadata struct {
	AssetData       types.AssetData  `gorm:"primary_key" json:"-"`
	RawMetadata     string           `sql:"type:text;"  json:"raw_metadata,omitempty"`
	URI             string           `json:"token_uri"`
	Name            string           `json:"name,omitempty"`
	ExternalURL     string           `json:"external_url,omitempty"`
	Image           string           `json:"image,omitempty"`
	Description     string           `sql:"type:text;" json:"description,omitempty"`
	BackgroundColor string           `json:"background_color,omitempty"`
	Attributes      []AssetAttribute `json:"attributes,omitempty" gorm:"foreignkey:AssetData;association_foreigkey:AssetData" `
	RawAttributes   string           `sql:"type:text;" json:"raw_attributes,omitempty"`
}

func (*AssetMetadata) AfterFind

func (meta *AssetMetadata) AfterFind(scope *gorm.Scope)

AfterFind populates Attribute Data upon querying for AssetMetadata. Gorm doesn't like our AssetData property as a primary key in Preload, as it tries to treat it as a collection of bytes instead of a key. This effectively implements preload implicitly, and accounts for Gorm's issues with byte slices and pointers.

func (*AssetMetadata) SetAssetData

func (meta *AssetMetadata) SetAssetData(assetData types.AssetData)

func (*AssetMetadata) UnmarshalJSON

func (meta *AssetMetadata) UnmarshalJSON(data []byte) error

type Cancellation

type Cancellation struct {
	Maker  *types.Address `gorm:"primary_key"`
	Sender *types.Address `gorm:"primary_key"`
	Epoch  *types.Uint256
}

func (*Cancellation) Save

func (cancellation *Cancellation) Save(db *gorm.DB) *gorm.DB

type CancellationConsumer

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

func NewRecordCancellationConsumer

func NewRecordCancellationConsumer(db *gorm.DB, concurrency int, publisher channels.Publisher) *CancellationConsumer

func (*CancellationConsumer) Consume

func (consumer *CancellationConsumer) Consume(msg channels.Delivery)

type Exchange

type Exchange struct {
	Address *types.Address `gorm:"primary_key"`
	Network int64          `gorm:"index"`
}

type ExchangeLookup

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

func NewExchangeLookup

func NewExchangeLookup(db *gorm.DB) *ExchangeLookup

func (*ExchangeLookup) ExchangeIsKnown

func (lookup *ExchangeLookup) ExchangeIsKnown(address *types.Address) <-chan uint

func (*ExchangeLookup) GetExchangesByNetwork

func (lookup *ExchangeLookup) GetExchangesByNetwork(network int64) ([]*types.Address, error)

func (*ExchangeLookup) GetNetworkByExchange

func (lookup *ExchangeLookup) GetNetworkByExchange(address *types.Address) (int64, error)

type FillRecord

type FillRecord struct {
	OrderHash              string `json:"orderHash"`
	FilledTakerAssetAmount string `json:"filledTakerAssetAmount"`
	Cancel                 bool   `json:"cancel"`
}

type HashMask

type HashMask struct {
	gorm.Model
	Mask       []byte
	Expiration time.Time
}

HashMask is a string of bytes. When a user signs the terms-of-service, they must provide a Nonce that makes the hash of the terms (along with the timestamp) match the hash mask. Matching the hash mask means that any 1 bits in the HashMask must also be 1 in the hash. 0s in the HashMask can be 1 or 0 in the hash.

type IndexConsumer

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

func NewIndexConsumer

func NewIndexConsumer(db *gorm.DB, status int64, concurrency int, publisher channels.Publisher) *IndexConsumer

func (*IndexConsumer) Consume

func (consumer *IndexConsumer) Consume(msg channels.Delivery)

type Indexer

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

func NewIndexer

func NewIndexer(db *gorm.DB, status int64, publisher channels.Publisher) *Indexer

func (*Indexer) Index

func (indexer *Indexer) Index(order *types.Order) error

Index takes an order and saves it to the database

func (*Indexer) RecordCancellation

func (indexer *Indexer) RecordCancellation(cancellation *Cancellation) error

func (*Indexer) RecordFill

func (indexer *Indexer) RecordFill(fillRecord *FillRecord) error

RecordFill takes information about a filled order and updates the corresponding database record, if any exists.

func (*Indexer) RecordSpend

func (indexer *Indexer) RecordSpend(makerAddress, tokenAddress, zrxAddress *types.Address, assetData types.AssetData, balance *types.Uint256) error

RecordSpend takes information about a token transfer, and updates any orders that might have become unfillable as a result of the transfer.

func (*Indexer) UpdateAndPublish

func (indexer *Indexer) UpdateAndPublish(query *gorm.DB, key string, value interface{}, unfillable bool) error

type Order

type Order struct {
	types.Order
	CreatedAt           time.Time
	UpdatedAt           time.Time
	OrderHash           []byte  `gorm:"primary_key"`
	Status              int64   `gorm:"index"`
	Price               float64 `gorm:"index:price"`
	FeeRate             float64 `gorm:"index:price"`
	MakerAssetRemaining *types.Uint256
	MakerFeeRemaining   *types.Uint256
	MakerAssetMetadata  *AssetMetadata
	TakerAssetMetadata  *AssetMetadata
}

func (*Order) Populate

func (order *Order) Populate()

func (*Order) Save

func (order *Order) Save(db *gorm.DB, status int64, publisher channels.Publisher) *gorm.DB

Save records the order in the database, defaulting to the specified status. Status should either be db.StatusOpen, or db.StatusUnfunded. If the order is filled based on order.TakerAssetAmountFilled + order.TakerAssetAmountCancelled the status will be recorded as db.StatusFilled regardless of the specified status.

func (*Order) TableName

func (order *Order) TableName() string

type Pair

type Pair struct {
	TokenA types.AssetData
	TokenB types.AssetData
}

Pair tracks pairs of tokens TokenA and TokenB

func GetAllTokenPairs

func GetAllTokenPairs(db *gorm.DB, offset, count, networkID int) ([]Pair, int, error)

GetAllTokenPairs returns an unfilitered list of Pairs based on the trading pairs currently present in the database, limited by a count and offset.

func GetTokenABPairs

func GetTokenABPairs(db *gorm.DB, tokenA, tokenB types.AssetData, networkID int) ([]Pair, int, error)

GetTokenABPairs returns a list of Pairs based on the trading pairs currrently present in the database, filtered to include only pairs that include both tokenA and tokenB. There should only be one distinct combination of both token pairs, so there is no offset or limit, but it still returns a list to provide the same return value as the other retrieval methods.

func GetTokenAPairs

func GetTokenAPairs(db *gorm.DB, tokenA types.AssetData, offset, count, networkID int) ([]Pair, int, error)

GetTokenAPairs returns a list of Pairs based on the trading pairs currrently present in the database, filtered to include only pairs that include tokenA and limited by a count and offset.

func (*Pair) MarshalJSON

func (pair *Pair) MarshalJSON() ([]byte, error)

type RecordFillConsumer

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

func NewRecordFillConsumer

func NewRecordFillConsumer(db *gorm.DB, concurrency int, publisher channels.Publisher) *RecordFillConsumer

func (*RecordFillConsumer) Consume

func (consumer *RecordFillConsumer) Consume(msg channels.Delivery)

type RecordSpendConsumer

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

func NewRecordSpendConsumer

func NewRecordSpendConsumer(db *gorm.DB, concurrency int, publisher channels.Publisher) *RecordSpendConsumer

func (*RecordSpendConsumer) Consume

func (consumer *RecordSpendConsumer) Consume(msg channels.Delivery)

type SpendRecord

type SpendRecord struct {
	TokenAddress   string `json:"tokenAddress"`
	AssetData      string `json:"assetData"`
	SpenderAddress string `json:"spenderAddress"`
	ZrxToken       string `json:"zrxToken"`
	Balance        string `json:"balance"`
}

type Terms

type Terms struct {
	gorm.Model
	Current    bool
	Lang       string
	Text       string `sql:"type:text;"`
	Valid      bool
	Difficulty int
}

Terms represents an instance of the terms of service. There should only be one Current version of the Terms for a given language at a given time, but older versions may remain Valid. The difficulty indicates how many 1 bits a HashMask must have when signing these terms.

func (*Terms) CheckSig

func (terms *Terms) CheckSig(sig *types.Signature, address *types.Address, timestamp string, nonce []byte, mask []byte) (bool, error)

CheckSig verifies that a signature is valid for a given Terms of use object

type TermsManager

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

TermsManager keeps track of a database instance and a cache and provides several interfaces for interacting with the Terms of Service in the database.

func NewTermsManager

func NewTermsManager(db *gorm.DB) *TermsManager

func NewTxTermsManager

func NewTxTermsManager(db *gorm.DB) *TermsManager

func (*TermsManager) CheckAddress

func (tm *TermsManager) CheckAddress(address *types.Address) <-chan bool

Check ensures that a given address has signed the terms

func (*TermsManager) CheckTerms

func (tm *TermsManager) CheckTerms(id uint, sig *types.Signature, address *types.Address, timestamp string, nonce []byte, mask []byte) (bool, error)

CheckTerms verifies that a signature is valid for a given Terms of use, specified by ID.

func (*TermsManager) ClearExpiredHashMasks

func (tm *TermsManager) ClearExpiredHashMasks()

ClearExpiredHashMasks deletes any expired hash masks. These can be deleted even if a corresponding signature has been created; they're only needed during the sign up process.

func (*TermsManager) GetHashMaskById

func (tm *TermsManager) GetHashMaskById(id uint) ([]byte, error)

GetHashMaskById retrieves a HashMask from the database given its ID.

func (*TermsManager) GetNewHashMask

func (tm *TermsManager) GetNewHashMask(terms *Terms) ([]byte, uint, error)

GetNewHashMask generates a HashMask for a Terms object, considering the difficulty for those Terms. It saves the HashMask in the database, and returns the Bytes along with the byte string.

func (*TermsManager) GetTerms

func (tm *TermsManager) GetTerms(language string) (*Terms, error)

GetTerms returns the current Terms object for a given language.

func (*TermsManager) SaveSig

func (tm *TermsManager) SaveSig(id uint, sig *types.Signature, address *types.Address, timestamp, host_ip string, nonce []byte, mask []byte) error

SaveSig verifies that a signature is valid for a given Terms, then saves it to the database

func (*TermsManager) UpdateTerms

func (tm *TermsManager) UpdateTerms(lang, text string) error

UpdateTerms creates a new Terms object in the database, deprecating the old terms

type TermsSig

type TermsSig struct {
	gorm.Model
	Signer    *types.Address `gorm:"index"`
	Timestamp string
	IP        string
	Nonce     []byte
	Banned    bool
	Signature *types.Signature
	TermsID   uint
}

TermsSig represent a user's signature of the terms of service. It tracks the address of the signer, when they signed it, the IP they submitted the signature from, the Nonce they used to make the HashMask match, the cryptographic signature, and a reference to the Terms object they signed.

Jump to

Keyboard shortcuts

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