Documentation ¶
Index ¶
- Variables
- func DownTick(price sdk.Dec, prec int) sdk.Dec
- func EvenTicks(basePrice sdk.Dec, numTicks, tickPrec 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 InitialPoolCoinSupply(x, y sdk.Int) sdk.Int
- 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 Ticks(basePrice sdk.Dec, numTicks, tickPrec int) []sdk.Dec
- 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) (amt 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) ProvidableXAmountOver(price sdk.Dec) (amt sdk.Int)
- func (pool *BasicPool) ProvidableYAmountUnder(price sdk.Dec) sdk.Int
- 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) BuyOrders() []Order
- func (ob *OrderBook) BuyOrdersAt(price sdk.Dec) []Order
- 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) Orders() []Order
- func (ob *OrderBook) OrdersAt(price sdk.Dec) []Order
- func (ob *OrderBook) SellAmountUnder(price sdk.Dec) sdk.Int
- func (ob *OrderBook) SellOrders() []Order
- func (ob *OrderBook) SellOrdersAt(price sdk.Dec) []Order
- 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 ¶
var ( MinCoinAmount = sdk.NewInt(100) MaxCoinAmount = sdk.NewIntWithDecimal(1, 40) )
The minimum and maximum coin amount used in the amm package.
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 EvenTicks ¶
EvenTicks returns ticks around basePrice with maximum number of numTicks*2+1. The gap is the same across all ticks. If basePrice fits in tick system, then the number of ticks will be numTicks*2+1. Else, the number will be numTicks*2.
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 InitialPoolCoinSupply ¶
InitialPoolCoinSupply returns ideal initial pool coin minting amount.
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 Ticks ¶
Ticks returns ticks around basePrice with maximum number of numTicks*2+1. If basePrice fits in tick system, then the number of ticks will be numTicks*2+1. Else, the number will be numTicks*2.
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. It is OK to pass an empty sdk.Int to ps when ps is not going to be used.
func (*BasicPool) BuyAmountOver ¶
BuyAmountOver returns the amount of buy orders for price greater than or equal to 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) ProvidableXAmountOver ¶
ProvidableXAmountOver returns the amount of x coin the pool would provide for price greater than or equal to given price.
func (*BasicPool) ProvidableYAmountUnder ¶
ProvidableYAmountUnder returns the amount of y coin the pool would provide for price less than or equal to given price.
func (*BasicPool) SellAmountUnder ¶
SellAmountUnder returns the amount of sell orders for price less than or equal to 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. Use Ticks or EvenTicks to generate ticks where pools put orders. ticks should have more than 1 element. The orders in the order book are just mocks, so the rest fields other than Direction, Price and Amount of BaseOrder will have no special meaning.
Example ¶
pools := []amm.Pool{ amm.NewBasicPool(sdk.NewInt(1000000000), sdk.NewInt(1000000000), sdk.Int{}), } ob := amm.PoolsOrderBook(pools, amm.Ticks(utils.ParseDec("1.0"), 6, int(defTickPrec))) fmt.Println(ob.FullString(int(defTickPrec))) ob = amm.PoolsOrderBook(pools, amm.EvenTicks(utils.ParseDec("1.0"), 3, int(defTickPrec))) fmt.Println(ob.FullString(int(defTickPrec)))
Output: +--------buy---------+------------price-------------+--------sell--------+ | 0 | 1.006000000000000000 | 989090 | | 0 | 1.005000000000000000 | 991061 | | 0 | 1.004000000000000000 | 993037 | | 0 | 1.003000000000000000 | 995019 | | 0 | 1.002000000000000000 | 997007 | | 0 | 1.001000000000000000 | 999000 | | 0 | 1.000000000000000000 | 0 | | 100010 | 0.999900000000000000 | 0 | | 100020 | 0.999800000000000000 | 0 | | 100030 | 0.999700000000000000 | 0 | | 100040 | 0.999600000000000000 | 0 | | 100050 | 0.999500000000000000 | 0 | | 100060 | 0.999400000000000000 | 0 | +--------------------+------------------------------+--------------------+ +--------buy---------+------------price-------------+--------sell--------+ | 0 | 1.003000000000000000 | 995019 | | 0 | 1.002000000000000000 | 997007 | | 0 | 1.001000000000000000 | 999000 | | 0 | 1.000000000000000000 | 0 | | 0 | 0.999900000000000000 | 0 | | 0 | 0.999800000000000000 | 0 | | 0 | 0.999700000000000000 | 0 | | 0 | 0.999600000000000000 | 0 | | 0 | 0.999500000000000000 | 0 | | 0 | 0.999400000000000000 | 0 | | 0 | 0.999300000000000000 | 0 | | 0 | 0.999200000000000000 | 0 | | 0 | 0.999100000000000000 | 0 | | 1001001 | 0.999000000000000000 | 0 | | 0 | 0.998900000000000000 | 0 | | 0 | 0.998800000000000000 | 0 | | 0 | 0.998700000000000000 | 0 | | 0 | 0.998600000000000000 | 0 | | 0 | 0.998500000000000000 | 0 | | 0 | 0.998400000000000000 | 0 | | 0 | 0.998300000000000000 | 0 | | 0 | 0.998200000000000000 | 0 | | 0 | 0.998100000000000000 | 0 | | 1002004 | 0.998000000000000000 | 0 | | 0 | 0.997900000000000000 | 0 | | 0 | 0.997800000000000000 | 0 | | 0 | 0.997700000000000000 | 0 | | 0 | 0.997600000000000000 | 0 | | 0 | 0.997500000000000000 | 0 | | 0 | 0.997400000000000000 | 0 | | 0 | 0.997300000000000000 | 0 | | 0 | 0.997200000000000000 | 0 | | 0 | 0.997100000000000000 | 0 | | 1003009 | 0.997000000000000000 | 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) BuyOrders ¶
BuyOrders returns all buy orders in the order book. Note that the orders are not sorted.
func (*OrderBook) BuyOrdersAt ¶
BuyOrdersAt returns buy orders at given price in the order book. Note that the orders are not sorted.
func (*OrderBook) BuyOrdersOver ¶
BuyOrdersOver returns buy orders in the order book for price greater or equal than given price. Note that the orders are not sorted.
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) Orders ¶
Orders returns all orders in the order book. Note that the orders are not sorted.
func (*OrderBook) OrdersAt ¶
OrdersAt returns orders at given price in the order book. Note that the orders are not sorted.
func (*OrderBook) SellAmountUnder ¶
SellAmountUnder returns the amount of sell orders in the order book for price less or equal than given price.
func (*OrderBook) SellOrders ¶
SellOrders returns all sell orders in the order book. Note that the orders are not sorted.
func (*OrderBook) SellOrdersAt ¶
SellOrdersAt returns sell orders at given price in the order book. Note that the orders are not sorted.
func (*OrderBook) SellOrdersUnder ¶
SellOrdersUnder returns sell orders in the order book for price less or equal than given price. Note that the orders are not sorted.
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) ProvidableXAmountOver(price sdk.Dec) sdk.Int ProvidableYAmountUnder(price sdk.Dec) 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