amm

package
v0.2.1-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DownTick

func DownTick(price sdk.Dec, prec int) sdk.Dec

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

func HighestTick(prec int) sdk.Dec

HighestTick returns the highest possible price tick.

func LowestTick

func LowestTick(prec int) sdk.Dec

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

func OfferCoinAmount(dir OrderDirection, price sdk.Dec, amt sdk.Int) sdk.Int

OfferCoinAmount returns the minimum offer coin amount for given order direction, price and order amount.

func PriceToDownTick

func PriceToDownTick(price sdk.Dec, prec int) sdk.Dec

PriceToDownTick returns the highest price tick under(or equal to) the price.

func PriceToUpTick

func PriceToUpTick(price sdk.Dec, prec int) sdk.Dec

PriceToUpTick returns the lowest price tick greater or equal than the price.

func RoundPrice

func RoundPrice(price sdk.Dec, prec int) sdk.Dec

RoundPrice returns rounded price using banker's rounding.

func RoundTickIndex

func RoundTickIndex(i int) int

RoundTickIndex returns rounded tick index using banker's rounding.

func TickFromIndex

func TickFromIndex(i, prec int) sdk.Dec

TickFromIndex returns a price for given tick index. See TickToIndex for more details about tick indices.

func TickToIndex

func TickToIndex(price sdk.Dec, prec int) int

TickToIndex returns a tick index for given price. Tick index 0 means the lowest possible price fit in ticks.

func TotalOpenAmount

func TotalOpenAmount(orders []Order) sdk.Int

TotalOpenAmount returns total open amount of orders.

func UpTick

func UpTick(price sdk.Dec, prec int) sdk.Dec

UpTick returns the next lowest price tick above the price.

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

func (order *BaseOrder) DecrRemainingOfferCoin(amt sdk.Int)

DecrRemainingOfferCoin decrements remaining offer coin amount of the order.

func (*BaseOrder) GetAmount

func (order *BaseOrder) GetAmount() sdk.Int

GetAmount returns the order amount.

func (*BaseOrder) GetDirection

func (order *BaseOrder) GetDirection() OrderDirection

GetDirection returns the order direction.

func (*BaseOrder) GetOfferCoin

func (order *BaseOrder) GetOfferCoin() sdk.Coin

func (*BaseOrder) GetOpenAmount

func (order *BaseOrder) GetOpenAmount() sdk.Int

GetOpenAmount returns open(not matched) amount of the order.

func (*BaseOrder) GetPrice

func (order *BaseOrder) GetPrice() sdk.Dec

GetPrice returns the order price.

func (*BaseOrder) GetReceivedDemandCoin

func (order *BaseOrder) GetReceivedDemandCoin() sdk.Coin

GetReceivedDemandCoin returns received demand coin of the order.

func (*BaseOrder) GetRemainingOfferCoin

func (order *BaseOrder) GetRemainingOfferCoin() sdk.Coin

GetRemainingOfferCoin returns remaining offer coin of the order.

func (*BaseOrder) IncrReceivedDemandCoin

func (order *BaseOrder) IncrReceivedDemandCoin(amt sdk.Int)

IncrReceivedDemandCoin increments received demand coin amount of the order.

func (*BaseOrder) IsMatched

func (order *BaseOrder) IsMatched() bool

IsMatched returns whether the order is matched or not.

func (*BaseOrder) SetMatched

func (order *BaseOrder) SetMatched(matched bool)

SetMatched sets whether the order is matched or not.

func (*BaseOrder) SetOpenAmount

func (order *BaseOrder) SetOpenAmount(amt sdk.Int)

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

func NewBasicPool(rx, ry, ps sdk.Int) *BasicPool

NewBasicPool returns a new BasicPool.

func (*BasicPool) Balances

func (pool *BasicPool) Balances() (rx, ry sdk.Int)

Balances returns the balances of the pool.

func (*BasicPool) BuyAmountOver

func (pool *BasicPool) BuyAmountOver(price sdk.Dec) sdk.Int

