Kujira.Fin.Book (kujira v0.1.51)

The aggregate of all existing orders on the book

Fields

  • :bids - A list of bids, descending by price (ie the limit is at index 0)

  • :asks - A list of asks, ascending by price (ie the limit is at index 0)

Summary

Types

@type t() :: %Kujira.Fin.Book{
  asks: [Kujira.Fin.Book.Price.t()],
  bids: [Kujira.Fin.Book.Price.t()]
}

Functions

Link to this function

from_query(map)

@spec from_query(map()) :: :error | {:ok, t()}
Link to this function

simulate_market_order(book, amount, atom)

@spec simulate_market_order(t(), integer(), :buy | :sell) ::
  {:ok, {integer(), Decimal.t()}, t()} | {:error, :insufficient_liquidity, t()}

WIP

Simulates a market swap on the book, prior to deduction of fees

Examples

A 10 unit (with 6dp) buy, with sell orders at 0.09, 0.10, 0.11 and 0.21

  • 10 base tokens are bought for 0.9 quote tokens, 9.1 remaining
  • 15 base tokens are bought for 1.5 quote tokens, 7.6 remaining
  • 18 base tokens are bought for 2.16 quote tokens, 5.44 remaining
  • Final 5.44 quote tokens buy at 0.21 - 25.904761. leaving 14.095239 of orders remaining

Total return: 10 + 15 + 18 + 25.904761 = 68.904761 Total spend = 10 Average price = 0.1451278526

iex> Kujira.Fin.Book.simulate_market_order(%Kujira.Fin.Book{asks: [ ...> %Kujira.Fin.Book.Price{price: Decimal.from_float(0.09), total: 10_000_000}, ...> %Kujira.Fin.Book.Price{price: Decimal.from_float(0.10), total: 15_000_000}, ...> %Kujira.Fin.Book.Price{price: Decimal.from_float(0.12), total: 18_000_000}, ...> %Kujira.Fin.Book.Price{price: Decimal.from_float(0.21), total: 40_000_000}, ...> ]}, 10_000_000, :buy) {:ok, {

68_904_761,
Decimal.from_float(0.1451278526),
%Kujira.Fin.Book{
  asks: [
    %Kujira.Fin.Book.Price{
      price: Decimal.from_float(0.21),
      total: 14_095_239
    }
  ]
}

}}