From 36d0fa236006b2e0dd662a2c0e602924802990c3 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Thu, 10 May 2018 15:52:35 -0400 Subject: [PATCH] =?UTF-8?q?Use=20lookup=20instead=20of=20filtering=20the?= =?UTF-8?q?=20list=20to=20get=20a=20symbol=E2=80=99s=20exchange=20rate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../explorer/exchange_rates/exchange_rates.ex | 13 +++++++ apps/explorer/lib/explorer/market/market.ex | 7 ++-- .../exchange_rates/exchange_rates_test.exs | 19 ++++++++-- .../test/explorer/market/market_test.exs | 37 ------------------- .../controllers/chain_controller.ex | 2 +- 5 files changed, 33 insertions(+), 45 deletions(-) diff --git a/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex b/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex index c200494150..5e2f6c8d79 100644 --- a/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex +++ b/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex @@ -90,6 +90,19 @@ defmodule Explorer.ExchangeRates do list_from_store(store()) end + @doc """ + Returns a specific rate from the tracked tickers by symbol + """ + @spec lookup(String.t()) :: Token.t() + def lookup(symbol) do + if store() == :ets do + case :ets.lookup(table_name(), symbol) do + [{_key, token} | _] -> token + _ -> nil + end + end + end + ## Undocumented public functions @doc false diff --git a/apps/explorer/lib/explorer/market/market.ex b/apps/explorer/lib/explorer/market/market.ex index ddb08eb398..73ee59fb69 100644 --- a/apps/explorer/lib/explorer/market/market.ex +++ b/apps/explorer/lib/explorer/market/market.ex @@ -12,10 +12,9 @@ defmodule Explorer.Market do @doc """ Get most recent exchange rate for the given symbol. """ - @spec fetch_exchange_rate(String.t()) :: Token.t() - def fetch_exchange_rate(symbol) do - ExchangeRates.list() - |> Enum.find(fn token -> token.symbol == symbol end) + @spec get_exchange_rate(String.t()) :: Token.t() + def get_exchange_rate(symbol) do + ExchangeRates.lookup(symbol) end @doc """ diff --git a/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs b/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs index e36902be99..235a4be295 100644 --- a/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs +++ b/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs @@ -97,13 +97,26 @@ defmodule Explorer.ExchangeRatesTest do ExchangeRates.init([]) rates = [ - %Token{id: "z", symbol: "z"}, - %Token{id: "a", symbol: "a"} + %Token{symbol: "z"}, + %Token{symbol: "a"} ] expected_rates = Enum.reverse(rates) - for rate <- rates, do: :ets.insert(ExchangeRates.table_name(), {rate.id, rate}) + for rate <- rates, do: :ets.insert(ExchangeRates.table_name(), {rate.symbol, rate}) assert expected_rates == ExchangeRates.list() end + + test "lookup/1" do + ExchangeRates.init([]) + + z = %Token{symbol: "z"} + + rates = [z, %Token{symbol: "a"}] + + for rate <- rates, do: :ets.insert(ExchangeRates.table_name(), {rate.symbol, rate}) + + assert z == ExchangeRates.lookup("z") + assert nil == ExchangeRates.lookup("nope") + end end diff --git a/apps/explorer/test/explorer/market/market_test.exs b/apps/explorer/test/explorer/market/market_test.exs index 3c4d318b0c..b989f6a1de 100644 --- a/apps/explorer/test/explorer/market/market_test.exs +++ b/apps/explorer/test/explorer/market/market_test.exs @@ -1,31 +1,10 @@ defmodule Explorer.MarketTest do use Explorer.DataCase - alias Explorer.ExchangeRates - alias Explorer.ExchangeRates.Token alias Explorer.Market alias Explorer.Market.MarketHistory alias Explorer.Repo - describe "fetch_exchange_rate/1" do - setup do - use_ets_store() - - {:ok, _} = ExchangeRates.start_link([]) - rate = %Token{id: "POA", symbol: "POA"} - :ets.insert(ExchangeRates.table_name(), {rate.id, rate}) - {:ok, %{rate: rate}} - end - - test "with matching symbol", %{rate: rate} do - assert Market.fetch_exchange_rate("POA") == rate - end - - test "with no matching symbol" do - assert Market.fetch_exchange_rate("ETH") == nil - end - end - test "fetch_recent_history/1" do today = Date.utc_today() @@ -105,20 +84,4 @@ defmodule Explorer.MarketTest do assert fetched_record.opening_price == new_record.opening_price end end - - defp use_ets_store do - # Use ets tables as ExchangeRates store and put some test data in to - # exercise Context filtering - exchange_config = Application.get_env(:explorer, Explorer.ExchangeRates) - - Application.put_env( - :explorer, - Explorer.ExchangeRates, - Keyword.put(exchange_config, :store, :ets) - ) - - on_exit(fn -> - Application.put_env(:explorer, Explorer.ExchangeRates, exchange_config) - end) - end end diff --git a/apps/explorer_web/lib/explorer_web/controllers/chain_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/chain_controller.ex index 5c4e73072c..1e50a2d208 100644 --- a/apps/explorer_web/lib/explorer_web/controllers/chain_controller.ex +++ b/apps/explorer_web/lib/explorer_web/controllers/chain_controller.ex @@ -12,7 +12,7 @@ defmodule ExplorerWeb.ChainController do "show.html", chain: Statistics.fetch(), market_history_data: Market.fetch_recent_history(30), - exchange_rate: Market.fetch_exchange_rate(coin()) || Token.null() + exchange_rate: Market.get_exchange_rate(coin()) || Token.null() ) end