Skip to content

Commit

Permalink
Merge pull request #147 from fremantle-capital/filter-by-product-alias
Browse files Browse the repository at this point in the history
Venue products can be filtered by alias
  • Loading branch information
rupurt authored Jun 21, 2020
2 parents cdcf0c9 + 779a599 commit bb2cc6d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/tai/lib/tai/venues/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Tai.Venues.Product do
- BTC = base asset
- USD = quote asset
"""
@type asset :: atom
@type asset :: Tai.Markets.Asset.symbol()
@type venue_asset :: String.t()

@typedoc """
Expand Down
6 changes: 3 additions & 3 deletions apps/tai/lib/tai/venues/start.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ defmodule Tai.Venues.Start do
| :success
| {:error, :timeout | term}

@enforce_keys ~w(venue status)a
defstruct ~w(
@enforce_keys ~w[venue status]a
defstruct ~w[
venue
status
timer
Expand All @@ -24,7 +24,7 @@ defmodule Tai.Venues.Start do
fees_reply
positions_reply
stream_reply
)a
]a
end

@type venue :: Tai.Venue.t()
Expand Down
21 changes: 20 additions & 1 deletion apps/tai/lib/tai/venues/start/products.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,27 @@ defmodule Tai.Venues.Start.Products do

defp apply_filter(products, query) when is_binary(query) do
products
|> Enum.reduce(%{}, fn p, acc -> Map.put(acc, p.symbol, p) end)
|> index_products()
|> Juice.squeeze(query)
|> Map.values()
|> Enum.uniq()
end

defp index_products(products) do
symbol_products =
products
|> Enum.reduce(
%{},
fn p, acc -> Map.put(acc, p.symbol, p) end
)

alias_products =
products
|> Enum.filter(& &1.alias)
|> Enum.reduce(%{}, fn p, acc ->
Map.put(acc, "#{p.base}_#{p.quote}_#{p.alias}", p)
end)

Map.merge(alias_products, symbol_products)
end
end
38 changes: 29 additions & 9 deletions apps/tai/test/tai/venues/start/products_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ defmodule Tai.Venues.Start.ProductsTest do
venue_id: :venue_a,
symbol: :eth_usdt
)
@product_c struct(
Tai.Venues.Product,
venue_id: :venue_a,
symbol: :ltc_usdt_200925,
base: :ltc,
quote: :usdt,
alias: "weekly"
)

def products(_venue_id) do
{:ok, [@product_a, @product_b]}
{:ok, [@product_a, @product_b, @product_c]}
end
end

Expand All @@ -37,8 +45,8 @@ defmodule Tai.Venues.Start.ProductsTest do
end
end

defmodule VenueProducts do
def filter(products), do: Enum.filter(products, &(&1.symbol == :btc_usdt))
defmodule ProductsFilter do
def run(products), do: Enum.filter(products, &(&1.symbol == :btc_usdt))
end

@base_venue struct(
Expand All @@ -61,7 +69,7 @@ defmodule Tai.Venues.Start.ProductsTest do
:ok
end

test "can filter products with a juice query" do
test "can filter products by symbol with a juice query" do
venue = @base_venue |> Map.put(:products, "eth_usdt")
TaiEvents.firehose_subscribe()

Expand All @@ -73,8 +81,20 @@ defmodule Tai.Venues.Start.ProductsTest do
assert Enum.at(products, 0).symbol == :eth_usdt
end

test "can filter products by base, quote & alias with a juice query" do
venue = @base_venue |> Map.put(:products, "ltc_usdt_weekly")
TaiEvents.firehose_subscribe()

start_supervised!({Tai.Venues.Start, venue})
assert_event(%Tai.Events.VenueStart{}, :info)

products = Tai.Venues.ProductStore.all()
assert Enum.count(products) == 1
assert Enum.at(products, 0).symbol == :ltc_usdt_200925
end

test "can filter products with a module function" do
venue = @base_venue |> Map.put(:products, {VenueProducts, :filter})
venue = @base_venue |> Map.put(:products, {ProductsFilter, :run})
TaiEvents.firehose_subscribe()

start_supervised!({Tai.Venues.Start, venue})
Expand All @@ -85,16 +105,16 @@ defmodule Tai.Venues.Start.ProductsTest do
assert Enum.at(products, 0).symbol == :btc_usdt
end

test "broadcasts a summary event" do
venue = @base_venue |> Map.put(:products, {VenueProducts, :filter})
test "broadcasts a summary event of unique products matching the filter" do
venue = @base_venue |> Map.put(:products, "*")
TaiEvents.firehose_subscribe()

start_supervised!({Tai.Venues.Start, venue})

assert_event(%Tai.Events.HydrateProducts{} = event, :info)
assert event.venue_id == venue.id
assert event.total == 2
assert event.filtered == 1
assert event.total == 3
assert event.filtered == 3
end

test "broadcasts a start error event when the adapter returns an error" do
Expand Down

0 comments on commit bb2cc6d

Please # to comment.