ellipxobj

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: MIT Imports: 15 Imported by: 0

README

GoDoc

EllipX objects

These structures are shared among various parts of the system in order to ensure consistent structure, and can be used to communicate with APIs.

Pairs

When a pair is mentioned, it comes in a specific order, and typically written with a _ between the two elements as unlike standard pairs, there is no guarantee that elements of a given pair will be 3 characters long.

If the order is a buy order, it means exchanging the first element for the second. Sell order works the other way around.

Documentation

Index

Constants

View Source
const TimeIdDataLen = 16 // number of bytes in a timeid

Variables

View Source
var (
	ErrOrderIdMissing      = errors.New("order id is required")
	ErrBrokerIdMissing     = errors.New("broker id is required")
	ErrOrderTypeNotValid   = errors.New("order type is not valid")
	ErrOrderStatusNotValid = errors.New("order status is not valid")
	ErrOrderNeedsAmount    = errors.New("order amount or spend limit is required")
)

Functions

This section is empty.

Types

type Amount

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

Amount is a fixed point value

func NewAmount

func NewAmount(value int64, decimals int) *Amount

NewAmount returns a new Amount object set to the specific value and decimals

func NewAmountFromFloat

func NewAmountFromFloat(f *big.Float, decimals int) (*Amount, big.Accuracy)

NewAmountFromFloat return a new Amount initialized with the value f stored with the specified number of decimals

func NewAmountFromFloat64

func NewAmountFromFloat64(f float64, exp int) (*Amount, big.Accuracy)

NewAmountFromFloat64 return a new Amount initialized with the value f stored with the specified number of decimals

func NewAmountFromString

func NewAmountFromString(s string, decimals int) (*Amount, error)

NewAmountFromString return a new Amount initialized with the passed string value

func (Amount) Float

func (a Amount) Float() *big.Float

func (*Amount) MarshalJSON

func (a *Amount) MarshalJSON() ([]byte, error)

func (*Amount) Mul

func (a *Amount) Mul(x, y *Amount) (*Amount, big.Accuracy)

Mul sets a=x*y and returns a

func (*Amount) Reciprocal

func (a *Amount) Reciprocal() (*Amount, big.Accuracy)

Reciprocal returns 1/a in a newly allocated Amount

func (*Amount) SetExp added in v0.0.2

func (a *Amount) SetExp(e int) *Amount

SetExp sets the number of decimals (exponent) of the amount, truncating or adding zeroes as needed

func (Amount) String

func (a Amount) String() string

func (*Amount) UnmarshalJSON added in v0.0.2

func (a *Amount) UnmarshalJSON(b []byte) error

type Order

type Order struct {
	OrderId     string      `json:"id"`                    // order ID assigned by the broker
	BrokerId    string      `json:"iss"`                   // id of the broker
	RequestTime uint64      `json:"iat"`                   // unix timestamp when the order was placed
	Unique      *TimeId     `json:"uniq,omitempty"`        // unique ID allocated on order igress
	Pair        PairName    `json:"pair"`                  // the name of the pair the order is on
	Type        OrderType   `json:"type"`                  // type of order (buy or sell)
	Status      OrderStatus `json:"status"`                // new orders will always be in "pending" state
	Flags       OrderFlags  `json:"flags,omitempty"`       // order flags
	Amount      *Amount     `json:"amount,omitempty"`      // optional amount, if nil SpendLimit must be set
	Price       *Amount     `json:"price,omitempty"`       // price, if nil this will be a market order
	SpendLimit  *Amount     `json:"spend_limit,omitempty"` // optional spending limit, if nil Amount must be set
	StopPrice   *Amount     `json:"stop_price,omitempty"`  // ignored if flag Stop is not set
}

func NewOrder

func NewOrder(pair PairName, typ OrderType) *Order

func (*Order) IsValid

func (o *Order) IsValid() error

func (*Order) Meta

func (o *Order) Meta() *OrderMeta

func (*Order) Reverse

func (o *Order) Reverse() *Order

Reverse reverses an order's pair, updating Amount and Price accordingly

func (*Order) SetId

func (o *Order) SetId(orderId, brokerId string) *Order

func (*Order) String

func (o *Order) String() string

type OrderFlags

type OrderFlags int
const (
	FlagImmediateOrCancel OrderFlags = 1 << iota // do not create an open order after execution
	FlagFillOrKill                               // if order can't be fully executed, cancel
	FlagStop
)

func (OrderFlags) MarshalJSON

func (f OrderFlags) MarshalJSON() ([]byte, error)

func (*OrderFlags) UnmarshalJSON

func (f *OrderFlags) UnmarshalJSON(j []byte) error

type OrderMeta

type OrderMeta struct {
	OrderId  string  `json:"id"`
	BrokerId string  `json:"iss"`
	Unique   *TimeId `json:"uniq,omitempty"`
}

