Documentation ¶
Index ¶
- func DownTick(price sdk.Dec, prec int) sdk.Dec
- func FindLastMatchableOrders(buyOrders, sellOrders []Order, matchPrice sdk.Dec) (lastBuyIdx, lastSellIdx int, ...)
- func FindMatchPrice(os OrderSource, tickPrec int) (matchPrice sdk.Dec, found bool)
- func HighestTick(prec int) sdk.Dec
- func LowestTick(prec int) sdk.Dec
- func MatchOrders(buyOrders, sellOrders []Order, matchPrice sdk.Dec) (quoteCoinDust sdk.Int, matched bool)
- func OfferCoinAmount(dir OrderDirection, price sdk.Dec, amt sdk.Int) sdk.Int
- func PriceToDownTick(price sdk.Dec, prec int) sdk.Dec
- func PriceToUpTick(price sdk.Dec, prec int) sdk.Dec
- func RoundPrice(price sdk.Dec, prec int) sdk.Dec
- func RoundTickIndex(i int) int
- func TickFromIndex(i, prec int) sdk.Dec
- func TickToIndex(price sdk.Dec, prec int) int
- func TotalOpenAmount(orders []Order) sdk.Int
- func UpTick(price sdk.Dec, prec int) sdk.Dec
- type BaseOrder
- func (order *BaseOrder) DecrRemainingOfferCoin(amt sdk.Int)
- func (order *BaseOrder) GetAmount() sdk.Int
- func (order *BaseOrder) GetDirection() OrderDirection
- func (order *BaseOrder) GetOfferCoin() sdk.Coin
- func (order *BaseOrder) GetOpenAmount() sdk.Int
- func (order *BaseOrder) GetPrice() sdk.Dec
- func (order *BaseOrder) GetReceivedDemandCoin() sdk.Coin
- func (order *BaseOrder) GetRemainingOfferCoin() sdk.Coin
- func (order *BaseOrder) IncrReceivedDemandCoin(amt sdk.Int)
- func (order *BaseOrder) IsMatched() bool
- func (order *BaseOrder) SetMatched(matched bool)
- func (order *BaseOrder) SetOpenAmount(amt sdk.Int)
- type BasicPool
- func (pool *BasicPool) Balances() (rx, ry sdk.Int)
- func (pool *BasicPool) BuyAmountOver(price sdk.Dec) sdk.Int
- func (pool *BasicPool) Deposit(x, y sdk.Int) (ax, ay, pc sdk.Int)
- func (pool *BasicPool) HighestBuyPrice() (price sdk.Dec, found bool)
- func (pool *BasicPool) IsDepleted() bool
- func (pool *BasicPool) LowestSellPrice() (price sdk.Dec, found bool)
- func (pool *BasicPool) PoolCoinSupply() sdk.Int
- func (pool *BasicPool) Price() sdk.Dec
- func (pool *BasicPool) SellAmountUnder(price sdk.Dec) sdk.Int
- func (pool *BasicPool) Withdraw(pc sdk.Int, feeRate sdk.Dec) (x, y sdk.Int)
- type MockPoolOrderSource
- type Order
- type OrderBook
- func (ob *OrderBook) Add(orders ...Order)
- func (ob *OrderBook) BuyAmountOver(price sdk.Dec) sdk.Int
- func (ob *OrderBook) BuyOrdersOver(price sdk.Dec) []Order
- func (ob *OrderBook) FullString(tickPrec int) string
- func (ob *OrderBook) HighestBuyPrice() (sdk.Dec, bool)
- func (ob *OrderBook) HighestPrice() (sdk.Dec, bool)
- func (ob *OrderBook) LowestPrice() (sdk.Dec, bool)
- func (ob *OrderBook) LowestSellPrice() (sdk.Dec, bool)
- func (ob *OrderBook) SellAmountUnder(price sdk.Dec) sdk.Int
- func (ob *OrderBook) SellOrdersUnder(price sdk.Dec) []Order
- func (ob *OrderBook) String() string
- type OrderDirection
- type OrderSource
- type OrderView
- type Pool
- type TickPrecision
- func (prec TickPrecision) DownTick(price sdk.Dec) sdk.Dec
- func (prec TickPrecision) HighestTick() sdk.Dec
- func (prec TickPrecision) LowestTick() sdk.Dec
- func (prec TickPrecision) PriceToDownTick(price sdk.Dec) sdk.Dec
- func (prec TickPrecision) PriceToUpTick(price sdk.Dec) sdk.Dec
- func (prec TickPrecision) RoundPrice(price sdk.Dec) sdk.Dec
- func (prec TickPrecision) TickFromIndex(i int) sdk.Dec
- func (prec TickPrecision) TickToIndex(tick sdk.Dec) int
- func (prec TickPrecision) UpTick(price sdk.Dec) sdk.Dec
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DownTick ¶
DownTick returns the next highest price tick under the price. DownTick doesn't check if the price is the lowest price tick.
func FindLastMatchableOrders ¶
func FindLastMatchableOrders(buyOrders, sellOrders []Order, matchPrice sdk.Dec) (lastBuyIdx, lastSellIdx int, lastBuyPartialMatchAmt, lastSellPartialMatchAmt sdk.Int, found bool)
FindLastMatchableOrders returns the last matchable order indexes for each buy/sell side. lastBuyPartialMatchAmt and lastSellPartialMatchAmt are the amount of partially matched portion of the last orders. FindLastMatchableOrders drops(ignores) an order if the orderer receives zero demand coin after truncation when the order is either fully matched or partially matched.
func FindMatchPrice ¶
func FindMatchPrice(os OrderSource, tickPrec int) (matchPrice sdk.Dec, found bool)
FindMatchPrice returns the best match price for given order sources. If there is no matchable orders, found will be false.
func HighestTick ¶
HighestTick returns the highest possible price tick.
func LowestTick ¶
LowestTick returns the lowest possible price tick.
func MatchOrders ¶
func MatchOrders(buyOrders, sellOrders []Order, matchPrice sdk.Dec) (quoteCoinDust sdk.Int, matched bool)
MatchOrders matches orders at given matchPrice if matchable. Note that MatchOrders modifies the orders in the parameters. quoteCoinDust is the difference between total paid quote coin and total received quote coin. quoteCoinDust can be positive because of the decimal truncation.
func OfferCoinAmount ¶
OfferCoinAmount returns the minimum offer coin amount for given order direction, price and order amount.
func PriceToDownTick ¶
PriceToDownTick returns the highest price tick under(or equal to) the price.
func PriceToUpTick ¶
PriceToUpTick returns the lowest price tick greater or equal than the price.
func RoundPrice ¶
RoundPrice returns rounded price using banker's rounding.
func RoundTickIndex ¶
RoundTickIndex returns rounded tick index using banker's rounding.
func TickFromIndex ¶
TickFromIndex returns a price for given tick index. See TickToIndex for more details about tick indices.
func TickToIndex ¶
TickToIndex returns a tick index for given price. Tick index 0 means the lowest possible price fit in ticks.
func TotalOpenAmount ¶
TotalOpenAmount returns total open amount of orders.
Types ¶
type BaseOrder ¶
type BaseOrder struct { Direction OrderDirection Price sdk.Dec Amount sdk.Int OpenAmount sdk.Int OfferCoin sdk.Coin RemainingOfferCoin sdk.Coin ReceivedDemandCoin sdk.Coin Matched bool }
BaseOrder is the base struct for an Order.
func NewBaseOrder ¶
func NewBaseOrder(dir OrderDirection, price sdk.Dec, amt sdk.Int, offerCoin sdk.Coin, demandCoinDenom string) *BaseOrder
NewBaseOrder returns a new BaseOrder.
func (*BaseOrder) DecrRemainingOfferCoin ¶
DecrRemainingOfferCoin decrements remaining offer coin amount of the order.
func (*BaseOrder) GetDirection ¶
func (order *BaseOrder) GetDirection() OrderDirection
GetDirection returns the order direction.
func (*BaseOrder) GetOfferCoin ¶
func (*BaseOrder) GetOpenAmount ¶
GetOpenAmount returns open(not matched) amount of the order.
func (*BaseOrder) GetReceivedDemandCoin ¶
GetReceivedDemandCoin returns received demand coin of the order.
func (*BaseOrder) GetRemainingOfferCoin ¶
GetRemainingOfferCoin returns remaining offer coin of the order.
func (*BaseOrder) IncrReceivedDemandCoin ¶
IncrReceivedDemandCoin increments received demand coin amount of the order.
func (*BaseOrder) SetMatched ¶
SetMatched sets whether the order is matched or not.
func (*BaseOrder) SetOpenAmount ¶
SetOpenAmount sets open amount of the order.
type BasicPool ¶
type BasicPool struct {
// contains filtered or unexported fields
}
BasicPool is the basic pool type.
func NewBasicPool ¶
NewBasicPool returns a new BasicPool.
func (*BasicPool) BuyAmountOver ¶
BuyAmountOver returns the amount of buy orders for price greater or equal than given price.
func (*BasicPool) Deposit ¶
Deposit returns accepted x and y coin amount and minted pool coin amount when someone deposits x and y coins.
func (*BasicPool) HighestBuyPrice ¶
HighestBuyPrice returns the highest buy price of the pool.
func (*BasicPool) IsDepleted ¶
IsDepleted returns whether the pool is depleted or not.
func (*BasicPool) LowestSellPrice ¶
LowestSellPrice returns the lowest sell price of the pool.
func (*BasicPool) PoolCoinSupply ¶
PoolCoinSupply returns the pool coin supply.
func (*BasicPool) SellAmountUnder ¶
SellAmountUnder returns the amount of sell orders for price less or equal than given price.
type MockPoolOrderSource ¶
type MockPoolOrderSource struct { Pool // contains filtered or unexported fields }
MockPoolOrderSource demonstrates how to implement a pool OrderSource.
func NewMockPoolOrderSource ¶
func NewMockPoolOrderSource(pool Pool, baseCoinDenom, quoteCoinDenom string) *MockPoolOrderSource
NewMockPoolOrderSource returns a new MockPoolOrderSource for testing.
func (*MockPoolOrderSource) BuyOrdersOver ¶
func (os *MockPoolOrderSource) BuyOrdersOver(price sdk.Dec) []Order
BuyOrdersOver returns buy orders for price greater or equal than given price.
func (*MockPoolOrderSource) SellOrdersUnder ¶
func (os *MockPoolOrderSource) SellOrdersUnder(price sdk.Dec) []Order
SellOrdersUnder returns sell orders for price less or equal than given price.
type Order ¶
type Order interface { GetDirection() OrderDirection GetPrice() sdk.Dec GetAmount() sdk.Int // The original order amount GetOpenAmount() sdk.Int SetOpenAmount(amt sdk.Int) GetOfferCoin() sdk.Coin GetRemainingOfferCoin() sdk.Coin DecrRemainingOfferCoin(amt sdk.Int) // Decrement remaining offer coin amount GetReceivedDemandCoin() sdk.Coin IncrReceivedDemandCoin(amt sdk.Int) // Increment received demand coin amount IsMatched() bool SetMatched(matched bool) }
Order is the universal interface of an order.
type OrderBook ¶
type OrderBook struct {
// contains filtered or unexported fields
}
OrderBook is an order book.
func NewOrderBook ¶
NewOrderBook returns a new OrderBook.
func PoolsOrderBook ¶
PoolsOrderBook returns an order book with orders made by pools. The order book has at most (numTicks*2+1) ticks visible, which includes basePrice, numTicks ticks over basePrice and numTicks ticks under basePrice. PoolsOrderBook assumes that basePrice is on ticks.
Example ¶
pools := []amm.Pool{ amm.NewBasicPool(sdk.NewInt(1000000), sdk.NewInt(1000000), sdk.Int{}), } ob := amm.PoolsOrderBook(pools, squad.ParseDec("1.0"), 6, int(defTickPrec)) fmt.Println(ob.FullString(int(defTickPrec)))
Output: +--------buy---------+------------price-------------+--------sell--------+ | 0 | 1.006000000000000000 | 989 | | 0 | 1.005000000000000000 | 991 | | 0 | 1.004000000000000000 | 993 | | 0 | 1.003000000000000000 | 995 | | 0 | 1.002000000000000000 | 997 | | 0 | 1.001000000000000000 | 999 | | 0 | 1.000000000000000000 | 0 | | 100 | 0.999900000000000000 | 0 | | 100 | 0.999800000000000000 | 0 | | 100 | 0.999700000000000000 | 0 | | 100 | 0.999600000000000000 | 0 | | 100 | 0.999500000000000000 | 0 | | 100 | 0.999400000000000000 | 0 | +--------------------+------------------------------+--------------------+
func (*OrderBook) BuyAmountOver ¶
BuyAmountOver returns the amount of buy orders in the order book for price greater or equal than given price.
func (*OrderBook) BuyOrdersOver ¶
BuyOrdersOver returns buy orders in the order book for price greater or equal than given price.
func (*OrderBook) FullString ¶
FullString returns a full string representation of the order book. FullString includes all possible price ticks from the order book's highest price to the lowest price.
func (*OrderBook) HighestBuyPrice ¶
HighestBuyPrice returns the highest buy price in the order book.
func (*OrderBook) LowestSellPrice ¶
LowestSellPrice returns the lowest sell price in the order book.
func (*OrderBook) SellAmountUnder ¶
SellAmountUnder returns the amount of sell orders in the order book for price less or equal than given price.
func (*OrderBook) SellOrdersUnder ¶
SellOrdersUnder returns sell orders in the order book for price less or equal than given price.
type OrderDirection ¶
type OrderDirection int
OrderDirection specifies an order direction, either buy or sell.
const ( Buy OrderDirection = iota + 1 Sell )
OrderDirection enumerations.
func (OrderDirection) String ¶
func (dir OrderDirection) String() string
type OrderSource ¶
type OrderSource interface { OrderView BuyOrdersOver(price sdk.Dec) []Order // Includes the price SellOrdersUnder(price sdk.Dec) []Order // Includes the price }
OrderSource is the interface which provides a view of orders and also provides a way to extract orders from it.
func MergeOrderSources ¶
func MergeOrderSources(sources ...OrderSource) OrderSource
MergeOrderSources returns a merged order source of multiple sources.
type OrderView ¶
type OrderView interface { HighestBuyPrice() (price sdk.Dec, found bool) LowestSellPrice() (price sdk.Dec, found bool) BuyAmountOver(price sdk.Dec) sdk.Int // Includes the price SellAmountUnder(price sdk.Dec) sdk.Int // Includes the price }
OrderView is the interface which provides a view of orders.
type Pool ¶
type Pool interface { OrderView Balances() (rx, ry sdk.Int) PoolCoinSupply() sdk.Int Price() sdk.Dec IsDepleted() bool Deposit(x, y sdk.Int) (ax, ay, pc sdk.Int) Withdraw(pc sdk.Int, feeRate sdk.Dec) (x, y sdk.Int) }
Pool is the interface of a pool. It also satisfies OrderView interface.
type TickPrecision ¶
type TickPrecision int
TickPrecision represents a tick precision.
func (TickPrecision) HighestTick ¶
func (prec TickPrecision) HighestTick() sdk.Dec
func (TickPrecision) LowestTick ¶
func (prec TickPrecision) LowestTick() sdk.Dec
func (TickPrecision) PriceToDownTick ¶
func (prec TickPrecision) PriceToDownTick(price sdk.Dec) sdk.Dec
func (TickPrecision) PriceToUpTick ¶
func (prec TickPrecision) PriceToUpTick(price sdk.Dec) sdk.Dec
func (TickPrecision) RoundPrice ¶
func (prec TickPrecision) RoundPrice(price sdk.Dec) sdk.Dec
func (TickPrecision) TickFromIndex ¶
func (prec TickPrecision) TickFromIndex(i int) sdk.Dec
func (TickPrecision) TickToIndex ¶
func (prec TickPrecision) TickToIndex(tick sdk.Dec) int