amm

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	MinCoinAmount = sdk.NewInt(100)
	MaxCoinAmount = sdk.NewIntWithDecimal(1, 40)
)

The minimum and maximum coin amount used in the amm package.

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 EvenTicks

func EvenTicks(basePrice sdk.Dec, numTicks, tickPrec int) []sdk.Dec

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

func HighestTick(prec int) sdk.Dec

HighestTick returns the highest possible price tick.

func InitialPoolCoinSupply

func InitialPoolCoinSupply(x, y sdk.Int) sdk.Int

InitialPoolCoinSupply returns ideal initial pool coin minting amount.

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 Ticks

func Ticks(basePrice sdk.Dec, numTicks, tickPrec int) []sdk.Dec

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

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. It is OK to pass an empty sdk.Int to ps when ps is not going to be used.

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) (amt sdk.Int)

BuyAmountOver returns the amount of buy orders for price greater than or equal to 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) ProvidableXAmountOver

func (pool *BasicPool) ProvidableXAmountOver(price sdk.Dec) (amt sdk.Int)

ProvidableXAmountOver returns the amount of x coin the pool would provide for price greater than or equal to given price.

func (*BasicPool) ProvidableYAmountUnder

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

ProvidableYAmountUnder returns the amount of y coin the pool would provide for price less than or equal to given price.

func (*BasicPool) SellAmountUnder

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

SellAmountUnder returns the amount of sell orders for price less than or equal to 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.

func DropSmallOrders

func DropSmallOrders(orders []Order, matchPrice sdk.Dec) []Order

DropSmallOrders returns filtered orders, where orders with too small amount are dropped.

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, ticks []sdk.Dec) *OrderBook

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

func (ob *OrderBook) BuyOrders() []Order

BuyOrders returns all buy orders in the order book. Note that the orders are not sorted.

func (*OrderBook) BuyOrdersAt

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

BuyOrdersAt returns buy orders at given price in the order book. Note that the orders are not sorted.

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. Note that the orders are not sorted.

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

func (ob *OrderBook) Orders() []Order

Orders returns all orders in the order book. Note that the orders are not sorted.

func (*OrderBook) OrdersAt

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

OrdersAt returns orders at given price in the order book. Note that the orders are not sorted.

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

func (ob *OrderBook) SellOrders() []Order

SellOrders returns all sell orders in the order book. Note that the orders are not sorted.

func (*OrderBook) SellOrdersAt

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

SellOrdersAt returns sell orders at given price in the order book. Note that the orders are not sorted.

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. Note that the orders are not sorted.

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