Documentation
¶
Overview ¶
kraken is a utility package with common functions shared across other packagges.
Index ¶
- Constants
- func AddFormField(writer *multipart.Writer, parent string, child string, v any) error
- func CreateReadCloser(b []byte) io.ReadCloser
- func Maps(p map[string]any, s ...map[string]any) map[string]any
- func Sign(key string, message []byte) (string, error)
- func StructToMap(s any) (map[string]any, error)
- func ToJSON(v any) string
- func ToJSONIndent(v any) string
- func Traverse[V any](m any, keys ...any) (*V, error)
- func UUID() string
- type Action
- type Book
- func (b *Book) BestAsk() *Level
- func (b *Book) BestBid() *Level
- func (b *Book) EnforceDepth()
- func (b *Book) EnforceOrder()
- func (b *Book) L2Checksum(checksum string) *ChecksumResult
- func (b *Book) L3Checksum(checksum string) *ChecksumResult
- func (b *Book) Midpoint() *Money
- func (b *Book) Spread() *Money
- func (b *Book) Update(opts *BookUpdateOptions)
- func (b *Book) WorstAsk() *Level
- func (b *Book) WorstBid() *Level
- type BookCrossedResult
- type BookDirection
- type BookUpdateOptions
- type Callback
- type CallbackManager
- func (cg *CallbackManager[T]) Call(v T)
- func (cg *CallbackManager[T]) Deregister(c *Callback[T]) *Callback[T]
- func (cg *CallbackManager[T]) Map() map[*Callback[T]]bool
- func (cg *CallbackManager[T]) Once(action Action[T]) *Callback[T]
- func (cg *CallbackManager[T]) Recurring(action Action[T]) *Callback[T]
- func (cg *CallbackManager[T]) Register(c *Callback[T]) *Callback[T]
- func (cg *CallbackManager[T]) Reset()
- func (cg *CallbackManager[T]) SleepUntilDisabled(action Action[T]) *Callback[T]
- type ChecksumPart
- type ChecksumResult
- type EpochCounter
- type Event
- type Level
- type Map
- func (m *Map[K, V]) Clone() *Map[K, V]
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Get(k K) (*V, error)
- func (m *Map[K, V]) Keys() []K
- func (m *Map[K, V]) Length() int
- func (m *Map[K, V]) Range(f func(K, *V))
- func (m *Map[K, V]) Raw() map[K]*V
- func (m *Map[K, V]) Reset()
- func (m *Map[K, V]) Set(key K, value *V)
- func (m *Map[K, V]) SetMap(r map[K]*V)
- type MaxDepthExceededResult
- type Money
- func (m *Money) Abs() *Money
- func (x *Money) Add(y *Money) *Money
- func (x *Money) Cmp(y *Money) int
- func (m *Money) Copy() *Money
- func (x *Money) Div(y *Money) *Money
- func (x *Money) Exp(y *Money) *Money
- func (m *Money) Float64() float64
- func (m *Money) Int64() int64
- func (m *Money) MarshalJSON() ([]byte, error)
- func (x *Money) Mul(y *Money) *Money
- func (m *Money) OffsetPercent(o *Money) *Money
- func (m *Money) OffsetTicks(o *Money) *Money
- func (m *Money) Rat() *big.Rat
- func (m *Money) RoundToGranularity() *Money
- func (m *Money) ScalingFactor() *big.Int
- func (m *Money) SetDecimals(d int64) *Money
- func (m *Money) SetGranularity(t int64) *Money
- func (m *Money) SetSize(s *Money) *Money
- func (m *Money) Sign() int
- func (m *Money) SmallestIncrement() *Money
- func (m *Money) String() string
- func (x *Money) Sub(y *Money) *Money
- func (m *Money) UnmarshalJSON(data []byte) error
- type MultipartFile
- type MultipartForm
- type NewRequestOptions
- type Order
- type REST
- type Request
- func (r *Request) GetMediaType() string
- func (r *Request) JoinPath(p any)
- func (r *Request) SetBody(v any) error
- func (r *Request) SetHeader(key string, value any)
- func (r *Request) SetHeaders(h map[string]any)
- func (r *Request) SetQuery(q map[string]any)
- func (r *Request) SetURL(base string, s ...any) error
- type Response
- type Side
- type WebSocket
- type WebSocketMessage
Constants ¶
const ( Bid = "bid" Ask = "ask" )
const DefaultDecimals = 12
Default decimal places set for integer constructors.
Variables ¶
This section is empty.
Functions ¶
func AddFormField ¶
AddFormField fills a form field with a key defined by parent[child] or child if parent is empty and writes them into multipart.Writer. Accepted values: string, []byte, func() (MultipartFile, error), and map[string]any
func CreateReadCloser ¶
func CreateReadCloser(b []byte) io.ReadCloser
CreateReadCloser constructs an io.ReadCloser from the a slice of byte characters.
func StructToMap ¶
StructToMap converts a struct into a map[string]any object.
func ToJSONIndent ¶
ToJSONIndent returns the JSON string of v with indents or panics.
Types ¶
type Book ¶
type Book struct {
// Number of price levels for checksum.
MaxDepth int `json:"maxDepth,omitempty"`
// Whether to eliminate book crossings for each update.
NoBookCrossing bool
// Whether to trim price levels outside of the max depth.
// Must be disable for whole books.
EnableMaxDepth bool
Bids *Side
Asks *Side
OnUpdated *CallbackManager[*BookUpdateOptions]
OnBookCrossed *CallbackManager[*BookCrossedResult]
OnMaxDepthExceeded *CallbackManager[*MaxDepthExceededResult]
OnChecksummed *CallbackManager[*ChecksumResult]
}
Order book structure for L2 and L3.
func (*Book) EnforceDepth ¶
func (b *Book) EnforceDepth()
EnforceDepth checks whether the length of each side of the book exceeds the max depth and attempts to removes the worst out-of-depth price levels.
func (*Book) EnforceOrder ¶
func (b *Book) EnforceOrder()
EnforceOrder check whether the bid is greater or equal to the ask and attempts to remove older conflicting price levels.
func (*Book) L2Checksum ¶
func (b *Book) L2Checksum(checksum string) *ChecksumResult
L2Checksum verifies that the L2 book is synchronized with the exchange.
func (*Book) L3Checksum ¶
func (b *Book) L3Checksum(checksum string) *ChecksumResult
L3Checksum verifies that the L3 book is synchronized with the exchange.
func (*Book) Update ¶
func (b *Book) Update(opts *BookUpdateOptions)
Update routes the BookUpdateOptions to the correct side of the book and enforces checks to preserve book integrity.
type BookCrossedResult ¶
type BookUpdateOptions ¶
type BookUpdateOptions struct {
Direction BookDirection `json:"direction,omitempty"`
ID string `json:"orderid,omitempty"`
Price *Money `json:"price,omitempty"`
Quantity *Money `json:"quantity,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
}
BookUpdateOptions is used to communicate an update to the Book.
type CallbackManager ¶
type CallbackManager[T any] struct { // contains filtered or unexported fields }
CallbackManager manages the lifecycle of a collection of generic Callback structs.
func NewCallbackManager ¶
func NewCallbackManager[T any]() *CallbackManager[T]
NewCallbackManager constructs a new CallbackManager structs.
func (*CallbackManager[T]) Call ¶
func (cg *CallbackManager[T]) Call(v T)
Call fans out Event objects across all callbacks in the map.
func (*CallbackManager[T]) Deregister ¶
func (cg *CallbackManager[T]) Deregister(c *Callback[T]) *Callback[T]
Reregister removes a Callback struct from the map.
func (*CallbackManager[T]) Map ¶
func (cg *CallbackManager[T]) Map() map[*Callback[T]]bool
Map returns a clone of the internal Callback map.
func (*CallbackManager[T]) Once ¶
func (cg *CallbackManager[T]) Once(action Action[T]) *Callback[T]
Once adds a Callback to the map that is deregistered after first execution.
func (*CallbackManager[T]) Recurring ¶
func (cg *CallbackManager[T]) Recurring(action Action[T]) *Callback[T]
Recurring adds a recurring Callback struct to the map.
func (*CallbackManager[T]) Register ¶
func (cg *CallbackManager[T]) Register(c *Callback[T]) *Callback[T]
Register adds a Callback struct to the map.
func (*CallbackManager[T]) Reset ¶
func (cg *CallbackManager[T]) Reset()
Reset clears all Callback struct from the map.
func (*CallbackManager[T]) SleepUntilDisabled ¶
func (cg *CallbackManager[T]) SleepUntilDisabled(action Action[T]) *Callback[T]
SleepUntilDisabled adds a Callback struct to the map and pauses the current goroutine until the callback is disabled.
type ChecksumPart ¶
type ChecksumPart struct {
Level *Level `json:"-"`
Order *Order `json:"order,omitempty"`
Price string `json:"price,omitempty"`
Quantity string `json:"quantity,omitempty"`
Concatenated string `json:"concatenated,omitempty"`
}
ChecksumPart contains information regarding a price level or order.
type ChecksumResult ¶
type ChecksumResult struct {
Level int `json:"level,omitempty"`
ServerChecksum string `json:"serverChecksum,omitempty"`
LocalChecksum string `json:"localChecksum,omitempty"`
Match bool `json:"match,omitempty"`
AskParts []*ChecksumPart `json:"askParts,omitempty"`
BidParts []*ChecksumPart `json:"bidParts,omitempty"`
Asks string `json:"asks,omitempty"`
Bids string `json:"bids,omitempty"`
}
ChecksumResult contains the result of the book checksum validation.
type EpochCounter ¶
EpochCounter is the default counter for the module, used for nonce generation.
func NewEpochCounter ¶
func NewEpochCounter() *EpochCounter
NewEpochCounter constructs a NonceGenerator.
func (*EpochCounter) Get ¶
func (c *EpochCounter) Get() string
Get concatenates the unix epoch value and 3 leading zero counter values.
type Level ¶
type Level struct {
Price *Money `json:"price,omitempty"`
Quantity *Money `json:"quantity,omitempty"`
Timestamp time.Time `json:"time,omitempty"`
Lower *Level `json:"-"`
Higher *Level `json:"-"`
// contains filtered or unexported fields
}
Level contains price level information.
func (*Level) GetPriceString ¶
GetPriceString returns the level's price.
func (*Level) GetQuantityString ¶
GetQuantityString returns the level's total quantity.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map wraps a map and a mutex for concurrent operations.
type MaxDepthExceededResult ¶
type MaxDepthExceededResult struct {
Side BookDirection `json:"side,omitempty"`
CurrentDepth int `json:"currentDepth,omitempty"`
MaxDepth int `json:"maxDepth,omitempty"`
Worst *Level `json:"worst,omitempty"`
}
type Money ¶
type Money struct {
// Unscaled integer representation.
Integer *big.Int
// Granularity.
Granularity int64
// Amount of decimal places.
Decimals int64
}
Money implements fixed-point arithmetic.
func NewMoneyFromBigFloat ¶
NewMoneyFromBigFloat creates a new Money object from a big.Float.
func NewMoneyFromBigInt ¶
NewMoneyFromBigInt creates a new Money object from a big.Int.
func NewMoneyFromFloat64 ¶
NewMoneyFromFloat64 creates a new Money object from a float64.
func NewMoneyFromInt64 ¶
NewMoneyFromInt64 creates a new Money object from an int64.
func NewMoneyFromString ¶
NewMoneyFromString creates a new Money object from a string.
func (*Money) Float64 ¶
Float64 returns the floating point representation of m with potential loss of precision.
func (*Money) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Money) OffsetPercent ¶
OffsetPercent returns the adjustment of m by %o. Formula: m * (1 + o). The multiplicand decimals are set to the max precision of both m and o.
func (*Money) OffsetTicks ¶
OffsetTicks returns the adjustment of m by an increment proportional to o.
func (*Money) RoundToGranularity ¶
RoundToGranularity returns the rounding of m to the granularity constraint.
func (*Money) ScalingFactor ¶
ScalingFactor returns 10 ^ decimals in big.Int.
func (*Money) SetDecimals ¶
SetDecimals returns m with adjusted decimal places.
func (*Money) SetGranularity ¶
SetGranularity configures the raw integer of m to always be a multiple of t.
func (*Money) SmallestIncrement ¶
SmallestIncrement returns the smallest possible increment of m.
func (*Money) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type MultipartFile ¶
type MultipartFile interface {
Name() string
io.ReadCloser
}
MultipartFile is an interface to attach a file into the multipart form. os.File implements this interface.
type MultipartForm ¶
MultipartForm is a combined representation of bytes.Buffer and multipart.Writer, both essential for setting the body.
func CreateMultipartForm ¶
func CreateMultipartForm(m map[string]any) (*MultipartForm, error)
CreateMultipartForm constructs a MultipartForm from the given map[string]any. See AddFormField for accepted values.
type NewRequestOptions ¶
type NewRequestOptions struct {
Method string
Path any
PathValues map[string]any
Query map[string]any
Headers map[string]any
Body any
}
NewRequestOptions contains the parameters for REST.NewRequest.
type Order ¶
type Order struct {
ID string `json:"id,omitempty"`
LimitPrice *Money `json:"limitPrice,omitempty"`
Quantity *Money `json:"quantity,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
Level *Level `json:"-"`
}
Order contains information regarding a limit order.
type REST ¶
REST API wrapper for the HTTP client.
func (*REST) NewRequest ¶
func (r *REST) NewRequest(opts *NewRequestOptions) (*Request, error)
NewRequest creates a new Request.
func (*REST) Request ¶
func (r *REST) Request(cfg *NewRequestOptions) (*Response, error)
Request helps to call REST.NewRequest and REST.Do in one line.
type Request ¶
Request is a wrapper around http.Request to assist with internal functions.
func NewRequest ¶
func NewRequest() *Request
NewRequest initializes a Request object with default values.
func (*Request) GetMediaType ¶
GetMediaType retrieves the Content-Type header without additional parameters.
func (*Request) SetBody ¶
SetBody sets the request body based on the current Content-Type of the Request.
It automatically encodes the provided value `v` according to the media type:
- "multipart/form-data": expects v to be a map[string]any and sets it as a multipart form.
- "application/x-www-form-urlencoded": expects v to be a map[string]any and encodes it as form values.
- "application/json": marshals v as JSON.
Returns an error if the Content-Type is unsupported or if the input type doesn't match the expected format.
func (*Request) SetHeaders ¶
SetHeaders ranges over the hash map and calls Request.SetHeader.
func (*Request) SetQuery ¶
SetQuery converts a map[string]any into a url.Values object and sets it as the URL query.
type Response ¶
type Response struct {
Request *Request `json:"request,omitempty"`
Body []byte `json:"body,omitempty"`
BodyMap map[string]any `json:"-"`
*http.Response `json:"-"`
}
Response is a wrapper around http.Response with an already read body.
type Side ¶
type Side struct {
Direction BookDirection
High *Level
Low *Level
Last *Level
Levels *Map[string, Level]
// contains filtered or unexported fields
}
Side encompasses the price levels in one side of the book.
func (*Side) FindAdjacent ¶
FindAdjacent finds the nearest price level close to the given price.
func (*Side) FindAdjacentAbove ¶
FindAdjacentAbove finds the nearest price level above the given price.
func (*Side) FindAdjacentBelow ¶
FindAdjacentBelow finds the nearest price level from below the given price.
func (*Side) Update ¶
func (s *Side) Update(opts *BookUpdateOptions)
Update interprets a BookUpdateOptions message and decides if it should add, update, or delete the price level.
type WebSocket ¶
type WebSocket struct {
Reconnect func()
ReconnectWait time.Duration
DoReconnect bool
OnConnected *CallbackManager[any]
OnDisconnected *CallbackManager[error]
OnSent *CallbackManager[*WebSocketMessage]
OnReceived *CallbackManager[*WebSocketMessage]
URL string
Insecure bool
// contains filtered or unexported fields
}
WebSocket implements a common structure for the WebSocket APIs.
func NewWebSocket ¶
func NewWebSocket() *WebSocket
NewWebSocket creates a new WebSocket object with default values.
func (*WebSocket) Disconnect ¶
Disconnect stops the connection.
type WebSocketMessage ¶
type WebSocketMessage struct {
// contains filtered or unexported fields
}
func NewWebSocketMessage ¶
func NewWebSocketMessage(d []byte) *WebSocketMessage
func (*WebSocketMessage) Bytes ¶
func (m *WebSocketMessage) Bytes() []byte
func (*WebSocketMessage) JSON ¶
func (m *WebSocketMessage) JSON(v any) error
func (*WebSocketMessage) String ¶
func (m *WebSocketMessage) String() string