BuyAmountOver returns the amount of buy orders for price greater or equal than given price.

func (*BasicPool) Deposit

func (pool *BasicPool) Deposit(x, y sdk.Int) (ax, ay, pc sdk.Int)

Deposit returns accepted x and y coin amount and minted pool coin amount when someone deposits x and y coins.

func (*BasicPool) HighestBuyPrice

func (pool *BasicPool) HighestBuyPrice() (price sdk.Dec, found bool)

HighestBuyPrice returns the highest buy price of the pool.

func (*BasicPool) IsDepleted

func (pool *BasicPool) IsDepleted() bool

IsDepleted returns whether the pool is depleted or not.

func (*BasicPool) LowestSellPrice

func (pool *BasicPool) LowestSellPrice() (price sdk.Dec, found bool)

LowestSellPrice returns the lowest sell price of the pool.

func (*BasicPool) PoolCoinSupply

func (pool *BasicPool) PoolCoinSupply() sdk.Int

PoolCoinSupply returns the pool coin supply.

func (*BasicPool) Price

func (pool *BasicPool) Price() sdk.Dec

Price returns the pool price.

func (*BasicPool) SellAmountUnder

func (pool *BasicPool) SellAmountUnder(price sdk.Dec) sdk.Int

SellAmountUnder returns the amount of sell orders for price less or equal than given price.

func (*BasicPool) Withdraw

func (pool *BasicPool) Withdraw(pc sdk.Int, feeRate sdk.Dec) (x, y sdk.Int)

Withdraw returns withdrawn x and y coin amount when someone withdraws pc pool coin. Withdraw also takes care of the fee rate.

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

func NewOrderBook(orders ...Order) *OrderBook

NewOrderBook returns a new OrderBook.

func PoolsOrderBook

func PoolsOrderBook(pools []Pool, basePrice sdk.Dec, numTicks, tickPrec int) *OrderBook

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) Add

func (ob *OrderBook) Add(orders ...Order)

Add adds orders to the order book.

func (*OrderBook) BuyAmountOver

func (ob *OrderBook) BuyAmountOver(price sdk.Dec) sdk.Int

BuyAmountOver returns the amount of buy orders in the order book for price greater or equal than given price.

func (*OrderBook) BuyOrdersOver

func (ob *OrderBook) BuyOrdersOver(price sdk.Dec) []Order

BuyOrdersOver returns buy orders in the order book for price greater or equal than given price.

func (*OrderBook) FullString

func (ob *OrderBook) FullString(tickPrec int) string

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

func (ob *OrderBook) HighestBuyPrice() (sdk.Dec, bool)

HighestBuyPrice returns the highest buy price in the order book.

func (*OrderBook) HighestPrice

func (ob *OrderBook) HighestPrice() (sdk.Dec, bool)

func (*OrderBook) LowestPrice

func (ob *OrderBook) LowestPrice() (sdk.Dec, bool)

func (*OrderBook) LowestSellPrice

func (ob *OrderBook) LowestSellPrice() (sdk.Dec, bool)

LowestSellPrice returns the lowest sell price in the order book.

func (*OrderBook) SellAmountUnder

func (ob *OrderBook) SellAmountUnder(price sdk.Dec) sdk.Int

SellAmountUnder returns the amount of sell orders in the order book for price less or equal than given price.

func (*OrderBook) SellOrdersUnder

func (ob *OrderBook) SellOrdersUnder(price sdk.Dec) []Order

SellOrdersUnder returns sell orders in the order book for price less or equal than given price.

func (*OrderBook) String

func (ob *OrderBook) String() string

String returns a compact string representation of the order book. String includes a tick only when there is at least one order on it.

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) DownTick

func (prec TickPrecision) DownTick(price sdk.Dec) sdk.Dec

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

func (TickPrecision) UpTick

func (prec TickPrecision) UpTick(price sdk.Dec) sdk.Dec

Jump to

Keyboard shortcuts

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