type OrderStatus

type OrderStatus int
const (
	OrderInvalid OrderStatus = -1
	OrderPending OrderStatus = iota
	OrderRunning
	OrderOpen
	OrderStop // pending for a trigger
	OrderDone
	OrderCancel // cancelled or overwritten order
)

func OrderStatusByString

func OrderStatusByString(s string) OrderStatus

func (OrderStatus) IsValid

func (s OrderStatus) IsValid() bool

func (OrderStatus) MarshalJSON added in v0.0.4

func (s OrderStatus) MarshalJSON() ([]byte, error)

func (OrderStatus) String

func (s OrderStatus) String() string

func (*OrderStatus) UnmarshalJSON added in v0.0.4

func (s *OrderStatus) UnmarshalJSON(b []byte) error

type OrderType

type OrderType int
const (
	TypeInvalid OrderType = -1
	TypeBid     OrderType = iota // buy
	TypeAsk                      // sell
)

func OrderTypeByString

func OrderTypeByString(v string) OrderType

func (OrderType) IsValid

func (t OrderType) IsValid() bool

func (OrderType) MarshalJSON added in v0.0.3

func (t OrderType) MarshalJSON() ([]byte, error)

func (OrderType) Reverse

func (t OrderType) Reverse() OrderType

func (OrderType) String

func (t OrderType) String() string

func (*OrderType) UnmarshalJSON added in v0.0.3

func (t *OrderType) UnmarshalJSON(b []byte) error

type PairName

type PairName [2]string

func Pair

func Pair(a, b string) PairName

func (*PairName) Hash added in v0.0.9

func (p *PairName) Hash() [32]byte

Hash returns a 32 bytes hash (sha256) representing the pair name with a nil character between the two names

func (PairName) String added in v0.0.2

func (p PairName) String() string

func (*PairName) UnmarshalJSON added in v0.0.8

func (p *PairName) UnmarshalJSON(v []byte) error

type Signature

type Signature struct {
	Header    string `json:"header"` // base64url encoded header
	Signature string `json:"sig"`    // base64url encoded signature
}

type SignatureHeader added in v0.0.7

type SignatureHeader struct {
	Type   string `json:"typ"` // order | trade
	Issuer string `json:"iss"` // name of signature issuer (if broker, broker id)
	Alg    string `json:"alg"` // jwt-style algorithm name
}

type SignedObject added in v0.0.7

type SignedObject struct {
	Type       string       `json:"typ"`    // order | trade
	Object     string       `json:"object"` // base64url encoded json
	Signatures []*Signature `json:"sigs"`
}

func (*SignedObject) Sign added in v0.0.7

func (s *SignedObject) Sign(rand io.Reader, issuer string, k crypto.Signer) error

type TimeId

type TimeId struct {
	Type  string `json:"type"` // order | trade
	Unix  uint64 `json:"unix"` // unix timestamp in seconds
	Nano  uint32 `json:"nano"` // [0, 999999999]
	Index uint32 `json:"idx"`  // index if multiple ids are generated with the same unix/nano values
}

func NewTimeId added in v0.0.2

func NewTimeId() *TimeId

NewTimeId returns a new TimeId for now

func NewUniqueTimeId added in v0.0.2

func NewUniqueTimeId() *TimeId

NewUniqueTimeId returns a unique (in the local process) TimeId

func (TimeId) Bytes

func (t TimeId) Bytes() [TimeIdDataLen]byte

Bytes returns a 128bits bigendian sortable version of this TimeId

func (TimeId) Cmp added in v0.0.10

func (a TimeId) Cmp(b TimeId) int

Cmp returns an integer comparing two TimeId time point. The result will be 0 if a == b, -1 if a < b, and +1 if a > b.

func (TimeId) MarshalJSON added in v0.0.6

func (t TimeId) MarshalJSON() ([]byte, error)

func (TimeId) String added in v0.0.2

func (t TimeId) String() string

func (TimeId) Time

func (t TimeId) Time() time.Time

Time returns the TimeId timestamp, which may be when the ID was generated

func (*TimeId) UnmarshalJSON added in v0.0.6

func (t *TimeId) UnmarshalJSON(b []byte) error

type TimeIdUnique added in v0.0.2

type TimeIdUnique struct {
	Last TimeId
}

func (*TimeIdUnique) Unique added in v0.0.2

func (u *TimeIdUnique) Unique(t *TimeId)

Unique ensures the provided TimeId is always higher than the latest one and will update it if not the case

type Trade

type Trade struct {
	Id     *TimeId // trade id
	Pair   PairName
	Bid    *OrderMeta
	Ask    *OrderMeta
	Type   OrderType // taker's order type
	Amount *Amount
	Price  *Amount
}

Trade represents a trade that happened, where two orders matched

Jump to

Keyboard shortcuts

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