diff --git a/lib/zcash_explorer/application.ex b/lib/zcash_explorer/application.ex index 11fc210..b4ab540 100644 --- a/lib/zcash_explorer/application.ex +++ b/lib/zcash_explorer/application.ex @@ -31,7 +31,6 @@ defmodule ZcashExplorer.Application do {Cachex, name: :app_cache, warmers: [ - warmer(module: ZcashExplorer.Price.PriceWarmer, state: {}), warmer(module: ZcashExplorer.Metrics.MetricsWarmer, state: {}), warmer(module: ZcashExplorer.Metrics.MempoolInfoWarmer, state: {}), warmer(module: ZcashExplorer.Metrics.NetworkSolpsWarmer, state: {}), diff --git a/lib/zcash_explorer/price/price_service.ex b/lib/zcash_explorer/price/price_service.ex deleted file mode 100644 index bcad94e..0000000 --- a/lib/zcash_explorer/price/price_service.ex +++ /dev/null @@ -1,24 +0,0 @@ -defmodule ZcashExplorer.Price.PriceService do - @moduledoc false - - def fetch_current_price() do - case fetch_price() do - nil -> - {:error, "Unable to fetch ZEC price in USD"} - - price -> - {:ok, price} - end - end - - defp fetch_price() do - url() - |> HTTPoison.get!([{"User-agent", "curl/7.83.1"}]) - |> Map.get(:body) - |> Poison.decode!() - end - - defp url() do - "https://api.coingecko.com/api/v3/simple/price?ids=zcash&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true" - end -end \ No newline at end of file diff --git a/lib/zcash_explorer/price/price_warmer.ex b/lib/zcash_explorer/price/price_warmer.ex deleted file mode 100644 index 34fd29a..0000000 --- a/lib/zcash_explorer/price/price_warmer.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule ZcashExplorer.Price.PriceWarmer do - @moduledoc """ - Price warmer which caches Zec prices every 30s. - """ - alias ZcashExplorer.Price.PriceService - use Cachex.Warmer - - @doc """ - Returns the interval for this warmer. - """ - def interval, - do: :timer.seconds(30) - - @doc """ - Executes this cache warmer. - """ - def execute(_state) do - PriceService.fetch_current_price() |> handle_result - end - - # ignores the warmer result in case of error - defp handle_result({:error, _reason}), - do: :ignore - - defp handle_result({:ok, price}) do - {:ok, [{"price", price}]} - end -end diff --git a/lib/zcash_explorer_web/live/orchard_pool_live.ex b/lib/zcash_explorer_web/live/orchard_pool_live.ex new file mode 100644 index 0000000..09494f1 --- /dev/null +++ b/lib/zcash_explorer_web/live/orchard_pool_live.ex @@ -0,0 +1,48 @@ +defmodule ZcashExplorerWeb.OrchardPoolLive do + use ZcashExplorerWeb, :live_view + import Phoenix.LiveView.Helpers + @impl true + def render(assigns) do + ~L""" +

+ <%= orchard_value(@blockchain_info["valuePools"]) %> ZEC +

+ """ + end + + @impl true + def mount(_params, _session, socket) do + if connected?(socket), do: Process.send_after(self(), :update, 15000) + + case Cachex.get(:app_cache, "metrics") do + {:ok, info} -> + {:ok, %{"build" => build}} = Cachex.get(:app_cache, "info") + info = Map.put(info, "build", build) + {:ok, assign(socket, :blockchain_info, info)} + + {:error, _reason} -> + {:ok, assign(socket, :blockchain_info, "loading...")} + end + end + + @impl true + def handle_info(:update, socket) do + Process.send_after(self(), :update, 15000) + {:ok, info} = Cachex.get(:app_cache, "metrics") + {:ok, %{"build" => build}} = Cachex.get(:app_cache, "info") + info = Map.put(info, "build", build) + {:noreply, assign(socket, :blockchain_info, info)} + end + + defp orchard_value(value_pools) do + value_pools |> get_value_pools |> Map.get("orchard") + end + + defp get_value_pools(value_pools) do + Enum.map(value_pools, fn %{"id" => name, "chainValue" => value} -> {name, value} end) + |> Map.new() + end + + + end + \ No newline at end of file diff --git a/lib/zcash_explorer_web/live/price_live.ex b/lib/zcash_explorer_web/live/price_live.ex deleted file mode 100644 index cb7e745..0000000 --- a/lib/zcash_explorer_web/live/price_live.ex +++ /dev/null @@ -1,69 +0,0 @@ -defmodule ZcashExplorerWeb.PriceLive do - use ZcashExplorerWeb, :live_view - import Phoenix.LiveView.Helpers - @impl true - def render(assigns) do - ~L""" -
-

- $<%= @price["zcash"]["usd"] %> -

- <%= if @price["zcash"]["usd_24h_change"] > 0 do %> -

- - - - Increased by - - <%= Float.ceil(@price["zcash"]["usd_24h_change"], 4) %> % -

- <% end %> - - <%= if @price["zcash"]["usd_24h_change"] < 0 do %> -

- - - - - - Decreased by - - <%= Float.ceil(@price["zcash"]["usd_24h_change"], 4) %>% -

- <% end %> - - - -
- -
-
- """ - end - - @impl true - def mount(_params, _session, socket) do - if connected?(socket), do: Process.send_after(self(), :update, 3000) - - case Cachex.get(:app_cache, "price") do - {:ok, price} -> - {:ok, assign(socket, :price, price)} - - {:error, _reason} -> - {:ok, assign(socket, :price, "updating...")} - end - end - - @impl true - def handle_info(:update, socket) do - Process.send_after(self(), :update, 3000) - {:ok, price} = Cachex.get(:app_cache, "price") - {:noreply, assign(socket, :price, price)} - end -end diff --git a/lib/zcash_explorer_web/router.ex b/lib/zcash_explorer_web/router.ex index e9bd4b6..6fc4cfc 100644 --- a/lib/zcash_explorer_web/router.ex +++ b/lib/zcash_explorer_web/router.ex @@ -30,6 +30,7 @@ defmodule ZcashExplorerWeb.Router do live "/index/recent_blocks", RecentBlocksLive live "/index/recent_transactions", RecentTransactionsLive live "/live/raw_mempool", RawMempoolLive + live "/live/orchard_pool", OrchardPoolLive live "/live/nodes", NodesLive live "/vkdetails", VkLive live "/blockchain-info-live", BlockChainInfoLive diff --git a/lib/zcash_explorer_web/templates/page/index.html.heex b/lib/zcash_explorer_web/templates/page/index.html.heex index d9aacb9..0878d84 100644 --- a/lib/zcash_explorer_web/templates/page/index.html.heex +++ b/lib/zcash_explorer_web/templates/page/index.html.heex @@ -2,16 +2,24 @@
+
-
- - +
+ +
-

Price

+

Blockchain Size

- <%= live_render(@conn, ZcashExplorerWeb.PriceLive) %> +
+ <%= live_render(@conn, ZcashExplorerWeb.OrchardPoolLive) %> + +