From 372fb0d51d6e2e43a0ea3df6249de3e3026fd498 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 28 Aug 2019 12:57:01 +0300 Subject: [PATCH] remove CoinMarketCap logic --- apps/explorer/config/config.exs | 3 - .../lib/explorer/exchange_rates/source.ex | 25 +------- .../exchange_rates/source/coin_market_cap.ex | 52 ---------------- .../exchange_rates/source/token_bridge.ex | 2 +- .../source/coin_market_cap_test.exs | 59 ------------------- 5 files changed, 2 insertions(+), 139 deletions(-) delete mode 100644 apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex delete mode 100644 apps/explorer/test/explorer/exchange_rates/source/coin_market_cap_test.exs diff --git a/apps/explorer/config/config.exs b/apps/explorer/config/config.exs index 39307741cd..10e8a66e82 100644 --- a/apps/explorer/config/config.exs +++ b/apps/explorer/config/config.exs @@ -31,9 +31,6 @@ config :explorer, Explorer.ChainSpec.GenesisData, enabled: false, chain_spec_pat config :explorer, Explorer.Chain.Cache.BlockNumber, enabled: true -config :explorer, Explorer.ExchangeRates.Source.CoinMarketCap, - pages: String.to_integer(System.get_env("COINMARKETCAP_PAGES") || "10") - config :explorer, Explorer.ExchangeRates.Source.CoinGecko, coin_id: System.get_env("COIN_GECKO_ID", "poa-network") balances_update_interval = diff --git a/apps/explorer/lib/explorer/exchange_rates/source.ex b/apps/explorer/lib/explorer/exchange_rates/source.ex index 6d95e48796..fdb0e06fc3 100644 --- a/apps/explorer/lib/explorer/exchange_rates/source.ex +++ b/apps/explorer/lib/explorer/exchange_rates/source.ex @@ -2,8 +2,6 @@ defmodule Explorer.ExchangeRates.Source do @moduledoc """ Behaviour for fetching exchange rates from external sources. """ - - alias Explorer.ExchangeRates.Source.CoinMarketCap alias Explorer.ExchangeRates.Token alias HTTPoison.{Error, Response} @@ -12,28 +10,7 @@ defmodule Explorer.ExchangeRates.Source do """ @spec fetch_exchange_rates(module) :: {:ok, [Token.t()]} | {:error, any} def fetch_exchange_rates(source \\ exchange_rates_source()) do - if(source == CoinMarketCap) do - fetch_exchange_rates_from_paginable_source(source) - else - fetch_exchange_rates_request(source) - end - end - - defp fetch_exchange_rates_from_paginable_source(source, page \\ 1) do - case HTTPoison.get(source.source_url(page), headers()) do - {:ok, %Response{body: body, status_code: 200}} -> - cond do - body =~ Explorer.coin() -> {:ok, source.format_data(body)} - page == source.max_page_number -> {:error, "exchange rates not found for this network"} - true -> fetch_exchange_rates_from_paginable_source(source, page + 1) - end - - {:ok, %Response{body: body, status_code: status_code}} when status_code in 400..502 -> - {:error, decode_json(body)["error"]} - - {:error, %Error{reason: reason}} -> - {:error, reason} - end + fetch_exchange_rates_request(source) end defp fetch_exchange_rates_request(source) do diff --git a/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex b/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex deleted file mode 100644 index efe592e0fa..0000000000 --- a/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex +++ /dev/null @@ -1,52 +0,0 @@ -defmodule Explorer.ExchangeRates.Source.CoinMarketCap do - @moduledoc """ - Adapter for fetching exchange rates from https://coinmarketcap.com. - """ - - alias Explorer.ExchangeRates.{Source, Token} - - import Source, only: [decode_json: 1, to_decimal: 1] - - @behaviour Source - - @impl Source - def format_data(data) do - for item <- decode_json(data), not is_nil(item["last_updated"]) do - {last_updated_as_unix, _} = Integer.parse(item["last_updated"]) - last_updated = DateTime.from_unix!(last_updated_as_unix) - - %Token{ - available_supply: to_decimal(item["available_supply"]), - total_supply: to_decimal(item["total_supply"]), - btc_value: to_decimal(item["price_btc"]), - id: item["id"], - last_updated: last_updated, - market_cap_usd: to_decimal(item["market_cap_usd"]), - name: item["name"], - symbol: item["symbol"], - usd_value: to_decimal(item["price_usd"]), - volume_24h_usd: to_decimal(item["24h_volume_usd"]) - } - end - end - - @impl Source - def source_url do - source_url(1) - end - - def source_url(page) do - "#{base_url()}/v1/ticker/?start=#{page - 1}00" - end - - def max_page_number, do: config(:pages) - - defp base_url do - config(:base_url) || "https://api.coinmarketcap.com" - end - - @spec config(atom()) :: term - defp config(key) do - Application.get_env(:explorer, __MODULE__, [])[key] - end -end diff --git a/apps/explorer/lib/explorer/exchange_rates/source/token_bridge.ex b/apps/explorer/lib/explorer/exchange_rates/source/token_bridge.ex index 03234ff649..531e3ab181 100644 --- a/apps/explorer/lib/explorer/exchange_rates/source/token_bridge.ex +++ b/apps/explorer/lib/explorer/exchange_rates/source/token_bridge.ex @@ -45,7 +45,7 @@ defmodule Explorer.ExchangeRates.Source.TokenBridge do @spec secondary_source() :: module() defp secondary_source do - config(:secondary_source) || Explorer.ExchangeRates.Source.CoinMarketCap + config(:secondary_source) || Explorer.ExchangeRates.Source.CoinGecko end @spec config(atom()) :: term diff --git a/apps/explorer/test/explorer/exchange_rates/source/coin_market_cap_test.exs b/apps/explorer/test/explorer/exchange_rates/source/coin_market_cap_test.exs deleted file mode 100644 index e0f1693212..0000000000 --- a/apps/explorer/test/explorer/exchange_rates/source/coin_market_cap_test.exs +++ /dev/null @@ -1,59 +0,0 @@ -defmodule Explorer.ExchangeRates.Source.CoinMarketCapTest do - use ExUnit.Case - - alias Explorer.ExchangeRates.Token - alias Explorer.ExchangeRates.Source.CoinMarketCap - - @json """ - [ - { - "id": "poa-network", - "name": "POA Network", - "symbol": "POA", - "rank": "103", - "price_usd": "0.485053", - "price_btc": "0.00007032", - "24h_volume_usd": "20185000.0", - "market_cap_usd": "98941986.0", - "available_supply": "203981804.0", - "total_supply": "254473964.0", - "max_supply": null, - "percent_change_1h": "-0.66", - "percent_change_24h": "12.34", - "percent_change_7d": "49.15", - "last_updated": "1523473200" - } - ] - """ - - describe "format_data/1" do - test "returns valid tokens with valid data" do - expected_date = ~N[2018-04-11 19:00:00] |> DateTime.from_naive!("Etc/UTC") - - expected = [ - %Token{ - available_supply: Decimal.new("203981804.0"), - total_supply: Decimal.new("254473964.0"), - btc_value: Decimal.new("0.00007032"), - id: "poa-network", - last_updated: expected_date, - market_cap_usd: Decimal.new("98941986.0"), - name: "POA Network", - symbol: "POA", - usd_value: Decimal.new("0.485053"), - volume_24h_usd: Decimal.new("20185000.0") - } - ] - - assert expected == CoinMarketCap.format_data(@json) - end - - test "returns nothing when given bad data" do - bad_data = """ - [{"id": "poa-network"}] - """ - - assert [] = CoinMarketCap.format_data(bad_data) - end - end -end